Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
from time import sleep
|
| 3 |
from huggingface_hub import HfApi
|
| 4 |
from datetime import datetime, timedelta
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
#
|
|
@@ -18,16 +20,11 @@ import gradio as gr
|
|
| 18 |
#
|
| 19 |
|
| 20 |
|
| 21 |
-
# --- Configuration ---
|
| 22 |
-
# Get Space ID and Token from environment variables (Secrets)
|
| 23 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 24 |
-
# REPOS = ["lainlives/bldr", "lainlives/starter"]
|
| 25 |
repos_env = os.getenv("REPOS")
|
| 26 |
-
|
| 27 |
-
# RESTART_TIMES = ["06:00", "12:00", "18:00", "00:00"]
|
| 28 |
|
| 29 |
|
| 30 |
-
times_env = os.getenv("RESTART_TIMES")
|
| 31 |
REPOS = [t.strip() for t in repos_env.split(",")]
|
| 32 |
RESTART_TIMES = [t.strip() for t in times_env.split(",")]
|
| 33 |
HELPER = "lainlives/starter"
|
|
@@ -92,28 +89,16 @@ def get_last_reboot_status():
|
|
| 92 |
return f"Next target: {next_reboot_datetime.strftime('%Y-%m-%d %H:%M:%S')}"
|
| 93 |
|
| 94 |
|
| 95 |
-
def
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 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():
|
|
@@ -152,7 +137,6 @@ def check_and_reboot():
|
|
| 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()
|
|
@@ -160,6 +144,22 @@ def pipeline():
|
|
| 160 |
return countdown, status, target_info
|
| 161 |
|
| 162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
# --- Gradio Interface ---
|
| 164 |
with gr.Blocks() as demo:
|
| 165 |
gr.Markdown("## Space Rebooter")
|
|
@@ -178,4 +178,5 @@ with gr.Blocks() as demo:
|
|
| 178 |
)
|
| 179 |
|
| 180 |
if __name__ == "__main__":
|
|
|
|
| 181 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import time
|
| 3 |
from time import sleep
|
| 4 |
from huggingface_hub import HfApi
|
| 5 |
from datetime import datetime, timedelta
|
| 6 |
+
import threading
|
| 7 |
import gradio as gr
|
| 8 |
|
| 9 |
#
|
|
|
|
| 20 |
#
|
| 21 |
|
| 22 |
|
|
|
|
|
|
|
| 23 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 24 |
repos_env = os.getenv("REPOS")
|
| 25 |
+
times_env = os.getenv("RESTART_TIMES")
|
|
|
|
| 26 |
|
| 27 |
|
|
|
|
| 28 |
REPOS = [t.strip() for t in repos_env.split(",")]
|
| 29 |
RESTART_TIMES = [t.strip() for t in times_env.split(",")]
|
| 30 |
HELPER = "lainlives/starter"
|
|
|
|
| 89 |
return f"Next target: {next_reboot_datetime.strftime('%Y-%m-%d %H:%M:%S')}"
|
| 90 |
|
| 91 |
|
| 92 |
+
def trigger_helper():
|
| 93 |
+
if HF_TOKEN:
|
| 94 |
+
api = HfApi()
|
| 95 |
+
try:
|
| 96 |
+
print(f"Restarting space {HELPER}...")
|
| 97 |
+
api.pause_space(repo_id=HELPER)
|
| 98 |
+
sleep(3) # Brief pause to ensure state registers
|
| 99 |
+
api.restart_space(repo_id=HELPER, token=HF_TOKEN)
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"Failed to restart {HELPER}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
|
| 104 |
def check_and_reboot():
|
|
|
|
| 137 |
def pipeline():
|
| 138 |
# Run the check
|
| 139 |
status = check_and_reboot()
|
|
|
|
| 140 |
# Return updated UI elements
|
| 141 |
countdown = time_until_next_reboot()
|
| 142 |
target_info = get_last_reboot_status()
|
|
|
|
| 144 |
return countdown, status, target_info
|
| 145 |
|
| 146 |
|
| 147 |
+
def sanitize():
|
| 148 |
+
now = datetime.now()
|
| 149 |
+
target_time = datetime.strptime(SELF_TIME, "%H:%M").time()
|
| 150 |
+
next_run = datetime.combine(now.date(), target_time)
|
| 151 |
+
|
| 152 |
+
# If time has passed today, schedule for tomorrow
|
| 153 |
+
if next_run <= now:
|
| 154 |
+
next_run += timedelta(days=1)
|
| 155 |
+
|
| 156 |
+
delay = (next_run - now).total_seconds()
|
| 157 |
+
|
| 158 |
+
timer = threading.Timer(delay, trigger_helper)
|
| 159 |
+
timer.start()
|
| 160 |
+
print(f"Scheduled for {next_run}")
|
| 161 |
+
|
| 162 |
+
|
| 163 |
# --- Gradio Interface ---
|
| 164 |
with gr.Blocks() as demo:
|
| 165 |
gr.Markdown("## Space Rebooter")
|
|
|
|
| 178 |
)
|
| 179 |
|
| 180 |
if __name__ == "__main__":
|
| 181 |
+
sanitize()
|
| 182 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|