File size: 1,436 Bytes
23bfe85
 
f70fcdd
 
 
23bfe85
f70fcdd
 
23bfe85
 
 
 
 
 
f70fcdd
 
 
23bfe85
bb10a6d
23bfe85
 
bb10a6d
f70fcdd
 
 
 
 
 
23bfe85
 
 
 
 
 
 
 
 
f70fcdd
 
 
23bfe85
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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"}