Spaces:
Sleeping
Sleeping
Update app.py
Browse files123423456789101112131415891011121718131415162021222317181920212226232425293031262728293031323334353637383940414243444546474849333435365051525354555657585960616263646566676869703940417172737475447677787980818283844647858687import osimport subprocessimport gradio as grimport threadingimport timeimport zipfileimport gradio as grimport subprocessimport threading
output_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_model status.value = "Training started... this may take a while (15β60+ mins)."# PathsOUTPUT_DIR = "train_output"ZIP_FILE = "python_ai_trained_model.zip"LOG_FILE = "train_log.txt"
# Run train.py subprocess.run(["python", "train.py"], check=True)# Shared statesstart_time = Noneend_time = None
# Compress trained model status.value = "Training complete. Compressing model..." with zipfile.ZipFile(zipped_model, 'w', zipfile.ZIP_DEFLATED) as zipf: for root, _, files in os.walk(output_path):# 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_path) arcname = os.path.relpath(filepath, OUTPUT_DIR) zipf.write(filepath, arcname)
# Move zip to Gradio-visible path status.value = "Done. Model ready for download." download_link.visible = True# 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 lines return ''.join(lines)
# Background training functiondef run_training(status_box, time_box, download_file, model_size_box, log_box): global start_time, end_time
start_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 file with 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)
def start_training(): training_thread = threading.Thread(target=run_training) training_thread.start() return "Training in progress...", None 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 = True status_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 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.") 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") train_btn.click(start_training, outputs=[status, download_link]) 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)
status.render() download_link.render() 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