Spaces:
Sleeping
Sleeping
Déploiement depuis GitHub Actions
Browse files- README.md +3 -3
- space_app.py +23 -18
README.md
CHANGED
|
@@ -15,7 +15,7 @@ pinned: false
|
|
| 15 |
API FastAPI servant un modèle scikit-learn (RandomForest) entraîné sur le jeu de données Iris.
|
| 16 |
Déployée automatiquement depuis GitHub Actions.
|
| 17 |
|
| 18 |
-
- `GET /` : interface
|
| 19 |
-
- `
|
| 20 |
-
- `
|
| 21 |
- `GET /docs` : documentation Swagger
|
|
|
|
| 15 |
API FastAPI servant un modèle scikit-learn (RandomForest) entraîné sur le jeu de données Iris.
|
| 16 |
Déployée automatiquement depuis GitHub Actions.
|
| 17 |
|
| 18 |
+
- `GET /` : interface Gradio
|
| 19 |
+
- `POST /predict` : prédiction (API FastAPI)
|
| 20 |
+
- `GET /health` : état du service
|
| 21 |
- `GET /docs` : documentation Swagger
|
space_app.py
CHANGED
|
@@ -1,23 +1,26 @@
|
|
| 1 |
-
"""Point d'entrée du Space Hugging Face (SDK Gradio).
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
| 5 |
"""
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
import gradio as gr
|
| 9 |
-
import uvicorn
|
| 10 |
-
|
| 11 |
-
from app.main import CLASSES, IrisInput, app, predict
|
| 12 |
-
|
| 13 |
try:
|
| 14 |
-
#
|
| 15 |
import spaces
|
| 16 |
gpu = spaces.GPU
|
| 17 |
except ImportError:
|
| 18 |
def gpu(fn):
|
| 19 |
return fn
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
@gpu
|
| 23 |
def gradio_predict(sepal_length, sepal_width, petal_length, petal_width):
|
|
@@ -38,15 +41,17 @@ demo = gr.Interface(
|
|
| 38 |
],
|
| 39 |
outputs=gr.Label(num_top_classes=len(CLASSES), label="Espèce prédite"),
|
| 40 |
title="Classification d'Iris (MLOps)",
|
| 41 |
-
description="Modèle scikit-learn servi par FastAPI. API : /predict · Swagger : /docs",
|
| 42 |
)
|
| 43 |
|
| 44 |
-
# ssr_mode=False : sinon, sur Hugging Face, Gradio lance un serveur Node.js qui occupe le port 7860
|
| 45 |
-
app = gr.mount_gradio_app(app, demo, path="/gradio", ssr_mode=False)
|
| 46 |
-
|
| 47 |
if __name__ == "__main__":
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Point d'entrée du Space Hugging Face (SDK Gradio, matériel ZeroGPU).
|
| 2 |
|
| 3 |
+
C'est Gradio qui lance le serveur (demo.launch) : ZeroGPU ne démarre correctement que
|
| 4 |
+
de cette façon. On y ajoute ensuite les routes de l'API FastAPI (app/main.py) :
|
| 5 |
+
/predict et /health, documentées sur /docs.
|
| 6 |
"""
|
| 7 |
import os
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
try:
|
| 10 |
+
# ZeroGPU exige au moins une fonction marquée @spaces.GPU (paquet présent sur Hugging Face)
|
| 11 |
import spaces
|
| 12 |
gpu = spaces.GPU
|
| 13 |
except ImportError:
|
| 14 |
def gpu(fn):
|
| 15 |
return fn
|
| 16 |
|
| 17 |
+
import gradio as gr
|
| 18 |
+
from fastapi.routing import APIRoute
|
| 19 |
+
|
| 20 |
+
from app.main import CLASSES, IrisInput, app as api, predict
|
| 21 |
+
|
| 22 |
+
API_PATHS = {"/predict", "/health"}
|
| 23 |
+
|
| 24 |
|
| 25 |
@gpu
|
| 26 |
def gradio_predict(sepal_length, sepal_width, petal_length, petal_width):
|
|
|
|
| 41 |
],
|
| 42 |
outputs=gr.Label(num_top_classes=len(CLASSES), label="Espèce prédite"),
|
| 43 |
title="Classification d'Iris (MLOps)",
|
| 44 |
+
description="Modèle scikit-learn servi par FastAPI. API : POST /predict · Swagger : /docs",
|
| 45 |
)
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
if __name__ == "__main__":
|
| 48 |
+
demo.launch(
|
| 49 |
+
server_name=os.getenv("GRADIO_SERVER_NAME", "0.0.0.0"),
|
| 50 |
+
server_port=int(os.getenv("GRADIO_SERVER_PORT", "7860")),
|
| 51 |
+
ssr_mode=False, # sinon Gradio lance un serveur Node.js en plus sur Hugging Face
|
| 52 |
+
app_kwargs={"docs_url": "/docs"},
|
| 53 |
+
prevent_thread_lock=True,
|
| 54 |
+
)
|
| 55 |
+
# Routes FastAPI placées en tête pour qu'elles passent avant celles de Gradio
|
| 56 |
+
demo.app.router.routes[0:0] = [r for r in api.routes if isinstance(r, APIRoute) and r.path in API_PATHS]
|
| 57 |
+
demo.block_thread()
|