Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,46 +1,16 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from pathlib import Path
|
| 3 |
import os
|
| 4 |
-
import
|
| 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 |
-
|
| 25 |
-
|
| 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("
|
| 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 |
-
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Check for trained model zip
|
| 5 |
+
model_zip = "trained_model.zip"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
with gr.Blocks() as demo:
|
| 8 |
+
gr.Markdown("## 🤖 Python AI Model Trainer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
if os.path.exists(model_zip):
|
| 11 |
+
gr.Markdown("### ✅ Model trained! Click below to download:")
|
| 12 |
+
gr.File(value=model_zip, label="Download trained model")
|
| 13 |
+
else:
|
| 14 |
+
gr.Markdown("### 🚫 No trained model found yet. Run train.py to train it.")
|
| 15 |
|
| 16 |
demo.launch()
|