Spaces:
Sleeping
Sleeping
Update app.py
Browse files8 unmodified lines91011121213141516 unmodified lines323334353536373738394016 unmodified lines5758596061626364656667686970717262637374756576777879807071818283848575868777788889909192939495969798991001011028 unmodified linesmodel_dir = "trained_model"zip_file = "trained_model.zip"
# Try loading model for test tab# Try loading the generator model once at the top (for better performance)try: generator = pipeline("text-generation", model=model_dir, tokenizer="distilgpt2")except:16 unmodified lines
if os.path.exists(model_dir): shutil.make_archive("trained_model", "zip", model_dir) return "β
Training complete!", os.path.abspath(zip_file) return "β
Training complete!", zip_file else: return "β Training failed.", "" return "β Training failed.", None
def read_logs(): if os.path.exists(log_path):16 unmodified lines except Exception as e: return f"β Error: {e}"
def upload_model(file): global generator if file is None: return "No model uploaded." shutil.unpack_archive(file.name, model_dir) try: generator = pipeline("text-generation", model=model_dir, tokenizer="distilgpt2") return "β
Model uploaded and loaded successfully!" except Exception as e: return f"β Failed to load model: {e}"
# === UI ===with gr.Blocks() as app: with gr.Tab("π§ Train AI"): gr.Markdown("## π₯ Upload your dataset and π― train a Godot AI") with gr.Tab("π§ Train Python AI"): gr.Markdown("## π₯ Upload your dataset and π― train a *Python AI* model") file_input = gr.File(label="Upload JSONL Dataset") upload_btn = gr.Button("Upload") upload_btn = gr.Button("Upload Dataset") status_box = gr.Textbox(label="Upload Status")
start_btn = gr.Button("π Start Training") log_output = gr.Textbox(label="π Training Logs", lines=15) refresh_btn = gr.Button("π Refresh Logs") download_btn = gr.DownloadButton(label="π₯ Download Trained Model", visible=False)
download_btn = gr.File(label="π₯ Download Trained Model", visible=False)
upload_btn.click(fn=upload_file, inputs=file_input, outputs=status_box) start_btn.click(fn=start_training, outputs=[status_box, download_btn]) refresh_btn.click(fn=read_logs, outputs=log_output) start_btn.click(fn=read_logs, outputs=log_output)
with gr.Tab("π Test AI"): gr.Markdown("## π‘ Try your trained Godot AI below") with gr.Tab("π Test Python AI"): gr.Markdown("## π‘ Try your trained Python AI below") prompt_input = gr.Textbox(label="Enter Prompt") test_btn = gr.Button("π Test AI") response_output = gr.Textbox(label="AI Response", lines=10) test_btn.click(fn=generate_response, inputs=prompt_input, outputs=response_output)
with gr.Tab("π€ Upload Pretrained Model"): gr.Markdown("## π¦ Upload a trained Python AI model (.zip) to test") model_file_input = gr.File(label="Upload Model ZIP") model_upload_btn = gr.Button("Upload Model") model_status = gr.Textbox(label="Model Upload Status") model_upload_btn.click(fn=upload_model, inputs=model_file_input, outputs=model_status)
app.launch()app.py+29-11