Spaces:
Sleeping
Sleeping
Walid Sobhi commited on
Add Space files
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Stack 2.9 - HuggingFace Space
|
| 3 |
+
Code Assistant using Qwen2.5-Coder
|
| 4 |
+
"""
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
# Load base model - uses 1.5B model which fits in free tier
|
| 10 |
+
MODEL_NAME = "Qwen/Qwen2.5-Coder-1.5B"
|
| 11 |
+
|
| 12 |
+
print(f"Loading {MODEL_NAME}...")
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
+
MODEL_NAME,
|
| 16 |
+
torch_dtype=torch.float16,
|
| 17 |
+
device_map="auto",
|
| 18 |
+
trust_remote_code=True
|
| 19 |
+
)
|
| 20 |
+
print("Model loaded!")
|
| 21 |
+
|
| 22 |
+
def generate(prompt, system_prompt="You are a helpful coding assistant.", max_tokens=512, temperature=0.7):
|
| 23 |
+
"""Generate response from the model"""
|
| 24 |
+
messages = [
|
| 25 |
+
{"role": "system", "content": system_prompt},
|
| 26 |
+
{"role": "user", "content": prompt}
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 30 |
+
inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 31 |
+
|
| 32 |
+
outputs = model.generate(
|
| 33 |
+
**inputs,
|
| 34 |
+
max_new_tokens=max_tokens,
|
| 35 |
+
temperature=temperature,
|
| 36 |
+
do_sample=True,
|
| 37 |
+
pad_token_id=tokenizer.pad_token_id
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
+
# Remove the input prompt from response
|
| 42 |
+
return response[len(text):].strip()
|
| 43 |
+
|
| 44 |
+
# Build Gradio UI
|
| 45 |
+
with gr.Blocks(title="Stack 2.9 - Code Assistant") as demo:
|
| 46 |
+
gr.Markdown("""
|
| 47 |
+
# Stack 2.9 - Code Assistant
|
| 48 |
+
**Powered by Qwen2.5-Coder-1.5B** fine-tuned on Stack Overflow data
|
| 49 |
+
|
| 50 |
+
Write code, debug, or ask programming questions!
|
| 51 |
+
""")
|
| 52 |
+
|
| 53 |
+
with gr.Row():
|
| 54 |
+
with gr.Column(scale=1):
|
| 55 |
+
system_prompt = gr.Textbox(
|
| 56 |
+
label="System Prompt",
|
| 57 |
+
value="You are a helpful coding assistant specialized in programming.",
|
| 58 |
+
lines=3
|
| 59 |
+
)
|
| 60 |
+
prompt = gr.Textbox(
|
| 61 |
+
label="Your Message",
|
| 62 |
+
placeholder="Write a Python function to calculate fibonacci numbers...",
|
| 63 |
+
lines=6
|
| 64 |
+
)
|
| 65 |
+
with gr.Row():
|
| 66 |
+
max_tokens = gr.Slider(32, 1024, value=512, step=32, label="Max Tokens")
|
| 67 |
+
temperature = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature")
|
| 68 |
+
submit = gr.Button("Generate", variant="primary")
|
| 69 |
+
|
| 70 |
+
with gr.Column(scale=2):
|
| 71 |
+
output = gr.Textbox(label="Response", lines=15)
|
| 72 |
+
|
| 73 |
+
examples = [
|
| 74 |
+
["Write a Python function to calculate fibonacci numbers"],
|
| 75 |
+
["Explain what this code does: def foo(x): return x * 2"],
|
| 76 |
+
["Debug this code: for i in range(10): print(i)"],
|
| 77 |
+
["Write a SQL query to find duplicate emails"],
|
| 78 |
+
]
|
| 79 |
+
|
| 80 |
+
gr.Examples(examples=examples, inputs=[prompt])
|
| 81 |
+
|
| 82 |
+
submit.click(
|
| 83 |
+
fn=generate,
|
| 84 |
+
inputs=[prompt, system_prompt, max_tokens, temperature],
|
| 85 |
+
outputs=output
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
prompt.submit(
|
| 89 |
+
fn=generate,
|
| 90 |
+
inputs=[prompt, system_prompt, max_tokens, temperature],
|
| 91 |
+
outputs=output
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|