sghorbal commited on
Commit
6cd757a
·
1 Parent(s): 9d1a552

One More Test!

Browse files
Files changed (1) hide show
  1. app.py +48 -70
app.py CHANGED
@@ -1,77 +1,55 @@
1
- import json
2
- import os
3
- import requests
4
- import threading
5
  from fastapi import FastAPI
6
  from fastapi.responses import JSONResponse
7
  import gradio as gr
8
-
9
- URLS_FILE = "urls.json"
10
- INTERVAL = 3600 # 1h
11
 
12
  app = FastAPI()
13
 
14
- def load_urls():
15
- if os.path.exists(URLS_FILE):
16
- with open(URLS_FILE, "r") as f:
17
- return json.load(f)
18
- return []
19
-
20
- def save_urls(urls):
21
- with open(URLS_FILE, "w") as f:
22
- json.dump(urls, f)
23
-
24
- def add_url(url):
25
- urls = load_urls()
26
- if url and url not in urls:
27
- urls.append(url)
28
- save_urls(urls)
29
- return urls
30
-
31
- def remove_url(url):
32
- urls = load_urls()
33
- if url in urls:
34
- urls.remove(url)
35
- save_urls(urls)
36
- return urls
37
-
38
- def ping_all():
39
- for url in load_urls():
40
- try:
41
- requests.get(url, timeout=5)
42
- print(f"[OK] {url}")
43
- except Exception as e:
44
- print(f"[ERR] {url} -> {e}")
45
-
46
- def schedule_ping():
47
- ping_all()
48
- threading.Timer(INTERVAL, schedule_ping).start()
49
-
50
- # Lancer au démarrage
51
- schedule_ping()
52
-
53
  @app.get("/check_health")
54
- async def check_health():
55
- return JSONResponse({"status": "ok", "message": "App is running"})
56
-
57
- # Gradio UI
58
- with gr.Blocks() as interface:
59
- gr.Markdown("## 🌐 Gestionnaire de Keep-Alive")
60
-
61
- with gr.Row():
62
- input_url = gr.Textbox(label="Ajouter une URL")
63
- add_btn = gr.Button("Ajouter")
64
-
65
- url_list = gr.List(label="URLs suivies", value=load_urls())
66
-
67
- with gr.Row():
68
- input_remove = gr.Textbox(label="Supprimer une URL")
69
- remove_btn = gr.Button("Supprimer")
70
-
71
- add_btn.click(fn=add_url, inputs=input_url, outputs=url_list)
72
- remove_btn.click(fn=remove_url, inputs=input_remove, outputs=url_list)
73
-
74
- @app.get("/")
75
- def root():
76
- return gr.mount_gradio_app(app, interface, path="/")
77
-
 
 
 
 
 
1
  from fastapi import FastAPI
2
  from fastapi.responses import JSONResponse
3
  import gradio as gr
4
+ import threading
5
+ import time
6
+ import requests
7
 
8
  app = FastAPI()
9
 
10
+ # Liste d'URLs à pinger
11
+ url_list = ["https://example.com"]
12
+
13
+ # Fonction Gradio pour ajouter / retirer des URLs
14
+ def manage_urls(action, url):
15
+ if action == "Ajouter":
16
+ if url not in url_list:
17
+ url_list.append(url)
18
+ return f"Ajouté: {url}"
19
+ elif action == "Supprimer":
20
+ if url in url_list:
21
+ url_list.remove(url)
22
+ return f"Supprimé: {url}"
23
+ return "Action invalide"
24
+
25
+ # Interface Gradio
26
+ interface = gr.Interface(
27
+ fn=manage_urls,
28
+ inputs=[
29
+ gr.Radio(choices=["Ajouter", "Supprimer"], label="Action"),
30
+ gr.Textbox(label="URL")
31
+ ],
32
+ outputs="text",
33
+ title="Gestion des URLs 🔄",
34
+ )
35
+
36
+ # Route pour vérifier que l'app est vivante
 
 
 
 
 
 
 
 
 
 
 
 
37
  @app.get("/check_health")
38
+ def check_health():
39
+ return JSONResponse(content={"status": "ok", "nb_urls": len(url_list)})
40
+
41
+ # Background thread pour pinger les URLs toutes les heures
42
+ def ping_loop():
43
+ while True:
44
+ for url in url_list:
45
+ try:
46
+ requests.get(url, timeout=5)
47
+ print(f"[ping] {url}")
48
+ except Exception as e:
49
+ print(f"[ping] {url} | {e}")
50
+ time.sleep(3600) # 1h
51
+
52
+ threading.Thread(target=ping_loop, daemon=True).start()
53
+
54
+ # Monter Gradio sur /
55
+ gr.mount_gradio_app(app, interface, path="/")