Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,13 +20,8 @@ import os, tempfile, shutil, asyncio, threading, time
|
|
| 20 |
from datetime import datetime
|
| 21 |
|
| 22 |
# ==== Async upload wrapper ====
|
| 23 |
-
import threading
|
| 24 |
-
import asyncio
|
| 25 |
-
|
| 26 |
def start_async_upload(local_dir, hf_repo, output_log):
|
| 27 |
-
"""
|
| 28 |
-
Starts async model upload in a background thread.
|
| 29 |
-
"""
|
| 30 |
def runner():
|
| 31 |
output_log.append(f"[INFO] π Async upload thread started for repo: {hf_repo}")
|
| 32 |
asyncio.run(async_upload_model(local_dir, hf_repo, output_log))
|
|
@@ -35,16 +30,8 @@ def start_async_upload(local_dir, hf_repo, output_log):
|
|
| 35 |
threading.Thread(target=runner, daemon=True).start()
|
| 36 |
|
| 37 |
|
| 38 |
-
import os
|
| 39 |
-
import shutil
|
| 40 |
-
import tempfile
|
| 41 |
-
import asyncio
|
| 42 |
-
from huggingface_hub import HfApi, HfFolder, Repository
|
| 43 |
-
|
| 44 |
async def async_upload_model(local_dir, hf_repo, output_log, max_retries=3):
|
| 45 |
-
"""
|
| 46 |
-
Uploads a local model directory to HF Hub asynchronously with detailed logging.
|
| 47 |
-
"""
|
| 48 |
try:
|
| 49 |
token = HfFolder.get_token()
|
| 50 |
api = HfApi()
|
|
@@ -97,6 +84,7 @@ async def async_upload_model(local_dir, hf_repo, output_log, max_retries=3):
|
|
| 97 |
except Exception as e:
|
| 98 |
output_log.append(f"[ERROR] β Unexpected error during upload: {e}")
|
| 99 |
|
|
|
|
| 100 |
# ==== GPU check ====
|
| 101 |
def check_gpu_status():
|
| 102 |
return "π Zero GPU Ready - GPU will be allocated when training starts"
|
|
@@ -107,18 +95,12 @@ def log_message(output_log, msg):
|
|
| 107 |
print(line)
|
| 108 |
output_log.append(line)
|
| 109 |
|
|
|
|
| 110 |
# ==== Main Training ====
|
| 111 |
@spaces.GPU(duration=300)
|
| 112 |
def train_model(base_model, dataset_name, num_epochs, batch_size, learning_rate, hf_repo):
|
| 113 |
-
"""
|
| 114 |
-
Fine-tune a base model using LoRA with train/test split and async upload.
|
| 115 |
-
"""
|
| 116 |
output_log = []
|
| 117 |
-
test_split=0.2
|
| 118 |
-
|
| 119 |
-
def log_message(log_list, msg):
|
| 120 |
-
print(msg)
|
| 121 |
-
log_list.append(msg)
|
| 122 |
|
| 123 |
try:
|
| 124 |
log_message(output_log, "π Initializing training sequence...")
|
|
@@ -132,7 +114,6 @@ def train_model(base_model, dataset_name, num_epochs, batch_size, learning_rate,
|
|
| 132 |
# ===== Load dataset =====
|
| 133 |
log_message(output_log, f"\nπ Loading dataset: {dataset_name} ...")
|
| 134 |
dataset = load_dataset(dataset_name)
|
| 135 |
-
# Determine train/test split
|
| 136 |
dataset = dataset["train"].train_test_split(test_size=test_split)
|
| 137 |
train_dataset = dataset["train"]
|
| 138 |
test_dataset = dataset["test"]
|
|
@@ -212,11 +193,10 @@ You are a wise teacher interpreting Bhagavad Gita with deep insights.
|
|
| 212 |
warmup_steps=10,
|
| 213 |
logging_steps=5,
|
| 214 |
save_strategy="epoch",
|
| 215 |
-
evaluation_strategy="epoch",
|
| 216 |
fp16=device == "cuda",
|
| 217 |
optim="adamw_torch",
|
| 218 |
learning_rate=learning_rate,
|
| 219 |
-
max_steps=100,
|
| 220 |
)
|
| 221 |
|
| 222 |
trainer = Trainer(
|
|
@@ -245,6 +225,7 @@ You are a wise teacher interpreting Bhagavad Gita with deep insights.
|
|
| 245 |
|
| 246 |
return "\n".join(output_log)
|
| 247 |
|
|
|
|
| 248 |
# ==== Gradio Interface ====
|
| 249 |
def create_interface():
|
| 250 |
with gr.Blocks(title="PromptWizard β Qwen Trainer") as demo:
|
|
@@ -280,6 +261,7 @@ def create_interface():
|
|
| 280 |
|
| 281 |
return demo
|
| 282 |
|
|
|
|
| 283 |
if __name__ == "__main__":
|
| 284 |
demo = create_interface()
|
| 285 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 20 |
from datetime import datetime
|
| 21 |
|
| 22 |
# ==== Async upload wrapper ====
|
|
|
|
|
|
|
|
|
|
| 23 |
def start_async_upload(local_dir, hf_repo, output_log):
|
| 24 |
+
"""Starts async model upload in a background thread."""
|
|
|
|
|
|
|
| 25 |
def runner():
|
| 26 |
output_log.append(f"[INFO] π Async upload thread started for repo: {hf_repo}")
|
| 27 |
asyncio.run(async_upload_model(local_dir, hf_repo, output_log))
|
|
|
|
| 30 |
threading.Thread(target=runner, daemon=True).start()
|
| 31 |
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
async def async_upload_model(local_dir, hf_repo, output_log, max_retries=3):
|
| 34 |
+
"""Uploads a local model directory to HF Hub asynchronously with detailed logging."""
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
token = HfFolder.get_token()
|
| 37 |
api = HfApi()
|
|
|
|
| 84 |
except Exception as e:
|
| 85 |
output_log.append(f"[ERROR] β Unexpected error during upload: {e}")
|
| 86 |
|
| 87 |
+
|
| 88 |
# ==== GPU check ====
|
| 89 |
def check_gpu_status():
|
| 90 |
return "π Zero GPU Ready - GPU will be allocated when training starts"
|
|
|
|
| 95 |
print(line)
|
| 96 |
output_log.append(line)
|
| 97 |
|
| 98 |
+
|
| 99 |
# ==== Main Training ====
|
| 100 |
@spaces.GPU(duration=300)
|
| 101 |
def train_model(base_model, dataset_name, num_epochs, batch_size, learning_rate, hf_repo):
|
|
|
|
|
|
|
|
|
|
| 102 |
output_log = []
|
| 103 |
+
test_split = 0.2
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
try:
|
| 106 |
log_message(output_log, "π Initializing training sequence...")
|
|
|
|
| 114 |
# ===== Load dataset =====
|
| 115 |
log_message(output_log, f"\nπ Loading dataset: {dataset_name} ...")
|
| 116 |
dataset = load_dataset(dataset_name)
|
|
|
|
| 117 |
dataset = dataset["train"].train_test_split(test_size=test_split)
|
| 118 |
train_dataset = dataset["train"]
|
| 119 |
test_dataset = dataset["test"]
|
|
|
|
| 193 |
warmup_steps=10,
|
| 194 |
logging_steps=5,
|
| 195 |
save_strategy="epoch",
|
|
|
|
| 196 |
fp16=device == "cuda",
|
| 197 |
optim="adamw_torch",
|
| 198 |
learning_rate=learning_rate,
|
| 199 |
+
max_steps=100, # Limit for demo
|
| 200 |
)
|
| 201 |
|
| 202 |
trainer = Trainer(
|
|
|
|
| 225 |
|
| 226 |
return "\n".join(output_log)
|
| 227 |
|
| 228 |
+
|
| 229 |
# ==== Gradio Interface ====
|
| 230 |
def create_interface():
|
| 231 |
with gr.Blocks(title="PromptWizard β Qwen Trainer") as demo:
|
|
|
|
| 261 |
|
| 262 |
return demo
|
| 263 |
|
| 264 |
+
|
| 265 |
if __name__ == "__main__":
|
| 266 |
demo = create_interface()
|
| 267 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|