Spaces:
Sleeping
Sleeping
feat: chatbot with gradio
Browse files- chatbot_gui.py +47 -0
- requirements.txt +2 -0
chatbot_gui.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import httpx
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Cargamos variables de entorno
|
| 7 |
+
load_dotenv(os.path.join(os.path.dirname(__file__), '../.env'))
|
| 8 |
+
|
| 9 |
+
API_URL = os.getenv("API_URL", "https://sherlock-production-67ce.up.railway.app/final_inference")
|
| 10 |
+
|
| 11 |
+
async def responder(message, history):
|
| 12 |
+
# Usamos un bloque 'async with' para gestionar el cliente de HTTPX
|
| 13 |
+
# Definimos un timeout alto (60s) porque File Search puede demorar
|
| 14 |
+
limits = httpx.Limits(max_keepalive_connections=5, max_connections=10)
|
| 15 |
+
timeout = httpx.Timeout(60.0, connect=10.0)
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
async with httpx.AsyncClient(timeout=timeout, limits=limits) as client:
|
| 19 |
+
payload = {"query": message}
|
| 20 |
+
|
| 21 |
+
response = await client.post(API_URL, json=payload)
|
| 22 |
+
response.raise_for_status()
|
| 23 |
+
|
| 24 |
+
data = response.json()
|
| 25 |
+
#print(f"DEBUG: Lo que respondió el backend -> {data}")
|
| 26 |
+
# Ajusta 'response' según el nombre del campo que devuelva tu FastAPI
|
| 27 |
+
return data.get("answer", "No se obtuvo respuesta del servidor.")
|
| 28 |
+
|
| 29 |
+
except httpx.HTTPStatusError as e:
|
| 30 |
+
return f"❌ Error del servidor ({e.response.status_code}): {e.response.text}"
|
| 31 |
+
except httpx.RequestError as e:
|
| 32 |
+
return f"⚠️ Error de conexión: No se pudo contactar a la API. ¿Está encendida?"
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return f"🔥 Error inesperado: {str(e)}"
|
| 35 |
+
|
| 36 |
+
examples = examples = ["¿Cuáles son las obligaciones de la entidad crediticia en un crédito digital?"]
|
| 37 |
+
|
| 38 |
+
# Configuración de Gradio
|
| 39 |
+
demo = gr.ChatInterface(
|
| 40 |
+
fn=responder,
|
| 41 |
+
title="Asistente Almah",
|
| 42 |
+
description="Consulta información técnica basada en los manuales subidos.",
|
| 43 |
+
examples=examples,
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
httpx
|