Spaces:
Running
Running
sghorbal commited on
Commit ·
6cd757a
1
Parent(s): 9d1a552
One More Test!
Browse files
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 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
app = FastAPI()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 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 |
-
|
| 55 |
-
return JSONResponse({"status": "ok", "
|
| 56 |
-
|
| 57 |
-
#
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 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="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|