Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
from pathlib import Path
|
| 4 |
-
import torch
|
| 5 |
-
import zipfile
|
| 6 |
import os
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(model_dir, local_files_only=True)
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(model_dir, local_files_only=True)
|
| 14 |
|
| 15 |
-
# Inference function
|
| 16 |
def generate_code(prompt):
|
| 17 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 18 |
-
|
| 19 |
-
outputs = model.generate(
|
| 20 |
-
**inputs,
|
| 21 |
-
max_new_tokens=128,
|
| 22 |
-
pad_token_id=tokenizer.eos_token_id
|
| 23 |
-
)
|
| 24 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 25 |
|
| 26 |
-
# Create a downloadable zip of the trained model
|
| 27 |
def zip_model():
|
| 28 |
zip_path = Path("trained_model.zip")
|
| 29 |
-
with zipfile.ZipFile(zip_path, "w"
|
| 30 |
-
for
|
| 31 |
-
for file in
|
| 32 |
-
filepath = Path(
|
| 33 |
-
|
| 34 |
-
zipf.write(filepath, arcname)
|
| 35 |
return str(zip_path)
|
| 36 |
|
| 37 |
-
# Gradio UI
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
-
gr.Markdown("#
|
| 40 |
-
|
|
|
|
| 41 |
output = gr.Textbox(label="AI Response")
|
| 42 |
|
| 43 |
with gr.Row():
|
| 44 |
-
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
| 50 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from pathlib import Path
|
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
+
import zipfile
|
| 5 |
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
model_dir = Path(_file_).parent / "trained_model"
|
| 10 |
|
| 11 |
+
# Gracefully fail if model not trained yet
|
| 12 |
+
if not model_dir.exists():
|
| 13 |
+
raise RuntimeError("❌ Model not trained yet! Please train it using train.py.")
|
| 14 |
+
|
| 15 |
+
# Load locally
|
| 16 |
tokenizer = AutoTokenizer.from_pretrained(model_dir, local_files_only=True)
|
| 17 |
model = AutoModelForCausalLM.from_pretrained(model_dir, local_files_only=True)
|
| 18 |
|
|
|
|
| 19 |
def generate_code(prompt):
|
| 20 |
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=512)
|
| 21 |
+
outputs = model.generate(**inputs, max_new_tokens=100, pad_token_id=tokenizer.eos_token_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
|
|
|
|
| 24 |
def zip_model():
|
| 25 |
zip_path = Path("trained_model.zip")
|
| 26 |
+
with zipfile.ZipFile(zip_path, "w") as zf:
|
| 27 |
+
for foldername, _, filenames in os.walk(model_dir):
|
| 28 |
+
for file in filenames:
|
| 29 |
+
filepath = Path(foldername) / file
|
| 30 |
+
zf.write(filepath, filepath.relative_to(model_dir.parent))
|
|
|
|
| 31 |
return str(zip_path)
|
| 32 |
|
|
|
|
| 33 |
with gr.Blocks() as demo:
|
| 34 |
+
gr.Markdown("# 🤖 Trained Python Code Generator")
|
| 35 |
+
|
| 36 |
+
prompt = gr.Textbox(label="Enter prompt")
|
| 37 |
output = gr.Textbox(label="AI Response")
|
| 38 |
|
| 39 |
with gr.Row():
|
| 40 |
+
gen_btn = gr.Button("Generate")
|
| 41 |
+
dl_btn = gr.Button("Download Model ZIP")
|
| 42 |
|
| 43 |
+
gen_btn.click(generate_code, inputs=prompt, outputs=output)
|
| 44 |
+
dl_btn.click(zip_model, outputs=gr.File(label="Download ZIP"))
|
| 45 |
|
| 46 |
demo.launch()
|