Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,31 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from fastapi import FastAPI, Request
|
| 3 |
from fastapi.responses import FileResponse
|
| 4 |
from fastapi.staticfiles import StaticFiles
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
-
# --- Configuraci贸n de la App
|
| 8 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
| 9 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 10 |
-
templates_dir = "templates"
|
| 11 |
|
| 12 |
-
#
|
| 13 |
@app.get("/")
|
| 14 |
-
async def
|
| 15 |
"""
|
| 16 |
-
|
|
|
|
| 17 |
"""
|
| 18 |
-
path = os.path.join(
|
| 19 |
if os.path.exists(path):
|
| 20 |
return FileResponse(path, media_type="text/html")
|
| 21 |
return {"error": "index.html not found"}
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
#
|
| 25 |
-
# La interfaz visual de Gradio no es importante aqu铆, solo su capacidad de servidor.
|
| 26 |
-
with gr.Blocks(title="Synapse AI") as demo:
|
| 27 |
-
gr.HTML("""
|
| 28 |
-
<div style="text-align: center; padding: 20px; color: #888;">
|
| 29 |
-
<h1>Synapse AI Backend</h1>
|
| 30 |
-
<p>El servidor est谩 activo. La aplicaci贸n principal se sirve directamente.</p>
|
| 31 |
-
<p>Cargando la conciencia colectiva...</p>
|
| 32 |
-
</div>
|
| 33 |
-
""")
|
| 34 |
-
|
| 35 |
-
# Montamos la app de FastAPI dentro de la app de Gradio.
|
| 36 |
-
# Esta es la parte que permite a Gradio manejar las rutas de FastAPI.
|
| 37 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
| 38 |
-
|
| 39 |
-
# ===================================================================
|
| 40 |
-
# LA CORRECCI脫N CLAVE EST脕 AQU脥
|
| 41 |
-
# ===================================================================
|
| 42 |
-
# Le decimos expl铆citamente a Gradio que inicie el servidor web y se quede
|
| 43 |
-
# escuchando. Esta es la l铆nea que faltaba. Sin ella, el script
|
| 44 |
-
# simplemente termina su ejecuci贸n.
|
| 45 |
if __name__ == "__main__":
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, Request
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
from fastapi.staticfiles import StaticFiles
|
| 4 |
import os
|
| 5 |
+
import uvicorn
|
| 6 |
|
| 7 |
+
# --- Configuraci贸n de la App ---
|
| 8 |
app = FastAPI()
|
| 9 |
+
|
| 10 |
+
# 1. Montar el directorio 'static' para servir CSS y JS
|
| 11 |
+
# Esto es crucial para que index.html pueda encontrar main.js y style.css
|
| 12 |
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
| 13 |
|
| 14 |
+
# 2. Definir la ruta ra铆z para servir nuestra aplicaci贸n principal
|
| 15 |
@app.get("/")
|
| 16 |
+
async def read_root(request: Request):
|
| 17 |
"""
|
| 18 |
+
Esta es la 煤nica ruta que importa. Sirve el archivo index.html
|
| 19 |
+
que contiene toda la l贸gica de Synapse AI.
|
| 20 |
"""
|
| 21 |
+
path = os.path.join("templates", "index.html")
|
| 22 |
if os.path.exists(path):
|
| 23 |
return FileResponse(path, media_type="text/html")
|
| 24 |
return {"error": "index.html not found"}
|
| 25 |
|
| 26 |
+
# 3. Punto de entrada para el servidor cuando se ejecuta en Hugging Face
|
| 27 |
+
# Esta secci贸n permite que Hugging Face inicie la aplicaci贸n correctamente.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
if __name__ == "__main__":
|
| 29 |
+
# Hugging Face Spaces asigna el puerto a trav茅s de la variable de entorno PORT
|
| 30 |
+
port = int(os.environ.get("PORT", 7860))
|
| 31 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|