Update app.py
Browse files
app.py
CHANGED
@@ -28,9 +28,13 @@ def init_model():
|
|
28 |
return model, tokenizer
|
29 |
|
30 |
|
|
|
|
|
|
|
31 |
def clear_chat_history():
|
32 |
-
|
33 |
-
|
|
|
34 |
|
35 |
# def init_chat_history():
|
36 |
# with st.chat_message("assistant", avatar='🤖'):
|
@@ -64,26 +68,50 @@ def init_chat_history():
|
|
64 |
|
65 |
return st.session_state.messages
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
def main():
|
68 |
model, tokenizer = init_model()
|
69 |
messages = init_chat_history()
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
83 |
print(json.dumps(messages, ensure_ascii=False), flush=True)
|
84 |
|
85 |
-
|
86 |
-
|
|
|
87 |
|
88 |
if __name__ == "__main__":
|
89 |
main()
|
|
|
28 |
return model, tokenizer
|
29 |
|
30 |
|
31 |
+
# def clear_chat_history():
|
32 |
+
# del st.session_state.messages
|
33 |
+
# 清空聊天历史
|
34 |
def clear_chat_history():
|
35 |
+
# 把会话状态中的消息记录清空,并刷新页面
|
36 |
+
st.session_state.messages = []
|
37 |
+
st.experimental_rerun()
|
38 |
|
39 |
# def init_chat_history():
|
40 |
# with st.chat_message("assistant", avatar='🤖'):
|
|
|
68 |
|
69 |
return st.session_state.messages
|
70 |
|
71 |
+
# def main():
|
72 |
+
# model, tokenizer = init_model()
|
73 |
+
# messages = init_chat_history()
|
74 |
+
|
75 |
+
# if prompt := st.chat_input("Shift + Enter 换行, Enter 发送"):
|
76 |
+
# with st.chat_message("user", avatar='🧑💻'):
|
77 |
+
# st.markdown(prompt)
|
78 |
+
# messages.append({"role": "user", "content": prompt})
|
79 |
+
# print(f"[user] {prompt}", flush=True)
|
80 |
+
# with st.chat_message("assistant", avatar='🤖'):
|
81 |
+
# placeholder = st.empty()
|
82 |
+
# for response in model.chat(tokenizer, messages, stream=True):
|
83 |
+
# placeholder.markdown(response)
|
84 |
+
# if torch.backends.mps.is_available():
|
85 |
+
# torch.mps.empty_cache()
|
86 |
+
# messages.append({"role": "assistant", "content": response})
|
87 |
+
# print(json.dumps(messages, ensure_ascii=False), flush=True)
|
88 |
+
|
89 |
+
# st.button("清空对话", on_click=clear_chat_history)
|
90 |
+
# 主函数
|
91 |
def main():
|
92 |
model, tokenizer = init_model()
|
93 |
messages = init_chat_history()
|
94 |
|
95 |
+
# 在侧边栏或表单中创建一个输入框和一个提交按钮
|
96 |
+
with st.sidebar.form("chat_form"):
|
97 |
+
user_input = st.text_input("请输入您的问题")
|
98 |
+
submit_button = st.form_submit_button("发送")
|
99 |
+
|
100 |
+
# 如果用户点击了发送按钮,就把用户的输入添加到消息记录中,并显示出来
|
101 |
+
if submit_button:
|
102 |
+
messages.append({"role": "user", "content": user_input})
|
103 |
+
st.write(f"🧑💻: {user_input}")
|
104 |
+
print(f"[user] {user_input}", flush=True)
|
105 |
+
|
106 |
+
# 调用模型来生成回复,并添加到消息记录中,并显示出来
|
107 |
+
reply = model.chat(tokenizer, messages, stream=False)
|
108 |
+
messages.append({"role": "assistant", "content": reply})
|
109 |
+
st.write(f"🤖: {reply}")
|
110 |
print(json.dumps(messages, ensure_ascii=False), flush=True)
|
111 |
|
112 |
+
# 如果有清空对话的按钮,就绑定清空聊天历史的函数
|
113 |
+
if st.button("清空对话"):
|
114 |
+
clear_chat_history()
|
115 |
|
116 |
if __name__ == "__main__":
|
117 |
main()
|