Upload qwen_base.py
Browse files- qwen_base.py +118 -0
qwen_base.py
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Minimal Qwen 3 base chat app.
|
| 3 |
+
|
| 4 |
+
Loads Qwen/Qwen3-30B-A3B-Instruct-2507 in 4-bit mode and exposes a
|
| 5 |
+
simple Gradio chat interface. No custom metrics, quantum simulation,
|
| 6 |
+
phenomenology, volition, or custom logits processing.
|
| 7 |
+
|
| 8 |
+
Requirements:
|
| 9 |
+
pip install -U torch transformers accelerate bitsandbytes gradio
|
| 10 |
+
|
| 11 |
+
Run:
|
| 12 |
+
python qwen_base.py
|
| 13 |
+
|
| 14 |
+
Notes:
|
| 15 |
+
- A CUDA-capable GPU is recommended for this 30B-class model.
|
| 16 |
+
- 4-bit loading reduces memory use but does not make the model small.
|
| 17 |
+
- Set QWEN_MODEL to use a different compatible Hugging Face model ID.
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
import os
|
| 21 |
+
|
| 22 |
+
import gradio as gr
|
| 23 |
+
import torch
|
| 24 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
MODEL_ID = os.getenv(
|
| 28 |
+
"QWEN_MODEL",
|
| 29 |
+
"Qwen/Qwen3-30B-A3B-Instruct-2507",
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 33 |
+
|
| 34 |
+
load_kwargs = {
|
| 35 |
+
"device_map": "auto",
|
| 36 |
+
"torch_dtype": torch.bfloat16 if torch.cuda.is_available() else torch.float32,
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
if torch.cuda.is_available():
|
| 40 |
+
load_kwargs["quantization_config"] = BitsAndBytesConfig(
|
| 41 |
+
load_in_4bit=True,
|
| 42 |
+
bnb_4bit_quant_type="nf4",
|
| 43 |
+
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 44 |
+
bnb_4bit_use_double_quant=True,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, **load_kwargs)
|
| 48 |
+
model.eval()
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def respond(message, history, max_new_tokens, temperature):
|
| 52 |
+
messages = []
|
| 53 |
+
for item in history or []:
|
| 54 |
+
# Gradio Chatbot(type="messages") supplies dictionaries.
|
| 55 |
+
if isinstance(item, dict):
|
| 56 |
+
role = item.get("role")
|
| 57 |
+
text = item.get("content", "")
|
| 58 |
+
if role in ("user", "assistant") and isinstance(text, str):
|
| 59 |
+
messages.append({"role": role, "content": text})
|
| 60 |
+
# Also accept legacy (user, assistant) tuples.
|
| 61 |
+
elif isinstance(item, (tuple, list)) and len(item) == 2:
|
| 62 |
+
user_text, assistant_text = item
|
| 63 |
+
if user_text:
|
| 64 |
+
messages.append({"role": "user", "content": str(user_text)})
|
| 65 |
+
if assistant_text:
|
| 66 |
+
messages.append({"role": "assistant", "content": str(assistant_text)})
|
| 67 |
+
|
| 68 |
+
messages.append({"role": "user", "content": message})
|
| 69 |
+
|
| 70 |
+
prompt = tokenizer.apply_chat_template(
|
| 71 |
+
messages,
|
| 72 |
+
tokenize=False,
|
| 73 |
+
add_generation_prompt=True,
|
| 74 |
+
)
|
| 75 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 76 |
+
|
| 77 |
+
generation_kwargs = {
|
| 78 |
+
"max_new_tokens": int(max_new_tokens),
|
| 79 |
+
"do_sample": float(temperature) > 0,
|
| 80 |
+
"pad_token_id": tokenizer.eos_token_id,
|
| 81 |
+
}
|
| 82 |
+
if float(temperature) > 0:
|
| 83 |
+
generation_kwargs["temperature"] = float(temperature)
|
| 84 |
+
generation_kwargs["top_p"] = 0.95
|
| 85 |
+
|
| 86 |
+
with torch.inference_mode():
|
| 87 |
+
output = model.generate(**inputs, **generation_kwargs)
|
| 88 |
+
|
| 89 |
+
new_tokens = output[0, inputs["input_ids"].shape[1]:]
|
| 90 |
+
answer = tokenizer.decode(new_tokens, skip_special_tokens=True).strip()
|
| 91 |
+
return answer
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
demo = gr.ChatInterface(
|
| 95 |
+
fn=respond,
|
| 96 |
+
title="Qwen Base Chat",
|
| 97 |
+
description=f"Minimal chat interface for {MODEL_ID}",
|
| 98 |
+
type="messages",
|
| 99 |
+
additional_inputs=[
|
| 100 |
+
gr.Slider(
|
| 101 |
+
minimum=64,
|
| 102 |
+
maximum=4096,
|
| 103 |
+
value=1024,
|
| 104 |
+
step=64,
|
| 105 |
+
label="Max new tokens",
|
| 106 |
+
),
|
| 107 |
+
gr.Slider(
|
| 108 |
+
minimum=0.0,
|
| 109 |
+
maximum=1.5,
|
| 110 |
+
value=0.7,
|
| 111 |
+
step=0.05,
|
| 112 |
+
label="Temperature (0 = greedy)",
|
| 113 |
+
),
|
| 114 |
+
],
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
if __name__ == "__main__":
|
| 118 |
+
demo.queue().launch()
|