Percy3822 commited on
Commit
330f9ce
Β·
verified Β·
1 Parent(s): 6600b62

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -9,7 +9,7 @@ log_path = "train.log"
9
  model_dir = "trained_model"
10
  zip_file = "trained_model.zip"
11
 
12
- # Try loading model for test tab
13
  try:
14
  generator = pipeline("text-generation", model=model_dir, tokenizer="distilgpt2")
15
  except:
@@ -32,9 +32,9 @@ def start_training():
32
 
33
  if os.path.exists(model_dir):
34
  shutil.make_archive("trained_model", "zip", model_dir)
35
- return "βœ… Training complete!", os.path.abspath(zip_file)
36
  else:
37
- return "❌ Training failed.", ""
38
 
39
  def read_logs():
40
  if os.path.exists(log_path):
@@ -57,28 +57,46 @@ def generate_response(prompt):
57
  except Exception as e:
58
  return f"❌ Error: {e}"
59
 
 
 
 
 
 
 
 
 
 
 
 
60
  # === UI ===
61
  with gr.Blocks() as app:
62
- with gr.Tab("🧠 Train AI"):
63
- gr.Markdown("## πŸ“₯ Upload your dataset and 🎯 train a Godot AI")
64
  file_input = gr.File(label="Upload JSONL Dataset")
65
- upload_btn = gr.Button("Upload")
66
  status_box = gr.Textbox(label="Upload Status")
67
 
68
  start_btn = gr.Button("πŸš€ Start Training")
69
  log_output = gr.Textbox(label="πŸ“œ Training Logs", lines=15)
70
- refresh_btn = gr.Button("πŸ” Refresh Logs")
71
- download_btn = gr.DownloadButton(label="πŸ“₯ Download Trained Model", visible=False)
72
 
73
  upload_btn.click(fn=upload_file, inputs=file_input, outputs=status_box)
74
  start_btn.click(fn=start_training, outputs=[status_box, download_btn])
75
- refresh_btn.click(fn=read_logs, outputs=log_output)
76
 
77
- with gr.Tab("πŸš€ Test AI"):
78
- gr.Markdown("## πŸ’‘ Try your trained Godot AI below")
79
  prompt_input = gr.Textbox(label="Enter Prompt")
80
  test_btn = gr.Button("πŸ” Test AI")
81
  response_output = gr.Textbox(label="AI Response", lines=10)
82
  test_btn.click(fn=generate_response, inputs=prompt_input, outputs=response_output)
83
 
 
 
 
 
 
 
 
84
  app.launch()
 
9
  model_dir = "trained_model"
10
  zip_file = "trained_model.zip"
11
 
12
+ # Try loading the generator model once at the top (for better performance)
13
  try:
14
  generator = pipeline("text-generation", model=model_dir, tokenizer="distilgpt2")
15
  except:
 
32
 
33
  if os.path.exists(model_dir):
34
  shutil.make_archive("trained_model", "zip", model_dir)
35
+ return "βœ… Training complete!", zip_file
36
  else:
37
+ return "❌ Training failed.", None
38
 
39
  def read_logs():
40
  if os.path.exists(log_path):
 
57
  except Exception as e:
58
  return f"❌ Error: {e}"
59
 
60
+ def upload_model(file):
61
+ global generator
62
+ if file is None:
63
+ return "No model uploaded."
64
+ shutil.unpack_archive(file.name, model_dir)
65
+ try:
66
+ generator = pipeline("text-generation", model=model_dir, tokenizer="distilgpt2")
67
+ return "βœ… Model uploaded and loaded successfully!"
68
+ except Exception as e:
69
+ return f"❌ Failed to load model: {e}"
70
+
71
  # === UI ===
72
  with gr.Blocks() as app:
73
+ with gr.Tab("🧠 Train Python AI"):
74
+ gr.Markdown("## πŸ“₯ Upload your dataset and 🎯 train a *Python AI* model")
75
  file_input = gr.File(label="Upload JSONL Dataset")
76
+ upload_btn = gr.Button("Upload Dataset")
77
  status_box = gr.Textbox(label="Upload Status")
78
 
79
  start_btn = gr.Button("πŸš€ Start Training")
80
  log_output = gr.Textbox(label="πŸ“œ Training Logs", lines=15)
81
+
82
+ download_btn = gr.File(label="πŸ“₯ Download Trained Model", visible=False)
83
 
84
  upload_btn.click(fn=upload_file, inputs=file_input, outputs=status_box)
85
  start_btn.click(fn=start_training, outputs=[status_box, download_btn])
86
+ start_btn.click(fn=read_logs, outputs=log_output)
87
 
88
+ with gr.Tab("πŸš€ Test Python AI"):
89
+ gr.Markdown("## πŸ’‘ Try your trained Python AI below")
90
  prompt_input = gr.Textbox(label="Enter Prompt")
91
  test_btn = gr.Button("πŸ” Test AI")
92
  response_output = gr.Textbox(label="AI Response", lines=10)
93
  test_btn.click(fn=generate_response, inputs=prompt_input, outputs=response_output)
94
 
95
+ with gr.Tab("πŸ“€ Upload Pretrained Model"):
96
+ gr.Markdown("## πŸ“¦ Upload a trained Python AI model (.zip) to test")
97
+ model_file_input = gr.File(label="Upload Model ZIP")
98
+ model_upload_btn = gr.Button("Upload Model")
99
+ model_status = gr.Textbox(label="Model Upload Status")
100
+ model_upload_btn.click(fn=upload_model, inputs=model_file_input, outputs=model_status)
101
+
102
  app.launch()