Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,49 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
import gradio as gr
|
| 4 |
+
import threading
|
| 5 |
+
import time
|
| 6 |
+
import zipfile
|
| 7 |
+
|
| 8 |
+
output_path = "train_output"
|
| 9 |
+
zipped_model = "python_ai_trained_model.zip"
|
| 10 |
+
status = gr.Textbox(label="Status", value="Ready", interactive=False)
|
| 11 |
+
download_link = gr.File(label="Download Trained Model", visible=False)
|
| 12 |
+
|
| 13 |
+
def run_training():
|
| 14 |
+
global zipped_model
|
| 15 |
+
status.value = "Training started... this may take a while (15–60+ mins)."
|
| 16 |
+
|
| 17 |
+
# Run train.py
|
| 18 |
+
subprocess.run(["python", "train.py"], check=True)
|
| 19 |
+
|
| 20 |
+
# Compress trained model
|
| 21 |
+
status.value = "Training complete. Compressing model..."
|
| 22 |
+
with zipfile.ZipFile(zipped_model, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 23 |
+
for root, _, files in os.walk(output_path):
|
| 24 |
+
for file in files:
|
| 25 |
+
filepath = os.path.join(root, file)
|
| 26 |
+
arcname = os.path.relpath(filepath, output_path)
|
| 27 |
+
zipf.write(filepath, arcname)
|
| 28 |
+
|
| 29 |
+
# Move zip to Gradio-visible path
|
| 30 |
+
status.value = "Done. Model ready for download."
|
| 31 |
+
download_link.visible = True
|
| 32 |
+
|
| 33 |
+
def start_training():
|
| 34 |
+
training_thread = threading.Thread(target=run_training)
|
| 35 |
+
training_thread.start()
|
| 36 |
+
return "Training in progress...", None
|
| 37 |
+
|
| 38 |
+
with gr.Blocks() as demo:
|
| 39 |
+
gr.Markdown("## 🧠 Python AI Model Trainer (StarCoder 7B)")
|
| 40 |
+
gr.Markdown("Click the button below to start fine-tuning your custom Python AI. After training, download the model and use it anywhere.")
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
train_btn = gr.Button("🚀 Start Training")
|
| 44 |
+
train_btn.click(start_training, outputs=[status, download_link])
|
| 45 |
+
|
| 46 |
+
status.render()
|
| 47 |
+
download_link.render()
|
| 48 |
+
|
| 49 |
+
demo.launch()
|