Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,23 @@
|
|
| 1 |
import torch
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 6 |
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
|
|
|
|
|
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
MODEL_NAME,
|
| 10 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 11 |
device_map="auto",
|
|
|
|
| 12 |
trust_remote_code=True
|
| 13 |
)
|
| 14 |
|
| 15 |
def chat(user_input, history):
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
for h in history:
|
| 19 |
-
messages.append({"role": "user", "content": h[0]})
|
| 20 |
-
messages.append({"role": "assistant", "content": h[1]})
|
| 21 |
-
|
| 22 |
-
messages.append({"role": "user", "content": user_input})
|
| 23 |
|
| 24 |
prompt = tokenizer.apply_chat_template(
|
| 25 |
messages,
|
|
@@ -32,9 +30,9 @@ def chat(user_input, history):
|
|
| 32 |
outputs = model.generate(
|
| 33 |
**inputs,
|
| 34 |
max_new_tokens=512,
|
| 35 |
-
do_sample=True,
|
| 36 |
temperature=0.7,
|
| 37 |
-
top_p=0.9
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
response = tokenizer.decode(
|
|
@@ -42,14 +40,15 @@ def chat(user_input, history):
|
|
| 42 |
skip_special_tokens=True
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
gr.Markdown("## 🤖 Qwen Chatbot")
|
| 50 |
|
| 51 |
-
chatbot = gr.Chatbot()
|
| 52 |
-
msg = gr.Textbox(label="Your message")
|
| 53 |
clear = gr.Button("Clear")
|
| 54 |
|
| 55 |
msg.submit(chat, [msg, chatbot], [chatbot, msg])
|
|
|
|
| 1 |
import torch
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
MODEL_NAME = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 6 |
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 8 |
+
MODEL_NAME, trust_remote_code=True
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
MODEL_NAME,
|
|
|
|
| 13 |
device_map="auto",
|
| 14 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 15 |
trust_remote_code=True
|
| 16 |
)
|
| 17 |
|
| 18 |
def chat(user_input, history):
|
| 19 |
+
# history is already in messages format
|
| 20 |
+
messages = history + [{"role": "user", "content": user_input}]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
prompt = tokenizer.apply_chat_template(
|
| 23 |
messages,
|
|
|
|
| 30 |
outputs = model.generate(
|
| 31 |
**inputs,
|
| 32 |
max_new_tokens=512,
|
|
|
|
| 33 |
temperature=0.7,
|
| 34 |
+
top_p=0.9,
|
| 35 |
+
do_sample=True
|
| 36 |
)
|
| 37 |
|
| 38 |
response = tokenizer.decode(
|
|
|
|
| 40 |
skip_special_tokens=True
|
| 41 |
)
|
| 42 |
|
| 43 |
+
messages.append({"role": "assistant", "content": response})
|
| 44 |
+
|
| 45 |
+
return messages, ""
|
| 46 |
|
| 47 |
with gr.Blocks() as demo:
|
| 48 |
gr.Markdown("## 🤖 Qwen Chatbot")
|
| 49 |
|
| 50 |
+
chatbot = gr.Chatbot(type="messages")
|
| 51 |
+
msg = gr.Textbox(label="Your message", autofocus=True)
|
| 52 |
clear = gr.Button("Clear")
|
| 53 |
|
| 54 |
msg.submit(chat, [msg, chatbot], [chatbot, msg])
|