-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_bot.py
102 lines (81 loc) · 3.41 KB
/
chat_bot.py
1
2
3
4
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
# 请输入以下命令启动前端
# export PATH="$PATH:/home/nvidia/.local/bin"
# streamlit run chat_bot.py --server.port 5000
# 外网端口:http://36.150.110.74:9519/
from ui_methods import *
# 设置日志级别为 ERROR,这样只会显示错误信息,忽略警告
logging.getLogger("streamlit").setLevel(logging.ERROR)
allow_multi_modal = True
# 初始化会话状态
if 'counter' not in st.session_state:
st.session_state.counter = 0
if st.session_state.counter == 0:
recovery_list = []
with open('data.pkl', 'wb') as f:
pickle.dump(recovery_list, f)
st.session_state.counter = 1
else:
try:
with open('data.pkl', 'rb') as f:
recovery_list = pickle.load(f) # {'role':'user/assistant','type':'string/pic',content:''}
except FileNotFoundError:
st.error('未找到 data.pkl 文件')
recovery_list = []
# 初始化主界面
st.title("卡拉米のDEMO")
st.write("我的第一个专属机器人,它可以回答你的问题,也可以和你聊天。")
logo_display()
background_set()
hide_streamlit_header_footer()
display_existing_messages(recovery_list)
# 初始化侧边栏
st.sidebar.header("其他功能")
st.sidebar.subheader("向量数据库建库")
if st.sidebar.button('执行', help='向量数据库建库'):
# **在下面添加向量库建库函数**
message_text = "向量数据库建库执行成功!"
with st.chat_message('assistant'):
message_placeholder = st.empty()
full_response = ""
# 模拟流式输出,逐字添加字符
for char in message_text:
full_response += char
# 更新占位符的内容
message_placeholder.markdown(full_response)
# 添加延迟,控制输出速度
time.sleep(0.05)
recovery_list.append({'role': 'assistant', 'type': "string", 'content': full_response})
st.sidebar.subheader("传图")
uploaded_file = st.sidebar.file_uploader("向机器人传图", type=['png', 'jpg', 'jpeg', 'txt'])
# 上传图片功能
if st.sidebar.button('上传', help='提交图片'):
image = Image.open(uploaded_file)
add_user_message_to_session(image, recovery_list)
if allow_multi_modal:
# save_path = '../aws_hackathon_demo/wei-test-data/Bright16 Air/'
save_path = './resource/pic/store/'
save_name = generate_unique_time_string_with_uuid() + '.png'
image.save(save_path + save_name)
# 此处添加返回信息
message_text = "收到图片啦!"
with st.chat_message('assistant'):
# response = pic_recognize_response(image)
message_placeholder = st.empty()
full_response = ""
# 模拟流式输出,逐字添加字符
for char in message_text:
full_response += char
# 更新占位符的内容
message_placeholder.markdown(full_response)
# 添加延迟,控制输出速度
time.sleep(0.05)
recovery_list.append({'role': 'assistant', 'type': "string", 'content': full_response})
# 输入框
query = st.chat_input("你可以问我任何你想问的问题")
if query:
add_user_message_to_session(query, recovery_list) # 存储问句
response = generate_assistant_response(query, recovery_list) # 回答生成
# 存储对话
with open('data.pkl', 'wb') as f:
pickle.dump(recovery_list, f)