Spaces:
Sleeping
Sleeping
Commit ·
4f3536e
1
Parent(s): fc85342
Update app.py
Browse files
app.py
CHANGED
|
@@ -302,75 +302,63 @@ PREDEFINED = [
|
|
| 302 |
"How do I remove a package with apt?"
|
| 303 |
]
|
| 304 |
|
| 305 |
-
# ----------
|
| 306 |
-
def
|
| 307 |
-
"""Add (user, bot) to history and return updated history + clear input."""
|
| 308 |
if not message or not str(message).strip():
|
| 309 |
-
return history
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
|
|
|
| 314 |
return history, ""
|
| 315 |
|
| 316 |
-
def
|
| 317 |
if not message or not str(message).strip():
|
| 318 |
-
return history, ""
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
|
|
|
| 323 |
return history, ""
|
| 324 |
|
| 325 |
-
def clear_history():
|
| 326 |
-
return [], ""
|
| 327 |
-
|
| 328 |
-
# ---------- UI: two chatboxes side-by-side, dropdown populates input (not auto-send) ----------
|
| 329 |
with gr.Blocks() as demo:
|
| 330 |
gr.Markdown("## Ubuntu Chatbot Comparison — No Attention (left) vs Attention (right)")
|
| 331 |
-
gr.Markdown("
|
| 332 |
|
| 333 |
with gr.Row():
|
| 334 |
-
#
|
| 335 |
with gr.Column(scale=1):
|
| 336 |
gr.Markdown("### No Attention Model")
|
| 337 |
-
|
| 338 |
-
|
| 339 |
with gr.Row():
|
| 340 |
-
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
# Clear button
|
| 354 |
-
clear_no.click(lambda: ([], ""), inputs=None, outputs=[chat_no, inp_no])
|
| 355 |
-
|
| 356 |
-
# RIGHT: attn
|
| 357 |
with gr.Column(scale=1):
|
| 358 |
-
gr.Markdown("### Attention Model")
|
| 359 |
-
|
| 360 |
-
|
| 361 |
with gr.Row():
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
clear_att.click(lambda: ([], ""), inputs=None, outputs=[chat_att, inp_att])
|
| 374 |
|
| 375 |
if __name__ == "__main__":
|
| 376 |
-
demo.launch()
|
|
|
|
| 302 |
"How do I remove a package with apt?"
|
| 303 |
]
|
| 304 |
|
| 305 |
+
# ---------- Reply functions for custom Chatbot UI ----------
|
| 306 |
+
def reply_no_attn(message, history):
|
|
|
|
| 307 |
if not message or not str(message).strip():
|
| 308 |
+
return history + [{"role": "user", "content": message}], ""
|
| 309 |
+
bot_reply = generate_reply_no_attn(message)
|
| 310 |
+
history = history + [
|
| 311 |
+
{"role": "user", "content": message},
|
| 312 |
+
{"role": "assistant", "content": bot_reply}
|
| 313 |
+
]
|
| 314 |
return history, ""
|
| 315 |
|
| 316 |
+
def reply_attn(message, history):
|
| 317 |
if not message or not str(message).strip():
|
| 318 |
+
return history + [{"role": "user", "content": message}], ""
|
| 319 |
+
bot_reply = generate_reply_attn(message)
|
| 320 |
+
history = history + [
|
| 321 |
+
{"role": "user", "content": message},
|
| 322 |
+
{"role": "assistant", "content": bot_reply}
|
| 323 |
+
]
|
| 324 |
return history, ""
|
| 325 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
with gr.Blocks() as demo:
|
| 327 |
gr.Markdown("## Ubuntu Chatbot Comparison — No Attention (left) vs Attention (right)")
|
| 328 |
+
gr.Markdown("Use dropdown to quickly fill the chat input. Each chat keeps its own history. Clear button is built in per chat.")
|
| 329 |
|
| 330 |
with gr.Row():
|
| 331 |
+
# Left column: No Attention Model
|
| 332 |
with gr.Column(scale=1):
|
| 333 |
gr.Markdown("### No Attention Model")
|
| 334 |
+
chatbot_left = gr.Chatbot(label="No Attention Chatbot")
|
|
|
|
| 335 |
with gr.Row():
|
| 336 |
+
txt_left = gr.Textbox(show_label=False, placeholder="Type your message here...").style(container=False)
|
| 337 |
+
send_left = gr.Button("Send")
|
| 338 |
+
dd_left = gr.Dropdown(choices=PREDEFINED, label="Quick prompts (left)", interactive=True)
|
| 339 |
+
def set_input_left(selected):
|
| 340 |
+
return selected
|
| 341 |
+
dd_left.change(fn=set_input_left, inputs=dd_left, outputs=txt_left)
|
| 342 |
+
def clear_left():
|
| 343 |
+
return [], ""
|
| 344 |
+
send_left.click(fn=reply_no_attn, inputs=[txt_left, chatbot_left], outputs=[chatbot_left, txt_left])
|
| 345 |
+
chatbot_left.clear(fn=clear_left, inputs=None, outputs=[chatbot_left, txt_left])
|
| 346 |
+
|
| 347 |
+
# Right column: With Attention Model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
with gr.Column(scale=1):
|
| 349 |
+
gr.Markdown("### With Attention Model")
|
| 350 |
+
chatbot_right = gr.Chatbot(label="Attention Chatbot")
|
|
|
|
| 351 |
with gr.Row():
|
| 352 |
+
txt_right = gr.Textbox(show_label=False, placeholder="Type your message here...").style(container=False)
|
| 353 |
+
send_right = gr.Button("Send")
|
| 354 |
+
dd_right = gr.Dropdown(choices=PREDEFINED, label="Quick prompts (right)", interactive=True)
|
| 355 |
+
def set_input_right(selected):
|
| 356 |
+
return selected
|
| 357 |
+
dd_right.change(fn=set_input_right, inputs=dd_right, outputs=txt_right)
|
| 358 |
+
def clear_right():
|
| 359 |
+
return [], ""
|
| 360 |
+
send_right.click(fn=reply_attn, inputs=[txt_right, chatbot_right], outputs=[chatbot_right, txt_right])
|
| 361 |
+
chatbot_right.clear(fn=clear_right, inputs=None, outputs=[chatbot_right, txt_right])
|
|
|
|
|
|
|
| 362 |
|
| 363 |
if __name__ == "__main__":
|
| 364 |
+
demo.launch()
|