Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,37 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import subprocess
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
outputs=
|
| 13 |
-
title="Model Trainer",
|
| 14 |
-
description="Click the button to train the model using train.py"
|
| 15 |
-
)
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import shutil
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
+
|
| 6 |
+
# Load the trained model and tokenizer
|
| 7 |
+
model_dir = "trained_model"
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_dir)
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_dir)
|
| 10 |
+
|
| 11 |
+
# Generate response function
|
| 12 |
+
def generate_code(prompt):
|
| 13 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 14 |
+
outputs = model.generate(**inputs, max_length=256)
|
| 15 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 16 |
+
|
| 17 |
+
# Save model to accessible directory for download
|
| 18 |
+
def save_model_for_download():
|
| 19 |
+
if os.path.exists("/home/user/app/files/trained_model"):
|
| 20 |
+
shutil.rmtree("/home/user/app/files/trained_model")
|
| 21 |
+
shutil.copytree("trained_model", "/home/user/app/files/trained_model")
|
| 22 |
+
return "Model saved to files tab. You can now download it."
|
| 23 |
|
| 24 |
+
with gr.Blocks() as demo:
|
| 25 |
+
gr.Markdown("# Trained Python Code Generator")
|
| 26 |
+
with gr.Row():
|
| 27 |
+
inp = gr.Textbox(label="Enter a prompt")
|
| 28 |
+
out = gr.Textbox(label="Generated Python Code")
|
| 29 |
+
btn = gr.Button("Generate")
|
| 30 |
+
btn.click(fn=generate_code, inputs=inp, outputs=out)
|
| 31 |
|
| 32 |
+
gr.Markdown("## Download Trained Model")
|
| 33 |
+
save_btn = gr.Button("Save model to files tab")
|
| 34 |
+
save_status = gr.Textbox(label="Status")
|
| 35 |
+
save_btn.click(fn=save_model_for_download, outputs=save_status)
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
demo.launch(
|