Spaces:
Sleeping
Sleeping
File size: 1,797 Bytes
4d2f2ad 556feab 4d2f2ad | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 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() |