Spaces:
Sleeping
Sleeping
Tracy André
commited on
Commit
·
53bf046
1
Parent(s):
d65ab6d
updated
Browse files- app.py +76 -29
- requirements.txt +3 -4
app.py
CHANGED
|
@@ -1,36 +1,83 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Hugging Face Space compatible version of the agricultural analysis app.
|
| 3 |
-
This is the main entry point for deployment on Hugging Face Spaces.
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
import os
|
| 7 |
-
import
|
| 8 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 12 |
-
|
| 13 |
-
# Import the main Gradio app
|
| 14 |
from gradio_app import create_gradio_app
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if __name__ == "__main__":
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import json
|
| 3 |
import gradio as gr
|
| 4 |
+
from fastapi import FastAPI, Header, HTTPException, Request
|
| 5 |
+
from fastapi.responses import JSONResponse
|
| 6 |
+
from gradio.routes import mount_gradio_app
|
| 7 |
|
| 8 |
+
# Import your existing Gradio app
|
|
|
|
|
|
|
|
|
|
| 9 |
from gradio_app import create_gradio_app
|
| 10 |
|
| 11 |
+
# ========= Configuration =========
|
| 12 |
+
PORT = int(os.environ.get("PORT", 7860))
|
| 13 |
+
MCP_BEARER = os.getenv("MCP_BEARER", "") # définir dans Spaces > Settings > Variables and secrets
|
| 14 |
+
|
| 15 |
+
# ========= App FastAPI (parent) =========
|
| 16 |
+
api = FastAPI(title="MCP + Gradio on HF Spaces")
|
| 17 |
+
|
| 18 |
+
@api.get("/health")
|
| 19 |
+
def health():
|
| 20 |
+
return {"ok": True}
|
| 21 |
+
|
| 22 |
+
def _check_auth(authorization: str | None):
|
| 23 |
+
"""Vérifie le header Authorization: Bearer <token> si MCP_BEARER est défini."""
|
| 24 |
+
if not MCP_BEARER:
|
| 25 |
+
return
|
| 26 |
+
if not authorization or not authorization.startswith("Bearer "):
|
| 27 |
+
raise HTTPException(status_code=401, detail="Missing or invalid Authorization header")
|
| 28 |
+
token = authorization.split(" ", 1)[1]
|
| 29 |
+
if token != MCP_BEARER:
|
| 30 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 31 |
+
|
| 32 |
+
# ========= Endpoint MCP (exemple minimal) =========
|
| 33 |
+
@api.post("/mcp")
|
| 34 |
+
async def mcp_endpoint(request: Request, authorization: str | None = Header(None)):
|
| 35 |
+
_check_auth(authorization)
|
| 36 |
+
try:
|
| 37 |
+
payload = await request.json()
|
| 38 |
+
except Exception:
|
| 39 |
+
raise HTTPException(status_code=400, detail="Invalid JSON")
|
| 40 |
+
|
| 41 |
+
# TODO: remplace ici par ta logique MCP réelle (dispatch, tools, resources, etc.)
|
| 42 |
+
# Pour démarrer, on renvoie simplement le payload reçu.
|
| 43 |
+
return JSONResponse({"status": "ok", "echo": payload})
|
| 44 |
+
|
| 45 |
+
# ========= UI Gradio =========
|
| 46 |
+
# Use your existing comprehensive agricultural analysis interface
|
| 47 |
+
demo = create_gradio_app()
|
| 48 |
|
| 49 |
+
# Monte Gradio sous la racine "/"
|
| 50 |
+
app = mount_gradio_app(api, demo, path="/")
|
| 51 |
+
|
| 52 |
+
# ========= Entrée (pour exécution locale éventuelle) =========
|
| 53 |
if __name__ == "__main__":
|
| 54 |
+
# En local uniquement ; sur Spaces, le runner est géré par la plateforme.
|
| 55 |
+
import uvicorn
|
| 56 |
+
uvicorn.run(app, host="0.0.0.0", port=PORT)
|
| 57 |
+
|
| 58 |
+
# ========= Tests curl (exemples en commentaires) =========
|
| 59 |
+
# Healthcheck (public, GET)
|
| 60 |
+
# curl -s https://<ORG>-<SPACE>.hf.space/health
|
| 61 |
+
|
| 62 |
+
# MCP sans auth (si MCP_BEARER non défini)
|
| 63 |
+
# curl -s -X POST https://<ORG>-<SPACE>.hf.space/mcp \
|
| 64 |
+
# -H "Content-Type: application/json" \
|
| 65 |
+
# -d '{"ping":"pong"}'
|
| 66 |
+
|
| 67 |
+
# MCP avec Bearer (si MCP_BEARER défini)
|
| 68 |
+
# curl -s -X POST https://<ORG>-<SPACE>.hf.space/mcp \
|
| 69 |
+
# -H "Authorization: Bearer <TON_TOKEN>" \
|
| 70 |
+
# -H "Content-Type: application/json" \
|
| 71 |
+
# -d '{"ping":"pong"}'
|
| 72 |
+
|
| 73 |
+
# Test de l'UI Gradio
|
| 74 |
+
# curl -s https://<ORG>-<SPACE>.hf.space/
|
| 75 |
+
|
| 76 |
+
# Exemples de payloads MCP pour tester les fonctionnalités agricoles:
|
| 77 |
+
# curl -s -X POST https://<ORG>-<SPACE>.hf.space/mcp \
|
| 78 |
+
# -H "Content-Type: application/json" \
|
| 79 |
+
# -d '{"method": "analyze_weed_pressure", "params": {"years": [2020, 2021, 2022], "plots": ["P1", "P2"]}}'
|
| 80 |
+
|
| 81 |
+
# curl -s -X POST https://<ORG>-<SPACE>.hf.space/mcp \
|
| 82 |
+
# -H "Content-Type: application/json" \
|
| 83 |
+
# -d '{"method": "predict_future_pressure", "params": {"target_years": [2025, 2026], "max_ift": 1.0}}'
|
requirements.txt
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
pandas>=2.0.0
|
| 2 |
numpy>=1.24.0
|
| 3 |
matplotlib>=3.6.0
|
| 4 |
seaborn>=0.12.0
|
| 5 |
scikit-learn>=1.3.0
|
| 6 |
-
gradio>=4.0.0
|
| 7 |
-
mcp>=1.0.0
|
| 8 |
datasets>=2.14.0
|
| 9 |
huggingface_hub>=0.17.0
|
| 10 |
openpyxl>=3.1.0
|
| 11 |
plotly>=5.15.0
|
| 12 |
-
fastapi>=0.104.0
|
| 13 |
-
uvicorn>=0.24.0
|
| 14 |
python-multipart>=0.0.6
|
|
|
|
| 1 |
+
fastapi>=0.112
|
| 2 |
+
uvicorn[standard]>=0.30
|
| 3 |
+
gradio>=4.43
|
| 4 |
pandas>=2.0.0
|
| 5 |
numpy>=1.24.0
|
| 6 |
matplotlib>=3.6.0
|
| 7 |
seaborn>=0.12.0
|
| 8 |
scikit-learn>=1.3.0
|
|
|
|
|
|
|
| 9 |
datasets>=2.14.0
|
| 10 |
huggingface_hub>=0.17.0
|
| 11 |
openpyxl>=3.1.0
|
| 12 |
plotly>=5.15.0
|
|
|
|
|
|
|
| 13 |
python-multipart>=0.0.6
|