Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131415161718192021222324252627282930import gradio as grfrom transformers import AutoModelForCausalLM, AutoTokenizerimport torchmodel_name = "Percy3822/python_ai_coder" # Replace with your model repo name after trainingtokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float16, device_map="auto")def generate_code(prompt):inputs = tokenizer(prompt, return_tensors="pt").to(model.device)with torch.no_grad():outputs = model.generate(**inputs,max_new_tokens=128,do_sample=True,temperature=0.8,top_p=0.95,eos_token_id=tokenizer.eos_token_id)return tokenizer.decode(outputs[0], skip_special_tokens=True)iface = gr.Interface(fn=generate_code,inputs=gr.Textbox(lines=5, placeholder="Ask me to write/fix/explain Python code..."),outputs="text",title="Python AI Assistant (Trained on StarCoder)",description="Ask it to write functions, fix bugs, explain code, etc.")iface.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import osimport subprocessimport gradio as grimport threadingimport timeimport zipfileoutput_path = "train_output"zipped_model = "python_ai_trained_model.zip"status = gr.Textbox(label="Status", value="Ready", interactive=False)download_link = gr.File(label="Download Trained Model", visible=False)def run_training():global zipped_modelstatus.value = "Training started... this may take a while (15–60+ mins)."# Run train.pysubprocess.run(["python", "train.py"], check=True)# Compress trained modelstatus.value = "Training complete. Compressing model..."with zipfile.ZipFile(zipped_model, 'w', zipfile.ZIP_DEFLATED) as zipf:for root, _, files in os.walk(output_path):for file in files:filepath = os.path.join(root, file)arcname = os.path.relpath(filepath, output_path)zipf.write(filepath, arcname)# Move zip to Gradio-visible pathstatus.value = "Done. Model ready for download."download_link.visible = Truedef start_training():training_thread = threading.Thread(target=run_training)training_thread.start()return "Training in progress...", Nonewith gr.Blocks() as demo:gr.Markdown("## 🧠 Python AI Model Trainer (StarCoder 7B)")gr.Markdown("Click the button below to start fine-tuning your custom Python AI. After training, download the model and use it anywhere.")with gr.Row():train_btn = gr.Button("🚀 Start Training")train_btn.click(start_training, outputs=[status, download_link])status.render()download_link.render()demo.launch()
app.py+48-29