Percy3822 commited on
Commit
ac2c6e9
·
verified ·
1 Parent(s): 462909d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -29
app.py CHANGED
@@ -1,30 +1,49 @@
 
 
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
- import torch
4
-
5
- model_name = "Percy3822/python_ai_coder" # Replace with your model repo name after training
6
- tokenizer = AutoTokenizer.from_pretrained(model_name)
7
- model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")
8
-
9
- def generate_code(prompt):
10
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
11
- with torch.no_grad():
12
- outputs = model.generate(
13
- **inputs,
14
- max_new_tokens=128,
15
- do_sample=True,
16
- temperature=0.8,
17
- top_p=0.95,
18
- eos_token_id=tokenizer.eos_token_id
19
- )
20
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
21
-
22
- iface = gr.Interface(
23
- fn=generate_code,
24
- inputs=gr.Textbox(lines=5, placeholder="Ask me to write/fix/explain Python code..."),
25
- outputs="text",
26
- title="Python AI Assistant (Trained on StarCoder)",
27
- description="Ask it to write functions, fix bugs, explain code, etc."
28
- )
29
-
30
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import subprocess
3
  import gradio as gr
4
+ import threading
5
+ import time
6
+ import zipfile
7
+
8
+ output_path = "train_output"
9
+ zipped_model = "python_ai_trained_model.zip"
10
+ status = gr.Textbox(label="Status", value="Ready", interactive=False)
11
+ download_link = gr.File(label="Download Trained Model", visible=False)
12
+
13
+ def run_training():
14
+ global zipped_model
15
+ status.value = "Training started... this may take a while (15–60+ mins)."
16
+
17
+ # Run train.py
18
+ subprocess.run(["python", "train.py"], check=True)
19
+
20
+ # Compress trained model
21
+ status.value = "Training complete. Compressing model..."
22
+ with zipfile.ZipFile(zipped_model, 'w', zipfile.ZIP_DEFLATED) as zipf:
23
+ for root, _, files in os.walk(output_path):
24
+ for file in files:
25
+ filepath = os.path.join(root, file)
26
+ arcname = os.path.relpath(filepath, output_path)
27
+ zipf.write(filepath, arcname)
28
+
29
+ # Move zip to Gradio-visible path
30
+ status.value = "Done. Model ready for download."
31
+ download_link.visible = True
32
+
33
+ def start_training():
34
+ training_thread = threading.Thread(target=run_training)
35
+ training_thread.start()
36
+ return "Training in progress...", None
37
+
38
+ with gr.Blocks() as demo:
39
+ gr.Markdown("## 🧠 Python AI Model Trainer (StarCoder 7B)")
40
+ gr.Markdown("Click the button below to start fine-tuning your custom Python AI. After training, download the model and use it anywhere.")
41
+
42
+ with gr.Row():
43
+ train_btn = gr.Button("🚀 Start Training")
44
+ train_btn.click(start_training, outputs=[status, download_link])
45
+
46
+ status.render()
47
+ download_link.render()
48
+
49
+ demo.launch()