Spaces:
Sleeping
Sleeping
Update app.py
Browse files9 unmodified lines10111213141516171819202 unmodified lines2324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687889 unmodified linesZIP_FILE = "python_ai_trained_model.zip"LOG_FILE = "train_log.txt"# Shared statesstart_time = Noneend_time = None# Function: Zip the trained modeldef zip_trained_model():with zipfile.ZipFile(ZIP_FILE, 'w', zipfile.ZIP_DEFLATED) as zipf:for root, _, files in os.walk(OUTPUT_DIR):2 unmodified linesarcname = os.path.relpath(filepath, OUTPUT_DIR)zipf.write(filepath, arcname)# Function: Tail the logs from trainingdef tail_logs():if not os.path.exists(LOG_FILE):return ""with open(LOG_FILE, 'r') as f:lines = f.readlines()[-20:] # last 20 linesreturn ''.join(lines)# Background training functiondef run_training(status_box, time_box, download_file, model_size_box, log_box):global start_time, end_timestart_time = time.time()status_box.value = "π Training started... Please wait. This can take 30β90+ minutes."time_box.value = "Training in progress..."log_box.value = ""# Run training and redirect stdout/stderr to log filewith open(LOG_FILE, "w") as log:process = subprocess.Popen(["python", "train.py"], stdout=log, stderr=log)while process.poll() is None:log_box.value = tail_logs()time.sleep(5)end_time = time.time()elapsed = round(end_time - start_time, 2)time_box.value = f"β Training completed in {elapsed // 60:.0f} min {elapsed % 60:.0f} sec."status_box.value = "π Compressing trained model..."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_FILEdownload_file.visible = Truestatus_box.value = "β Done! You can now download your trained model."# Trigger button clickdef 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 has started..."# Gradio UIwith gr.Blocks() as demo:gr.Markdown("## π§ Python AI Trainer (StarCoder 7B)")gr.Markdown("Train a custom Python AI that can write, fix, and explain Python code.")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)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()9 unmodified lines101112131415162 unmodified lines1920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001019 unmodified linesZIP_FILE = "python_ai_trained_model.zip"LOG_FILE = "train_log.txt"# Zip functiondef zip_trained_model():with zipfile.ZipFile(ZIP_FILE, 'w', zipfile.ZIP_DEFLATED) as zipf:for root, _, files in os.walk(OUTPUT_DIR):2 unmodified linesarcname = os.path.relpath(filepath, OUTPUT_DIR)zipf.write(filepath, arcname)# Tail logs functiondef 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 runnerdef 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 filewith open(LOG_FILE, "w") as log:log.write("π Launching train.py...\n")# Start trainingwith 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 statusif process.returncode != 0:status_box.value = f"β Training failed with exit code {process.returncode}"log_box.value = tail_logs()return# Training successelapsed = 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 itzip_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_FILEdownload_file.visible = Truestatus_box.value = "β Training complete. Download below."# Button triggerdef 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 UIwith 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])status_box.render()time_box.render()model_size_box.render()log_box.render()download_file.render()demo.launch()
app.py+41-28