Jithin James commited on
Commit
c757ce4
·
1 Parent(s): 06d3bc9

added a dropbox that selects elements correctly

Browse files
Files changed (1) hide show
  1. chatbot-demo.py +48 -17
chatbot-demo.py CHANGED
@@ -1,37 +1,68 @@
1
  import gradio as gr
2
 
3
 
4
- def get_model_response(user_input, history, user_chats):
5
- print(user_input, history, user_chats)
6
- response = f'Hello, you said "{user_input}"'
7
- history += [user_input, response]
8
- user_chats.append(user_input)
9
- responses = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)]
10
- return responses, history, user_chats
 
 
11
 
12
 
13
  def clear_chat():
14
- return [], []
15
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  with gr.Blocks() as demo:
18
- state = gr.State([])
19
  user_chats = gr.State(value=[])
 
 
20
 
21
- chat = gr.Chatbot()
22
  with gr.Row():
23
  with gr.Column(scale=5):
24
- txt = gr.Textbox(placeholder="Say Hi!", label="press Enter to submit")
25
- txt.submit(
 
 
26
  get_model_response,
27
- [txt, state, user_chats],
28
- [chat, state, user_chats],
 
 
 
 
 
 
29
  )
30
  with gr.Column(scale=1, min_width=250):
31
- choices = ["test"]
32
- drop_down = gr.Dropdown(label="Clear Till", choices=user_chats.value)
33
  clear_btn = gr.Button("Clear", label="Clear chat history")
34
- clear_btn.click(clear_chat, outputs=[state, chat])
 
 
 
 
 
 
 
 
 
35
 
36
  with gr.Accordion(label="Red-Teaming", open=True):
37
  gr.Markdown("*instructions on what to do if you break the model*")
 
1
  import gradio as gr
2
 
3
 
4
+ def get_model_response(chat_txt, user_chats, full_chat_history):
5
+ response = f'Hello, you said "{chat_txt}"'
6
+ full_chat_history = full_chat_history + [chat_txt, response]
7
+ user_chats.append(chat_txt)
8
+ responses = [
9
+ (full_chat_history[i], full_chat_history[i + 1])
10
+ for i in range(0, len(full_chat_history) - 1, 2)
11
+ ]
12
+ return responses, full_chat_history, user_chats
13
 
14
 
15
  def clear_chat():
16
+ return [], [], [], []
17
 
18
 
19
+ def clear_dropdown():
20
+ return gr.update(choices=[], value="")
21
+
22
+
23
+ def add_to_dropdown(txt, drop_down_options):
24
+ drop_down_options = drop_down_options + [txt]
25
+ return gr.update(choices=drop_down_options), drop_down_options
26
+
27
+
28
+ drop_down = gr.Dropdown(
29
+ label="Clear Till", choices=[], interactive=True, multiselect=False
30
+ )
31
  with gr.Blocks() as demo:
 
32
  user_chats = gr.State(value=[])
33
+ full_chat_history = gr.State(value=[])
34
+ clear_dropdown_options = gr.State(value=[])
35
 
36
+ chatbox = gr.Chatbot()
37
  with gr.Row():
38
  with gr.Column(scale=5):
39
+ user_chat_txt = gr.Textbox(
40
+ placeholder="Say Hi!", label="press Enter to submit"
41
+ )
42
+ user_chat_txt.submit(
43
  get_model_response,
44
+ inputs=[user_chat_txt, user_chats, full_chat_history],
45
+ outputs=[chatbox, full_chat_history, user_chats],
46
+ )
47
+ user_chat_txt.submit(lambda: gr.update(value=""), [], user_chat_txt)
48
+ user_chat_txt.submit(
49
+ add_to_dropdown,
50
+ inputs=[user_chat_txt, clear_dropdown_options],
51
+ outputs=[drop_down, clear_dropdown_options],
52
  )
53
  with gr.Column(scale=1, min_width=250):
54
+ drop_down.render()
 
55
  clear_btn = gr.Button("Clear", label="Clear chat history")
56
+ clear_btn.click(
57
+ clear_chat,
58
+ outputs=[
59
+ full_chat_history,
60
+ chatbox,
61
+ user_chats,
62
+ clear_dropdown_options,
63
+ ],
64
+ )
65
+ clear_btn.click(clear_dropdown, outputs=drop_down)
66
 
67
  with gr.Accordion(label="Red-Teaming", open=True):
68
  gr.Markdown("*instructions on what to do if you break the model*")