| | 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() |
| |
|
| | |
| | HF_TOKEN = os.getenv("HF_TOKEN") |
| | WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET") |
| | SPACES_TO_RESTART = [ |
| | "OrganizedProgrammers/DocFinder", |
| | "OrganizedProgrammers/SpecSplitter" |
| | ] |
| |
|
| | api = HfApi() |
| |
|
| | |
| | fastapi_app = FastAPI() |
| |
|
| | @fastapi_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=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)}") |
| | |
| | |
| | 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"} |
| |
|
| | @fastapi_app.get("/health") |
| | async def health_check(): |
| | return { |
| | "status": "healthy", |
| | "spaces": SPACES_TO_RESTART |
| | } |
| |
|
| | |
| | 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 |
| | """) |
| |
|
| | |
| | fastapi_app = gr.mount_gradio_app(fastapi_app, gradio_app, path="/") |
| |
|
| | |
| | if __name__ == "__main__": |
| | uvicorn.run(fastapi_app, host="0.0.0.0", port=7860) |
| |
|