om4r932 commited on
Commit
23bfe85
·
1 Parent(s): bb10a6d

Reformat app.py + add package

Browse files
Files changed (2) hide show
  1. app.py +26 -25
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,44 +1,45 @@
1
- from fastapi import FastAPI, Request
2
- import requests
3
- from dotenv import load_dotenv
4
  import os
5
 
6
- load_dotenv()
7
  app = FastAPI()
 
8
 
9
  # Configuration
10
- HF_TOKEN = os.getenv("HF_TOKEN") # Token avec droits d'écriture
11
- SPACES = ["OrganizedProgrammers/DocFinder", "OrganizedProgrammers/SpecSplitter"] # Liste des Spaces à redémarrer
12
-
13
- def restart_space(space_id):
14
- """Redémarre un Space Hugging Face via l'API."""
15
- api_url = f"https://api.huggingface.co/spaces/{space_id}/restart"
16
- headers = {"Authorization": f"Bearer {HF_TOKEN}"}
17
- response = requests.post(api_url, headers=headers, verify=False)
18
-
19
- if response.status_code == 200:
20
- print(f"Space redémarré avec succès: {space_id}")
21
- else:
22
- print(f"Échec du redémarrage: {space_id} - {response.text}")
23
 
24
  @app.post("/webhook")
25
  async def handle_webhook(request: Request):
 
26
  received_secret = request.headers.get("X-Webhook-Secret")
27
- expected_secret = os.getenv("WEBHOOK_SECRET")
 
28
 
29
- # Vérifier l'authenticité
30
- if received_secret != expected_secret:
31
- return {"error": "incorrect secret"}, 400
32
  payload = await request.json()
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("Dataset mis à jour, redémarrage des Spaces...")
39
- for space_id in SPACES:
40
- restart_space(space_id)
 
 
 
 
 
 
41
 
42
  return {"message": "Spaces mis à jour avec succès"}
43
 
44
- return {"message": "Aucune action nécessaire"}
 
 
 
 
 
1
+ from fastapi import FastAPI, Request, HTTPException
2
+ from huggingface_hub import HfApi
 
3
  import os
4
 
 
5
  app = FastAPI()
6
+ api = HfApi()
7
 
8
  # Configuration
9
+ HF_TOKEN = os.getenv("HF_TOKEN")
10
+ WEBHOOK_SECRET = os.getenv("WEBHOOK_SECRET")
11
+ SPACES_TO_RESTART = [
12
+ "OrganizedProgrammers/DpcFinder",
13
+ "OrganizedProgrammers/SpecSplitter"
14
+ ]
 
 
 
 
 
 
 
15
 
16
  @app.post("/webhook")
17
  async def handle_webhook(request: Request):
18
+ # Vérification du secret
19
  received_secret = request.headers.get("X-Webhook-Secret")
20
+ if received_secret != WEBHOOK_SECRET:
21
+ raise HTTPException(status_code=400, detail="Invalid secret")
22
 
 
 
 
23
  payload = await request.json()
24
 
25
  # Vérifier si c'est une mise à jour de dataset
26
  if (payload.get("repo", {}).get("type") == "dataset" and
27
  payload.get("event", {}).get("action") == "update"):
28
 
29
+ print(f"Dataset {payload['repo']['name']} mis à jour!")
30
+
31
+ # Redémarrer tous les Spaces liés
32
+ for space_id in SPACES_TO_RESTART:
33
+ try:
34
+ api.restart_space(space_id, token=HF_TOKEN)
35
+ print(f"✅ Space redémarré: {space_id}")
36
+ except Exception as e:
37
+ print(f"❌ Erreur redémarrage {space_id}: {e}")
38
 
39
  return {"message": "Spaces mis à jour avec succès"}
40
 
41
+ return {"message": "Aucune action nécessaire"}
42
+
43
+ @app.get("/")
44
+ async def root():
45
+ return {"message": "Webhook server is running"}
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  fastapi
2
  uvicorn[standard]
3
  python-dotenv
4
- requests
 
 
1
  fastapi
2
  uvicorn[standard]
3
  python-dotenv
4
+ requests
5
+ huggingface_hub