Nafiul Huda commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,30 +9,29 @@ gguf_filename = "Qwen3-0.6B-UD-Q8_K_XL.gguf"
|
|
| 9 |
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_id, gguf_file=gguf_filename)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
-
model_id,
|
| 13 |
-
|
| 14 |
-
device_map="cpu",
|
| 15 |
-
dtype=torch.float32
|
| 16 |
)
|
| 17 |
|
| 18 |
-
def
|
| 19 |
-
messages = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
input_text = tokenizer.apply_chat_template(
|
| 21 |
-
messages, tokenize=False,
|
| 22 |
-
add_generation_prompt=True,
|
| 23 |
-
enable_thinking=True
|
| 24 |
)
|
| 25 |
inputs = tokenizer([input_text], return_tensors="pt").to("cpu")
|
| 26 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 27 |
|
| 28 |
thread = Thread(target=model.generate, kwargs=dict(
|
| 29 |
-
**inputs,
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
temperature=0.6,
|
| 33 |
-
top_p=0.95,
|
| 34 |
-
top_k=20,
|
| 35 |
-
do_sample=True
|
| 36 |
))
|
| 37 |
thread.start()
|
| 38 |
|
|
@@ -42,15 +41,20 @@ def generate(message):
|
|
| 42 |
clean = re.sub(r"<think>.*?</think>", "", partial, flags=re.DOTALL)
|
| 43 |
clean = re.sub(r"<think>.*$", "", clean, flags=re.DOTALL)
|
| 44 |
yield clean.strip()
|
| 45 |
-
|
| 46 |
thread.join()
|
| 47 |
|
| 48 |
-
demo = gr.
|
| 49 |
-
fn=
|
| 50 |
-
inputs=gr.Textbox(label="Message"),
|
| 51 |
-
outputs=gr.Textbox(label="Response"),
|
| 52 |
title="Qwen3-0.6B",
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
)
|
| 55 |
|
| 56 |
demo.launch(ssr_mode=False)
|
|
|
|
| 9 |
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(model_id, gguf_file=gguf_filename)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
model_id, gguf_file=gguf_filename,
|
| 13 |
+
device_map="cpu", dtype=torch.float32
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
+
def respond(message, history, system_prompt, max_tokens, temperature, top_p, top_k):
|
| 17 |
+
messages = []
|
| 18 |
+
if system_prompt and system_prompt.strip():
|
| 19 |
+
messages.append({"role": "system", "content": system_prompt.strip()})
|
| 20 |
+
for turn in history:
|
| 21 |
+
content = re.sub(r"<think>.*?</think>", "", turn["content"], flags=re.DOTALL)
|
| 22 |
+
messages.append({"role": turn["role"], "content": content})
|
| 23 |
+
messages.append({"role": "user", "content": message})
|
| 24 |
+
|
| 25 |
input_text = tokenizer.apply_chat_template(
|
| 26 |
+
messages, tokenize=False, add_generation_prompt=True, enable_thinking=True
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
inputs = tokenizer([input_text], return_tensors="pt").to("cpu")
|
| 29 |
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 30 |
|
| 31 |
thread = Thread(target=model.generate, kwargs=dict(
|
| 32 |
+
**inputs, streamer=streamer,
|
| 33 |
+
max_new_tokens=int(max_tokens), temperature=float(temperature),
|
| 34 |
+
top_p=float(top_p), top_k=int(top_k), do_sample=True
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
))
|
| 36 |
thread.start()
|
| 37 |
|
|
|
|
| 41 |
clean = re.sub(r"<think>.*?</think>", "", partial, flags=re.DOTALL)
|
| 42 |
clean = re.sub(r"<think>.*$", "", clean, flags=re.DOTALL)
|
| 43 |
yield clean.strip()
|
|
|
|
| 44 |
thread.join()
|
| 45 |
|
| 46 |
+
demo = gr.ChatInterface(
|
| 47 |
+
fn=respond,
|
|
|
|
|
|
|
| 48 |
title="Qwen3-0.6B",
|
| 49 |
+
description="Lightweight reasoning model. Streaming enabled.",
|
| 50 |
+
additional_inputs=[
|
| 51 |
+
gr.Textbox(label="System Prompt", placeholder="You are a helpful assistant.", value="", lines=2),
|
| 52 |
+
gr.Slider(64, 4096, value=2048, step=64, label="Max Tokens"),
|
| 53 |
+
gr.Slider(0.05, 2.0, value=0.6, step=0.05, label="Temperature"),
|
| 54 |
+
gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-P"),
|
| 55 |
+
gr.Slider(1, 100, value=20, step=1, label="Top-K"),
|
| 56 |
+
],
|
| 57 |
+
additional_inputs_accordion=gr.Accordion("Settings", open=False),
|
| 58 |
)
|
| 59 |
|
| 60 |
demo.launch(ssr_mode=False)
|