Fasty commited on
Commit
a74acfe
·
1 Parent(s): fcdc0b6

merged changes with solin's code

Browse files
Files changed (1) hide show
  1. app.py +38 -8
app.py CHANGED
@@ -449,7 +449,18 @@ def update_styles(theme, font_size):
449
  """
450
  return style
451
 
452
- # Gradio build stuff actually starts here
 
 
 
 
 
 
 
 
 
 
 
453
  with gr.Blocks(css=None) as demo:
454
  dynamic_styles = gr.HTML(value=update_styles("Dark", "Medium"))
455
 
@@ -457,7 +468,7 @@ with gr.Blocks(css=None) as demo:
457
  with gr.TabItem("💬 Chatbot"):
458
  gr.HTML('<div id="header_section"></div>')
459
  header_desc = gr.HTML('<p id="header_description">' + translations["English"]["header_description"] + '</p>')
460
- chatbot = gr.Chatbot(label="", height=500, show_label=False, elem_id="chatbot")
461
 
462
  with gr.Row():
463
  user_input = gr.Textbox(
@@ -466,17 +477,20 @@ with gr.Blocks(css=None) as demo:
466
  placeholder=translations["English"]["user_input_placeholder"],
467
  interactive=True,
468
  elem_id="user_input",
469
- scale=15
470
  )
471
  send_button = gr.Button(
472
  value=translations["English"]["send_button"],
473
  elem_id="send_button",
474
- scale=1
475
  )
476
- conversation_state = gr.State(value=[])
477
 
 
 
 
478
  gr.HTML("<br>")
479
- gr.HTML("<h3 style='text-align: center; color: {text_color};'>Need inspiration? Try asking about one of these topics:</h3>")
480
 
481
  example_questions = gr.Radio(
482
  choices=[
@@ -493,9 +507,19 @@ with gr.Blocks(css=None) as demo:
493
  )
494
 
495
  example_questions.change(
496
- fn=lambda x: chat_with_bot_stream(x, conversation_state.value),
497
  inputs=example_questions,
 
 
 
 
 
 
498
  outputs=[chatbot, conversation_state]
 
 
 
 
499
  )
500
 
501
  send_button.click(
@@ -507,6 +531,12 @@ with gr.Blocks(css=None) as demo:
507
  inputs=None,
508
  outputs=user_input
509
  )
 
 
 
 
 
 
510
 
511
  # SETTINGS PAGE
512
  with gr.TabItem(translations["English"]["settings_tab_item"]):
@@ -547,4 +577,4 @@ with gr.Blocks(css=None) as demo:
547
  ]
548
  )
549
 
550
- demo.launch()
 
449
  """
450
  return style
451
 
452
+ def load_chat_history(chat_history):
453
+ if chat_history is None:
454
+ return []
455
+ return [
456
+ (
457
+ msg["content"] if msg["role"] == "user" else None,
458
+ msg["content"] if msg["role"] == "assistant" else None
459
+ )
460
+ for msg in chat_history
461
+ ]
462
+
463
+ # Gradio stuff
464
  with gr.Blocks(css=None) as demo:
465
  dynamic_styles = gr.HTML(value=update_styles("Dark", "Medium"))
466
 
 
468
  with gr.TabItem("💬 Chatbot"):
469
  gr.HTML('<div id="header_section"></div>')
470
  header_desc = gr.HTML('<p id="header_description">' + translations["English"]["header_description"] + '</p>')
471
+ chatbot = gr.Chatbot(label="", height=500, show_label=False, elem_id="chatbot", show_copy_button=False, show_copy_all_button=False, show_share_button=False)
472
 
473
  with gr.Row():
474
  user_input = gr.Textbox(
 
477
  placeholder=translations["English"]["user_input_placeholder"],
478
  interactive=True,
479
  elem_id="user_input",
480
+ scale=12
481
  )
482
  send_button = gr.Button(
483
  value=translations["English"]["send_button"],
484
  elem_id="send_button",
485
+ scale=2
486
  )
487
+ clear_button = gr.Button("🗑️ Clear", elem_id="send_button", scale=2)
488
 
489
+ conversation_state = gr.BrowserState([])
490
+ demo.load(load_chat_history, inputs=[conversation_state], outputs=[chatbot])
491
+
492
  gr.HTML("<br>")
493
+ gr.HTML("<h3 style='text-align: center;'>Need inspiration? Try asking about one of these topics:</h3>")
494
 
495
  example_questions = gr.Radio(
496
  choices=[
 
507
  )
508
 
509
  example_questions.change(
510
+ fn=lambda x: chat_with_bot_stream(x, conversation_state.value[:1]),
511
  inputs=example_questions,
512
+ outputs=[chatbot, conversation_state]
513
+ )
514
+
515
+ user_input.submit(
516
+ fn=chat_with_bot_stream,
517
+ inputs=[user_input, conversation_state],
518
  outputs=[chatbot, conversation_state]
519
+ ).then(
520
+ fn=lambda: "",
521
+ inputs=None,
522
+ outputs=user_input
523
  )
524
 
525
  send_button.click(
 
531
  inputs=None,
532
  outputs=user_input
533
  )
534
+
535
+ clear_button.click(
536
+ fn=lambda: ([], []),
537
+ inputs=None,
538
+ outputs=[chatbot, conversation_state]
539
+ )
540
 
541
  # SETTINGS PAGE
542
  with gr.TabItem(translations["English"]["settings_tab_item"]):
 
577
  ]
578
  )
579
 
580
+ demo.launch()