Spaces:
Running on Zero
Running on Zero
Commit ·
d177d64
1
Parent(s): 8609216
fix: production FastAPI root + Gradio 5.16 on ZeroGPU
Browse files- README.md +1 -1
- app.py +10 -29
- requirements.txt +1 -1
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: 🏥
|
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
|
|
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: blue
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.16.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
---
|
app.py
CHANGED
|
@@ -5,15 +5,13 @@ except ImportError:
|
|
| 5 |
has_spaces = False
|
| 6 |
|
| 7 |
import os
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
-
from
|
| 10 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
-
from backend.app.api.v1.router import api_router
|
| 12 |
from backend.app.ml.manager import model_manager
|
| 13 |
-
from backend.app.core.database import init_db
|
| 14 |
from backend.app.core.config import settings
|
| 15 |
|
| 16 |
-
# ZeroGPU
|
| 17 |
if has_spaces:
|
| 18 |
@spaces.GPU
|
| 19 |
def predict_ner(text: str):
|
|
@@ -54,31 +52,14 @@ with gr.Blocks(title="SanjeevaniAI Healthcare API") as demo:
|
|
| 54 |
btn = gr.Button("⚡ Run ZeroGPU Clinical NER", variant="primary")
|
| 55 |
btn.click(fn=predict_ner, inputs=inp, outputs=out)
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
CORSMiddleware,
|
| 60 |
-
allow_origins=["*"],
|
| 61 |
-
allow_credentials=True,
|
| 62 |
-
allow_methods=["*"],
|
| 63 |
-
allow_headers=["*"],
|
| 64 |
-
)
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
@
|
| 68 |
-
async def startup_init():
|
| 69 |
-
await init_db()
|
| 70 |
-
try:
|
| 71 |
-
model_manager.initialize()
|
| 72 |
-
except Exception as e:
|
| 73 |
-
print(f"ML Model initialization notice: {e}")
|
| 74 |
-
|
| 75 |
-
# Mount all FastAPI REST API routes
|
| 76 |
-
demo.app.include_router(api_router, prefix="/api/v1")
|
| 77 |
-
|
| 78 |
-
# Convenience root health check
|
| 79 |
-
@demo.app.get("/health", tags=["Health"])
|
| 80 |
async def root_health():
|
| 81 |
-
return
|
| 82 |
|
| 83 |
if __name__ == "__main__":
|
| 84 |
-
|
|
|
|
|
|
| 5 |
has_spaces = False
|
| 6 |
|
| 7 |
import os
|
| 8 |
+
import uvicorn
|
| 9 |
import gradio as gr
|
| 10 |
+
from backend.app.main import app as fastapi_app
|
|
|
|
|
|
|
| 11 |
from backend.app.ml.manager import model_manager
|
|
|
|
| 12 |
from backend.app.core.config import settings
|
| 13 |
|
| 14 |
+
# ZeroGPU worker function
|
| 15 |
if has_spaces:
|
| 16 |
@spaces.GPU
|
| 17 |
def predict_ner(text: str):
|
|
|
|
| 52 |
btn = gr.Button("⚡ Run ZeroGPU Clinical NER", variant="primary")
|
| 53 |
btn.click(fn=predict_ner, inputs=inp, outputs=out)
|
| 54 |
|
| 55 |
+
# Mount Gradio UI under /gradio so FastAPI handles root / and all /api/v1/ routes directly with zero interference
|
| 56 |
+
app = gr.mount_gradio_app(fastapi_app, demo, path="/gradio")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# Add convenience root health endpoint
|
| 59 |
+
@app.get("/health", tags=["Health"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
async def root_health():
|
| 61 |
+
return {"status": "healthy", "app": settings.APP_NAME, "version": settings.APP_VERSION, "hardware": "ZeroGPU"}
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
port = int(os.environ.get("PORT", 7860))
|
| 65 |
+
uvicorn.run(app, host="0.0.0.0", port=port)
|
requirements.txt
CHANGED
|
@@ -11,7 +11,7 @@ safetensors>=0.4.0
|
|
| 11 |
sentence-transformers>=2.2.0
|
| 12 |
seqeval>=1.2.2
|
| 13 |
spaces>=0.19.0
|
| 14 |
-
gradio=
|
| 15 |
|
| 16 |
# Data Processing & Utilities
|
| 17 |
numpy>=1.24.0
|
|
|
|
| 11 |
sentence-transformers>=2.2.0
|
| 12 |
seqeval>=1.2.2
|
| 13 |
spaces>=0.19.0
|
| 14 |
+
gradio>=5.16.0
|
| 15 |
|
| 16 |
# Data Processing & Utilities
|
| 17 |
numpy>=1.24.0
|