Spaces:
Running
on
Zero
Running
on
Zero
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch, threading, time, spaces
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 4 |
+
|
| 5 |
+
# ---------------------
|
| 6 |
+
# Model Config
|
| 7 |
+
# ---------------------
|
| 8 |
+
MODEL_ID = "WeiboAI/VibeThinker-1.5B"
|
| 9 |
+
SYSTEM_PROMPT = "You are a concise solver. Respond briefly with the correct answer."
|
| 10 |
+
|
| 11 |
+
print(f"⏳ Loading {MODEL_ID} …")
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
MODEL_ID,
|
| 15 |
+
trust_remote_code=True,
|
| 16 |
+
low_cpu_mem_usage=True,
|
| 17 |
+
dtype=torch.bfloat16,
|
| 18 |
+
device_map="auto"
|
| 19 |
+
)
|
| 20 |
+
print("✅ Model ready.")
|
| 21 |
+
|
| 22 |
+
# ---------------------
|
| 23 |
+
# Chat Function
|
| 24 |
+
# ---------------------
|
| 25 |
+
@spaces.GPU(duration=60)
|
| 26 |
+
def chat_fn(message, history):
|
| 27 |
+
history = history or []
|
| 28 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 29 |
+
for user_msg, bot_msg in history:
|
| 30 |
+
if user_msg: messages.append({"role": "user", "content": user_msg})
|
| 31 |
+
if bot_msg: messages.append({"role": "assistant", "content": bot_msg})
|
| 32 |
+
messages.append({"role": "user", "content": message})
|
| 33 |
+
|
| 34 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 35 |
+
inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
|
| 36 |
+
|
| 37 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 38 |
+
gen_kwargs = dict(
|
| 39 |
+
**inputs,
|
| 40 |
+
streamer=streamer,
|
| 41 |
+
max_new_tokens=200,
|
| 42 |
+
temperature=0.3,
|
| 43 |
+
top_p=0.9,
|
| 44 |
+
do_sample=False,
|
| 45 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 46 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 47 |
+
repetition_penalty=1.15
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
thread = threading.Thread(target=model.generate, kwargs=gen_kwargs)
|
| 51 |
+
thread.start()
|
| 52 |
+
|
| 53 |
+
partial_text = ""
|
| 54 |
+
for new_text in streamer:
|
| 55 |
+
partial_text += new_text
|
| 56 |
+
yield partial_text
|
| 57 |
+
|
| 58 |
+
# ---------------------
|
| 59 |
+
# UI
|
| 60 |
+
# ---------------------
|
| 61 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 62 |
+
gr.Markdown("## 💡 VibeThinker-1.5B · Edge/ZeroGPU (Streaming Stable)")
|
| 63 |
+
chatbot = gr.Chatbot(label="Chatbot", height=500)
|
| 64 |
+
msg_box = gr.Textbox(label="Textbox", placeholder="Type here…")
|
| 65 |
+
send_btn = gr.Button("Send", variant="primary")
|
| 66 |
+
|
| 67 |
+
def user_message(message, history):
|
| 68 |
+
history = history or []
|
| 69 |
+
return "", history + [[message, None]]
|
| 70 |
+
|
| 71 |
+
def bot_response(history):
|
| 72 |
+
user_message = history[-1][0]
|
| 73 |
+
response = ""
|
| 74 |
+
for partial in chat_fn(user_message, history[:-1]):
|
| 75 |
+
response = partial
|
| 76 |
+
history[-1][1] = response
|
| 77 |
+
yield history
|
| 78 |
+
|
| 79 |
+
msg_box.submit(user_message, [msg_box, chatbot], [msg_box, chatbot], queue=False).then(
|
| 80 |
+
bot_response, chatbot, chatbot
|
| 81 |
+
)
|
| 82 |
+
send_btn.click(user_message, [msg_box, chatbot], [msg_box, chatbot], queue=False).then(
|
| 83 |
+
bot_response, chatbot, chatbot
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
demo.queue(max_size=16).launch()
|