Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|