Spaces:
Sleeping
Sleeping
修改部分注释
Browse files- crazy_functions/crazy_utils.py +15 -29
crazy_functions/crazy_utils.py
CHANGED
@@ -65,7 +65,6 @@ def request_gpt_model_in_new_thread_with_ui_alive(
|
|
65 |
from request_llm.bridge_chatgpt import predict_no_ui_long_connection
|
66 |
# 用户反馈
|
67 |
chatbot.append([inputs_show_user, ""])
|
68 |
-
msg = '正常'
|
69 |
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
70 |
executor = ThreadPoolExecutor(max_workers=16)
|
71 |
mutable = ["", time.time()]
|
@@ -73,6 +72,9 @@ def request_gpt_model_in_new_thread_with_ui_alive(
|
|
73 |
retry_op = retry_times_at_unknown_error
|
74 |
exceeded_cnt = 0
|
75 |
while True:
|
|
|
|
|
|
|
76 |
try:
|
77 |
# 【第一种情况】:顺利完成
|
78 |
result = predict_no_ui_long_connection(
|
@@ -109,6 +111,7 @@ def request_gpt_model_in_new_thread_with_ui_alive(
|
|
109 |
time.sleep(5)
|
110 |
return mutable[0] # 放弃
|
111 |
|
|
|
112 |
future = executor.submit(_req_gpt, inputs, history, sys_prompt)
|
113 |
while True:
|
114 |
# yield一次以刷新前端页面
|
@@ -169,17 +172,20 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
|
169 |
n_frag = len(inputs_array)
|
170 |
# 用户反馈
|
171 |
chatbot.append(["请开始多线程操作。", ""])
|
172 |
-
msg = '正常'
|
173 |
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
174 |
-
#
|
175 |
mutable = [["", time.time(), "等待中"] for _ in range(n_frag)]
|
176 |
|
|
|
177 |
def _req_gpt(index, inputs, history, sys_prompt):
|
178 |
gpt_say = ""
|
179 |
retry_op = retry_times_at_unknown_error
|
180 |
exceeded_cnt = 0
|
181 |
mutable[index][2] = "执行中"
|
182 |
while True:
|
|
|
|
|
|
|
183 |
try:
|
184 |
# 【第一种情况】:顺利完成
|
185 |
# time.sleep(10); raise RuntimeError("测试")
|
@@ -241,7 +247,6 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
|
241 |
break
|
242 |
# 更好的UI视觉效果
|
243 |
observe_win = []
|
244 |
-
# print([mutable[thread_index][2] for thread_index, _ in enumerate(worker_done)])
|
245 |
# 每个线程都要“喂狗”(看门狗)
|
246 |
for thread_index, _ in enumerate(worker_done):
|
247 |
mutable[thread_index][1] = time.time()
|
@@ -251,49 +256,30 @@ def request_gpt_model_multi_threads_with_very_awesome_ui_and_high_efficiency(
|
|
251 |
replace('\n', '').replace('```', '...').replace(
|
252 |
' ', '.').replace('<br/>', '.....').replace('$', '.')+"`... ]"
|
253 |
observe_win.append(print_something_really_funny)
|
|
|
254 |
stat_str = ''.join([f'`{mutable[thread_index][2]}`: {obs}\n\n'
|
255 |
if not done else f'`{mutable[thread_index][2]}`\n\n'
|
256 |
for thread_index, done, obs in zip(range(len(worker_done)), worker_done, observe_win)])
|
|
|
257 |
chatbot[-1] = [chatbot[-1][0], f'多线程操作已经开始,完成情况: \n\n{stat_str}' + ''.join(['.']*(cnt % 10+1))]
|
258 |
-
msg = "正常"
|
259 |
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
|
|
260 |
# 异步任务结束
|
261 |
gpt_response_collection = []
|
262 |
for inputs_show_user, f in zip(inputs_show_user_array, futures):
|
263 |
gpt_res = f.result()
|
264 |
gpt_response_collection.extend([inputs_show_user, gpt_res])
|
265 |
-
|
|
|
266 |
if show_user_at_complete:
|
267 |
for inputs_show_user, f in zip(inputs_show_user_array, futures):
|
268 |
gpt_res = f.result()
|
269 |
chatbot.append([inputs_show_user, gpt_res])
|
270 |
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
271 |
-
time.sleep(
|
272 |
return gpt_response_collection
|
273 |
|
274 |
|
275 |
-
def WithRetry(f):
|
276 |
-
"""
|
277 |
-
装饰器函数,用于自动重试。
|
278 |
-
"""
|
279 |
-
def decorated(retry, res_when_fail, *args, **kwargs):
|
280 |
-
assert retry >= 0
|
281 |
-
while True:
|
282 |
-
try:
|
283 |
-
res = yield from f(*args, **kwargs)
|
284 |
-
return res
|
285 |
-
except:
|
286 |
-
retry -= 1
|
287 |
-
if retry<0:
|
288 |
-
print("达到最大重试次数")
|
289 |
-
break
|
290 |
-
else:
|
291 |
-
print("重试中……")
|
292 |
-
continue
|
293 |
-
return res_when_fail
|
294 |
-
return decorated
|
295 |
-
|
296 |
-
|
297 |
def breakdown_txt_to_satisfy_token_limit(txt, get_token_fn, limit):
|
298 |
def cut(txt_tocut, must_break_at_empty_line): # 递归
|
299 |
if get_token_fn(txt_tocut) <= limit:
|
|
|
65 |
from request_llm.bridge_chatgpt import predict_no_ui_long_connection
|
66 |
# 用户反馈
|
67 |
chatbot.append([inputs_show_user, ""])
|
|
|
68 |
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
69 |
executor = ThreadPoolExecutor(max_workers=16)
|
70 |
mutable = ["", time.time()]
|
|
|
72 |
retry_op = retry_times_at_unknown_error
|
73 |
exceeded_cnt = 0
|
74 |
while True:
|
75 |
+
# watchdog error
|
76 |
+
if len(mutable) >= 2 and (time.time()-mutable[1]) > 5:
|
77 |
+
raise RuntimeError("检测到程序终止。")
|
78 |
try:
|
79 |
# 【第一种情况】:顺利完成
|
80 |
result = predict_no_ui_long_connection(
|
|
|
111 |
time.sleep(5)
|
112 |
return mutable[0] # 放弃
|
113 |
|
114 |
+
# 提交任务
|
115 |
future = executor.submit(_req_gpt, inputs, history, sys_prompt)
|
116 |
while True:
|
117 |
# yield一次以刷新前端页面
|
|
|
172 |
n_frag = len(inputs_array)
|
173 |
# 用户反馈
|
174 |
chatbot.append(["请开始多线程操作。", ""])
|
|
|
175 |
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
176 |
+
# 跨线程传递
|
177 |
mutable = [["", time.time(), "等待中"] for _ in range(n_frag)]
|
178 |
|
179 |
+
#
|
180 |
def _req_gpt(index, inputs, history, sys_prompt):
|
181 |
gpt_say = ""
|
182 |
retry_op = retry_times_at_unknown_error
|
183 |
exceeded_cnt = 0
|
184 |
mutable[index][2] = "执行中"
|
185 |
while True:
|
186 |
+
# watchdog error
|
187 |
+
if len(mutable[index]) >= 2 and (time.time()-mutable[index][1]) > 5:
|
188 |
+
raise RuntimeError("检测到程序终止。")
|
189 |
try:
|
190 |
# 【第一种情况】:顺利完成
|
191 |
# time.sleep(10); raise RuntimeError("测试")
|
|
|
247 |
break
|
248 |
# 更好的UI视觉效果
|
249 |
observe_win = []
|
|
|
250 |
# 每个线程都要“喂狗”(看门狗)
|
251 |
for thread_index, _ in enumerate(worker_done):
|
252 |
mutable[thread_index][1] = time.time()
|
|
|
256 |
replace('\n', '').replace('```', '...').replace(
|
257 |
' ', '.').replace('<br/>', '.....').replace('$', '.')+"`... ]"
|
258 |
observe_win.append(print_something_really_funny)
|
259 |
+
# 在前端打印些好玩的东西
|
260 |
stat_str = ''.join([f'`{mutable[thread_index][2]}`: {obs}\n\n'
|
261 |
if not done else f'`{mutable[thread_index][2]}`\n\n'
|
262 |
for thread_index, done, obs in zip(range(len(worker_done)), worker_done, observe_win)])
|
263 |
+
# 在前端打印些好玩的东西
|
264 |
chatbot[-1] = [chatbot[-1][0], f'多线程操作已经开始,完成情况: \n\n{stat_str}' + ''.join(['.']*(cnt % 10+1))]
|
|
|
265 |
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
266 |
+
|
267 |
# 异步任务结束
|
268 |
gpt_response_collection = []
|
269 |
for inputs_show_user, f in zip(inputs_show_user_array, futures):
|
270 |
gpt_res = f.result()
|
271 |
gpt_response_collection.extend([inputs_show_user, gpt_res])
|
272 |
+
|
273 |
+
# 是否在结束时,在界面上显示结果
|
274 |
if show_user_at_complete:
|
275 |
for inputs_show_user, f in zip(inputs_show_user_array, futures):
|
276 |
gpt_res = f.result()
|
277 |
chatbot.append([inputs_show_user, gpt_res])
|
278 |
yield from update_ui(chatbot=chatbot, history=[]) # 刷新界面
|
279 |
+
time.sleep(0.3)
|
280 |
return gpt_response_collection
|
281 |
|
282 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
def breakdown_txt_to_satisfy_token_limit(txt, get_token_fn, limit):
|
284 |
def cut(txt_tocut, must_break_at_empty_line): # 递归
|
285 |
if get_token_fn(txt_tocut) <= limit:
|