File size: 2,918 Bytes
e02d537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
85
86
87
import gradio as gr
import torch, threading, time, spaces
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer

# ---------------------
# Model Config
# ---------------------
MODEL_ID = "WeiboAI/VibeThinker-1.5B"
SYSTEM_PROMPT = "You are a concise solver. Respond briefly with the correct answer."

print(f"⏳ Loading {MODEL_ID} …")
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    trust_remote_code=True,
    low_cpu_mem_usage=True,
    dtype=torch.bfloat16,
    device_map="auto"
)
print("✅ Model ready.")

# ---------------------
# Chat Function
# ---------------------
@spaces.GPU(duration=60)
def chat_fn(message, history):
    history = history or []
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]
    for user_msg, bot_msg in history:
        if user_msg: messages.append({"role": "user", "content": user_msg})
        if bot_msg: messages.append({"role": "assistant", "content": bot_msg})
    messages.append({"role": "user", "content": message})

    prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
    inputs = tokenizer([prompt], return_tensors="pt").to(model.device)

    streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
    gen_kwargs = dict(
        **inputs,
        streamer=streamer,
        max_new_tokens=200,
        temperature=0.3,
        top_p=0.9,
        do_sample=False,
        pad_token_id=tokenizer.eos_token_id,
        eos_token_id=tokenizer.eos_token_id,
        repetition_penalty=1.15
    )

    thread = threading.Thread(target=model.generate, kwargs=gen_kwargs)
    thread.start()

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

# ---------------------
# UI
# ---------------------
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("## 💡 VibeThinker-1.5B · Edge/ZeroGPU (Streaming Stable)")
    chatbot = gr.Chatbot(label="Chatbot", height=500)
    msg_box = gr.Textbox(label="Textbox", placeholder="Type here…")
    send_btn = gr.Button("Send", variant="primary")

    def user_message(message, history):
        history = history or []
        return "", history + [[message, None]]

    def bot_response(history):
        user_message = history[-1][0]
        response = ""
        for partial in chat_fn(user_message, history[:-1]):
            response = partial
            history[-1][1] = response
            yield history

    msg_box.submit(user_message, [msg_box, chatbot], [msg_box, chatbot], queue=False).then(
        bot_response, chatbot, chatbot
    )
    send_btn.click(user_message, [msg_box, chatbot], [msg_box, chatbot], queue=False).then(
        bot_response, chatbot, chatbot
    )

if __name__ == "__main__":
    demo.queue(max_size=16).launch()