Spaces:
Running
Running
Feat: Persistent Editor State
Browse files
app.py
CHANGED
|
@@ -83,6 +83,7 @@ class DynamicState:
|
|
| 83 |
"",
|
| 84 |
"",
|
| 85 |
LANGUAGE_CONFIG["en"]["bot_default"],
|
|
|
|
| 86 |
)
|
| 87 |
|
| 88 |
|
|
@@ -303,8 +304,10 @@ def update_interface_language(selected_lang, convo_state, dynamic_state):
|
|
| 303 |
theme = gr.themes.Base(font="system-ui", primary_hue="stone")
|
| 304 |
|
| 305 |
with gr.Blocks(theme=theme, css_paths="styles.css") as demo:
|
|
|
|
| 306 |
convo_state = gr.State(ConvoState)
|
| 307 |
dynamic_state = gr.State(DynamicState)
|
|
|
|
| 308 |
|
| 309 |
bot_default = LANGUAGE_CONFIG["en"]["bot_default"] + [
|
| 310 |
{
|
|
@@ -380,6 +383,14 @@ with gr.Blocks(theme=theme, css_paths="styles.css") as demo:
|
|
| 380 |
)
|
| 381 |
|
| 382 |
intro_md = gr.Markdown(LANGUAGE_CONFIG["en"]["introduction"], visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
# 交互逻辑
|
| 385 |
stateful_ui = (control_button, thought_editor, next_turn_btn)
|
|
@@ -402,7 +413,8 @@ with gr.Blocks(theme=theme, css_paths="styles.css") as demo:
|
|
| 402 |
for response in convo_state.generate_ai_response(
|
| 403 |
prompt, content, dynamic_state
|
| 404 |
):
|
| 405 |
-
|
|
|
|
| 406 |
|
| 407 |
gr.on(
|
| 408 |
[control_button.click, prompt_input.submit, thought_editor.submit],
|
|
@@ -414,7 +426,7 @@ with gr.Blocks(theme=theme, css_paths="styles.css") as demo:
|
|
| 414 |
).then(
|
| 415 |
wrap_stream_generator,
|
| 416 |
[convo_state, dynamic_state, prompt_input, thought_editor],
|
| 417 |
-
[thought_editor, thought_editor, chatbot],
|
| 418 |
concurrency_limit=1000,
|
| 419 |
).then(
|
| 420 |
lambda d: d.ui_state_controller(),
|
|
@@ -427,7 +439,7 @@ with gr.Blocks(theme=theme, css_paths="styles.css") as demo:
|
|
| 427 |
next_turn_btn.click(
|
| 428 |
lambda d: d.reset_workspace(),
|
| 429 |
[dynamic_state],
|
| 430 |
-
stateful_ui + (thought_editor, prompt_input, chatbot),
|
| 431 |
concurrency_limit=None,
|
| 432 |
show_progress=False
|
| 433 |
)
|
|
|
|
| 83 |
"",
|
| 84 |
"",
|
| 85 |
LANGUAGE_CONFIG["en"]["bot_default"],
|
| 86 |
+
DEFAULT_PERSISTENT
|
| 87 |
)
|
| 88 |
|
| 89 |
|
|
|
|
| 304 |
theme = gr.themes.Base(font="system-ui", primary_hue="stone")
|
| 305 |
|
| 306 |
with gr.Blocks(theme=theme, css_paths="styles.css") as demo:
|
| 307 |
+
DEFAULT_PERSISTENT = {"prompt_input":"", "thought_editor":""}
|
| 308 |
convo_state = gr.State(ConvoState)
|
| 309 |
dynamic_state = gr.State(DynamicState)
|
| 310 |
+
persistent_state = gr.BrowserState(DEFAULT_PERSISTENT)
|
| 311 |
|
| 312 |
bot_default = LANGUAGE_CONFIG["en"]["bot_default"] + [
|
| 313 |
{
|
|
|
|
| 383 |
)
|
| 384 |
|
| 385 |
intro_md = gr.Markdown(LANGUAGE_CONFIG["en"]["introduction"], visible=False)
|
| 386 |
+
|
| 387 |
+
@demo.load(inputs=[persistent_state], outputs=[prompt_input, thought_editor])
|
| 388 |
+
def recover_persistent_state(persistant_state):
|
| 389 |
+
if persistant_state["prompt_input"] or persistant_state["thought_editor"]:
|
| 390 |
+
return persistant_state["prompt_input"], persistant_state["thought_editor"]
|
| 391 |
+
else:
|
| 392 |
+
return gr.update(), gr.update()
|
| 393 |
+
|
| 394 |
|
| 395 |
# 交互逻辑
|
| 396 |
stateful_ui = (control_button, thought_editor, next_turn_btn)
|
|
|
|
| 413 |
for response in convo_state.generate_ai_response(
|
| 414 |
prompt, content, dynamic_state
|
| 415 |
):
|
| 416 |
+
print(response)
|
| 417 |
+
yield response + ({"prompt_input": convo_state.current["user"], "thought_editor":convo_state.current["cot"]},)
|
| 418 |
|
| 419 |
gr.on(
|
| 420 |
[control_button.click, prompt_input.submit, thought_editor.submit],
|
|
|
|
| 426 |
).then(
|
| 427 |
wrap_stream_generator,
|
| 428 |
[convo_state, dynamic_state, prompt_input, thought_editor],
|
| 429 |
+
[thought_editor, thought_editor, chatbot, persistent_state],
|
| 430 |
concurrency_limit=1000,
|
| 431 |
).then(
|
| 432 |
lambda d: d.ui_state_controller(),
|
|
|
|
| 439 |
next_turn_btn.click(
|
| 440 |
lambda d: d.reset_workspace(),
|
| 441 |
[dynamic_state],
|
| 442 |
+
stateful_ui + (thought_editor, prompt_input, chatbot, persistent_state),
|
| 443 |
concurrency_limit=None,
|
| 444 |
show_progress=False
|
| 445 |
)
|