Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
3 |
+
from threading import Thread
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load the model and tokenizer
|
7 |
+
model_name = "Qwen/Qwen2.5-Coder-1.5B"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float32, low_cpu_mem_usage=True)
|
10 |
+
|
11 |
+
def generate_code(prompt, max_length):
|
12 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
13 |
+
streamer = TextIteratorStreamer(tokenizer)
|
14 |
+
|
15 |
+
generation_kwargs = dict(inputs, streamer=streamer, max_length=max_length)
|
16 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
17 |
+
thread.start()
|
18 |
+
|
19 |
+
generated_text = ""
|
20 |
+
for new_text in streamer:
|
21 |
+
generated_text += new_text
|
22 |
+
yield generated_text
|
23 |
+
|
24 |
+
# Gradio interface
|
25 |
+
iface = gr.Interface(
|
26 |
+
fn=generate_code,
|
27 |
+
inputs=[
|
28 |
+
gr.Textbox(lines=5, label="Enter your prompt"),
|
29 |
+
gr.Slider(minimum=50, maximum=500, value=200, step=10, label="Max Length")
|
30 |
+
],
|
31 |
+
outputs=gr.Code(language="python", label="Generated Code"),
|
32 |
+
title="Qwen2.5-Coder-1.5B Code Generator",
|
33 |
+
description="Enter a prompt to generate Python code using Qwen2.5-Coder-1.5B",
|
34 |
+
examples=[
|
35 |
+
["Write a Python function to calculate the factorial of a number."],
|
36 |
+
["Create a class representing a simple bank account with deposit and withdraw methods."]
|
37 |
+
]
|
38 |
+
)
|
39 |
+
|
40 |
+
iface.launch()
|