Delete app.py
Browse filesfrom transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import gradio as gr
model_name = "Qwen/Qwen2.5-0.5B-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
device_map="auto"
)
def chat(user_input):
# 系统提示,仍然用于引导模型的行为
messages = [
{"role": "system", "content": "你是一个中文助理,回答要简短清晰,不超过50字。"},
{"role": "user", "content": user_input}
]
# 生成的文本内容
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
inputs = tokenizer(text, return_tensors="pt").to(model.device)
# 生成模型输出
outputs = model.generate(
**inputs,
max_new_tokens=50,
do_sample=True,
temperature=0.7,
)
# 解码生成的文本,并去掉 "user" 和 "system" 的部分,只返回纯粹的回答
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
# 只保留模型的回答部分,可以根据需要进一步清理
if "user" in answer: # 如果模型回答中还包含“user”,去掉
answer = answer.split("user:")[-1]
return answer.strip()
iface = gr.Interface(
fn=chat,
inputs=gr.Textbox(lines=3, label="输入你的问题"),
outputs=gr.Textbox(lines=10, label="回答"),
title="Qwen 0.5B 中文问答机器人"
)
iface.launch()
|
@@ -1,49 +0,0 @@
|
|
| 1 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 2 |
-
import torch
|
| 3 |
-
import gradio as gr
|
| 4 |
-
|
| 5 |
-
model_name = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 6 |
-
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
-
model_name,
|
| 10 |
-
torch_dtype=torch.float16,
|
| 11 |
-
device_map="auto"
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
def chat(user_input):
|
| 15 |
-
# 系统提示,仍然用于引导模型的行为
|
| 16 |
-
messages = [
|
| 17 |
-
{"role": "system", "content": "你是一个中文助理,回答要简短清晰,不超过50字。"},
|
| 18 |
-
{"role": "user", "content": user_input}
|
| 19 |
-
]
|
| 20 |
-
|
| 21 |
-
# 生成的文本内容
|
| 22 |
-
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 23 |
-
inputs = tokenizer(text, return_tensors="pt").to(model.device)
|
| 24 |
-
|
| 25 |
-
# 生成模型输出
|
| 26 |
-
outputs = model.generate(
|
| 27 |
-
**inputs,
|
| 28 |
-
max_new_tokens=50,
|
| 29 |
-
do_sample=True,
|
| 30 |
-
temperature=0.7,
|
| 31 |
-
)
|
| 32 |
-
|
| 33 |
-
# 解码生成的文本,并去掉 "user" 和 "system" 的部分,只返回纯粹的回答
|
| 34 |
-
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 35 |
-
|
| 36 |
-
# 只保留模型的回答部分,可以根据需要进一步清理
|
| 37 |
-
if "user" in answer: # 如果模型回答中还包含“user”,去掉
|
| 38 |
-
answer = answer.split("user:")[-1]
|
| 39 |
-
|
| 40 |
-
return answer.strip()
|
| 41 |
-
|
| 42 |
-
iface = gr.Interface(
|
| 43 |
-
fn=chat,
|
| 44 |
-
inputs=gr.Textbox(lines=3, label="输入你的问题"),
|
| 45 |
-
outputs=gr.Textbox(lines=10, label="回答"),
|
| 46 |
-
title="Qwen 0.5B 中文问答机器人"
|
| 47 |
-
)
|
| 48 |
-
|
| 49 |
-
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|