Spaces:
Running on Zero
Running on Zero
Bobby Claude Opus 4.6 commited on
Commit ·
1a642f9
1
Parent(s): 5436ad3
auto-restart HF Space every hour to keep Zero GPU healthy
Browse filesCo-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import importlib.util
|
|
| 5 |
import json
|
| 6 |
import os
|
| 7 |
import sys
|
|
|
|
| 8 |
import time
|
| 9 |
from datetime import datetime
|
| 10 |
from typing import Any, Dict, Generator, List, Optional, Tuple
|
|
@@ -1319,6 +1320,32 @@ with gr.Blocks(theme="Taithrah/Minimal", css=css, js=custom_js, head=head_html,
|
|
| 1319 |
)
|
| 1320 |
|
| 1321 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1322 |
if __name__ == "__main__":
|
| 1323 |
if pipeline is None:
|
| 1324 |
logger.critical("Pipeline failed to initialize. Exiting.")
|
|
|
|
| 5 |
import json
|
| 6 |
import os
|
| 7 |
import sys
|
| 8 |
+
import threading
|
| 9 |
import time
|
| 10 |
from datetime import datetime
|
| 11 |
from typing import Any, Dict, Generator, List, Optional, Tuple
|
|
|
|
| 1320 |
)
|
| 1321 |
|
| 1322 |
|
| 1323 |
+
RESTART_INTERVAL_SECONDS = 3600 # 1 hour
|
| 1324 |
+
|
| 1325 |
+
def _auto_restart_space():
|
| 1326 |
+
"""Background thread that restarts this HF Space every hour to keep Zero GPU healthy."""
|
| 1327 |
+
space_id = os.getenv("SPACE_ID")
|
| 1328 |
+
if not space_id:
|
| 1329 |
+
return
|
| 1330 |
+
try:
|
| 1331 |
+
from huggingface_hub import HfApi
|
| 1332 |
+
api = HfApi()
|
| 1333 |
+
except Exception:
|
| 1334 |
+
logger.warning("huggingface_hub not available; auto-restart disabled")
|
| 1335 |
+
return
|
| 1336 |
+
logger.info("Auto-restart thread started — will restart %s every %ds", space_id, RESTART_INTERVAL_SECONDS)
|
| 1337 |
+
time.sleep(RESTART_INTERVAL_SECONDS)
|
| 1338 |
+
logger.info("Auto-restart: restarting space %s now", space_id)
|
| 1339 |
+
try:
|
| 1340 |
+
api.restart_space(space_id)
|
| 1341 |
+
except Exception as e:
|
| 1342 |
+
logger.error("Auto-restart failed: %s", e)
|
| 1343 |
+
|
| 1344 |
+
if RUNNING_ON_SPACES:
|
| 1345 |
+
_restart_thread = threading.Thread(target=_auto_restart_space, daemon=True)
|
| 1346 |
+
_restart_thread.start()
|
| 1347 |
+
|
| 1348 |
+
|
| 1349 |
if __name__ == "__main__":
|
| 1350 |
if pipeline is None:
|
| 1351 |
logger.critical("Pipeline failed to initialize. Exiting.")
|