Spaces:
Running on Zero
Running on Zero
Add Qwen3.5-35B-A3B AWQ chat app with ZeroGPU support
Browse files
app.py
CHANGED
|
@@ -1,69 +1,133 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
-
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
-
messages.extend(history)
|
| 22 |
-
|
| 23 |
-
messages.append({"role": "user", "content": message})
|
| 24 |
-
|
| 25 |
-
response = ""
|
| 26 |
-
|
| 27 |
-
for message in client.chat_completion(
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
-
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
demo.launch()
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, TextIteratorStreamer
|
| 5 |
+
from threading import Thread
|
| 6 |
+
|
| 7 |
+
MODEL_ID = "QuantTrio/Qwen3.5-35B-A3B-AWQ"
|
| 8 |
+
|
| 9 |
+
print("Loading tokenizer...")
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 11 |
+
|
| 12 |
+
print("Loading model...")
|
| 13 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
+
MODEL_ID,
|
| 15 |
+
torch_dtype=torch.float16,
|
| 16 |
+
device_map="cpu",
|
| 17 |
+
trust_remote_code=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
+
model.eval()
|
| 20 |
+
print("Model loaded.")
|
| 21 |
+
|
| 22 |
+
@spaces.GPU(duration=120)
|
| 23 |
+
def respond(message, history, system_message, max_new_tokens, temperature, top_p, enable_thinking):
|
| 24 |
+
model.to("cuda")
|
| 25 |
+
|
| 26 |
+
messages = [{"role": "system", "content": system_message}]
|
| 27 |
+
for user_msg, assistant_msg in history:
|
| 28 |
+
if user_msg:
|
| 29 |
+
messages.append({"role": "user", "content": user_msg})
|
| 30 |
+
if assistant_msg:
|
| 31 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 32 |
+
messages.append({"role": "user", "content": message})
|
| 33 |
+
|
| 34 |
+
text = tokenizer.apply_chat_template(
|
| 35 |
+
messages,
|
| 36 |
+
tokenize=False,
|
| 37 |
+
add_generation_prompt=True,
|
| 38 |
+
chat_template_kwargs={"enable_thinking": enable_thinking},
|
| 39 |
+
)
|
| 40 |
+
inputs = tokenizer([text], return_tensors="pt").to("cuda")
|
| 41 |
+
|
| 42 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 43 |
+
|
| 44 |
+
generation_kwargs = dict(
|
| 45 |
+
**inputs,
|
| 46 |
+
streamer=streamer,
|
| 47 |
+
max_new_tokens=max_new_tokens,
|
| 48 |
+
temperature=temperature,
|
| 49 |
+
top_p=top_p,
|
| 50 |
+
top_k=20,
|
| 51 |
+
do_sample=True,
|
| 52 |
+
repetition_penalty=1.1,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 56 |
+
thread.start()
|
| 57 |
+
|
| 58 |
+
partial = ""
|
| 59 |
+
thinking_done = False
|
| 60 |
+
display_text = ""
|
| 61 |
+
|
| 62 |
+
for new_text in streamer:
|
| 63 |
+
partial += new_text
|
| 64 |
+
if enable_thinking:
|
| 65 |
+
if "</think>" in partial and not thinking_done:
|
| 66 |
+
thinking_done = True
|
| 67 |
+
parts = partial.split("</think>", 1)
|
| 68 |
+
display_text = parts[1].lstrip("\n")
|
| 69 |
+
elif thinking_done:
|
| 70 |
+
display_text = partial.split("</think>", 1)[1].lstrip("\n")
|
| 71 |
+
else:
|
| 72 |
+
display_text = ""
|
| 73 |
+
else:
|
| 74 |
+
display_text = partial
|
| 75 |
+
yield display_text
|
| 76 |
+
|
| 77 |
+
model.to("cpu")
|
| 78 |
+
torch.cuda.empty_cache()
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
with gr.Blocks(title="Qwen3.5-35B-A3B Chat") as demo:
|
| 82 |
+
gr.Markdown("# Qwen3.5-35B-A3B AWQ Chat\nPowered by ZeroGPU (H200) | 4-bit AWQ quantized | 25.5 GB")
|
| 83 |
+
|
| 84 |
+
chatbot = gr.Chatbot(height=500, label="Qwen3.5-35B-A3B")
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
| 87 |
+
msg = gr.Textbox(
|
| 88 |
+
placeholder="Type your message here...",
|
| 89 |
+
label="Your message",
|
| 90 |
+
scale=4,
|
| 91 |
+
)
|
| 92 |
+
send_btn = gr.Button("Send", variant="primary", scale=1)
|
| 93 |
+
|
| 94 |
+
with gr.Accordion("Settings", open=False):
|
| 95 |
+
system_message = gr.Textbox(
|
| 96 |
+
value="You are a helpful, smart, and concise AI assistant.",
|
| 97 |
+
label="System message",
|
| 98 |
+
)
|
| 99 |
+
enable_thinking = gr.Checkbox(value=True, label="Enable thinking mode (chain of thought)")
|
| 100 |
+
max_new_tokens = gr.Slider(minimum=64, maximum=8192, value=2048, step=64, label="Max new tokens")
|
| 101 |
+
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=1.0, step=0.1, label="Temperature")
|
| 102 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
|
| 103 |
+
|
| 104 |
+
def user_turn(user_message, history):
|
| 105 |
+
return "", history + [[user_message, None]]
|
| 106 |
+
|
| 107 |
+
def bot_turn(history, system_message, max_new_tokens, temperature, top_p, enable_thinking):
|
| 108 |
+
user_message = history[-1][0]
|
| 109 |
+
bot_history = history[:-1]
|
| 110 |
+
response = ""
|
| 111 |
+
for chunk in respond(user_message, bot_history, system_message, max_new_tokens, temperature, top_p, enable_thinking):
|
| 112 |
+
response = chunk
|
| 113 |
+
history[-1][1] = response
|
| 114 |
+
yield history
|
| 115 |
|
| 116 |
+
msg.submit(user_turn, [msg, chatbot], [msg, chatbot]).then(
|
| 117 |
+
bot_turn, [chatbot, system_message, max_new_tokens, temperature, top_p, enable_thinking], chatbot
|
| 118 |
+
)
|
| 119 |
+
send_btn.click(user_turn, [msg, chatbot], [msg, chatbot]).then(
|
| 120 |
+
bot_turn, [chatbot, system_message, max_new_tokens, temperature, top_p, enable_thinking], chatbot
|
| 121 |
+
)
|
| 122 |
|
| 123 |
+
gr.Examples(
|
| 124 |
+
examples=[
|
| 125 |
+
["What is the square root of 144? Think step by step."],
|
| 126 |
+
["Write a Python function to check if a number is prime."],
|
| 127 |
+
["Explain quantum entanglement in simple terms."],
|
| 128 |
+
["Translate 'Hello, how are you?' into French, Spanish, and Japanese."],
|
| 129 |
+
],
|
| 130 |
+
inputs=msg,
|
| 131 |
+
)
|
| 132 |
|
| 133 |
+
demo.launch()
|
|
|