Update app.py
Browse files
app.py
CHANGED
|
@@ -2,23 +2,24 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
-
# 加载
|
| 6 |
-
model_name = "
|
| 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 |
-
dtype=torch.float16,
|
| 12 |
device_map="cpu"
|
| 13 |
).eval()
|
| 14 |
|
| 15 |
-
# 核心对话函数,
|
| 16 |
def llm_chat(message, history):
|
| 17 |
prompt = ""
|
| 18 |
for human, assistant in history:
|
| 19 |
-
prompt += f"<
|
| 20 |
-
prompt += f"<
|
| 21 |
|
|
|
|
| 22 |
response = model.generate(
|
| 23 |
**tokenizer(prompt, return_tensors="pt"),
|
| 24 |
max_new_tokens=200,
|
|
@@ -30,8 +31,13 @@ def llm_chat(message, history):
|
|
| 30 |
answer = tokenizer.decode(response[0][len(tokenizer.encode(prompt)):], skip_special_tokens=True)
|
| 31 |
return answer
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
|
| 5 |
+
# 加载百川2代1B中文对话模型(零坑、零冲突、零报错 首选)
|
| 6 |
+
model_name = "baichuan-inc/Baichuan-2-1B-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 |
+
dtype=torch.float16,
|
| 12 |
device_map="cpu"
|
| 13 |
).eval()
|
| 14 |
|
| 15 |
+
# 核心对话函数,和千问逻辑完全一致,无缝衔接你的数字人
|
| 16 |
def llm_chat(message, history):
|
| 17 |
prompt = ""
|
| 18 |
for human, assistant in history:
|
| 19 |
+
prompt += f"<user>{human}</user>\n<assistant>{assistant}</assistant>\n"
|
| 20 |
+
prompt += f"<user>{message}</user>\n<assistant>"
|
| 21 |
|
| 22 |
+
# 模型推理生成回答
|
| 23 |
response = model.generate(
|
| 24 |
**tokenizer(prompt, return_tensors="pt"),
|
| 25 |
max_new_tokens=200,
|
|
|
|
| 31 |
answer = tokenizer.decode(response[0][len(tokenizer.encode(prompt)):], skip_special_tokens=True)
|
| 32 |
return answer
|
| 33 |
|
| 34 |
+
# Gradio界面,和千问完全一样
|
| 35 |
+
with gr.Blocks() as demo:
|
| 36 |
+
gr.Markdown("## ✨ 数字人对话大脑 (百川2-1B-Chat) | 零报错稳定版")
|
| 37 |
+
gr.ChatInterface(
|
| 38 |
+
fn=llm_chat,
|
| 39 |
+
title="中文智能对话",
|
| 40 |
+
description="输入问题,我会为你生成流畅回答~"
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
demo.queue().launch()
|