Spaces:
Sleeping
Sleeping
File size: 3,226 Bytes
ac2c6e9 e865ed1 ac2c6e9 e865ed1 ac2c6e9 20a9e4d e865ed1 ac2c6e9 e865ed1 ac2c6e9 20a9e4d e865ed1 20a9e4d e865ed1 20a9e4d e865ed1 20a9e4d e865ed1 20a9e4d e865ed1 20a9e4d e865ed1 20a9e4d e865ed1 ac2c6e9 20a9e4d ac2c6e9 20a9e4d e865ed1 20a9e4d e865ed1 a0e05b2 e865ed1 20a9e4d e865ed1 20a9e4d e865ed1 20a9e4d e865ed1 ac2c6e9 e865ed1 20a9e4d e865ed1 ac2c6e9 e865ed1 20a9e4d e865ed1 20a9e4d a0e05b2 ac2c6e9 20a9e4d cf20708 ac2c6e9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
import os
import time
import zipfile
import gradio as gr
import subprocess
import threading
# Paths
OUTPUT_DIR = "train_output"
ZIP_FILE = "python_ai_trained_model.zip"
LOG_FILE = "train_log.txt"
# Zip function
def zip_trained_model():
with zipfile.ZipFile(ZIP_FILE, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(OUTPUT_DIR):
for file in files:
filepath = os.path.join(root, file)
arcname = os.path.relpath(filepath, OUTPUT_DIR)
zipf.write(filepath, arcname)
# Tail logs function
def tail_logs(n=20):
if not os.path.exists(LOG_FILE):
return ""
with open(LOG_FILE, 'r') as f:
return ''.join(f.readlines()[-n:])
# Background training runner
def run_training(status_box, time_box, download_file, model_size_box, log_box):
start_time = time.time()
status_box.value = "π Training started..."
time_box.value = "Training in progress..."
log_box.value = ""
# Create log file
with open(LOG_FILE, "w") as log:
log.write("π Launching train.py...\n")
# Start training
with open(LOG_FILE, "a") as log:
process = subprocess.Popen(["python", "train.py"], stdout=log, stderr=subprocess.STDOUT)
while process.poll() is None:
log_box.update(value=tail_logs())
time.sleep(5)
# Check exit status
if process.returncode != 0:
status_box.value = f"β Training failed with exit code {process.returncode}"
log_box.value = tail_logs()
return
# Training success
elapsed = round(time.time() - start_time, 2)
time_box.value = f"β
Completed in {elapsed // 60:.0f} min {elapsed % 60:.0f} sec"
status_box.value = "π Compressing model..."
# Zip it
zip_trained_model()
size_mb = round(os.path.getsize(ZIP_FILE) / (1024 * 1024), 2)
model_size_box.value = f"π¦ Model Size: {size_mb} MB"
download_file.value = ZIP_FILE
download_file.visible = True
status_box.value = "β
Training complete. Download below."
# Button trigger
def start_training(status_box, time_box, download_file, model_size_box, log_box):
thread = threading.Thread(target=run_training, args=(status_box, time_box, download_file, model_size_box, log_box))
thread.start()
return "Training process started."
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("## π§ Python AI Trainer (StarCoder 7B)")
gr.Markdown("Train your Python AI with 1 click. Watch logs. Download model when done.")
with gr.Row():
train_btn = gr.Button("π Start Training")
status_box = gr.Textbox(label="Status", value="Ready", interactive=False)
with gr.Row():
time_box = gr.Textbox(label="Training Time", interactive=False)
model_size_box = gr.Textbox(label="Final Model Size", interactive=False)
log_box = gr.Textbox(label="Live Training Logs", lines=20, interactive=False, value="")
download_file = gr.File(label="π₯ Download Trained Model (.zip)", visible=False)
train_btn.click(
fn=start_training,
inputs=[status_box, time_box, download_file, model_size_box, log_box],
outputs=[status_box]
)
demo.launch() |