Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -62,6 +62,30 @@ def fill_up_placeholders(txt):
|
|
| 62 |
"" if len(placeholders) >= 1 else txt
|
| 63 |
)
|
| 64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
async def chat_stream(
|
| 66 |
idx, local_data, instruction_txtbox, chat_state,
|
| 67 |
global_context, res_temp, res_topk, res_rpen, res_mnts, res_sample, ctx_num_lconv
|
|
@@ -243,13 +267,39 @@ with gr.Blocks(css=MODEL_SELECTION_CSS, theme='gradio/soft') as demo:
|
|
| 243 |
with gr.Row():
|
| 244 |
ctx_num_lconv = gr.Slider(2, 10, 3, step=1, label="number of recent talks to keep", interactive=True)
|
| 245 |
|
| 246 |
-
instruction_txtbox.submit(
|
| 247 |
chat_stream,
|
| 248 |
[idx, local_data, instruction_txtbox, chat_state,
|
| 249 |
global_context, res_temp, res_topk, res_rpen, res_mnts, res_sample, ctx_num_lconv],
|
| 250 |
[context_inspector, chatbot, local_data]
|
| 251 |
)
|
| 252 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
for btn in channel_btns:
|
| 254 |
btn.click(
|
| 255 |
set_chatbot,
|
|
@@ -266,6 +316,16 @@ with gr.Blocks(css=MODEL_SELECTION_CSS, theme='gradio/soft') as demo:
|
|
| 266 |
[btn],
|
| 267 |
[instruction_txtbox, example_block]
|
| 268 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
placeholder_txt1.change(
|
| 271 |
inputs=[template_txt, placeholder_txt1, placeholder_txt2, placeholder_txt3],
|
|
@@ -308,7 +368,7 @@ with gr.Blocks(css=MODEL_SELECTION_CSS, theme='gradio/soft') as demo:
|
|
| 308 |
outputs=[instruction_txtbox, placeholder_txt1, placeholder_txt2, placeholder_txt3],
|
| 309 |
fn=get_final_template
|
| 310 |
)
|
| 311 |
-
|
| 312 |
demo.load(
|
| 313 |
None,
|
| 314 |
inputs=None,
|
|
|
|
| 62 |
"" if len(placeholders) >= 1 else txt
|
| 63 |
)
|
| 64 |
|
| 65 |
+
def rollback_last(idx, ld, state):
|
| 66 |
+
res = [state["ppmanager_type"].from_json(json.dumps(ppm_str)) for ppm_str in ld]
|
| 67 |
+
last_user_message = res[idx].pingpongs[-1].ping
|
| 68 |
+
res[idx].pingpongs = res[idx].pingpongs[:-1]
|
| 69 |
+
|
| 70 |
+
return (
|
| 71 |
+
last_user_message,
|
| 72 |
+
res[idx].build_uis(),
|
| 73 |
+
str(res),
|
| 74 |
+
gr.update(interactive=False)
|
| 75 |
+
)
|
| 76 |
+
|
| 77 |
+
def reset_chat(idx, ld, state):
|
| 78 |
+
res = [state["ppmanager_type"].from_json(json.dumps(ppm_str)) for ppm_str in ld]
|
| 79 |
+
res[idx].pingpongs = []
|
| 80 |
+
|
| 81 |
+
return (
|
| 82 |
+
"",
|
| 83 |
+
[],
|
| 84 |
+
str(res),
|
| 85 |
+
gr.update(visible=True),
|
| 86 |
+
gr.update(interactive=False),
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
async def chat_stream(
|
| 90 |
idx, local_data, instruction_txtbox, chat_state,
|
| 91 |
global_context, res_temp, res_topk, res_rpen, res_mnts, res_sample, ctx_num_lconv
|
|
|
|
| 267 |
with gr.Row():
|
| 268 |
ctx_num_lconv = gr.Slider(2, 10, 3, step=1, label="number of recent talks to keep", interactive=True)
|
| 269 |
|
| 270 |
+
send_event = instruction_txtbox.submit(
|
| 271 |
chat_stream,
|
| 272 |
[idx, local_data, instruction_txtbox, chat_state,
|
| 273 |
global_context, res_temp, res_topk, res_rpen, res_mnts, res_sample, ctx_num_lconv],
|
| 274 |
[context_inspector, chatbot, local_data]
|
| 275 |
)
|
| 276 |
|
| 277 |
+
regen_event1 = regenerate.click(
|
| 278 |
+
rollback_last,
|
| 279 |
+
[idx, local_data, chat_state],
|
| 280 |
+
[instruction_txtbox, chatbot, local_data, regenerate]
|
| 281 |
+
)
|
| 282 |
+
regen_event2 = regen_event1.then(
|
| 283 |
+
chat_stream,
|
| 284 |
+
[idx, local_data, instruction_txtbox, chat_state,
|
| 285 |
+
global_context, res_temp, res_topk, res_rpen, res_mnts, res_sample, ctx_num_lconv],
|
| 286 |
+
[context_inspector, chatbot, local_data]
|
| 287 |
+
)
|
| 288 |
+
regen_event3 = regen_event2.then(
|
| 289 |
+
lambda: gr.update(interactive=True),
|
| 290 |
+
None,
|
| 291 |
+
regenerate
|
| 292 |
+
)
|
| 293 |
+
regen_event4 = regen_event3.then(
|
| 294 |
+
None, local_data, None,
|
| 295 |
+
_js="(v)=>{ setStorage('local_data',v) }"
|
| 296 |
+
)
|
| 297 |
+
|
| 298 |
+
stop.click(
|
| 299 |
+
None, None, None,
|
| 300 |
+
cancels=[send_event, regen_event1, regen_event2, regen_event3, regen_event4]
|
| 301 |
+
)
|
| 302 |
+
|
| 303 |
for btn in channel_btns:
|
| 304 |
btn.click(
|
| 305 |
set_chatbot,
|
|
|
|
| 316 |
[btn],
|
| 317 |
[instruction_txtbox, example_block]
|
| 318 |
)
|
| 319 |
+
|
| 320 |
+
clean.click(
|
| 321 |
+
reset_chat,
|
| 322 |
+
[idx, local_data, chat_state],
|
| 323 |
+
[instruction_txtbox, chatbot, local_data, example_block, regenerate]
|
| 324 |
+
).then(
|
| 325 |
+
None, local_data, None,
|
| 326 |
+
_js="(v)=>{ setStorage('local_data',v) }"
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
|
| 330 |
placeholder_txt1.change(
|
| 331 |
inputs=[template_txt, placeholder_txt1, placeholder_txt2, placeholder_txt3],
|
|
|
|
| 368 |
outputs=[instruction_txtbox, placeholder_txt1, placeholder_txt2, placeholder_txt3],
|
| 369 |
fn=get_final_template
|
| 370 |
)
|
| 371 |
+
|
| 372 |
demo.load(
|
| 373 |
None,
|
| 374 |
inputs=None,
|