Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ from datetime import datetime, timedelta
|
|
| 12 |
# This expects a HF_TOKEN and REPOS and RESTART_TIMES env vars
|
| 13 |
# Expected format is:
|
| 14 |
# RESTART_TIMES: 06:00,12:00,18:00,00:00
|
| 15 |
-
# REPOS: lainlives/bldr,lainlives/
|
| 16 |
#
|
| 17 |
#
|
| 18 |
#
|
|
@@ -31,8 +31,8 @@ repos_env = os.getenv("REPOS")
|
|
| 31 |
times_env = os.getenv("RESTART_TIMES")
|
| 32 |
REPOS = [t.strip() for t in repos_env.split(",")]
|
| 33 |
RESTART_TIMES = [t.strip() for t in times_env.split(",")]
|
| 34 |
-
|
| 35 |
-
|
| 36 |
# Global variable to track the specific target timestamp
|
| 37 |
next_reboot_datetime = None
|
| 38 |
|
|
@@ -92,6 +92,30 @@ def get_last_reboot_status():
|
|
| 92 |
return f"Next target: {next_reboot_datetime.strftime('%Y-%m-%d %H:%M:%S')}"
|
| 93 |
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
def check_and_reboot():
|
| 96 |
"""Checks if current time has passed the scheduled time."""
|
| 97 |
global next_reboot_datetime
|
|
@@ -110,8 +134,8 @@ def check_and_reboot():
|
|
| 110 |
try:
|
| 111 |
print(f"Restarting space {repo}...")
|
| 112 |
api.pause_space(repo_id=repo)
|
| 113 |
-
sleep(
|
| 114 |
-
api.restart_space(repo_id=repo, token=HF_TOKEN)
|
| 115 |
except Exception as e:
|
| 116 |
print(f"Failed to restart {repo}: {e}")
|
| 117 |
|
|
@@ -128,7 +152,7 @@ def check_and_reboot():
|
|
| 128 |
def pipeline():
|
| 129 |
# Run the check
|
| 130 |
status = check_and_reboot()
|
| 131 |
-
|
| 132 |
# Return updated UI elements
|
| 133 |
countdown = time_until_next_reboot()
|
| 134 |
target_info = get_last_reboot_status()
|
|
|
|
| 12 |
# This expects a HF_TOKEN and REPOS and RESTART_TIMES env vars
|
| 13 |
# Expected format is:
|
| 14 |
# RESTART_TIMES: 06:00,12:00,18:00,00:00
|
| 15 |
+
# REPOS: lainlives/bldr,lainlives/ztestzz
|
| 16 |
#
|
| 17 |
#
|
| 18 |
#
|
|
|
|
| 31 |
times_env = os.getenv("RESTART_TIMES")
|
| 32 |
REPOS = [t.strip() for t in repos_env.split(",")]
|
| 33 |
RESTART_TIMES = [t.strip() for t in times_env.split(",")]
|
| 34 |
+
HELPER = "lainlives/starter"
|
| 35 |
+
HELPER_TIME = "00:30"
|
| 36 |
# Global variable to track the specific target timestamp
|
| 37 |
next_reboot_datetime = None
|
| 38 |
|
|
|
|
| 92 |
return f"Next target: {next_reboot_datetime.strftime('%Y-%m-%d %H:%M:%S')}"
|
| 93 |
|
| 94 |
|
| 95 |
+
def check_helper():
|
| 96 |
+
"""Checks if current time has passed the scheduled time."""
|
| 97 |
+
|
| 98 |
+
now = datetime.now()
|
| 99 |
+
|
| 100 |
+
# Check if we have reached or passed the target time
|
| 101 |
+
if now >= HELPER_TIME:
|
| 102 |
+
status_msg = f"Reboot triggered at {now.strftime('%H:%M:%S')}"
|
| 103 |
+
print(status_msg)
|
| 104 |
+
|
| 105 |
+
# 1. Execute Reboots
|
| 106 |
+
if HF_TOKEN:
|
| 107 |
+
api = HfApi()
|
| 108 |
+
try:
|
| 109 |
+
print(f"Restarting space {HELPER}...")
|
| 110 |
+
api.pause_space(repo_id=HELPER)
|
| 111 |
+
sleep(3) # Brief pause to ensure state registers
|
| 112 |
+
api.restart_space(repo_id=HELPER, token=HF_TOKEN)
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"Failed to restart {HELPER}: {e}")
|
| 115 |
+
|
| 116 |
+
return "Space running normally."
|
| 117 |
+
|
| 118 |
+
|
| 119 |
def check_and_reboot():
|
| 120 |
"""Checks if current time has passed the scheduled time."""
|
| 121 |
global next_reboot_datetime
|
|
|
|
| 134 |
try:
|
| 135 |
print(f"Restarting space {repo}...")
|
| 136 |
api.pause_space(repo_id=repo)
|
| 137 |
+
sleep(3) # Brief pause to ensure state registers
|
| 138 |
+
api.restart_space(repo_id=repo, token=HF_TOKEN, factory_reboot=True)
|
| 139 |
except Exception as e:
|
| 140 |
print(f"Failed to restart {repo}: {e}")
|
| 141 |
|
|
|
|
| 152 |
def pipeline():
|
| 153 |
# Run the check
|
| 154 |
status = check_and_reboot()
|
| 155 |
+
check_helper()
|
| 156 |
# Return updated UI elements
|
| 157 |
countdown = time_until_next_reboot()
|
| 158 |
target_info = get_last_reboot_status()
|