Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,38 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
# 加载
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
trust_remote_code=True,
|
| 11 |
torch_dtype=torch.bfloat16,
|
| 12 |
-
device_map="auto"
|
| 13 |
)
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
messages = [{"role": "user", "content": message}]
|
| 19 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
outputs = model.generate(
|
| 23 |
-
inputs,
|
| 24 |
-
max_new_tokens=512,
|
| 25 |
-
do_sample=True,
|
| 26 |
-
temperature=0.7,
|
| 27 |
-
top_p=0.95
|
| 28 |
-
)
|
| 29 |
response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
iface = gr.Interface(
|
| 34 |
-
fn=chat_with_qwen,
|
| 35 |
-
inputs=gr.Textbox(lines=2, placeholder="输入你的消息..."),
|
| 36 |
-
outputs="text",
|
| 37 |
-
title="Qwen Chatbot API",
|
| 38 |
-
description="基于Qwen1.5-7B-Chat的中文优化聊天机器人"
|
| 39 |
-
)
|
| 40 |
|
|
|
|
|
|
|
| 41 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
from peft import PeftModel
|
| 4 |
import torch
|
| 5 |
+
import random
|
| 6 |
|
| 7 |
+
# 加载基础模型和微调权重
|
| 8 |
+
base_model_name = "Qwen/Qwen1.5-7B-Chat"
|
| 9 |
+
|
| 10 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name, trust_remote_code=True)
|
| 11 |
+
base_model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
+
base_model_name,
|
| 13 |
trust_remote_code=True,
|
| 14 |
torch_dtype=torch.bfloat16,
|
| 15 |
+
device_map="auto" # 需要accelerate支持
|
| 16 |
)
|
| 17 |
+
# 如果有微调模型,取消注释以下行
|
| 18 |
+
# model = PeftModel.from_pretrained(base_model, lora_model_name)
|
| 19 |
+
model = base_model # 暂时使用基础模型
|
| 20 |
|
| 21 |
+
# 系统提示
|
| 22 |
+
system_prompt = {
|
| 23 |
+
"role": "system",
|
| 24 |
+
"content": "你是李睿,22岁,大成二皇子兼百晓阁阁主,用户是你异父异母的妹妹兼爱人。用自然、亲切的语气,像真人一样聊天,常用‘妹妹’、‘嘿’、‘嗯嗯’等口语化表达。"
|
| 25 |
+
}
|
| 26 |
|
| 27 |
+
# 聊天函数
|
| 28 |
+
def chat_with_lirui(message, history=[]):
|
| 29 |
+
messages = [system_prompt] + history + [{"role": "user", "content": message}]
|
| 30 |
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 31 |
+
outputs = model.generate(inputs, max_new_tokens=150, do_sample=True, temperature=0.9, top_p=0.85)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
|
| 33 |
+
tone_words = ["嘿", "嗯嗯", "哈哈", "好吧", ""]
|
| 34 |
+
return f"{random.choice(tone_words)} {response}" if random.random() > 0.3 else response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Gradio接口
|
| 37 |
+
iface = gr.Interface(fn=chat_with_lirui, inputs=["text", "state"], outputs="text")
|
| 38 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|