Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,6 +9,8 @@ import pandas as pd
|
|
| 9 |
from datetime import datetime
|
| 10 |
from typing import Dict, Any, List, Optional
|
| 11 |
import threading
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# ----------------------------------------------------------------------
|
| 14 |
# Memory monitoring (no external dependencies)
|
|
@@ -48,6 +50,24 @@ def log_memory_usage():
|
|
| 48 |
# Schedule next check in 60 seconds
|
| 49 |
threading.Timer(60, log_memory_usage).start()
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
# ----------------------------------------------------------------------
|
| 52 |
# Plotly for dashboards
|
| 53 |
# ----------------------------------------------------------------------
|
|
|
|
| 9 |
from datetime import datetime
|
| 10 |
from typing import Dict, Any, List, Optional
|
| 11 |
import threading
|
| 12 |
+
import urllib.request
|
| 13 |
+
import time
|
| 14 |
|
| 15 |
# ----------------------------------------------------------------------
|
| 16 |
# Memory monitoring (no external dependencies)
|
|
|
|
| 50 |
# Schedule next check in 60 seconds
|
| 51 |
threading.Timer(60, log_memory_usage).start()
|
| 52 |
|
| 53 |
+
# ----------------------------------------------------------------------
|
| 54 |
+
# Keep‑alive to prevent idle timeout (runs every 10 minutes)
|
| 55 |
+
# ----------------------------------------------------------------------
|
| 56 |
+
def keep_alive():
|
| 57 |
+
"""Periodically ping the app to avoid idle timeout."""
|
| 58 |
+
url = "http://127.0.0.1:7860/"
|
| 59 |
+
while True:
|
| 60 |
+
time.sleep(600) # 10 minutes
|
| 61 |
+
try:
|
| 62 |
+
with urllib.request.urlopen(url, timeout=5) as response:
|
| 63 |
+
status = response.getcode()
|
| 64 |
+
logging.info(f"Keep‑alive ping: {status}")
|
| 65 |
+
except Exception as e:
|
| 66 |
+
logging.warning(f"Keep‑alive failed: {e}")
|
| 67 |
+
|
| 68 |
+
# Start keep‑alive thread (daemon so it exits when main process ends)
|
| 69 |
+
threading.Thread(target=keep_alive, daemon=True).start()
|
| 70 |
+
|
| 71 |
# ----------------------------------------------------------------------
|
| 72 |
# Plotly for dashboards
|
| 73 |
# ----------------------------------------------------------------------
|