Spaces:
Running
Running
Better status and error handling
Browse files
app.py
CHANGED
@@ -34,6 +34,7 @@ class DynamicState:
|
|
34 |
self.in_cot = True
|
35 |
self.current_language = "en"
|
36 |
self.waiting_api = False # 新增等待状态标志
|
|
|
37 |
|
38 |
def control_button_handler(self):
|
39 |
original_state = self.should_stream
|
@@ -55,8 +56,10 @@ class DynamicState:
|
|
55 |
)
|
56 |
control_variant = "secondary" if self.should_stream else "primary"
|
57 |
# 处理等待状态显示
|
58 |
-
if self.waiting_api:
|
59 |
status_suffix = lang_data["waiting_api"]
|
|
|
|
|
60 |
else:
|
61 |
status_suffix = (
|
62 |
lang_data["completed"]
|
@@ -64,22 +67,24 @@ class DynamicState:
|
|
64 |
else lang_data["interrupted"]
|
65 |
)
|
66 |
editor_label = f"{lang_data['editor_label']} - {status_suffix}"
|
67 |
-
|
68 |
-
return (
|
69 |
gr.update(value=control_value, variant=control_variant),
|
70 |
-
gr.update(label=editor_label),
|
71 |
gr.update(interactive=not self.should_stream),
|
72 |
)
|
73 |
-
|
74 |
-
|
|
|
|
|
75 |
"""重置工作区状态"""
|
76 |
self.stream_completed = False
|
77 |
self.should_stream = False
|
78 |
self.in_cot = True
|
|
|
79 |
return self.ui_state_controller() + (
|
80 |
"",
|
81 |
"",
|
82 |
-
LANGUAGE_CONFIG["en"]["bot_default"],
|
83 |
)
|
84 |
|
85 |
|
@@ -114,6 +119,7 @@ class ConvoState:
|
|
114 |
self.current_language = "en"
|
115 |
self.convo = []
|
116 |
self.initialize_new_round()
|
|
|
117 |
|
118 |
def initialize_new_round(self):
|
119 |
self.current = {}
|
@@ -217,18 +223,18 @@ class ConvoState:
|
|
217 |
time.sleep(0.005)
|
218 |
|
219 |
except Exception as e:
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
|
233 |
finally:
|
234 |
dynamic_state.should_stream = False
|
@@ -240,7 +246,18 @@ class ConvoState:
|
|
240 |
else lang_data["interrupted"]
|
241 |
)
|
242 |
editor_label = f"{lang_data['editor_label']} - {final_status}"
|
243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
|
245 |
|
246 |
def update_interface_language(selected_lang, convo_state, dynamic_state):
|
@@ -291,6 +308,14 @@ with gr.Blocks(theme=theme, css_paths="styles.css") as demo:
|
|
291 |
convo_state = gr.State(ConvoState)
|
292 |
dynamic_state = gr.State(DynamicState)
|
293 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
294 |
with gr.Row(variant=""):
|
295 |
title_md = gr.Markdown(
|
296 |
f"## {LANGUAGE_CONFIG['en']['title']} \n GitHub: https://github.com/Intelligent-Internet/CoT-Lab-Demo",
|
@@ -332,14 +357,7 @@ with gr.Blocks(theme=theme, css_paths="styles.css") as demo:
|
|
332 |
chatbot = gr.Chatbot(
|
333 |
type="messages",
|
334 |
height=300,
|
335 |
-
value=
|
336 |
-
+ [
|
337 |
-
{
|
338 |
-
"role": "assistant",
|
339 |
-
"content": f"Running `{os.getenv('API_MODEL')}` @ {os.getenv('API_URL')} \n Proformance subjects to API provider situation",
|
340 |
-
"metadata": {"title": f"API INFO"},
|
341 |
-
}
|
342 |
-
],
|
343 |
group_consecutive_messages=False,
|
344 |
show_copy_all_button=True,
|
345 |
show_share_button=True,
|
@@ -409,10 +427,11 @@ with gr.Blocks(theme=theme, css_paths="styles.css") as demo:
|
|
409 |
)
|
410 |
|
411 |
next_turn_btn.click(
|
412 |
-
lambda d: d.reset_workspace(),
|
413 |
[dynamic_state],
|
414 |
stateful_ui + (thought_editor, prompt_input, chatbot),
|
415 |
concurrency_limit=None,
|
|
|
416 |
)
|
417 |
|
418 |
lang_selector.change(
|
|
|
34 |
self.in_cot = True
|
35 |
self.current_language = "en"
|
36 |
self.waiting_api = False # 新增等待状态标志
|
37 |
+
self.label_passthrough = False
|
38 |
|
39 |
def control_button_handler(self):
|
40 |
original_state = self.should_stream
|
|
|
56 |
)
|
57 |
control_variant = "secondary" if self.should_stream else "primary"
|
58 |
# 处理等待状态显示
|
59 |
+
if self.waiting_api and self.should_stream:
|
60 |
status_suffix = lang_data["waiting_api"]
|
61 |
+
elif self.waiting_api and not self.should_stream:
|
62 |
+
status_suffix = lang_data["api_retry"]
|
63 |
else:
|
64 |
status_suffix = (
|
65 |
lang_data["completed"]
|
|
|
67 |
else lang_data["interrupted"]
|
68 |
)
|
69 |
editor_label = f"{lang_data['editor_label']} - {status_suffix}"
|
70 |
+
output = (
|
|
|
71 |
gr.update(value=control_value, variant=control_variant),
|
72 |
+
gr.update() if self.label_passthrough else gr.update(label=editor_label),
|
73 |
gr.update(interactive=not self.should_stream),
|
74 |
)
|
75 |
+
self.label_passthrough = False
|
76 |
+
return output
|
77 |
+
|
78 |
+
def reset_workspace(self, chatbot_value=None):
|
79 |
"""重置工作区状态"""
|
80 |
self.stream_completed = False
|
81 |
self.should_stream = False
|
82 |
self.in_cot = True
|
83 |
+
self.waiting_api = False
|
84 |
return self.ui_state_controller() + (
|
85 |
"",
|
86 |
"",
|
87 |
+
LANGUAGE_CONFIG["en"]["bot_default"] if chatbot_value is None else chatbot_value,
|
88 |
)
|
89 |
|
90 |
|
|
|
119 |
self.current_language = "en"
|
120 |
self.convo = []
|
121 |
self.initialize_new_round()
|
122 |
+
self.is_error = False
|
123 |
|
124 |
def initialize_new_round(self):
|
125 |
self.current = {}
|
|
|
223 |
time.sleep(0.005)
|
224 |
|
225 |
except Exception as e:
|
226 |
+
if str(e) == "list index out of range":
|
227 |
+
dynamic_state.stream_completed = True
|
228 |
+
else:
|
229 |
+
if str(e) == "The read operation timed out":
|
230 |
+
error_msg = lang_data["api_interrupted"]
|
231 |
+
else:
|
232 |
+
error_msg = "❓ " + str(e)
|
233 |
+
# full_response += f"\n\n[{error_msg}: {str(e)}]"
|
234 |
+
editor_label_error = f"{lang_data['editor_label']} - {error_msg}"
|
235 |
+
self.is_error = True
|
236 |
+
dynamic_state.label_passthrough = True
|
237 |
+
|
238 |
|
239 |
finally:
|
240 |
dynamic_state.should_stream = False
|
|
|
246 |
else lang_data["interrupted"]
|
247 |
)
|
248 |
editor_label = f"{lang_data['editor_label']} - {final_status}"
|
249 |
+
if not self.is_error:
|
250 |
+
yield full_response, gr.update(label=editor_label), self.flatten_output()
|
251 |
+
else:
|
252 |
+
yield full_response, gr.update(label=editor_label_error), self.flatten_output() + [
|
253 |
+
{
|
254 |
+
"role": "assistant",
|
255 |
+
"content": error_msg,
|
256 |
+
"metadata": {"title": f"❌Error"},
|
257 |
+
}
|
258 |
+
]
|
259 |
+
self.is_error = False
|
260 |
+
|
261 |
|
262 |
|
263 |
def update_interface_language(selected_lang, convo_state, dynamic_state):
|
|
|
308 |
convo_state = gr.State(ConvoState)
|
309 |
dynamic_state = gr.State(DynamicState)
|
310 |
|
311 |
+
bot_default = LANGUAGE_CONFIG["en"]["bot_default"] + [
|
312 |
+
{
|
313 |
+
"role": "assistant",
|
314 |
+
"content": f"Running `{os.getenv('API_MODEL')}` @ {os.getenv('API_URL')} \n Proformance subjects to API provider situation",
|
315 |
+
"metadata": {"title": f"API INFO"},
|
316 |
+
}
|
317 |
+
]
|
318 |
+
|
319 |
with gr.Row(variant=""):
|
320 |
title_md = gr.Markdown(
|
321 |
f"## {LANGUAGE_CONFIG['en']['title']} \n GitHub: https://github.com/Intelligent-Internet/CoT-Lab-Demo",
|
|
|
357 |
chatbot = gr.Chatbot(
|
358 |
type="messages",
|
359 |
height=300,
|
360 |
+
value=bot_default,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
361 |
group_consecutive_messages=False,
|
362 |
show_copy_all_button=True,
|
363 |
show_share_button=True,
|
|
|
427 |
)
|
428 |
|
429 |
next_turn_btn.click(
|
430 |
+
lambda d: d.reset_workspace(bot_default),
|
431 |
[dynamic_state],
|
432 |
stateful_ui + (thought_editor, prompt_input, chatbot),
|
433 |
concurrency_limit=None,
|
434 |
+
show_progress=False
|
435 |
)
|
436 |
|
437 |
lang_selector.change(
|
lang.py
CHANGED
@@ -33,7 +33,10 @@ LANGUAGE_CONFIG = {
|
|
33 |
},
|
34 |
],
|
35 |
"editor_default": "AI thought will start with this, leave blank to think freely",
|
36 |
-
"waiting_api": "⏳ Waiting for
|
|
|
|
|
|
|
37 |
},
|
38 |
"zh": {
|
39 |
"title": "CoT-Lab: 人机协同思维实验室\n在一轮对话中跟随、学习、迭代思维链。克隆Space并使用自己的API KEY可以获得更好的体验。",
|
@@ -69,6 +72,8 @@ LANGUAGE_CONFIG = {
|
|
69 |
{"role": "assistant", "content": "**Shift+Enter** 可以暂停/继续AI生成"},
|
70 |
],
|
71 |
"editor_default": "AI思维会以此开头,留空即为默认思考",
|
72 |
-
"waiting_api": "⏳ 等待
|
|
|
|
|
73 |
},
|
74 |
}
|
|
|
33 |
},
|
34 |
],
|
35 |
"editor_default": "AI thought will start with this, leave blank to think freely",
|
36 |
+
"waiting_api": "⏳ Waiting for API response",
|
37 |
+
"api_retry": "🔁 API no response, hit Shift+Enter to try again.",
|
38 |
+
"api_interrupted": "⚠️ Pasued, API connection interrupted. Hit Shift+Enter to reconnect"
|
39 |
+
|
40 |
},
|
41 |
"zh": {
|
42 |
"title": "CoT-Lab: 人机协同思维实验室\n在一轮对话中跟随、学习、迭代思维链。克隆Space并使用自己的API KEY可以获得更好的体验。",
|
|
|
72 |
{"role": "assistant", "content": "**Shift+Enter** 可以暂停/继续AI生成"},
|
73 |
],
|
74 |
"editor_default": "AI思维会以此开头,留空即为默认思考",
|
75 |
+
"waiting_api": "⏳ 等待API响应",
|
76 |
+
"api_retry": "🔁 API无响应, Shift+Enter 重试一次试试?",
|
77 |
+
"api_interrupted": "⚠️ 暂停,API连接意外中断,Shift+Enter 可重连"
|
78 |
},
|
79 |
}
|