Update frontend/app.py
Browse files- frontend/app.py +19 -21
frontend/app.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
-
|
| 2 |
|
| 3 |
import chainlit as cl
|
| 4 |
import httpx
|
| 5 |
-
import os
|
| 6 |
-
import
|
| 7 |
|
| 8 |
# URL do nosso backend FastAPI
|
| 9 |
API_URL = "http://localhost:8000"
|
|
@@ -37,24 +37,23 @@ async def start():
|
|
| 37 |
msg = cl.Message(content=f"Recebido: {file.name}. Processando...", author="Sistema")
|
| 38 |
await msg.send()
|
| 39 |
|
| 40 |
-
# 1. CORREÇÃO: Lendo o conteúdo do arquivo do disco (file.path)
|
| 41 |
file_content = None
|
|
|
|
| 42 |
try:
|
| 43 |
-
#
|
| 44 |
with open(file.path, "rb") as f:
|
| 45 |
file_content = f.read()
|
| 46 |
|
| 47 |
-
|
| 48 |
-
#
|
| 49 |
-
|
| 50 |
-
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
async with httpx.AsyncClient(timeout=300) as client:
|
| 55 |
-
# CORREÇÃO: O payload agora usa o `file_content` lido
|
| 56 |
-
files_payload = {"file": (file.name, file_content, file.mime)}
|
| 57 |
-
|
| 58 |
response = await client.post(
|
| 59 |
UPLOAD_URL,
|
| 60 |
files=files_payload
|
|
@@ -63,25 +62,23 @@ async def start():
|
|
| 63 |
# Se o upload/processamento for bem-sucedido
|
| 64 |
if response.status_code == 200:
|
| 65 |
RAG_INITIALIZED = True
|
| 66 |
-
# CORREÇÃO: Garante que é 1 argumento posicional
|
| 67 |
await msg.update(
|
| 68 |
f"✅ **{file.name}** processado com sucesso! Agora você pode fazer perguntas sobre o conteúdo do documento."
|
| 69 |
)
|
| 70 |
else:
|
| 71 |
RAG_INITIALIZED = False
|
| 72 |
-
# CORREÇÃO: Garante que é 1 argumento posicional
|
| 73 |
error_detail = response.json().get('detail', 'Erro desconhecido')
|
| 74 |
await msg.update(
|
| 75 |
f"❌ Erro ao processar o arquivo. Código: {response.status_code}. Detalhe: {error_detail}"
|
| 76 |
)
|
| 77 |
|
| 78 |
except httpx.ConnectError:
|
| 79 |
-
# CORREÇÃO: Garante que é 1 argumento posicional
|
| 80 |
await msg.update("Erro de conexão: Não foi possível se conectar ao backend. Verifique se ele está rodando.")
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
-
# CORREÇÃO:
|
| 84 |
-
|
|
|
|
| 85 |
|
| 86 |
@cl.on_message
|
| 87 |
async def main(message: cl.Message):
|
|
@@ -118,6 +115,7 @@ async def main(message: cl.Message):
|
|
| 118 |
except httpx.ConnectError:
|
| 119 |
await msg.update("Erro de conexão: Não foi possível se conectar ao backend. Verifique se ele está rodando.")
|
| 120 |
except Exception as e:
|
| 121 |
-
|
|
|
|
| 122 |
|
| 123 |
-
|
|
|
|
| 1 |
+
--- START OF FILE app (24).py ---
|
| 2 |
|
| 3 |
import chainlit as cl
|
| 4 |
import httpx
|
| 5 |
+
import os
|
| 6 |
+
import mimetypes # NOVO: Para tentar determinar o MIME type se o file.mime não existir
|
| 7 |
|
| 8 |
# URL do nosso backend FastAPI
|
| 9 |
API_URL = "http://localhost:8000"
|
|
|
|
| 37 |
msg = cl.Message(content=f"Recebido: {file.name}. Processando...", author="Sistema")
|
| 38 |
await msg.send()
|
| 39 |
|
|
|
|
| 40 |
file_content = None
|
| 41 |
+
|
| 42 |
try:
|
| 43 |
+
# 1. CORREÇÃO: Lendo o conteúdo do arquivo do disco (file.path)
|
| 44 |
with open(file.path, "rb") as f:
|
| 45 |
file_content = f.read()
|
| 46 |
|
| 47 |
+
# 2. CORREÇÃO: Obtendo o MIME Type de forma robusta
|
| 48 |
+
# Tentativa 1: file.type (padrão Chainlit moderno)
|
| 49 |
+
# Tentativa 2: Assumir "application/pdf" (já que o accept é só PDF)
|
| 50 |
+
mime_type = getattr(file, 'type', 'application/pdf')
|
| 51 |
|
| 52 |
+
# 3. Criando o payload
|
| 53 |
+
files_payload = {"file": (file.name, file_content, mime_type)}
|
| 54 |
+
|
| 55 |
+
# 4. Envia o arquivo para o backend
|
| 56 |
async with httpx.AsyncClient(timeout=300) as client:
|
|
|
|
|
|
|
|
|
|
| 57 |
response = await client.post(
|
| 58 |
UPLOAD_URL,
|
| 59 |
files=files_payload
|
|
|
|
| 62 |
# Se o upload/processamento for bem-sucedido
|
| 63 |
if response.status_code == 200:
|
| 64 |
RAG_INITIALIZED = True
|
|
|
|
| 65 |
await msg.update(
|
| 66 |
f"✅ **{file.name}** processado com sucesso! Agora você pode fazer perguntas sobre o conteúdo do documento."
|
| 67 |
)
|
| 68 |
else:
|
| 69 |
RAG_INITIALIZED = False
|
|
|
|
| 70 |
error_detail = response.json().get('detail', 'Erro desconhecido')
|
| 71 |
await msg.update(
|
| 72 |
f"❌ Erro ao processar o arquivo. Código: {response.status_code}. Detalhe: {error_detail}"
|
| 73 |
)
|
| 74 |
|
| 75 |
except httpx.ConnectError:
|
|
|
|
| 76 |
await msg.update("Erro de conexão: Não foi possível se conectar ao backend. Verifique se ele está rodando.")
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
+
# CORREÇÃO: Simplificando o bloco para evitar o erro de 2 argumentos
|
| 80 |
+
error_message = f"Ocorreu um erro: {e}"
|
| 81 |
+
await msg.update(error_message)
|
| 82 |
|
| 83 |
@cl.on_message
|
| 84 |
async def main(message: cl.Message):
|
|
|
|
| 115 |
except httpx.ConnectError:
|
| 116 |
await msg.update("Erro de conexão: Não foi possível se conectar ao backend. Verifique se ele está rodando.")
|
| 117 |
except Exception as e:
|
| 118 |
+
error_message = f"Ocorreu um erro: {e}"
|
| 119 |
+
await msg.update(error_message)
|
| 120 |
|
| 121 |
+
--- END OF FILE app (24).py ---
|