om4r932's picture
Reformat app.py + add package
23bfe85
raw
history blame
1.44 kB
from fastapi import FastAPI, Request, HTTPException
from huggingface_hub import HfApi
import os
app = FastAPI()
api = HfApi()
# Configuration
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):
# Vérification du secret
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()
# Vérifier si c'est une mise à jour de dataset
if (payload.get("repo", {}).get("type") == "dataset" and
payload.get("event", {}).get("action") == "update"):
print(f"Dataset {payload['repo']['name']} mis à jour!")
# Redémarrer tous les Spaces liés
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"}