Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,110 +1,117 @@
|
|
1 |
-
|
2 |
-
from transformers import AutoModel, AutoTokenizer
|
3 |
import gradio as gr
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
#
|
11 |
-
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
line = line.replace("*", "*")
|
53 |
-
line = line.replace("_", "_")
|
54 |
-
line = line.replace("-", "-")
|
55 |
-
line = line.replace(".", ".")
|
56 |
-
line = line.replace("!", "!")
|
57 |
-
line = line.replace("(", "(")
|
58 |
-
line = line.replace(")", ")")
|
59 |
-
line = line.replace("$", "$")
|
60 |
-
lines[i] = "<br>"+line
|
61 |
-
text = "".join(lines)
|
62 |
-
return text
|
63 |
-
|
64 |
-
|
65 |
-
def predict(input, chatbot, max_length, top_p, temperature, history, past_key_values):
|
66 |
-
chatbot.append((parse_text(input), ""))
|
67 |
-
for response, history, past_key_values in model.stream_chat(tokenizer, input, history, past_key_values=past_key_values,
|
68 |
-
return_past_key_values=True,
|
69 |
-
max_length=max_length, top_p=top_p,
|
70 |
-
temperature=temperature):
|
71 |
-
chatbot[-1] = (parse_text(input), parse_text(response))
|
72 |
-
|
73 |
-
yield chatbot, history, past_key_values
|
74 |
-
|
75 |
-
|
76 |
-
def reset_user_input():
|
77 |
-
return gr.update(value='')
|
78 |
-
|
79 |
-
|
80 |
-
def reset_state():
|
81 |
-
return [], [], None
|
82 |
|
83 |
|
84 |
with gr.Blocks() as demo:
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
|
|
2 |
import gradio as gr
|
3 |
+
from threading import Thread
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
5 |
+
device = "cuda" # the device to load the model onto
|
6 |
+
#device = "cpu" # the device to load the model onto
|
7 |
+
|
8 |
+
|
9 |
+
bot_avatar = "shuaikang/dl_logo_rect.png" # 聊天机器人头像位置
|
10 |
+
user_avatar = "shuaikang/user_avatar.jpg" # 用户头像位置
|
11 |
+
#model_path = "sethuiyer/Medichat-Llama3-8B" # 已下载的模型位置
|
12 |
+
#model_path = "johnsnowlabs/JSL-MedMX-7X"
|
13 |
+
model_path = "aaditya/Llama3-OpenBioLLM-8B"
|
14 |
+
|
15 |
+
# 存储全局的历史对话记录,Llama3支持系统prompt,所以这里默认设置!
|
16 |
+
llama3_chat_history = [
|
17 |
+
{"role": "system", "content": "You are a helpful assistant trained by MetaAI! But you are running with DataLearnerAI Code."}
|
18 |
+
]
|
19 |
+
|
20 |
+
# 初始化所有变量,用于载入模型
|
21 |
+
tokenizer = None
|
22 |
+
streamer = None
|
23 |
+
model = None
|
24 |
+
terminators = None
|
25 |
+
|
26 |
+
|
27 |
+
def init_model():
|
28 |
+
"""初始化模型,载入本地模型
|
29 |
+
"""
|
30 |
+
global tokenizer, model, streamer, terminators
|
31 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
32 |
+
model_path, local_files_only=True)
|
33 |
+
|
34 |
+
model = AutoModelForCausalLM.from_pretrained(
|
35 |
+
model_path,
|
36 |
+
torch_dtype=torch.float16,
|
37 |
+
device_map=device,
|
38 |
+
trust_remote_code=True
|
39 |
+
)
|
40 |
+
|
41 |
+
terminators = [
|
42 |
+
tokenizer.eos_token_id,
|
43 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
44 |
+
]
|
45 |
+
|
46 |
+
streamer = TextIteratorStreamer(
|
47 |
+
tokenizer,
|
48 |
+
skip_prompt=True,
|
49 |
+
skip_special_tokens=True
|
50 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
|
53 |
with gr.Blocks() as demo:
|
54 |
+
# step1: 载入模型
|
55 |
+
init_model()
|
56 |
+
|
57 |
+
# step2: 初始化gradio的chatbot应用,并添加按钮等信息
|
58 |
+
chatbot = gr.Chatbot(
|
59 |
+
height=900,
|
60 |
+
avatar_images=(user_avatar, bot_avatar)
|
61 |
+
)
|
62 |
+
msg = gr.Textbox()
|
63 |
+
clear = gr.ClearButton([msg, chatbot])
|
64 |
+
|
65 |
+
# 清楚历史记录
|
66 |
+
def clear_history():
|
67 |
+
global llama3_chat_history
|
68 |
+
llama3_chat_history = []
|
69 |
+
|
70 |
+
# 用于回复的方法
|
71 |
+
def respond(message, chat_history):
|
72 |
+
|
73 |
+
global llama3_chat_history, tokenizer, model, streamer
|
74 |
+
|
75 |
+
llama3_chat_history.append({"role": "user", "content": message})
|
76 |
+
|
77 |
+
# 使用Llama3自带的聊天模板,格式化对话记录
|
78 |
+
history_str = tokenizer.apply_chat_template(
|
79 |
+
llama3_chat_history,
|
80 |
+
tokenize=False,
|
81 |
+
add_generation_prompt=True
|
82 |
+
)
|
83 |
+
|
84 |
+
# tokenzier
|
85 |
+
inputs = tokenizer(history_str, return_tensors='pt').to(device)
|
86 |
+
|
87 |
+
chat_history.append([message, ""])
|
88 |
+
|
89 |
+
generation_kwargs = dict(
|
90 |
+
**inputs,
|
91 |
+
streamer=streamer,
|
92 |
+
max_new_tokens=4096,
|
93 |
+
num_beams=1,
|
94 |
+
do_sample=True,
|
95 |
+
top_p=0.8,
|
96 |
+
temperature=0.3,
|
97 |
+
eos_token_id=terminators
|
98 |
+
)
|
99 |
+
|
100 |
+
# 启动线程,用以监控流失输出结果
|
101 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
102 |
+
thread.start()
|
103 |
+
|
104 |
+
for new_text in streamer:
|
105 |
+
chat_history[-1][1] += new_text
|
106 |
+
yield "", chat_history
|
107 |
+
|
108 |
+
llama3_chat_history.append(
|
109 |
+
{"role": "assistant", "content": chat_history[-1][1]}
|
110 |
+
)
|
111 |
+
|
112 |
+
# 点击清楚按钮,触发历史记录清楚
|
113 |
+
clear.click(clear_history)
|
114 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
115 |
+
|
116 |
+
if __name__ == "__main__":
|
117 |
+
demo.queue(concurrency_count=1, max_size=1).launch(server_name="0.0.0.0", server_port=7860)
|