Spaces:
Running
Running
Update backend.py
Browse files- backend.py +8 -11
backend.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# backend.py β
|
| 2 |
import sqlite3
|
| 3 |
import threading
|
| 4 |
import time
|
|
@@ -21,7 +21,7 @@ scheduler_lock = threading.Lock()
|
|
| 21 |
RUN_TIMEOUT = 48 * 3600
|
| 22 |
MAX_RAM_PER_RUN_GB = 1.5
|
| 23 |
|
| 24 |
-
# ------------------------------ DATABASE
|
| 25 |
|
| 26 |
def init_db():
|
| 27 |
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
|
@@ -66,7 +66,7 @@ def get_user_by_username(username):
|
|
| 66 |
rows, _ = db_query("SELECT id, password_hash FROM users WHERE username = ?", (username,))
|
| 67 |
return rows[0] if rows else None
|
| 68 |
|
| 69 |
-
# ------------------------------
|
| 70 |
|
| 71 |
def signup_user(username, password):
|
| 72 |
if not username or not password:
|
|
@@ -81,11 +81,10 @@ def signup_user(username, password):
|
|
| 81 |
def login_user(username, password):
|
| 82 |
user = get_user_by_username(username)
|
| 83 |
if user and check_password_hash(user[1], password):
|
| 84 |
-
|
| 85 |
-
return user_id, f"Welcome back, {username}!"
|
| 86 |
return None, "Invalid username or password."
|
| 87 |
|
| 88 |
-
# ------------------------------
|
| 89 |
|
| 90 |
def ram_available():
|
| 91 |
return (psutil.virtual_memory().available / (1024**3)) >= MAX_RAM_PER_RUN_GB
|
|
@@ -93,7 +92,6 @@ def ram_available():
|
|
| 93 |
def queue_training_run(user_id, config):
|
| 94 |
_, run_id = db_query("INSERT INTO training_runs (user_id, arch_type, num_layers, learning_rate, epochs, batch_size) VALUES (?, ?, ?, ?, ?, ?)", (user_id, config['arch_type'], config['num_layers'], config['learning_rate'], config['epochs'], config['batch_size']))
|
| 95 |
training_queue.append({"run_id": run_id, "user_id": user_id, **config})
|
| 96 |
-
# Trigger the scheduler every time a new job is added
|
| 97 |
start_training_if_free()
|
| 98 |
return run_id
|
| 99 |
|
|
@@ -131,8 +129,9 @@ def get_user_runs(user_id):
|
|
| 131 |
rows, _ = db_query("SELECT id, arch_type, num_layers, status, started_at FROM training_runs WHERE user_id = ? ORDER BY id DESC", (user_id,))
|
| 132 |
return rows
|
| 133 |
|
| 134 |
-
def get_run_logs(run_id):
|
| 135 |
-
|
|
|
|
| 136 |
return rows[0] if rows else ("", "unknown")
|
| 137 |
|
| 138 |
def update_run_status(run_id, status):
|
|
@@ -273,8 +272,6 @@ def run_inference(run_id, prompt):
|
|
| 273 |
generated_ids = torch.argmax(logits, dim=-1)
|
| 274 |
return f"π§βπ³ Model says:\n{tokenizer.decode(generated_ids[0], skip_special_tokens=True)}"
|
| 275 |
|
| 276 |
-
# ------------------------------ PUBLISH (HF Token passed as argument) ------------------------------
|
| 277 |
-
|
| 278 |
def publish_run_to_hub(run_id, hf_token, repo_name, user_description=""):
|
| 279 |
try:
|
| 280 |
user_info = whoami(token=hf_token)
|
|
|
|
| 1 |
+
# backend.py β FINAL VERSION
|
| 2 |
import sqlite3
|
| 3 |
import threading
|
| 4 |
import time
|
|
|
|
| 21 |
RUN_TIMEOUT = 48 * 3600
|
| 22 |
MAX_RAM_PER_RUN_GB = 1.5
|
| 23 |
|
| 24 |
+
# ------------------------------ DATABASE ------------------------------
|
| 25 |
|
| 26 |
def init_db():
|
| 27 |
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
|
|
|
| 66 |
rows, _ = db_query("SELECT id, password_hash FROM users WHERE username = ?", (username,))
|
| 67 |
return rows[0] if rows else None
|
| 68 |
|
| 69 |
+
# ------------------------------ AUTHENTICATION ------------------------------
|
| 70 |
|
| 71 |
def signup_user(username, password):
|
| 72 |
if not username or not password:
|
|
|
|
| 81 |
def login_user(username, password):
|
| 82 |
user = get_user_by_username(username)
|
| 83 |
if user and check_password_hash(user[1], password):
|
| 84 |
+
return user[0], f"Welcome back, {username}!"
|
|
|
|
| 85 |
return None, "Invalid username or password."
|
| 86 |
|
| 87 |
+
# ------------------------------ TRAINING QUEUE & SCHEDULER ------------------------------
|
| 88 |
|
| 89 |
def ram_available():
|
| 90 |
return (psutil.virtual_memory().available / (1024**3)) >= MAX_RAM_PER_RUN_GB
|
|
|
|
| 92 |
def queue_training_run(user_id, config):
|
| 93 |
_, run_id = db_query("INSERT INTO training_runs (user_id, arch_type, num_layers, learning_rate, epochs, batch_size) VALUES (?, ?, ?, ?, ?, ?)", (user_id, config['arch_type'], config['num_layers'], config['learning_rate'], config['epochs'], config['batch_size']))
|
| 94 |
training_queue.append({"run_id": run_id, "user_id": user_id, **config})
|
|
|
|
| 95 |
start_training_if_free()
|
| 96 |
return run_id
|
| 97 |
|
|
|
|
| 129 |
rows, _ = db_query("SELECT id, arch_type, num_layers, status, started_at FROM training_runs WHERE user_id = ? ORDER BY id DESC", (user_id,))
|
| 130 |
return rows
|
| 131 |
|
| 132 |
+
def get_run_logs(user_id, run_id):
|
| 133 |
+
"""Securely fetches logs by checking ownership (user_id)."""
|
| 134 |
+
rows, _ = db_query("SELECT logs, status FROM training_runs WHERE id = ? AND user_id = ?", (run_id, user_id))
|
| 135 |
return rows[0] if rows else ("", "unknown")
|
| 136 |
|
| 137 |
def update_run_status(run_id, status):
|
|
|
|
| 272 |
generated_ids = torch.argmax(logits, dim=-1)
|
| 273 |
return f"π§βπ³ Model says:\n{tokenizer.decode(generated_ids[0], skip_special_tokens=True)}"
|
| 274 |
|
|
|
|
|
|
|
| 275 |
def publish_run_to_hub(run_id, hf_token, repo_name, user_description=""):
|
| 276 |
try:
|
| 277 |
user_info = whoami(token=hf_token)
|