Spaces:
Runtime error
Runtime error
Update scheduler.py
Browse files- scheduler.py +29 -23
scheduler.py
CHANGED
@@ -1,25 +1,24 @@
|
|
1 |
import os
|
2 |
import time
|
|
|
3 |
import schedule
|
4 |
import subprocess
|
5 |
import datetime
|
6 |
import psutil
|
7 |
|
8 |
-
# β
Persistent path in Hugging Face Spaces
|
9 |
MODEL_DIR = "/app/models"
|
10 |
TIMESTAMP_FILE = os.path.join(MODEL_DIR, "last_train.txt")
|
11 |
-
APP_PATH = "
|
|
|
12 |
TRAIN_INTERVAL = datetime.timedelta(days=2)
|
13 |
|
14 |
-
#
|
15 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
16 |
|
17 |
def get_latest_model():
|
18 |
"""Return the latest model file path, or None if no models exist."""
|
19 |
models = [f for f in os.listdir(MODEL_DIR) if f.endswith(".pth")]
|
20 |
-
if
|
21 |
-
return None
|
22 |
-
return os.path.join(MODEL_DIR, sorted(models)[-1])
|
23 |
|
24 |
def should_train():
|
25 |
"""Return True if it's time to retrain the model."""
|
@@ -47,36 +46,43 @@ def is_app_running():
|
|
47 |
def launch_app():
|
48 |
"""Start app.py if not already running."""
|
49 |
if not is_app_running():
|
50 |
-
print(f"[{datetime.datetime.now()}]
|
51 |
-
|
|
|
|
|
|
|
52 |
else:
|
53 |
-
print(f"[{datetime.datetime.now()}]
|
54 |
|
55 |
def train_model():
|
56 |
"""Train model if needed and launch app."""
|
57 |
if should_train():
|
58 |
-
print(f"[{datetime.datetime.now()}]
|
59 |
latest = get_latest_model()
|
60 |
-
if latest:
|
61 |
os.remove(latest)
|
62 |
-
print(f"
|
|
|
63 |
try:
|
64 |
-
subprocess.run([
|
65 |
update_timestamp()
|
66 |
-
print(f"[{datetime.datetime.now()}]
|
67 |
except subprocess.CalledProcessError as e:
|
68 |
-
print(f"[{datetime.datetime.now()}]
|
|
|
|
|
69 |
else:
|
70 |
-
print(f"[{datetime.datetime.now()}]
|
71 |
|
72 |
launch_app()
|
73 |
|
74 |
-
|
75 |
-
|
|
|
76 |
|
77 |
-
# Then every 60 minutes
|
78 |
-
schedule.every(60).minutes.do(train_model)
|
79 |
|
80 |
-
while True:
|
81 |
-
|
82 |
-
|
|
|
1 |
import os
|
2 |
import time
|
3 |
+
import sys
|
4 |
import schedule
|
5 |
import subprocess
|
6 |
import datetime
|
7 |
import psutil
|
8 |
|
|
|
9 |
MODEL_DIR = "/app/models"
|
10 |
TIMESTAMP_FILE = os.path.join(MODEL_DIR, "last_train.txt")
|
11 |
+
APP_PATH = "app.py"
|
12 |
+
TRAIN_SCRIPT = "train.py"
|
13 |
TRAIN_INTERVAL = datetime.timedelta(days=2)
|
14 |
|
15 |
+
# Ensure model dir exists
|
16 |
os.makedirs(MODEL_DIR, exist_ok=True)
|
17 |
|
18 |
def get_latest_model():
|
19 |
"""Return the latest model file path, or None if no models exist."""
|
20 |
models = [f for f in os.listdir(MODEL_DIR) if f.endswith(".pth")]
|
21 |
+
return os.path.join(MODEL_DIR, sorted(models)[-1]) if models else None
|
|
|
|
|
22 |
|
23 |
def should_train():
|
24 |
"""Return True if it's time to retrain the model."""
|
|
|
46 |
def launch_app():
|
47 |
"""Start app.py if not already running."""
|
48 |
if not is_app_running():
|
49 |
+
print(f"[{datetime.datetime.now()}] Launching app.py...", flush=True)
|
50 |
+
try:
|
51 |
+
subprocess.Popen([sys.executable, APP_PATH])
|
52 |
+
except FileNotFoundError:
|
53 |
+
print(f"[{datetime.datetime.now()}] app.py not found!", flush=True)
|
54 |
else:
|
55 |
+
print(f"[{datetime.datetime.now()}] app.py is already running.", flush=True)
|
56 |
|
57 |
def train_model():
|
58 |
"""Train model if needed and launch app."""
|
59 |
if should_train():
|
60 |
+
print(f"[{datetime.datetime.now()}] Training model...", flush=True)
|
61 |
latest = get_latest_model()
|
62 |
+
if latest and os.path.exists(latest):
|
63 |
os.remove(latest)
|
64 |
+
print(f" Deleted old model: {latest}", flush=True)
|
65 |
+
|
66 |
try:
|
67 |
+
subprocess.run([sys.executable, TRAIN_SCRIPT], check=True)
|
68 |
update_timestamp()
|
69 |
+
print(f"[{datetime.datetime.now()}] Training completed.", flush=True)
|
70 |
except subprocess.CalledProcessError as e:
|
71 |
+
print(f"[{datetime.datetime.now()}] Training failed: {e}", flush=True)
|
72 |
+
except FileNotFoundError:
|
73 |
+
print(f"[{datetime.datetime.now()}] train.py not found!", flush=True)
|
74 |
else:
|
75 |
+
print(f"[{datetime.datetime.now()}] Skipping training (recent model exists).", flush=True)
|
76 |
|
77 |
launch_app()
|
78 |
|
79 |
+
if __name__ == "__main__":
|
80 |
+
# Run once at startup
|
81 |
+
train_model()
|
82 |
|
83 |
+
# Then schedule every 60 minutes
|
84 |
+
# schedule.every(60).minutes.do(train_model)
|
85 |
|
86 |
+
while True:
|
87 |
+
schedule.run_pending()
|
88 |
+
time.sleep(3)
|