Spaces:
Sleeping
Sleeping
Update app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import osimport subprocessimport gradio as grimport threadingimport timeimport zipfileoutput_path = "train_output"zipped_model = "python_ai_trained_model.zip"status = gr.Textbox(label="Status", value="Ready", interactive=False)download_link = gr.File(label="Download Trained Model", visible=False)def run_training():global zipped_modelstatus.value = "Training started... this may take a while (15β60+ mins)."# Run train.pysubprocess.run(["python", "train.py"], check=True)# Compress trained modelstatus.value = "Training complete. Compressing model..."with zipfile.ZipFile(zipped_model, 'w', zipfile.ZIP_DEFLATED) as zipf:for root, _, files in os.walk(output_path):for file in files:filepath = os.path.join(root, file)arcname = os.path.relpath(filepath, output_path)zipf.write(filepath, arcname)# Move zip to Gradio-visible pathstatus.value = "Done. Model ready for download."download_link.visible = Truedef start_training():training_thread = threading.Thread(target=run_training)training_thread.start()return "Training in progress...", Nonewith gr.Blocks() as demo:gr.Markdown("## π§ Python AI Model Trainer (StarCoder 7B)")gr.Markdown("Click the button below to start fine-tuning your custom Python AI. After training, download the model and use it anywhere.")with gr.Row():train_btn = gr.Button("π Start Training")train_btn.click(start_training, outputs=[status, download_link])status.render()download_link.render()demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687import osimport timeimport zipfileimport gradio as grimport subprocessimport threading# PathsOUTPUT_DIR = "train_output"ZIP_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):for file in files:filepath = os.path.join(root, file)arcname = 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.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)", value=ZIP_FILE, 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()
app.py+69-31