|
import os |
|
import gradio as gr |
|
from huggingface_hub import WebhooksServer, WebhookPayload, HfApi |
|
|
|
|
|
HF_TOKEN = os.getenv("HF_TOKEN") |
|
WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET") |
|
SPACES_TO_RESTART = [ |
|
"OrganizedProgrammers/DpcFinder", |
|
"OrganizedProgrammers/SpecSplitter" |
|
] |
|
|
|
|
|
api = HfApi() |
|
|
|
|
|
with gr.Blocks(title="Dataset Update Webhook Server") as ui: |
|
gr.Markdown(""" |
|
# 🔄 Dataset Update Webhook Server |
|
|
|
Ce serveur redémarre automatiquement les Spaces configurés lorsqu'un dataset est mis à jour. |
|
|
|
## Spaces surveillés : |
|
- OrganizedProgrammers/DpcFinder |
|
- OrganizedProgrammers/SpecSplitter |
|
|
|
## Configuration |
|
Assurez-vous que votre webhook Hugging Face pointe vers `/webhooks/dataset_update` |
|
""") |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
gr.Markdown("### Status") |
|
status_text = gr.Textbox( |
|
value="🟢 Serveur en attente de webhooks...", |
|
label="État du serveur", |
|
interactive=False |
|
) |
|
|
|
with gr.Column(): |
|
gr.Markdown("### Dernière activité") |
|
activity_log = gr.Textbox( |
|
value="Aucune activité récente", |
|
label="Log d'activité", |
|
lines=5, |
|
interactive=False |
|
) |
|
|
|
|
|
app = WebhooksServer(ui=ui, webhook_secret=WEBHOOK_SECRET) |
|
|
|
@app.add_webhook |
|
async def dataset_update(payload: WebhookPayload): |
|
""" |
|
Webhook déclenché lors de la mise à jour d'un dataset. |
|
""" |
|
print(f"🔔 Webhook reçu - Type: {payload.repo.type}, Action: {payload.event.action}") |
|
print(f"📦 Repo: {payload.repo.name}") |
|
|
|
|
|
if payload.repo.type == "dataset" and payload.event.action == "update": |
|
print(f"✅ Dataset {payload.repo.name} mis à jour!") |
|
|
|
results = [] |
|
|
|
|
|
for space_id in SPACES_TO_RESTART: |
|
try: |
|
api.restart_space(space_id, token=HF_TOKEN) |
|
success_msg = f"✅ Space redémarré: {space_id}" |
|
print(success_msg) |
|
results.append(success_msg) |
|
except Exception as e: |
|
error_msg = f"❌ Erreur redémarrage {space_id}: {str(e)}" |
|
print(error_msg) |
|
results.append(error_msg) |
|
|
|
return { |
|
"message": "Spaces mis à jour avec succès", |
|
"dataset": payload.repo.name, |
|
"spaces_restarted": SPACES_TO_RESTART, |
|
"results": results |
|
} |
|
else: |
|
print(f"ℹ️ Événement ignoré - Type: {payload.repo.type}, Action: {payload.event.action}") |
|
return { |
|
"message": "Aucune action nécessaire", |
|
"reason": f"Type: {payload.repo.type}, Action: {payload.event.action}" |
|
} |
|
|
|
|
|
@app.add_webhook("/health") |
|
async def health_check(payload: WebhookPayload): |
|
""" |
|
Endpoint de santé pour vérifier que le serveur fonctionne. |
|
""" |
|
return { |
|
"status": "healthy", |
|
"message": "Webhook server is running", |
|
"configured_spaces": SPACES_TO_RESTART |
|
} |
|
|
|
|
|
if __name__ == "__main__": |
|
print("🚀 Démarrage du serveur de webhooks...") |
|
print(f"📋 Spaces configurés: {SPACES_TO_RESTART}") |
|
print(f"🔐 Secret configuré: {'✅' if WEBHOOK_SECRET else '❌'}") |
|
print(f"🔑 Token HF configuré: {'✅' if HF_TOKEN else '❌'}") |
|
|
|
|
|
app.launch() |
|
|