Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,29 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
|
|
| 4 |
|
| 5 |
-
# 加载千问1.8B中文聊天模型(CPU自动加载,量化优化
|
| 6 |
model_name = "Qwen/Qwen-1_8B-Chat"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
model_name,
|
| 10 |
trust_remote_code=True,
|
| 11 |
-
|
| 12 |
device_map="cpu"
|
| 13 |
).eval()
|
| 14 |
|
| 15 |
-
# 核心:对话函数,支持上下文历史对话(数字人必备
|
| 16 |
def llm_chat(message, history):
|
| 17 |
-
# 拼接历史对话+新问题
|
| 18 |
prompt = ""
|
| 19 |
for human, assistant in history:
|
| 20 |
prompt += f"<|im_start|>user\n{human}<|im_end|>\n<|im_start|>assistant\n{assistant}<|im_end|>\n"
|
| 21 |
prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 22 |
|
| 23 |
-
# 生成回答
|
| 24 |
response = model.generate(
|
| 25 |
**tokenizer(prompt, return_tensors="pt"),
|
| 26 |
-
max_new_tokens=200,
|
| 27 |
-
temperature=0.7,
|
| 28 |
top_p=0.95,
|
| 29 |
repetition_penalty=1.1,
|
| 30 |
do_sample=True
|
|
@@ -32,9 +31,8 @@ def llm_chat(message, history):
|
|
| 32 |
answer = tokenizer.decode(response[0][len(tokenizer.encode(prompt)):], skip_special_tokens=True)
|
| 33 |
return answer
|
| 34 |
|
| 35 |
-
# Gradio聊天界面,测试用
|
| 36 |
gr.ChatInterface(
|
| 37 |
llm_chat,
|
| 38 |
-
title="✅ 数字人对话大脑 (Qwen-1.8B-Chat)",
|
| 39 |
description="中文聊天模型 - CPU免费部署",
|
| 40 |
).queue().launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import transformers_stream_generator # ✅ 新增:解决流式生成库依赖问题
|
| 5 |
|
| 6 |
+
# 加载千问1.8B中文聊天模型(CPU自动加载,量化优化)
|
| 7 |
model_name = "Qwen/Qwen-1_8B-Chat"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
model_name,
|
| 11 |
trust_remote_code=True,
|
| 12 |
+
dtype=torch.float16, # ✅ 修改:torch_dtype → dtype 解决警告
|
| 13 |
device_map="cpu"
|
| 14 |
).eval()
|
| 15 |
|
| 16 |
+
# 核心:对话函数,支持上下文历史对话(数字人必备)
|
| 17 |
def llm_chat(message, history):
|
|
|
|
| 18 |
prompt = ""
|
| 19 |
for human, assistant in history:
|
| 20 |
prompt += f"<|im_start|>user\n{human}<|im_end|>\n<|im_start|>assistant\n{assistant}<|im_end|>\n"
|
| 21 |
prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 22 |
|
|
|
|
| 23 |
response = model.generate(
|
| 24 |
**tokenizer(prompt, return_tensors="pt"),
|
| 25 |
+
max_new_tokens=200,
|
| 26 |
+
temperature=0.7,
|
| 27 |
top_p=0.95,
|
| 28 |
repetition_penalty=1.1,
|
| 29 |
do_sample=True
|
|
|
|
| 31 |
answer = tokenizer.decode(response[0][len(tokenizer.encode(prompt)):], skip_special_tokens=True)
|
| 32 |
return answer
|
| 33 |
|
|
|
|
| 34 |
gr.ChatInterface(
|
| 35 |
llm_chat,
|
| 36 |
+
title="✅ 数字人对话大脑 (Qwen-1.8B-Chat) 零报错版",
|
| 37 |
description="中文聊天模型 - CPU免费部署",
|
| 38 |
).queue().launch()
|