Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,87 @@
|
|
| 1 |
import os
|
| 2 |
-
import subprocess
|
| 3 |
-
import gradio as gr
|
| 4 |
-
import threading
|
| 5 |
import time
|
| 6 |
import zipfile
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def run_training():
|
| 14 |
-
global zipped_model
|
| 15 |
-
status.value = "Training started... this may take a while (15β60+ mins)."
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
with zipfile.ZipFile(
|
| 23 |
-
for root, _, files in os.walk(
|
| 24 |
for file in files:
|
| 25 |
filepath = os.path.join(root, file)
|
| 26 |
-
arcname = os.path.relpath(filepath,
|
| 27 |
zipf.write(filepath, arcname)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
return "Training in progress...", None
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
-
gr.Markdown("## π§ Python AI
|
| 40 |
-
gr.Markdown("
|
| 41 |
-
|
| 42 |
with gr.Row():
|
| 43 |
train_btn = gr.Button("π Start Training")
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
download_link.render()
|
| 48 |
|
| 49 |
demo.launch()
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
| 2 |
import time
|
| 3 |
import zipfile
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import subprocess
|
| 6 |
+
import threading
|
| 7 |
|
| 8 |
+
# Paths
|
| 9 |
+
OUTPUT_DIR = "train_output"
|
| 10 |
+
ZIP_FILE = "python_ai_trained_model.zip"
|
| 11 |
+
LOG_FILE = "train_log.txt"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Shared states
|
| 14 |
+
start_time = None
|
| 15 |
+
end_time = None
|
| 16 |
|
| 17 |
+
# Function: Zip the trained model
|
| 18 |
+
def zip_trained_model():
|
| 19 |
+
with zipfile.ZipFile(ZIP_FILE, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 20 |
+
for root, _, files in os.walk(OUTPUT_DIR):
|
| 21 |
for file in files:
|
| 22 |
filepath = os.path.join(root, file)
|
| 23 |
+
arcname = os.path.relpath(filepath, OUTPUT_DIR)
|
| 24 |
zipf.write(filepath, arcname)
|
| 25 |
|
| 26 |
+
# Function: Tail the logs from training
|
| 27 |
+
def tail_logs():
|
| 28 |
+
if not os.path.exists(LOG_FILE):
|
| 29 |
+
return ""
|
| 30 |
+
with open(LOG_FILE, 'r') as f:
|
| 31 |
+
lines = f.readlines()[-20:] # last 20 lines
|
| 32 |
+
return ''.join(lines)
|
| 33 |
+
|
| 34 |
+
# Background training function
|
| 35 |
+
def run_training(status_box, time_box, download_file, model_size_box, log_box):
|
| 36 |
+
global start_time, end_time
|
| 37 |
+
|
| 38 |
+
start_time = time.time()
|
| 39 |
+
status_box.value = "π Training started... Please wait. This can take 30β90+ minutes."
|
| 40 |
+
time_box.value = "Training in progress..."
|
| 41 |
+
log_box.value = ""
|
| 42 |
+
|
| 43 |
+
# Run training and redirect stdout/stderr to log file
|
| 44 |
+
with open(LOG_FILE, "w") as log:
|
| 45 |
+
process = subprocess.Popen(["python", "train.py"], stdout=log, stderr=log)
|
| 46 |
+
while process.poll() is None:
|
| 47 |
+
log_box.value = tail_logs()
|
| 48 |
+
time.sleep(5)
|
| 49 |
|
| 50 |
+
end_time = time.time()
|
| 51 |
+
elapsed = round(end_time - start_time, 2)
|
| 52 |
+
time_box.value = f"β
Training completed in {elapsed // 60:.0f} min {elapsed % 60:.0f} sec."
|
|
|
|
| 53 |
|
| 54 |
+
status_box.value = "π Compressing trained model..."
|
| 55 |
+
zip_trained_model()
|
| 56 |
+
|
| 57 |
+
size_mb = round(os.path.getsize(ZIP_FILE) / (1024 * 1024), 2)
|
| 58 |
+
model_size_box.value = f"π¦ Model Size: {size_mb} MB"
|
| 59 |
+
|
| 60 |
+
download_file.visible = True
|
| 61 |
+
status_box.value = "β
Done! You can now download your trained model."
|
| 62 |
+
|
| 63 |
+
# Trigger button click
|
| 64 |
+
def start_training(status_box, time_box, download_file, model_size_box, log_box):
|
| 65 |
+
thread = threading.Thread(target=run_training, args=(status_box, time_box, download_file, model_size_box, log_box))
|
| 66 |
+
thread.start()
|
| 67 |
+
return "Training process has started..."
|
| 68 |
+
|
| 69 |
+
# Gradio UI
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
+
gr.Markdown("## π§ Python AI Trainer (StarCoder 7B)")
|
| 72 |
+
gr.Markdown("Train a custom Python AI that can write, fix, and explain Python code.")
|
| 73 |
+
|
| 74 |
with gr.Row():
|
| 75 |
train_btn = gr.Button("π Start Training")
|
| 76 |
+
status_box = gr.Textbox(label="Status", value="Ready", interactive=False)
|
| 77 |
+
|
| 78 |
+
with gr.Row():
|
| 79 |
+
time_box = gr.Textbox(label="Training Time", interactive=False)
|
| 80 |
+
model_size_box = gr.Textbox(label="Final Model Size", interactive=False)
|
| 81 |
+
|
| 82 |
+
log_box = gr.Textbox(label="Live Training Logs", lines=20, interactive=False)
|
| 83 |
+
download_file = gr.File(label="π₯ Download Trained Model (.zip)", value=ZIP_FILE, visible=False)
|
| 84 |
|
| 85 |
+
train_btn.click(fn=start_training, inputs=[status_box, time_box, download_file, model_size_box, log_box], outputs=[status_box])
|
|
|
|
| 86 |
|
| 87 |
demo.launch()
|