amaresh8053 commited on
Commit
4f3536e
·
1 Parent(s): fc85342

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -55
app.py CHANGED
@@ -302,75 +302,63 @@ PREDEFINED = [
302
  "How do I remove a package with apt?"
303
  ]
304
 
305
- # ---------- Handlers ----------
306
- def send_no_attn(message, history):
307
- """Add (user, bot) to history and return updated history + clear input."""
308
  if not message or not str(message).strip():
309
- return history, "" # no change, clear input
310
- reply = generate_reply_no_attn(message)
311
- if history is None:
312
- history = []
313
- history = history + [(message, reply)]
 
314
  return history, ""
315
 
316
- def send_attn(message, history):
317
  if not message or not str(message).strip():
318
- return history, ""
319
- reply = generate_reply_attn(message)
320
- if history is None:
321
- history = []
322
- history = history + [(message, reply)]
 
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("Choose a quick prompt (populates input) or type your own. Press Enter or click Send. Clear clears history.")
332
 
333
  with gr.Row():
334
- # LEFT: no-attn
335
  with gr.Column(scale=1):
336
  gr.Markdown("### No Attention Model")
337
- chat_no = gr.Chatbot(label="No-Attn Chat", height=420)
338
-
339
  with gr.Row():
340
- dd_no = gr.Dropdown(PREDEFINED, label="Quick prompts", interactive=True)
341
- clear_no = gr.Button("Clear Chat")
342
-
343
- inp_no = gr.Textbox(placeholder="Type here for no-attn model...", lines=1)
344
- send_no = gr.Button("Send")
345
-
346
- # Dropdown populates the textbox (not auto-send)
347
- dd_no.select(lambda x: gr.update(value=x), inputs=[dd_no], outputs=[inp_no])
348
-
349
- # Enter or Send -> call send_no_attn
350
- inp_no.submit(send_no_attn, inputs=[inp_no, chat_no], outputs=[chat_no, inp_no])
351
- send_no.click(send_no_attn, inputs=[inp_no, chat_no], outputs=[chat_no, inp_no])
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
- chat_att = gr.Chatbot(label="Attn Chat", height=420)
360
-
361
  with gr.Row():
362
- dd_att = gr.Dropdown(PREDEFINED, label="Quick prompts", interactive=True)
363
- clear_att = gr.Button("Clear Chat")
364
-
365
- inp_att = gr.Textbox(placeholder="Type here for attention model...", lines=1)
366
- send_att = gr.Button("Send")
367
-
368
- dd_att.select(lambda x: gr.update(value=x), inputs=[dd_att], outputs=[inp_att])
369
-
370
- inp_att.submit(send_attn, inputs=[inp_att, chat_att], outputs=[chat_att, inp_att])
371
- send_att.click(send_attn, inputs=[inp_att, chat_att], outputs=[chat_att, inp_att])
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()