Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +56 -0
- logo.jpg +0 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# chat_bot.py
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
from streamlit_chat import message
|
5 |
+
import zhipuai
|
6 |
+
|
7 |
+
zhipuai.api_key = "169859478749e13a43a2c1b3d3f0bce0.Bnq1AThjEwwmK1k8"
|
8 |
+
def getAnswerByZhiPu(t):
|
9 |
+
response = zhipuai.model_api.sse_invoke(
|
10 |
+
model="chatglm_pro",
|
11 |
+
prompt=[
|
12 |
+
{"role": "user", "content": t}
|
13 |
+
],
|
14 |
+
temperature=0.95,
|
15 |
+
top_p=0.7,
|
16 |
+
incremental=True
|
17 |
+
)
|
18 |
+
result = ''
|
19 |
+
for event in response.events():
|
20 |
+
result += event.data
|
21 |
+
return result
|
22 |
+
|
23 |
+
|
24 |
+
if 'prompts' not in st.session_state:
|
25 |
+
st.session_state['prompts'] = []
|
26 |
+
|
27 |
+
if 'generated' not in st.session_state:
|
28 |
+
st.session_state['generated'] = []
|
29 |
+
|
30 |
+
if 'past' not in st.session_state:
|
31 |
+
st.session_state['past'] = []
|
32 |
+
|
33 |
+
def end_click():
|
34 |
+
st.session_state['prompts'] = []
|
35 |
+
st.session_state['past'] = []
|
36 |
+
st.session_state['generated'] = []
|
37 |
+
st.session_state['user'] = ""
|
38 |
+
def chat_click():
|
39 |
+
if st.session_state['user'] != '':
|
40 |
+
chat_input = st.session_state['user']
|
41 |
+
output = getAnswerByZhiPu(chat_input)
|
42 |
+
st.session_state['past'].append(chat_input)
|
43 |
+
st.session_state['generated'].append(output)
|
44 |
+
st.session_state['prompts'].append(output)
|
45 |
+
st.session_state['user'] = ""
|
46 |
+
st.image("./logo.jpg", use_column_width='auto')
|
47 |
+
st.title("珠宝贝专属聊天机器人")
|
48 |
+
|
49 |
+
user_input = st.text_input("输入:", key="user")
|
50 |
+
chat_button = st.button("发送", on_click=chat_click)
|
51 |
+
end_button = st.button("新聊天", on_click=end_click)
|
52 |
+
|
53 |
+
if st.session_state['generated']:
|
54 |
+
for i in range(0, len(st.session_state['generated']), 1):
|
55 |
+
message(st.session_state['past'][i], is_user=True)
|
56 |
+
message(st.session_state['generated'][i], key=str(i))
|
logo.jpg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
streamlit_chat
|
2 |
+
zhipuai
|