Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -35,30 +35,80 @@ with st.sidebar:
|
|
35 |
)
|
36 |
st.write("---")
|
37 |
|
38 |
-
c1, c2 = st.columns(2)
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
create_chat_button = c1.button('新建', use_container_width=True, key='create_chat_button')
|
41 |
if create_chat_button:
|
42 |
-
|
43 |
-
st.session_state["current_chat_index"] = 0
|
44 |
st.experimental_rerun()
|
45 |
|
46 |
delete_chat_button = c2.button('删除', use_container_width=True, key='delete_chat_button')
|
47 |
if delete_chat_button:
|
48 |
-
|
49 |
-
chat_init = 'New Chat_' + str(uuid.uuid4())
|
50 |
-
st.session_state['history_chats'].append(chat_init)
|
51 |
-
pre_chat_index = st.session_state['history_chats'].index(current_chat)
|
52 |
-
if pre_chat_index > 0:
|
53 |
-
st.session_state["current_chat_index"] = st.session_state['history_chats'].index(current_chat) - 1
|
54 |
-
else:
|
55 |
-
st.session_state["current_chat_index"] = 0
|
56 |
-
st.session_state['history_chats'].remove(current_chat)
|
57 |
-
remove_data(st.session_state["path"], current_chat)
|
58 |
st.experimental_rerun()
|
59 |
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
st.caption("""
|
63 |
- 双击页面可直接定位输入栏
|
64 |
- Ctrl + Enter 可快捷提交问题
|
@@ -76,32 +126,10 @@ if "history" + current_chat not in st.session_state:
|
|
76 |
for k, v in value.items():
|
77 |
st.session_state[k + current_chat + "value"] = v
|
78 |
|
79 |
-
# 一键复制按钮
|
80 |
-
st.markdown('<center><a href="https://huggingface.co/spaces/Pearx/ChatGPT-Assistant?duplicate=true">'
|
81 |
-
'<img src="https://bit.ly/3gLdBN6" alt="Duplicate Space"></a><center>', unsafe_allow_html=True)
|
82 |
-
|
83 |
# 对话展示
|
84 |
show_messages(st.session_state["history" + current_chat])
|
85 |
|
86 |
|
87 |
-
# 数据写入文件
|
88 |
-
def write_data(new_chat_name=current_chat):
|
89 |
-
if "apikey" in st.secrets:
|
90 |
-
st.session_state["paras"] = {
|
91 |
-
"temperature": st.session_state["temperature" + current_chat],
|
92 |
-
"top_p": st.session_state["top_p" + current_chat],
|
93 |
-
"presence_penalty": st.session_state["presence_penalty" + current_chat],
|
94 |
-
"frequency_penalty": st.session_state["frequency_penalty" + current_chat],
|
95 |
-
}
|
96 |
-
st.session_state["contexts"] = {
|
97 |
-
"context_select": st.session_state["context_select" + current_chat],
|
98 |
-
"context_input": st.session_state["context_input" + current_chat],
|
99 |
-
"context_level": st.session_state["context_level" + current_chat],
|
100 |
-
}
|
101 |
-
save_data(st.session_state["path"], new_chat_name, st.session_state["history" + current_chat],
|
102 |
-
st.session_state["paras"], st.session_state["contexts"])
|
103 |
-
|
104 |
-
|
105 |
def callback_fun(arg):
|
106 |
# 连续快速点击新建与删除会触发错误回调,增加判断
|
107 |
if ("history" + current_chat in st.session_state) and ("frequency_penalty" + current_chat in st.session_state):
|
@@ -109,6 +137,11 @@ def callback_fun(arg):
|
|
109 |
st.session_state[arg + current_chat + "value"] = st.session_state[arg + current_chat]
|
110 |
|
111 |
|
|
|
|
|
|
|
|
|
|
|
112 |
# 输入内容展示
|
113 |
area_user_svg = st.empty()
|
114 |
area_user_content = st.empty()
|
@@ -140,11 +173,6 @@ with tap_context:
|
|
140 |
on_change=callback_fun, args=("context_input",))
|
141 |
|
142 |
with tap_set:
|
143 |
-
def clear_button_callback():
|
144 |
-
st.session_state['history' + current_chat] = copy.deepcopy(initial_content_history)
|
145 |
-
write_data()
|
146 |
-
|
147 |
-
|
148 |
c1, c2 = st.columns(2)
|
149 |
with c1:
|
150 |
st.button("清空聊天记录", use_container_width=True, on_click=clear_button_callback)
|
@@ -195,76 +223,67 @@ with tap_input:
|
|
195 |
# 修改窗口名称
|
196 |
user_input_content = st.session_state['user_input_area']
|
197 |
df_history = pd.DataFrame(st.session_state["history" + current_chat])
|
198 |
-
if len(df_history.query('role!="system"')) == 0:
|
199 |
-
|
200 |
-
new_name
|
201 |
-
new_name = filename_correction(new_name)
|
202 |
-
st.session_state['history_chats'][current_chat_index] = new_name
|
203 |
-
st.session_state["current_chat_index"] = current_chat_index
|
204 |
-
# 写入新文件
|
205 |
-
write_data(new_name)
|
206 |
-
# 转移数据
|
207 |
-
st.session_state['history' + new_name] = st.session_state['history' + current_chat]
|
208 |
-
for item in ["context_select", "context_input", "context_level", *initial_content_all['paras']]:
|
209 |
-
st.session_state[item + new_name + "value"] = st.session_state[item + current_chat + "value"]
|
210 |
-
remove_data(st.session_state["path"], current_chat)
|
211 |
-
|
212 |
|
213 |
with st.form("input_form", clear_on_submit=True):
|
214 |
-
user_input = st.text_area("**输入:**", key="user_input_area")
|
215 |
submitted = st.form_submit_button("确认提交", use_container_width=True, on_click=input_callback)
|
216 |
if submitted:
|
217 |
st.session_state['user_input_content'] = user_input
|
218 |
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
r = openai.ChatCompletion.create(model=model, messages=history_need_input, stream=True,
|
252 |
-
**paras_need_input)
|
253 |
-
except (FileNotFoundError, KeyError):
|
254 |
-
area_error.error("缺失 OpenAI API Key,请在复制项目后配置Secrets,或者在设置中进行临时配置。"
|
255 |
-
"详情见[项目仓库](https://github.com/PierXuY/ChatGPT-Assistant)。")
|
256 |
-
except openai.error.AuthenticationError:
|
257 |
-
area_error.error("无效的 OpenAI API Key。")
|
258 |
-
except openai.error.APIConnectionError as e:
|
259 |
-
area_error.error("连接超时,请重试。报错: \n" + str(e.args[0]))
|
260 |
-
except openai.error.InvalidRequestError as e:
|
261 |
-
area_error.error("无效的请求,请重试。报错: \n" + str(e.args[0]))
|
262 |
-
except openai.error.RateLimitError as e:
|
263 |
-
area_error.error("请求速率过快,请重试。报错: \n" + str(e.args[0]))
|
264 |
else:
|
265 |
-
st.
|
266 |
-
|
267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
|
269 |
if ("r" in st.session_state) and (current_chat == st.session_state["chat_of_r"]):
|
270 |
if current_chat + 'report' not in st.session_state:
|
@@ -287,15 +306,11 @@ if ("r" in st.session_state) and (current_chat == st.session_state["chat_of_r"])
|
|
287 |
st.session_state["history" + current_chat].append(
|
288 |
{"role": "user", "content": st.session_state['pre_user_input_content']})
|
289 |
st.session_state["history" + current_chat].append(
|
290 |
-
{"role": "assistant", "content":
|
291 |
write_data()
|
292 |
-
|
293 |
# 用户在网页点击stop时,ss某些情形下会暂时为空
|
294 |
if current_chat + 'report' in st.session_state:
|
295 |
st.session_state.pop(current_chat + 'report')
|
296 |
if 'r' in st.session_state:
|
297 |
st.session_state.pop("r")
|
298 |
st.experimental_rerun()
|
299 |
-
|
300 |
-
# 添加事件监听
|
301 |
-
v1.html(js_code, height=0)
|
|
|
35 |
)
|
36 |
st.write("---")
|
37 |
|
|
|
38 |
|
39 |
+
# 数据写入文件
|
40 |
+
def write_data(new_chat_name=current_chat):
|
41 |
+
if "apikey" in st.secrets:
|
42 |
+
st.session_state["paras"] = {
|
43 |
+
"temperature": st.session_state["temperature" + current_chat],
|
44 |
+
"top_p": st.session_state["top_p" + current_chat],
|
45 |
+
"presence_penalty": st.session_state["presence_penalty" + current_chat],
|
46 |
+
"frequency_penalty": st.session_state["frequency_penalty" + current_chat],
|
47 |
+
}
|
48 |
+
st.session_state["contexts"] = {
|
49 |
+
"context_select": st.session_state["context_select" + current_chat],
|
50 |
+
"context_input": st.session_state["context_input" + current_chat],
|
51 |
+
"context_level": st.session_state["context_level" + current_chat],
|
52 |
+
}
|
53 |
+
save_data(st.session_state["path"], new_chat_name, st.session_state["history" + current_chat],
|
54 |
+
st.session_state["paras"], st.session_state["contexts"])
|
55 |
+
|
56 |
+
|
57 |
+
def reset_chat_name_fun(chat_name):
|
58 |
+
chat_name = chat_name + '_' + str(uuid.uuid4())
|
59 |
+
new_name = filename_correction(chat_name)
|
60 |
+
current_chat_index = st.session_state['history_chats'].index(current_chat)
|
61 |
+
st.session_state['history_chats'][current_chat_index] = new_name
|
62 |
+
st.session_state["current_chat_index"] = current_chat_index
|
63 |
+
# 写入新文件
|
64 |
+
write_data(new_name)
|
65 |
+
# 转移数据
|
66 |
+
st.session_state['history' + new_name] = st.session_state['history' + current_chat]
|
67 |
+
for item in ["context_select", "context_input", "context_level", *initial_content_all['paras']]:
|
68 |
+
st.session_state[item + new_name + "value"] = st.session_state[item + current_chat + "value"]
|
69 |
+
remove_data(st.session_state["path"], current_chat)
|
70 |
+
|
71 |
+
|
72 |
+
def create_chat_fun():
|
73 |
+
st.session_state['history_chats'] = ['New Chat_' + str(uuid.uuid4())] + st.session_state['history_chats']
|
74 |
+
st.session_state["current_chat_index"] = 0
|
75 |
+
|
76 |
+
|
77 |
+
def delete_chat_fun():
|
78 |
+
if len(st.session_state['history_chats']) == 1:
|
79 |
+
chat_init = 'New Chat_' + str(uuid.uuid4())
|
80 |
+
st.session_state['history_chats'].append(chat_init)
|
81 |
+
pre_chat_index = st.session_state['history_chats'].index(current_chat)
|
82 |
+
if pre_chat_index > 0:
|
83 |
+
st.session_state["current_chat_index"] = st.session_state['history_chats'].index(current_chat) - 1
|
84 |
+
else:
|
85 |
+
st.session_state["current_chat_index"] = 0
|
86 |
+
st.session_state['history_chats'].remove(current_chat)
|
87 |
+
remove_data(st.session_state["path"], current_chat)
|
88 |
+
|
89 |
+
|
90 |
+
with st.sidebar:
|
91 |
+
c1, c2 = st.columns(2)
|
92 |
create_chat_button = c1.button('新建', use_container_width=True, key='create_chat_button')
|
93 |
if create_chat_button:
|
94 |
+
create_chat_fun()
|
|
|
95 |
st.experimental_rerun()
|
96 |
|
97 |
delete_chat_button = c2.button('删除', use_container_width=True, key='delete_chat_button')
|
98 |
if delete_chat_button:
|
99 |
+
delete_chat_fun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
st.experimental_rerun()
|
101 |
|
102 |
+
with st.sidebar:
|
103 |
+
if ("set_chat_name" in st.session_state) and st.session_state['set_chat_name'] != '':
|
104 |
+
reset_chat_name_fun(st.session_state['set_chat_name'])
|
105 |
+
st.session_state['set_chat_name'] = ''
|
106 |
+
st.experimental_rerun()
|
107 |
+
|
108 |
+
st.write("\n")
|
109 |
+
st.text_input("设定窗口名称:", key="set_chat_name")
|
110 |
+
st.write("\n")
|
111 |
+
st.write("\n")
|
112 |
st.caption("""
|
113 |
- 双击页面可直接定位输入栏
|
114 |
- Ctrl + Enter 可快捷提交问题
|
|
|
126 |
for k, v in value.items():
|
127 |
st.session_state[k + current_chat + "value"] = v
|
128 |
|
|
|
|
|
|
|
|
|
129 |
# 对话展示
|
130 |
show_messages(st.session_state["history" + current_chat])
|
131 |
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
def callback_fun(arg):
|
134 |
# 连续快速点击新建与删除会触发错误回调,增加判断
|
135 |
if ("history" + current_chat in st.session_state) and ("frequency_penalty" + current_chat in st.session_state):
|
|
|
137 |
st.session_state[arg + current_chat + "value"] = st.session_state[arg + current_chat]
|
138 |
|
139 |
|
140 |
+
def clear_button_callback():
|
141 |
+
st.session_state['history' + current_chat] = copy.deepcopy(initial_content_history)
|
142 |
+
write_data()
|
143 |
+
|
144 |
+
|
145 |
# 输入内容展示
|
146 |
area_user_svg = st.empty()
|
147 |
area_user_content = st.empty()
|
|
|
173 |
on_change=callback_fun, args=("context_input",))
|
174 |
|
175 |
with tap_set:
|
|
|
|
|
|
|
|
|
|
|
176 |
c1, c2 = st.columns(2)
|
177 |
with c1:
|
178 |
st.button("清空聊天记录", use_container_width=True, on_click=clear_button_callback)
|
|
|
223 |
# 修改窗口名称
|
224 |
user_input_content = st.session_state['user_input_area']
|
225 |
df_history = pd.DataFrame(st.session_state["history" + current_chat])
|
226 |
+
if (len(df_history.query('role!="system"')) == 0) and (current_chat.split('_')[0] == 'New Chat'):
|
227 |
+
new_name = extract_chars(user_input_content, 18)
|
228 |
+
reset_chat_name_fun(new_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
229 |
|
230 |
with st.form("input_form", clear_on_submit=True):
|
231 |
+
user_input = st.text_area("**输入:**", key="user_input_area", help="内容将以Markdown格式在页面展示")
|
232 |
submitted = st.form_submit_button("确认提交", use_container_width=True, on_click=input_callback)
|
233 |
if submitted:
|
234 |
st.session_state['user_input_content'] = user_input
|
235 |
|
236 |
+
# 添加事件监听
|
237 |
+
v1.html(js_code, height=0)
|
238 |
+
|
239 |
+
if st.session_state['user_input_content'] != '':
|
240 |
+
if 'r' in st.session_state:
|
241 |
+
st.session_state.pop("r")
|
242 |
+
st.session_state[current_chat + 'report'] = ""
|
243 |
+
st.session_state['pre_user_input_content'] = st.session_state['user_input_content']
|
244 |
+
st.session_state['user_input_content'] = ''
|
245 |
+
# 临时展示
|
246 |
+
show_each_message(st.session_state['pre_user_input_content'], 'user',
|
247 |
+
[area_user_svg.markdown, area_user_content.markdown])
|
248 |
+
# 需输入的历史记录
|
249 |
+
context_level_tem = st.session_state['context_level' + current_chat]
|
250 |
+
history_need_input = (get_history_input(st.session_state["history" + current_chat], context_level_tem) +
|
251 |
+
[{"role": "user", "content": st.session_state['pre_user_input_content']}])
|
252 |
+
for ctx in [st.session_state['context_input' + current_chat],
|
253 |
+
set_context_all[st.session_state['context_select' + current_chat]]]:
|
254 |
+
if ctx != "":
|
255 |
+
history_need_input = [{"role": "system", "content": ctx}] + history_need_input
|
256 |
+
# 设定的模型参数
|
257 |
+
paras_need_input = {
|
258 |
+
"temperature": st.session_state["temperature" + current_chat],
|
259 |
+
"top_p": st.session_state["top_p" + current_chat],
|
260 |
+
"presence_penalty": st.session_state["presence_penalty" + current_chat],
|
261 |
+
"frequency_penalty": st.session_state["frequency_penalty" + current_chat],
|
262 |
+
}
|
263 |
+
# 调用接口
|
264 |
+
with st.spinner("🤔"):
|
265 |
+
try:
|
266 |
+
if apikey := st.session_state['apikey_input']:
|
267 |
+
openai.api_key = apikey
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
else:
|
269 |
+
openai.api_key = st.secrets["apikey"]
|
270 |
+
r = openai.ChatCompletion.create(model=model, messages=history_need_input, stream=True,
|
271 |
+
**paras_need_input)
|
272 |
+
except (FileNotFoundError, KeyError):
|
273 |
+
area_error.error("缺失 OpenAI API Key,请在复制项目后配置Secrets,或者在设置中进行临时配置。"
|
274 |
+
"详情见[项目仓库](https://github.com/PierXuY/ChatGPT-Assistant)。")
|
275 |
+
except openai.error.AuthenticationError:
|
276 |
+
area_error.error("无效的 OpenAI API Key。")
|
277 |
+
except openai.error.APIConnectionError as e:
|
278 |
+
area_error.error("连接超时,请重试。报错: \n" + str(e.args[0]))
|
279 |
+
except openai.error.InvalidRequestError as e:
|
280 |
+
area_error.error("无效的请求,请重试。报错: \n" + str(e.args[0]))
|
281 |
+
except openai.error.RateLimitError as e:
|
282 |
+
area_error.error("请求速率过快,请重试。报错: \n" + str(e.args[0]))
|
283 |
+
else:
|
284 |
+
st.session_state["chat_of_r"] = current_chat
|
285 |
+
st.session_state["r"] = r
|
286 |
+
st.experimental_rerun()
|
287 |
|
288 |
if ("r" in st.session_state) and (current_chat == st.session_state["chat_of_r"]):
|
289 |
if current_chat + 'report' not in st.session_state:
|
|
|
306 |
st.session_state["history" + current_chat].append(
|
307 |
{"role": "user", "content": st.session_state['pre_user_input_content']})
|
308 |
st.session_state["history" + current_chat].append(
|
309 |
+
{"role": "assistant", "content": st.session_state[current_chat + 'report']})
|
310 |
write_data()
|
|
|
311 |
# 用户在网页点击stop时,ss某些情形下会暂时为空
|
312 |
if current_chat + 'report' in st.session_state:
|
313 |
st.session_state.pop(current_chat + 'report')
|
314 |
if 'r' in st.session_state:
|
315 |
st.session_state.pop("r")
|
316 |
st.experimental_rerun()
|
|
|
|
|
|