chatbot_almah / app.py
JuDaLe's picture
Update app.py
556feab verified
Raw
History Blame Contribute Delete
1.8 kB
import gradio as gr
import httpx
import os
from dotenv import load_dotenv
# Cargamos variables de entorno
load_dotenv(os.path.join(os.path.dirname(__file__), '../.env'))
API_URL = os.getenv("API_URL", "https://sherlock-production-67ce.up.railway.app/final_inference")
async def responder(message, history):
# Usamos un bloque 'async with' para gestionar el cliente de HTTPX
# Definimos un timeout alto (60s) porque File Search puede demorar
limits = httpx.Limits(max_keepalive_connections=5, max_connections=10)
timeout = httpx.Timeout(60.0, connect=10.0)
try:
async with httpx.AsyncClient(timeout=timeout, limits=limits) as client:
payload = {"query": message}
response = await client.post(API_URL, json=payload)
response.raise_for_status()
data = response.json()
#print(f"DEBUG: Lo que respondió el backend -> {data}")
# Ajusta 'response' según el nombre del campo que devuelva tu FastAPI
return data.get("answer", "No se obtuvo respuesta del servidor.")
except httpx.HTTPStatusError as e:
return f"❌ Error del servidor ({e.response.status_code}): {e.response.text}"
except httpx.RequestError as e:
return f"⚠️ Error de conexión: No se pudo contactar a la API. ¿Está encendida?"
except Exception as e:
return f"🔥 Error inesperado: {str(e)}"
examples = ["¿Cuáles son las obligaciones de la entidad crediticia en un crédito digital?"]
# Configuración de Gradio
demo = gr.ChatInterface(
fn=responder,
title="Asistente Almah",
description="Consulta información técnica basada en los manuales subidos.",
examples=examples,
)
if __name__ == "__main__":
demo.launch()