|
from fastapi import FastAPI, Request, HTTPException |
|
from huggingface_hub import HfApi |
|
import os |
|
|
|
app = FastAPI() |
|
api = HfApi() |
|
|
|
|
|
HF_TOKEN = os.getenv("HF_TOKEN") |
|
WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET") |
|
SPACES_TO_RESTART = [ |
|
"OrganizedProgrammers/DpcFinder", |
|
"OrganizedProgrammers/SpecSplitter" |
|
] |
|
|
|
@app.post("/webhook") |
|
async def handle_webhook(request: Request): |
|
|
|
received_secret = request.headers.get("X-Webhook-Secret") |
|
if received_secret != WEBHOOK_SECRET: |
|
raise HTTPException(status_code=400, detail="Invalid secret") |
|
|
|
payload = await request.json() |
|
|
|
|
|
if (payload.get("repo", {}).get("type") == "dataset" and |
|
payload.get("event", {}).get("action") == "update"): |
|
|
|
print(f"Dataset {payload['repo']['name']} mis à jour!") |
|
|
|
|
|
for space_id in SPACES_TO_RESTART: |
|
try: |
|
api.restart_space(space_id, token=HF_TOKEN) |
|
print(f"✅ Space redémarré: {space_id}") |
|
except Exception as e: |
|
print(f"❌ Erreur redémarrage {space_id}: {e}") |
|
|
|
return {"message": "Spaces mis à jour avec succès"} |
|
|
|
return {"message": "Aucune action nécessaire"} |
|
|
|
@app.get("/") |
|
async def root(): |
|
return {"message": "Webhook server is running"} |