Percy3822 commited on
Commit
e865ed1
Β·
verified Β·
1 Parent(s): ac2c6e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -31
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
- 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()
 
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()