Percy3822 commited on
Commit
20a9e4d
Β·
verified Β·
1 Parent(s): 587575a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -28
app.py CHANGED
@@ -10,11 +10,7 @@ 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):
@@ -23,66 +19,83 @@ def zip_trained_model():
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.value = ZIP_FILE
61
  download_file.visible = True
62
- status_box.value = "βœ… Done! You can now download your trained model."
63
 
64
- # Trigger button click
65
  def start_training(status_box, time_box, download_file, model_size_box, log_box):
66
  thread = threading.Thread(target=run_training, args=(status_box, time_box, download_file, model_size_box, log_box))
67
  thread.start()
68
- return "Training process has started..."
69
 
70
  # Gradio UI
71
  with gr.Blocks() as demo:
72
  gr.Markdown("## 🧠 Python AI Trainer (StarCoder 7B)")
73
- gr.Markdown("Train a custom Python AI that can write, fix, and explain Python code.")
74
 
75
  with gr.Row():
76
  train_btn = gr.Button("πŸš€ Start Training")
77
  status_box = gr.Textbox(label="Status", value="Ready", interactive=False)
78
-
79
  with gr.Row():
80
  time_box = gr.Textbox(label="Training Time", interactive=False)
81
  model_size_box = gr.Textbox(label="Final Model Size", interactive=False)
82
 
83
- log_box = gr.Textbox(label="Live Training Logs", lines=20, interactive=False)
84
  download_file = gr.File(label="πŸ“₯ Download Trained Model (.zip)", visible=False)
85
 
86
- train_btn.click(fn=start_training, inputs=[status_box, time_box, download_file, model_size_box, log_box], outputs=[status_box])
 
 
 
 
 
 
 
 
 
 
87
 
88
  demo.launch()
 
10
  ZIP_FILE = "python_ai_trained_model.zip"
11
  LOG_FILE = "train_log.txt"
12
 
13
+ # Zip function
 
 
 
 
14
  def zip_trained_model():
15
  with zipfile.ZipFile(ZIP_FILE, 'w', zipfile.ZIP_DEFLATED) as zipf:
16
  for root, _, files in os.walk(OUTPUT_DIR):
 
19
  arcname = os.path.relpath(filepath, OUTPUT_DIR)
20
  zipf.write(filepath, arcname)
21
 
22
+ # Tail logs function
23
+ def tail_logs(n=20):
24
  if not os.path.exists(LOG_FILE):
25
  return ""
26
  with open(LOG_FILE, 'r') as f:
27
+ return ''.join(f.readlines()[-n:])
 
28
 
29
+ # Background training runner
30
  def run_training(status_box, time_box, download_file, model_size_box, log_box):
 
 
31
  start_time = time.time()
32
+ status_box.value = "πŸš€ Training started..."
33
  time_box.value = "Training in progress..."
34
  log_box.value = ""
35
 
36
+ # Create log file
37
  with open(LOG_FILE, "w") as log:
38
+ log.write("πŸš€ Launching train.py...\n")
39
+
40
+ # Start training
41
+ with open(LOG_FILE, "a") as log:
42
+ process = subprocess.Popen(["python", "train.py"], stdout=log, stderr=subprocess.STDOUT)
43
  while process.poll() is None:
44
+ log_box.update(value=tail_logs())
45
  time.sleep(5)
46
 
47
+ # Check exit status
48
+ if process.returncode != 0:
49
+ status_box.value = f"❌ Training failed with exit code {process.returncode}"
50
+ log_box.value = tail_logs()
51
+ return
52
 
53
+ # Training success
54
+ elapsed = round(time.time() - start_time, 2)
55
+ time_box.value = f"βœ… Completed in {elapsed // 60:.0f} min {elapsed % 60:.0f} sec"
56
+ status_box.value = "πŸ”„ Compressing model..."
57
 
58
+ # Zip it
59
+ zip_trained_model()
60
  size_mb = round(os.path.getsize(ZIP_FILE) / (1024 * 1024), 2)
61
  model_size_box.value = f"πŸ“¦ Model Size: {size_mb} MB"
62
 
63
  download_file.value = ZIP_FILE
64
  download_file.visible = True
65
+ status_box.value = "βœ… Training complete. Download below."
66
 
67
+ # Button trigger
68
  def start_training(status_box, time_box, download_file, model_size_box, log_box):
69
  thread = threading.Thread(target=run_training, args=(status_box, time_box, download_file, model_size_box, log_box))
70
  thread.start()
71
+ return "Training process started."
72
 
73
  # Gradio UI
74
  with gr.Blocks() as demo:
75
  gr.Markdown("## 🧠 Python AI Trainer (StarCoder 7B)")
76
+ gr.Markdown("Train your Python AI with 1 click. Watch logs. Download model when done.")
77
 
78
  with gr.Row():
79
  train_btn = gr.Button("πŸš€ Start Training")
80
  status_box = gr.Textbox(label="Status", value="Ready", interactive=False)
81
+
82
  with gr.Row():
83
  time_box = gr.Textbox(label="Training Time", interactive=False)
84
  model_size_box = gr.Textbox(label="Final Model Size", interactive=False)
85
 
86
+ log_box = gr.Textbox(label="Live Training Logs", lines=20, interactive=False, value="")
87
  download_file = gr.File(label="πŸ“₯ Download Trained Model (.zip)", visible=False)
88
 
89
+ train_btn.click(
90
+ fn=start_training,
91
+ inputs=[status_box, time_box, download_file, model_size_box, log_box],
92
+ outputs=[status_box]
93
+ )
94
+
95
+ status_box.render()
96
+ time_box.render()
97
+ model_size_box.render()
98
+ log_box.render()
99
+ download_file.render()
100
 
101
  demo.launch()