Commit
·
061118b
1
Parent(s):
4ef1969
added clear button
Browse files- app.py +79 -12
- chatstate.py +1 -1
- img/retry.png +0 -0
- img/trash.png +0 -0
app.py
CHANGED
|
@@ -135,6 +135,7 @@ def instantiate_chatbot(sel, key):
|
|
| 135 |
key=key,
|
| 136 |
show_label=False,
|
| 137 |
show_share_button=False,
|
|
|
|
| 138 |
avatar_images=("img/usr.png", bot_icon_select(model_name)),
|
| 139 |
)
|
| 140 |
|
|
@@ -156,6 +157,26 @@ def instantiate_arrow_button(route, text_route):
|
|
| 156 |
return button
|
| 157 |
|
| 158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
def instantiate_text_box():
|
| 160 |
return gr.Textbox(label="Your message:", submit_btn=True, key="msg")
|
| 161 |
|
|
@@ -169,13 +190,39 @@ def instantiate_additional_settings():
|
|
| 169 |
return system_message
|
| 170 |
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
sel1 = instantiate_select_box(0, model_labels_list)
|
| 173 |
sel2 = instantiate_select_box(1, model_labels_list)
|
| 174 |
chatbot1 = instantiate_chatbot(sel1.value, "chat1")
|
| 175 |
chatbot2 = instantiate_chatbot(sel2.value, "chat2")
|
| 176 |
|
| 177 |
# to correctly align the left/right arrows
|
| 178 |
-
CSS = ".
|
| 179 |
|
| 180 |
with gr.Blocks(fill_width=True, title="Keras demo", css=CSS) as demo:
|
| 181 |
|
|
@@ -217,29 +264,49 @@ with gr.Blocks(fill_width=True, title="Keras demo", css=CSS) as demo:
|
|
| 217 |
if route == TextRoute.BOTH:
|
| 218 |
with gr.Row():
|
| 219 |
msg = instantiate_text_box()
|
| 220 |
-
with gr.Column(scale=0, min_width=
|
| 221 |
-
|
| 222 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
elif route == TextRoute.LEFT:
|
| 225 |
with gr.Row():
|
| 226 |
with gr.Column(scale=1):
|
| 227 |
msg = instantiate_text_box()
|
| 228 |
with gr.Column(scale=1):
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
elif route == TextRoute.RIGHT:
|
| 233 |
with gr.Row():
|
| 234 |
-
with gr.Column(scale=1, elem_classes="
|
| 235 |
-
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 237 |
with gr.Column(scale=1):
|
| 238 |
msg = instantiate_text_box()
|
|
|
|
|
|
|
| 239 |
|
| 240 |
-
|
| 241 |
-
clear = gr.ClearButton([msg, chatbot1, chatbot2])
|
| 242 |
-
system_message = instantiate_additional_settings()
|
| 243 |
|
| 244 |
# Route the submitted message to the left, right or both chatbots
|
| 245 |
if route == TextRoute.LEFT:
|
|
|
|
| 135 |
key=key,
|
| 136 |
show_label=False,
|
| 137 |
show_share_button=False,
|
| 138 |
+
show_copy_all_button=True,
|
| 139 |
avatar_images=("img/usr.png", bot_icon_select(model_name)),
|
| 140 |
)
|
| 141 |
|
|
|
|
| 157 |
return button
|
| 158 |
|
| 159 |
|
| 160 |
+
def instantiate_retry_button(route):
|
| 161 |
+
return gr.Button(
|
| 162 |
+
"",
|
| 163 |
+
size="sm",
|
| 164 |
+
scale=0,
|
| 165 |
+
min_width=40,
|
| 166 |
+
icon="img/retry.png",
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def instantiate_trash_button():
|
| 171 |
+
return gr.Button(
|
| 172 |
+
"",
|
| 173 |
+
size="sm",
|
| 174 |
+
scale=0,
|
| 175 |
+
min_width=40,
|
| 176 |
+
icon="img/trash.png",
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
|
| 180 |
def instantiate_text_box():
|
| 181 |
return gr.Textbox(label="Your message:", submit_btn=True, key="msg")
|
| 182 |
|
|
|
|
| 190 |
return system_message
|
| 191 |
|
| 192 |
|
| 193 |
+
def retry_fn(history):
|
| 194 |
+
if len(history) >= 2:
|
| 195 |
+
msg = history.pop(-1) # assistant message
|
| 196 |
+
msg = history.pop(-1) # user message
|
| 197 |
+
return msg["content"], history
|
| 198 |
+
else:
|
| 199 |
+
return gr.skip(), gr.skip()
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def retry_fn_both(history1, history2):
|
| 203 |
+
msg1, history1 = retry_fn(history1)
|
| 204 |
+
msg2, history2 = retry_fn(history2)
|
| 205 |
+
if isinstance(msg1, str) and isinstance(msg2, str):
|
| 206 |
+
if msg1 == msg2:
|
| 207 |
+
msg = msg1
|
| 208 |
+
else:
|
| 209 |
+
msg = msg1 + " / " + msg2
|
| 210 |
+
elif isinstance(msg1, str):
|
| 211 |
+
msg = msg1
|
| 212 |
+
elif isinstance(msg2, str):
|
| 213 |
+
msg = msg2
|
| 214 |
+
else:
|
| 215 |
+
msg = msg1
|
| 216 |
+
return msg, history1, history2
|
| 217 |
+
|
| 218 |
+
|
| 219 |
sel1 = instantiate_select_box(0, model_labels_list)
|
| 220 |
sel2 = instantiate_select_box(1, model_labels_list)
|
| 221 |
chatbot1 = instantiate_chatbot(sel1.value, "chat1")
|
| 222 |
chatbot2 = instantiate_chatbot(sel2.value, "chat2")
|
| 223 |
|
| 224 |
# to correctly align the left/right arrows
|
| 225 |
+
CSS = ".stick-to-the-right {align-items: end; justify-content: end}"
|
| 226 |
|
| 227 |
with gr.Blocks(fill_width=True, title="Keras demo", css=CSS) as demo:
|
| 228 |
|
|
|
|
| 264 |
if route == TextRoute.BOTH:
|
| 265 |
with gr.Row():
|
| 266 |
msg = instantiate_text_box()
|
| 267 |
+
with gr.Column(scale=0, min_width=100):
|
| 268 |
+
with gr.Row():
|
| 269 |
+
instantiate_arrow_button(TextRoute.LEFT, text_route)
|
| 270 |
+
retry = instantiate_retry_button(route)
|
| 271 |
+
with gr.Row():
|
| 272 |
+
instantiate_arrow_button(TextRoute.RIGHT, text_route)
|
| 273 |
+
trash = instantiate_trash_button()
|
| 274 |
+
retry.click(
|
| 275 |
+
retry_fn_both,
|
| 276 |
+
inputs=[chatbot1, chatbot2],
|
| 277 |
+
outputs=[msg, chatbot1, chatbot2],
|
| 278 |
+
)
|
| 279 |
+
trash.click(lambda: ("", [], []), outputs=[msg, chatbot1, chatbot2])
|
| 280 |
|
| 281 |
elif route == TextRoute.LEFT:
|
| 282 |
with gr.Row():
|
| 283 |
with gr.Column(scale=1):
|
| 284 |
msg = instantiate_text_box()
|
| 285 |
with gr.Column(scale=1):
|
| 286 |
+
with gr.Row():
|
| 287 |
+
instantiate_arrow_button(TextRoute.RIGHT, text_route)
|
| 288 |
+
retry = instantiate_retry_button(route)
|
| 289 |
+
with gr.Row():
|
| 290 |
+
instantiate_arrow_button(TextRoute.BOTH, text_route)
|
| 291 |
+
trash = instantiate_trash_button()
|
| 292 |
+
retry.click(retry_fn, inputs=[chatbot1], outputs=[msg, chatbot1])
|
| 293 |
+
trash.click(lambda: ("", []), outputs=[msg, chatbot1])
|
| 294 |
|
| 295 |
elif route == TextRoute.RIGHT:
|
| 296 |
with gr.Row():
|
| 297 |
+
with gr.Column(scale=1, elem_classes="stick-to-the-right"):
|
| 298 |
+
with gr.Row(elem_classes="stick-to-the-right"):
|
| 299 |
+
retry = instantiate_retry_button(route)
|
| 300 |
+
instantiate_arrow_button(TextRoute.LEFT, text_route)
|
| 301 |
+
with gr.Row(elem_classes="stick-to-the-right"):
|
| 302 |
+
trash = instantiate_trash_button()
|
| 303 |
+
instantiate_arrow_button(TextRoute.BOTH, text_route)
|
| 304 |
with gr.Column(scale=1):
|
| 305 |
msg = instantiate_text_box()
|
| 306 |
+
retry.click(retry_fn, inputs=[chatbot2], outputs=[msg, chatbot2])
|
| 307 |
+
trash.click(lambda: ("", []), outputs=[msg, chatbot2])
|
| 308 |
|
| 309 |
+
system_message = instantiate_additional_settings()
|
|
|
|
|
|
|
| 310 |
|
| 311 |
# Route the submitted message to the left, right or both chatbots
|
| 312 |
if route == TextRoute.LEFT:
|
chatstate.py
CHANGED
|
@@ -88,7 +88,7 @@ class ChatState:
|
|
| 88 |
self.add_to_history_as_user(message)
|
| 89 |
prompt = self.get_full_prompt()
|
| 90 |
response = self.model.generate(
|
| 91 |
-
prompt, max_length=
|
| 92 |
)
|
| 93 |
self.add_to_history_as_model(response)
|
| 94 |
return (message, response)
|
|
|
|
| 88 |
self.add_to_history_as_user(message)
|
| 89 |
prompt = self.get_full_prompt()
|
| 90 |
response = self.model.generate(
|
| 91 |
+
prompt, max_length=2048, strip_prompt=True
|
| 92 |
)
|
| 93 |
self.add_to_history_as_model(response)
|
| 94 |
return (message, response)
|
img/retry.png
ADDED
|
img/trash.png
ADDED
|