Change
Browse files- app.py +49 -82
- requirements.txt +2 -2
app.py
CHANGED
@@ -1,111 +1,78 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
from
|
|
|
|
|
|
|
4 |
|
5 |
# Configuration
|
6 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
7 |
WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET")
|
8 |
SPACES_TO_RESTART = [
|
9 |
-
"OrganizedProgrammers/
|
10 |
"OrganizedProgrammers/SpecSplitter"
|
11 |
]
|
12 |
|
13 |
-
# Initialiser l'API Hugging Face
|
14 |
api = HfApi()
|
15 |
|
16 |
-
#
|
17 |
-
|
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 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
59 |
|
60 |
# Vérifier si c'est une mise à jour de dataset
|
61 |
-
if payload.repo.type == "dataset" and
|
62 |
-
|
63 |
|
64 |
-
|
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 |
-
|
71 |
-
print(success_msg)
|
72 |
-
results.append(success_msg)
|
73 |
except Exception as e:
|
74 |
-
|
75 |
-
print(error_msg)
|
76 |
-
results.append(error_msg)
|
77 |
|
78 |
-
return {
|
79 |
-
|
80 |
-
|
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 |
-
|
92 |
-
|
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 |
-
"
|
100 |
-
"configured_spaces": SPACES_TO_RESTART
|
101 |
}
|
102 |
|
103 |
-
#
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
109 |
|
110 |
-
|
111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
5 |
-
|
|
|
1 |
fastapi
|
2 |
uvicorn[standard]
|
3 |
python-dotenv
|
4 |
+
huggingface_hub
|
5 |
+
gradio
|