File size: 2,354 Bytes
f70fcdd
c9903ae
d3ef431
 
f5702a3
d3ef431
 
f70fcdd
f5702a3
 
f70fcdd
23bfe85
 
 
d3ef431
23bfe85
 
f70fcdd
c9903ae
 
d3ef431
 
c9903ae
d3ef431
 
 
 
 
 
 
 
 
 
 
 
f70fcdd
 
d3ef431
 
f70fcdd
d3ef431
23bfe85
 
 
 
 
d3ef431
23bfe85
d3ef431
f70fcdd
d3ef431
 
 
c9903ae
d3ef431
 
c9903ae
 
d3ef431
c9903ae
23bfe85
d3ef431
 
 
 
 
 
 
 
c9903ae
d3ef431
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import os
import gradio as gr
from fastapi import FastAPI, Request, HTTPException
from huggingface_hub import HfApi
from dotenv import load_dotenv
import uvicorn
from threading import Thread

load_dotenv()

# Configuration
HF_TOKEN = os.getenv("HF_TOKEN")
WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET")
SPACES_TO_RESTART = [
    "OrganizedProgrammers/DocFinder",
    "OrganizedProgrammers/SpecSplitter"
]

api = HfApi()

# FastAPI pour les webhooks
fastapi_app = FastAPI()

@fastapi_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=401, detail="Invalid secret")
    
    try:
        payload = await request.json()
        print(f"📦 Payload reçu: {payload}")
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Invalid JSON: {str(e)}")
    
    # 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"}

@fastapi_app.get("/health")
async def health_check():
    return {
        "status": "healthy",
        "spaces": SPACES_TO_RESTART
    }

# Interface Gradio
with gr.Blocks() as gradio_app:
    gr.Markdown("""
    # 🔄 Dataset Update Webhook Server
    
    **Status**: Serveur actif et en attente de webhooks
    
    **URL du webhook**: `/webhook`
    
    **Spaces surveillés**:
    - OrganizedProgrammers/DocFinder  
    - OrganizedProgrammers/SpecSplitter
    """)

# Monter Gradio sur FastAPI
fastapi_app = gr.mount_gradio_app(fastapi_app, gradio_app, path="/")

# Lancer le serveur
if __name__ == "__main__":
    uvicorn.run(fastapi_app, host="0.0.0.0", port=7860)