File size: 2,626 Bytes
ddc3343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0ce785f
 
ddc3343
0ce785f
 
ddc3343
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import spaces  # noqa: F401  must be imported before torch
import torch
import gradio as gr
from threading import Thread
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer

MODEL_ID = "Qwen/Qwen2.5-Coder-3B-Instruct"

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    torch_dtype=torch.bfloat16,
    attn_implementation="sdpa",
).to("cuda").eval()


@spaces.GPU(duration=60)
def respond(
    message: str,
    history: list[dict[str, str]],
    system_message: str,
    max_tokens: int,
    temperature: float,
    top_p: float,
) -> str:
    """Generate a reply from the coding assistant, streaming it token by token."""
    messages = [{"role": "system", "content": system_message}]
    messages += history
    messages.append({"role": "user", "content": message})

    # apply_chat_template returns a BatchEncoding on new transformers, a tensor on old
    ids = tokenizer.apply_chat_template(
        messages, add_generation_prompt=True, return_tensors="pt"
    )
    input_ids = (ids["input_ids"] if not torch.is_tensor(ids) else ids).to("cuda")

    streamer = TextIteratorStreamer(
        tokenizer, skip_prompt=True, skip_special_tokens=True
    )
    gen_kwargs = dict(
        input_ids=input_ids, streamer=streamer, max_new_tokens=max_tokens
    )
    if temperature > 0.1:
        gen_kwargs.update(do_sample=True, temperature=temperature, top_p=top_p)
    else:
        gen_kwargs.update(do_sample=False)

    Thread(target=model.generate, kwargs=gen_kwargs, daemon=True).start()

    response = ""
    for new_text in streamer:
        response += new_text
        yield response


demo = gr.ChatInterface(
    fn=respond,
    title="Small Code Assistant",
    description="Qwen2.5-Coder-3B-Instruct streaming on ZeroGPU.",
    additional_inputs=[
        gr.Textbox(
            value="You are a helpful coding assistant.", label="System message"
        ),
        gr.Slider(
            minimum=64, maximum=2048, value=512, step=64, label="Max new tokens"
        ),
        gr.Slider(
            minimum=0.0, maximum=2.0, value=0.7, step=0.1, label="Temperature"
        ),
        gr.Slider(
            minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"
        ),
    ],
    examples=[
        ["Write a Python function that checks if a string is a palindrome."],
        ["Explain what this regex does: ^\\d{3}-\\d{4}$"],
        ["Fix the bug: `for i in range(len(items)): print(items[i+1])`"],
    ],
    cache_examples=False,
)

if __name__ == "__main__":
    demo.launch(mcp_server=True)