Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 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!",
|
| 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
|
| 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 |
-
|
| 71 |
-
download_btn = gr.
|
| 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 |
-
|
| 76 |
|
| 77 |
-
with gr.Tab("π Test AI"):
|
| 78 |
-
gr.Markdown("## π‘ Try your trained
|
| 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()
|