om4r932 commited on
Commit
d3ef431
·
1 Parent(s): 458c070
Files changed (2) hide show
  1. app.py +49 -82
  2. requirements.txt +2 -2
app.py CHANGED
@@ -1,111 +1,78 @@
1
  import os
2
  import gradio as gr
3
- from huggingface_hub import WebhooksServer, WebhookPayload, HfApi
 
 
 
4
 
5
  # Configuration
6
  HF_TOKEN = os.getenv("HF_TOKEN")
7
  WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET")
8
  SPACES_TO_RESTART = [
9
- "OrganizedProgrammers/DpcFinder",
10
  "OrganizedProgrammers/SpecSplitter"
11
  ]
12
 
13
- # Initialiser l'API Hugging Face
14
  api = HfApi()
15
 
16
- # Interface utilisateur personnalisée (optionnelle)
17
- with gr.Blocks(title="Dataset Update Webhook Server") as ui:
18
- gr.Markdown("""
19
- # 🔄 Dataset Update Webhook Server
20
-
21
- Ce serveur redémarre automatiquement les Spaces configurés lorsqu'un dataset est mis à jour.
22
-
23
- ## Spaces surveillés :
24
- - OrganizedProgrammers/DpcFinder
25
- - OrganizedProgrammers/SpecSplitter
26
-
27
- ## Configuration
28
- Assurez-vous que votre webhook Hugging Face pointe vers `/webhooks/dataset_update`
29
- """)
30
-
31
- with gr.Row():
32
- with gr.Column():
33
- gr.Markdown("### Status")
34
- status_text = gr.Textbox(
35
- value="🟢 Serveur en attente de webhooks...",
36
- label="État du serveur",
37
- interactive=False
38
- )
39
-
40
- with gr.Column():
41
- gr.Markdown("### Dernière activité")
42
- activity_log = gr.Textbox(
43
- value="Aucune activité récente",
44
- label="Log d'activité",
45
- lines=5,
46
- interactive=False
47
- )
48
 
49
- # Créer le serveur de webhooks avec l'interface personnalisée
50
- app = WebhooksServer(ui=ui, webhook_secret=WEBHOOK_SECRET)
51
-
52
- @app.add_webhook
53
- async def dataset_update(payload: WebhookPayload):
54
- """
55
- Webhook déclenché lors de la mise à jour d'un dataset.
56
- """
57
- print(f"🔔 Webhook reçu - Type: {payload.repo.type}, Action: {payload.event.action}")
58
- print(f"📦 Repo: {payload.repo.name}")
 
 
59
 
60
  # Vérifier si c'est une mise à jour de dataset
61
- if payload.repo.type == "dataset" and payload.event.action == "update":
62
- print(f" Dataset {payload.repo.name} mis à jour!")
63
 
64
- results = []
65
 
66
  # Redémarrer tous les Spaces liés
67
  for space_id in SPACES_TO_RESTART:
68
  try:
69
  api.restart_space(space_id, token=HF_TOKEN)
70
- success_msg = f"✅ Space redémarré: {space_id}"
71
- print(success_msg)
72
- results.append(success_msg)
73
  except Exception as e:
74
- error_msg = f"❌ Erreur redémarrage {space_id}: {str(e)}"
75
- print(error_msg)
76
- results.append(error_msg)
77
 
78
- return {
79
- "message": "Spaces mis à jour avec succès",
80
- "dataset": payload.repo.name,
81
- "spaces_restarted": SPACES_TO_RESTART,
82
- "results": results
83
- }
84
- else:
85
- print(f"ℹ️ Événement ignoré - Type: {payload.repo.type}, Action: {payload.event.action}")
86
- return {
87
- "message": "Aucune action nécessaire",
88
- "reason": f"Type: {payload.repo.type}, Action: {payload.event.action}"
89
- }
90
 
91
- # Webhook de test pour vérifier que le serveur fonctionne
92
- @app.add_webhook("/health")
93
- async def health_check(payload: WebhookPayload):
94
- """
95
- Endpoint de santé pour vérifier que le serveur fonctionne.
96
- """
97
  return {
98
  "status": "healthy",
99
- "message": "Webhook server is running",
100
- "configured_spaces": SPACES_TO_RESTART
101
  }
102
 
103
- # Démarrer le serveur
104
- if __name__ == "__main__":
105
- print("🚀 Démarrage du serveur de webhooks...")
106
- print(f"📋 Spaces configurés: {SPACES_TO_RESTART}")
107
- print(f"🔐 Secret configuré: {'✅' if WEBHOOK_SECRET else '❌'}")
108
- print(f"🔑 Token HF configuré: {'✅' if HF_TOKEN else '❌'}")
 
 
109
 
110
- # Le serveur se lance automatiquement
111
- app.launch()
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import gradio as gr
3
+ from fastapi import FastAPI, Request, HTTPException
4
+ from huggingface_hub import HfApi
5
+ import uvicorn
6
+ from threading import Thread
7
 
8
  # Configuration
9
  HF_TOKEN = os.getenv("HF_TOKEN")
10
  WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET")
11
  SPACES_TO_RESTART = [
12
+ "OrganizedProgrammers/DocFinder",
13
  "OrganizedProgrammers/SpecSplitter"
14
  ]
15
 
 
16
  api = HfApi()
17
 
18
+ # FastAPI pour les webhooks
19
+ fastapi_app = FastAPI()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
+ @fastapi_app.post("/webhook")
22
+ async def handle_webhook(request: Request):
23
+ # Vérification du secret
24
+ received_secret = request.headers.get("X-Webhook-Secret")
25
+ if received_secret != WEBHOOK_SECRET:
26
+ raise HTTPException(status_code=401, detail="Invalid secret")
27
+
28
+ try:
29
+ payload = await request.json()
30
+ print(f"📦 Payload reçu: {payload}")
31
+ except Exception as e:
32
+ raise HTTPException(status_code=400, detail=f"Invalid JSON: {str(e)}")
33
 
34
  # Vérifier si c'est une mise à jour de dataset
35
+ if (payload.get("repo", {}).get("type") == "dataset" and
36
+ payload.get("event", {}).get("action") == "update"):
37
 
38
+ print(f"✅ Dataset {payload['repo']['name']} mis à jour!")
39
 
40
  # Redémarrer tous les Spaces liés
41
  for space_id in SPACES_TO_RESTART:
42
  try:
43
  api.restart_space(space_id, token=HF_TOKEN)
44
+ print(f"✅ Space redémarré: {space_id}")
 
 
45
  except Exception as e:
46
+ print(f"❌ Erreur redémarrage {space_id}: {e}")
 
 
47
 
48
+ return {"message": "Spaces mis à jour avec succès"}
49
+
50
+ return {"message": "Aucune action nécessaire"}
 
 
 
 
 
 
 
 
 
51
 
52
+ @fastapi_app.get("/health")
53
+ async def health_check():
 
 
 
 
54
  return {
55
  "status": "healthy",
56
+ "spaces": SPACES_TO_RESTART
 
57
  }
58
 
59
+ # Interface Gradio
60
+ with gr.Blocks() as gradio_app:
61
+ gr.Markdown("""
62
+ # 🔄 Dataset Update Webhook Server
63
+
64
+ **Status**: Serveur actif et en attente de webhooks
65
+
66
+ **URL du webhook**: `/webhook`
67
 
68
+ **Spaces surveillés**:
69
+ - OrganizedProgrammers/DocFinder
70
+ - OrganizedProgrammers/SpecSplitter
71
+ """)
72
+
73
+ # Monter Gradio sur FastAPI
74
+ fastapi_app = gr.mount_gradio_app(fastapi_app, gradio_app, path="/")
75
+
76
+ # Lancer le serveur
77
+ if __name__ == "__main__":
78
+ uvicorn.run(fastapi_app, host="0.0.0.0", port=7860)
requirements.txt CHANGED
@@ -1,5 +1,5 @@
1
  fastapi
2
  uvicorn[standard]
3
  python-dotenv
4
- requests
5
- huggingface_hub
 
1
  fastapi
2
  uvicorn[standard]
3
  python-dotenv
4
+ huggingface_hub
5
+ gradio