Spaces:
Running
Running
NoeMartinezSanchez commited on
Commit ·
505f87d
1
Parent(s): dad3f31
Fix dashboard endpoint
Browse files- api/main.py +29 -71
api/main.py
CHANGED
|
@@ -43,6 +43,35 @@ app = FastAPI(
|
|
| 43 |
redoc_url="/api/redoc"
|
| 44 |
)
|
| 45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
# Configurar CORS
|
| 47 |
app.add_middleware(
|
| 48 |
CORSMiddleware,
|
|
@@ -111,7 +140,6 @@ async def startup_event():
|
|
| 111 |
else:
|
| 112 |
logger.warning(f"⚠️ test_set.json no encontrado en: {test_set_path}")
|
| 113 |
logger.warning(" La evaluación automática no se ejecutará")
|
| 114 |
-
# Listar archivos en evaluation/
|
| 115 |
eval_dir = "evaluation"
|
| 116 |
if os.path.exists(eval_dir):
|
| 117 |
files = os.listdir(eval_dir)
|
|
@@ -123,24 +151,6 @@ async def startup_event():
|
|
| 123 |
logger.error(f"Error inicializando RAG: {e}")
|
| 124 |
app.state.menu = {}
|
| 125 |
|
| 126 |
-
@app.get("/dashboard")
|
| 127 |
-
async def get_dashboard():
|
| 128 |
-
"""Servir el dashboard desde almacenamiento persistente"""
|
| 129 |
-
dashboard_path = "/data/dashboard.html"
|
| 130 |
-
|
| 131 |
-
if os.path.exists(dashboard_path):
|
| 132 |
-
return FileResponse(dashboard_path, media_type="text/html")
|
| 133 |
-
else:
|
| 134 |
-
return HTMLResponse(content="""
|
| 135 |
-
<html>
|
| 136 |
-
<head><title>Dashboard no disponible</title></head>
|
| 137 |
-
<body>
|
| 138 |
-
<h1>📊 Dashboard no disponible</h1>
|
| 139 |
-
<p>La evaluación automática aún no ha generado el dashboard.</p>
|
| 140 |
-
<p>Revisa los logs del Space para ver el progreso.</p>
|
| 141 |
-
</body>
|
| 142 |
-
</html>
|
| 143 |
-
""", status_code=202)
|
| 144 |
|
| 145 |
@app.get("/")
|
| 146 |
async def root():
|
|
@@ -363,58 +373,6 @@ async def get_menu():
|
|
| 363 |
return {"menu": app.state.menu}
|
| 364 |
return {"menu": {}}
|
| 365 |
|
| 366 |
-
@app.get("/dashboard", response_class=HTMLResponse)
|
| 367 |
-
async def serve_dashboard():
|
| 368 |
-
import subprocess
|
| 369 |
-
import tempfile
|
| 370 |
-
|
| 371 |
-
# Intentar múltiples ubicaciones (prioridad: /data/)
|
| 372 |
-
posibles_rutas = [
|
| 373 |
-
"/data/dashboard.html",
|
| 374 |
-
"/tmp/dashboard.html",
|
| 375 |
-
"/app/tmp/dashboard.html",
|
| 376 |
-
os.path.join(tempfile.gettempdir(), "dashboard.html"),
|
| 377 |
-
"evaluation/dashboard.html",
|
| 378 |
-
]
|
| 379 |
-
|
| 380 |
-
for ruta in posibles_rutas:
|
| 381 |
-
if os.path.exists(ruta):
|
| 382 |
-
try:
|
| 383 |
-
with open(ruta, "r", encoding="utf-8") as f:
|
| 384 |
-
content = f.read()
|
| 385 |
-
logger.info(f"✅ Dashboard encontrado en: {ruta}")
|
| 386 |
-
return HTMLResponse(content=content)
|
| 387 |
-
except Exception as e:
|
| 388 |
-
logger.error(f"Error leyendo {ruta}: {e}")
|
| 389 |
-
|
| 390 |
-
# Si no existe, intentar generarlo de nuevo
|
| 391 |
-
try:
|
| 392 |
-
from evaluation.generate_dashboard import generate_dashboard
|
| 393 |
-
dashboard_path = generate_dashboard(output_path="/data/dashboard.html")
|
| 394 |
-
if dashboard_path and os.path.exists(dashboard_path):
|
| 395 |
-
with open(dashboard_path, "r", encoding="utf-8") as f:
|
| 396 |
-
return HTMLResponse(content=f.read())
|
| 397 |
-
except Exception as e:
|
| 398 |
-
logger.error(f"Error generando dashboard: {e}")
|
| 399 |
-
|
| 400 |
-
# Mostrar estado de la evaluación
|
| 401 |
-
summary_path = "/data/evaluation_summary.json"
|
| 402 |
-
if os.path.exists(summary_path):
|
| 403 |
-
with open(summary_path, "r") as f:
|
| 404 |
-
summary = f.read()
|
| 405 |
-
return HTMLResponse(content=f"""
|
| 406 |
-
<html>
|
| 407 |
-
<head><title>Dashboard - Prepa en Línea SEP</title></head>
|
| 408 |
-
<body>
|
| 409 |
-
<h1>📊 Resultados de Evaluación</h1>
|
| 410 |
-
<pre>{summary}</pre>
|
| 411 |
-
<p>El dashboard HTML no está disponible. Los resultados están arriba.</p>
|
| 412 |
-
</body>
|
| 413 |
-
</html>
|
| 414 |
-
""")
|
| 415 |
-
|
| 416 |
-
return HTMLResponse(content="<h1>Dashboard no disponible</h1><p>La evaluación aún no ha generado resultados.</p>", status_code=202)
|
| 417 |
-
|
| 418 |
|
| 419 |
@app.get("/evaluation-results")
|
| 420 |
async def get_evaluation_results():
|
|
|
|
| 43 |
redoc_url="/api/redoc"
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# ============================================================
|
| 47 |
+
# ENDPOINT DEL DASHBOARD - Debe ser el primero después de crear la app
|
| 48 |
+
# ============================================================
|
| 49 |
+
@app.get("/dashboard", response_class=HTMLResponse)
|
| 50 |
+
async def get_dashboard():
|
| 51 |
+
"""Servir el dashboard desde el almacenamiento persistente"""
|
| 52 |
+
import os
|
| 53 |
+
from fastapi.responses import HTMLResponse
|
| 54 |
+
|
| 55 |
+
dashboard_path = "/data/dashboard.html"
|
| 56 |
+
|
| 57 |
+
if os.path.exists(dashboard_path):
|
| 58 |
+
with open(dashboard_path, "r", encoding="utf-8") as f:
|
| 59 |
+
html_content = f.read()
|
| 60 |
+
return HTMLResponse(content=html_content)
|
| 61 |
+
else:
|
| 62 |
+
return HTMLResponse(content=f"""
|
| 63 |
+
<html>
|
| 64 |
+
<head><title>Dashboard no disponible</title></head>
|
| 65 |
+
<body>
|
| 66 |
+
<h1>📊 Dashboard no disponible</h1>
|
| 67 |
+
<p>El archivo dashboard.html no existe en: {dashboard_path}</p>
|
| 68 |
+
<p>Ejecuta la evaluación automática primero.</p>
|
| 69 |
+
<hr>
|
| 70 |
+
<p><small>Endpoint probado correctamente.</small></p>
|
| 71 |
+
</body>
|
| 72 |
+
</html>
|
| 73 |
+
""", status_code=404)
|
| 74 |
+
|
| 75 |
# Configurar CORS
|
| 76 |
app.add_middleware(
|
| 77 |
CORSMiddleware,
|
|
|
|
| 140 |
else:
|
| 141 |
logger.warning(f"⚠️ test_set.json no encontrado en: {test_set_path}")
|
| 142 |
logger.warning(" La evaluación automática no se ejecutará")
|
|
|
|
| 143 |
eval_dir = "evaluation"
|
| 144 |
if os.path.exists(eval_dir):
|
| 145 |
files = os.listdir(eval_dir)
|
|
|
|
| 151 |
logger.error(f"Error inicializando RAG: {e}")
|
| 152 |
app.state.menu = {}
|
| 153 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
@app.get("/")
|
| 156 |
async def root():
|
|
|
|
| 373 |
return {"menu": app.state.menu}
|
| 374 |
return {"menu": {}}
|
| 375 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
|
| 377 |
@app.get("/evaluation-results")
|
| 378 |
async def get_evaluation_results():
|