Percy3822 commited on
Commit
481894e
·
verified ·
1 Parent(s): 10a7fed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -39
app.py CHANGED
@@ -1,46 +1,16 @@
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()
 
 
 
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()