Update frontend/app.py
Browse files- frontend/app.py +10 -20
frontend/app.py
CHANGED
|
@@ -16,13 +16,14 @@ async def start():
|
|
| 16 |
RAG_INITIALIZED = False
|
| 17 |
|
| 18 |
await cl.Message(
|
| 19 |
-
content="👋 **Bem-vindo ao RAG
|
| 20 |
author="Sistema"
|
| 21 |
).send()
|
| 22 |
|
|
|
|
| 23 |
files = await cl.AskFileMessage(
|
| 24 |
-
content="📂 Por favor, envie seu arquivo
|
| 25 |
-
accept=["
|
| 26 |
max_size_mb=20,
|
| 27 |
timeout=180,
|
| 28 |
).send()
|
|
@@ -33,7 +34,9 @@ async def start():
|
|
| 33 |
await msg.send()
|
| 34 |
|
| 35 |
try:
|
| 36 |
-
|
|
|
|
|
|
|
| 37 |
|
| 38 |
with open(file.path, "rb") as f:
|
| 39 |
file_content = f.read()
|
|
@@ -49,14 +52,13 @@ async def start():
|
|
| 49 |
RAG_INITIALIZED = True
|
| 50 |
data = response.json()
|
| 51 |
chunks = data.get("total_chunks", "?")
|
| 52 |
-
|
| 53 |
|
| 54 |
msg.content = (
|
| 55 |
f"✅ **Processamento Concluído!**\n\n"
|
| 56 |
f"📄 Arquivo: `{file.name}`\n"
|
| 57 |
-
f"📑 Páginas lidas: {pages}\n"
|
| 58 |
f"✂️ Segmentos gerados (Chunks): {chunks}\n\n"
|
| 59 |
-
f"
|
| 60 |
)
|
| 61 |
await msg.update()
|
| 62 |
else:
|
|
@@ -75,21 +77,18 @@ async def main(message: cl.Message):
|
|
| 75 |
|
| 76 |
if not RAG_INITIALIZED:
|
| 77 |
await cl.Message(
|
| 78 |
-
content="⚠️ Nenhum documento carregado. Por favor, recarregue a página para enviar um
|
| 79 |
author="Sistema"
|
| 80 |
).send()
|
| 81 |
return
|
| 82 |
|
| 83 |
-
# 1. Cria a mensagem principal
|
| 84 |
msg = cl.Message(content="")
|
| 85 |
await msg.send()
|
| 86 |
|
| 87 |
-
# 2. Cria o Step de Auditoria (inicialmente rodando)
|
| 88 |
audit_step = cl.Step(name="🔎 Processando & Auditoria", type="process")
|
| 89 |
audit_step.input = message.content
|
| 90 |
await audit_step.send()
|
| 91 |
|
| 92 |
-
# Buffer para detectar o separador de debug
|
| 93 |
debug_separator = "###__DEBUG__###"
|
| 94 |
is_debug_mode = False
|
| 95 |
debug_content = ""
|
|
@@ -105,31 +104,22 @@ async def main(message: cl.Message):
|
|
| 105 |
return
|
| 106 |
|
| 107 |
async for chunk in response.aiter_text():
|
| 108 |
-
# Se já estamos no modo debug, acumulamos apenas no Step
|
| 109 |
if is_debug_mode:
|
| 110 |
debug_content += chunk
|
| 111 |
continue
|
| 112 |
|
| 113 |
-
# Verifica se o chunk contém o início do separador
|
| 114 |
if debug_separator in chunk:
|
| 115 |
parts = chunk.split(debug_separator)
|
| 116 |
-
|
| 117 |
-
# Parte 1: Final da resposta normal
|
| 118 |
if parts[0]:
|
| 119 |
await msg.stream_token(parts[0])
|
| 120 |
-
|
| 121 |
-
# Ativa modo debug e guarda o resto
|
| 122 |
is_debug_mode = True
|
| 123 |
if len(parts) > 1:
|
| 124 |
debug_content += parts[1]
|
| 125 |
else:
|
| 126 |
-
# Fluxo normal de texto para o usuário
|
| 127 |
await msg.stream_token(chunk)
|
| 128 |
|
| 129 |
-
# Finalização
|
| 130 |
await msg.update()
|
| 131 |
|
| 132 |
-
# Atualiza o Step com o conteúdo de auditoria (Contexto recuperado)
|
| 133 |
if debug_content:
|
| 134 |
audit_step.output = debug_content
|
| 135 |
else:
|
|
|
|
| 16 |
RAG_INITIALIZED = False
|
| 17 |
|
| 18 |
await cl.Message(
|
| 19 |
+
content="👋 **Bem-vindo ao RAG Multi-Formato!**\n\nPara começar, faça o upload de um arquivo. Suporto: **PDF, TXT, Markdown (.md) e Python (.py)**.",
|
| 20 |
author="Sistema"
|
| 21 |
).send()
|
| 22 |
|
| 23 |
+
# MUDANÇA: Lista de extensões aceitas atualizada
|
| 24 |
files = await cl.AskFileMessage(
|
| 25 |
+
content="📂 Por favor, envie seu arquivo:",
|
| 26 |
+
accept=[".pdf", ".txt", ".md", ".py"],
|
| 27 |
max_size_mb=20,
|
| 28 |
timeout=180,
|
| 29 |
).send()
|
|
|
|
| 34 |
await msg.send()
|
| 35 |
|
| 36 |
try:
|
| 37 |
+
# Pega o MIME type ou define genericamente como octet-stream se falhar,
|
| 38 |
+
# o backend agora confia mais na extensão do arquivo.
|
| 39 |
+
mime_type = getattr(file, 'type', 'application/octet-stream')
|
| 40 |
|
| 41 |
with open(file.path, "rb") as f:
|
| 42 |
file_content = f.read()
|
|
|
|
| 52 |
RAG_INITIALIZED = True
|
| 53 |
data = response.json()
|
| 54 |
chunks = data.get("total_chunks", "?")
|
| 55 |
+
# Removemos 'total_pages' do destaque principal pois txt/py não tem página
|
| 56 |
|
| 57 |
msg.content = (
|
| 58 |
f"✅ **Processamento Concluído!**\n\n"
|
| 59 |
f"📄 Arquivo: `{file.name}`\n"
|
|
|
|
| 60 |
f"✂️ Segmentos gerados (Chunks): {chunks}\n\n"
|
| 61 |
+
f"Base de conhecimento atualizada! Pode perguntar."
|
| 62 |
)
|
| 63 |
await msg.update()
|
| 64 |
else:
|
|
|
|
| 77 |
|
| 78 |
if not RAG_INITIALIZED:
|
| 79 |
await cl.Message(
|
| 80 |
+
content="⚠️ Nenhum documento carregado. Por favor, recarregue a página para enviar um arquivo.",
|
| 81 |
author="Sistema"
|
| 82 |
).send()
|
| 83 |
return
|
| 84 |
|
|
|
|
| 85 |
msg = cl.Message(content="")
|
| 86 |
await msg.send()
|
| 87 |
|
|
|
|
| 88 |
audit_step = cl.Step(name="🔎 Processando & Auditoria", type="process")
|
| 89 |
audit_step.input = message.content
|
| 90 |
await audit_step.send()
|
| 91 |
|
|
|
|
| 92 |
debug_separator = "###__DEBUG__###"
|
| 93 |
is_debug_mode = False
|
| 94 |
debug_content = ""
|
|
|
|
| 104 |
return
|
| 105 |
|
| 106 |
async for chunk in response.aiter_text():
|
|
|
|
| 107 |
if is_debug_mode:
|
| 108 |
debug_content += chunk
|
| 109 |
continue
|
| 110 |
|
|
|
|
| 111 |
if debug_separator in chunk:
|
| 112 |
parts = chunk.split(debug_separator)
|
|
|
|
|
|
|
| 113 |
if parts[0]:
|
| 114 |
await msg.stream_token(parts[0])
|
|
|
|
|
|
|
| 115 |
is_debug_mode = True
|
| 116 |
if len(parts) > 1:
|
| 117 |
debug_content += parts[1]
|
| 118 |
else:
|
|
|
|
| 119 |
await msg.stream_token(chunk)
|
| 120 |
|
|
|
|
| 121 |
await msg.update()
|
| 122 |
|
|
|
|
| 123 |
if debug_content:
|
| 124 |
audit_step.output = debug_content
|
| 125 |
else:
|