Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Hugging Face Spaces entry point (Gradio SDK, free tier).
|
| 3 |
+
|
| 4 |
+
Docker Spaces are paid, so the existing FastAPI endpoints from main.py are
|
| 5 |
+
served through Gradio's own server instead. On Spaces the platform expects
|
| 6 |
+
the app to start via `demo.launch()`, which manages the port itself, so we
|
| 7 |
+
don't run uvicorn directly.
|
| 8 |
+
|
| 9 |
+
The API is unchanged:
|
| 10 |
+
GET /hello
|
| 11 |
+
POST /predict
|
| 12 |
+
/docs (interactive API documentation)
|
| 13 |
+
A small Gradio page is shown at "/".
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
# ZeroGPU hardware refuses to start unless at least one function is
|
| 17 |
+
# decorated with @spaces.GPU. The model runs on CPU, so this placeholder is
|
| 18 |
+
# never called. On CPU hardware or locally `spaces` may be missing; skip it.
|
| 19 |
+
try:
|
| 20 |
+
import spaces
|
| 21 |
+
|
| 22 |
+
@spaces.GPU
|
| 23 |
+
def _zerogpu_placeholder():
|
| 24 |
+
return None
|
| 25 |
+
except ImportError:
|
| 26 |
+
pass
|
| 27 |
+
|
| 28 |
+
import gradio as gr
|
| 29 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 30 |
+
from fastapi.routing import APIRoute
|
| 31 |
+
from starlette.middleware import Middleware
|
| 32 |
+
|
| 33 |
+
import main # FastAPI app, Pydantic schema and the loaded model
|
| 34 |
+
|
| 35 |
+
# Reuse the exact route definitions (validation + response model) from main.py.
|
| 36 |
+
api_routes = [route for route in main.app.routes if isinstance(route, APIRoute)]
|
| 37 |
+
|
| 38 |
+
with gr.Blocks(title="MindScope API") as demo:
|
| 39 |
+
gr.Markdown(
|
| 40 |
+
"""
|
| 41 |
+
# MindScope API
|
| 42 |
+
This Space hosts the prediction API for the MindScope project.
|
| 43 |
+
|
| 44 |
+
- `GET /hello` — health check
|
| 45 |
+
- `POST /predict` — returns `predicted_mental_health_score`
|
| 46 |
+
- `/docs` — interactive API documentation
|
| 47 |
+
|
| 48 |
+
Educational project. Predictions are machine learning estimates,
|
| 49 |
+
not clinical assessments.
|
| 50 |
+
"""
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch(
|
| 55 |
+
server_name="0.0.0.0",
|
| 56 |
+
app_kwargs={
|
| 57 |
+
# Registered before Gradio's own routes, so they take priority.
|
| 58 |
+
"routes": api_routes,
|
| 59 |
+
# Same CORS policy as main.py, so the Vercel frontend can call the API.
|
| 60 |
+
"middleware": [
|
| 61 |
+
Middleware(
|
| 62 |
+
CORSMiddleware,
|
| 63 |
+
allow_origins=["*"],
|
| 64 |
+
allow_methods=["*"],
|
| 65 |
+
allow_headers=["*"],
|
| 66 |
+
)
|
| 67 |
+
],
|
| 68 |
+
"docs_url": "/docs",
|
| 69 |
+
},
|
| 70 |
+
)
|