Spaces:
Runtime error
Runtime error
Jithin James
commited on
Commit
·
c757ce4
1
Parent(s):
06d3bc9
added a dropbox that selects elements correctly
Browse files- 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(
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
|
|
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 |
-
|
22 |
with gr.Row():
|
23 |
with gr.Column(scale=5):
|
24 |
-
|
25 |
-
|
|
|
|
|
26 |
get_model_response,
|
27 |
-
[
|
28 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
)
|
30 |
with gr.Column(scale=1, min_width=250):
|
31 |
-
|
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(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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*")
|