added sidebar and beter chat management
Browse files
app.css
CHANGED
|
@@ -8,20 +8,11 @@
|
|
| 8 |
max-height: 1500px;
|
| 9 |
}
|
| 10 |
|
| 11 |
-
#sidebar {
|
| 12 |
-
height: 100%;
|
| 13 |
-
background-color: var(--block-background-fill);
|
| 14 |
-
padding: 12px;
|
| 15 |
-
border-right: 1px solid var(--border-color-primary);
|
| 16 |
-
overflow-y: auto;
|
| 17 |
-
}
|
| 18 |
-
|
| 19 |
#chat-column {
|
| 20 |
height: 100%;
|
| 21 |
display: flex;
|
| 22 |
flex-direction: column;
|
| 23 |
padding: 1px;
|
| 24 |
-
min-width: 0;
|
| 25 |
overflow: hidden;
|
| 26 |
}
|
| 27 |
|
|
@@ -33,4 +24,53 @@
|
|
| 33 |
|
| 34 |
.icon-button-wrapper {
|
| 35 |
display: hidden;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
}
|
|
|
|
| 8 |
max-height: 1500px;
|
| 9 |
}
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
#chat-column {
|
| 12 |
height: 100%;
|
| 13 |
display: flex;
|
| 14 |
flex-direction: column;
|
| 15 |
padding: 1px;
|
|
|
|
| 16 |
overflow: hidden;
|
| 17 |
}
|
| 18 |
|
|
|
|
| 24 |
|
| 25 |
.icon-button-wrapper {
|
| 26 |
display: hidden;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
#conversations-radio input[type="radio"] {
|
| 30 |
+
opacity: 0;
|
| 31 |
+
position: absolute;
|
| 32 |
+
width: 0;
|
| 33 |
+
height: 0;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
label.selected {
|
| 37 |
+
background: var(--checkbox-label-background-fill-hover) !important;
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
label span {
|
| 41 |
+
margin: 0;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
#conversations-radio > div:last-child {
|
| 45 |
+
display: grid;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
#send-btn, #stop-btn {
|
| 49 |
+
font-size: 0;
|
| 50 |
+
height: 100%;
|
| 51 |
+
min-width: 40px;
|
| 52 |
+
}
|
| 53 |
+
#send-btn::after {
|
| 54 |
+
content: "";
|
| 55 |
+
display: inline-block;
|
| 56 |
+
width: 20px;
|
| 57 |
+
height: 20px;
|
| 58 |
+
background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cline x1='22' y1='2' x2='11' y2='13'/%3E%3Cpolygon points='22 2 15 22 11 13 2 9 22 2'/%3E%3C/svg%3E") no-repeat center;
|
| 59 |
+
background-size: contain;
|
| 60 |
+
}
|
| 61 |
+
#stop-btn::after {
|
| 62 |
+
content: "";
|
| 63 |
+
display: inline-block;
|
| 64 |
+
width: 20px;
|
| 65 |
+
height: 20px;
|
| 66 |
+
background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='white'%3E%3Crect x='4' y='4' width='16' height='16' rx='2'/%3E%3C/svg%3E") no-repeat center;
|
| 67 |
+
background-size: contain;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
.sidebar-parent:has(.sidebar.open:not(.right)) {
|
| 71 |
+
padding-left: 320px;
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
.sidebar {
|
| 75 |
+
border-right: 1px solid var(--border-color-primary);
|
| 76 |
}
|
app.py
CHANGED
|
@@ -61,10 +61,8 @@ JS_SAVE_STATE = """(stateJson) => {
|
|
| 61 |
|
| 62 |
def _conv_choices(state_value):
|
| 63 |
return gr.update(
|
| 64 |
-
choices=[c["label"] for c in state_value["conversations"]],
|
| 65 |
-
value=
|
| 66 |
-
(c["label"] for c in state_value["conversations"]
|
| 67 |
-
if c["key"] == state_value.get("conversation_id")), None),
|
| 68 |
)
|
| 69 |
|
| 70 |
|
|
@@ -202,16 +200,11 @@ class GradioEvents:
|
|
| 202 |
def select_conversation(choice, state_value):
|
| 203 |
if not choice:
|
| 204 |
return gr.skip()
|
| 205 |
-
|
| 206 |
-
for c in state_value["conversations"]:
|
| 207 |
-
if c["label"] == choice:
|
| 208 |
-
conv_id = c["key"]
|
| 209 |
-
break
|
| 210 |
-
if not conv_id or conv_id == state_value.get("conversation_id"):
|
| 211 |
return gr.skip()
|
| 212 |
|
| 213 |
-
state_value["conversation_id"] =
|
| 214 |
-
ctx = state_value["conversation_contexts"].get(
|
| 215 |
return (
|
| 216 |
gr.update(value=ctx.get("history", [])),
|
| 217 |
gr.update(value=state_value),
|
|
@@ -221,19 +214,12 @@ class GradioEvents:
|
|
| 221 |
def delete_selected_conversation(choice, state_value):
|
| 222 |
if not choice:
|
| 223 |
return gr.skip()
|
| 224 |
-
target_id = None
|
| 225 |
-
for c in state_value["conversations"]:
|
| 226 |
-
if c["label"] == choice:
|
| 227 |
-
target_id = c["key"]
|
| 228 |
-
break
|
| 229 |
-
if not target_id:
|
| 230 |
-
return gr.skip()
|
| 231 |
|
| 232 |
-
state_value["conversation_contexts"].pop(
|
| 233 |
state_value["conversations"] = [
|
| 234 |
-
c for c in state_value["conversations"] if c["key"] !=
|
| 235 |
]
|
| 236 |
-
was_active = state_value.get("conversation_id") ==
|
| 237 |
if was_active:
|
| 238 |
state_value["conversation_id"] = ""
|
| 239 |
return (
|
|
@@ -305,7 +291,7 @@ with gr.Blocks(fill_width=True, title="Demo Chat") as demo:
|
|
| 305 |
js_save_input = gr.Textbox(visible=False, elem_id="js-save-input")
|
| 306 |
|
| 307 |
with gr.Row(elem_id="main-row"):
|
| 308 |
-
with gr.
|
| 309 |
new_chat_btn = gr.Button(
|
| 310 |
value="New Conversation",
|
| 311 |
variant="primary",
|
|
@@ -321,7 +307,7 @@ with gr.Blocks(fill_width=True, title="Demo Chat") as demo:
|
|
| 321 |
variant="stop",
|
| 322 |
)
|
| 323 |
|
| 324 |
-
with gr.Column(
|
| 325 |
with gr.Row():
|
| 326 |
clear_btn = gr.Button(
|
| 327 |
value="Clear History",
|
|
@@ -347,14 +333,16 @@ with gr.Blocks(fill_width=True, title="Demo Chat") as demo:
|
|
| 347 |
"Send",
|
| 348 |
variant="primary",
|
| 349 |
scale=0,
|
| 350 |
-
min_width=
|
|
|
|
| 351 |
)
|
| 352 |
stop_btn = gr.Button(
|
| 353 |
"Stop",
|
| 354 |
variant="stop",
|
| 355 |
scale=0,
|
| 356 |
-
min_width=
|
| 357 |
visible=False,
|
|
|
|
| 358 |
)
|
| 359 |
|
| 360 |
new_chat_btn.click(
|
|
|
|
| 61 |
|
| 62 |
def _conv_choices(state_value):
|
| 63 |
return gr.update(
|
| 64 |
+
choices=[(c["label"], c["key"]) for c in state_value["conversations"]],
|
| 65 |
+
value=state_value.get("conversation_id") or None,
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
|
| 68 |
|
|
|
|
| 200 |
def select_conversation(choice, state_value):
|
| 201 |
if not choice:
|
| 202 |
return gr.skip()
|
| 203 |
+
if choice == state_value.get("conversation_id"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
return gr.skip()
|
| 205 |
|
| 206 |
+
state_value["conversation_id"] = choice
|
| 207 |
+
ctx = state_value["conversation_contexts"].get(choice, {})
|
| 208 |
return (
|
| 209 |
gr.update(value=ctx.get("history", [])),
|
| 210 |
gr.update(value=state_value),
|
|
|
|
| 214 |
def delete_selected_conversation(choice, state_value):
|
| 215 |
if not choice:
|
| 216 |
return gr.skip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
|
| 218 |
+
state_value["conversation_contexts"].pop(choice, None)
|
| 219 |
state_value["conversations"] = [
|
| 220 |
+
c for c in state_value["conversations"] if c["key"] != choice
|
| 221 |
]
|
| 222 |
+
was_active = state_value.get("conversation_id") == choice
|
| 223 |
if was_active:
|
| 224 |
state_value["conversation_id"] = ""
|
| 225 |
return (
|
|
|
|
| 291 |
js_save_input = gr.Textbox(visible=False, elem_id="js-save-input")
|
| 292 |
|
| 293 |
with gr.Row(elem_id="main-row"):
|
| 294 |
+
with gr.Sidebar():
|
| 295 |
new_chat_btn = gr.Button(
|
| 296 |
value="New Conversation",
|
| 297 |
variant="primary",
|
|
|
|
| 307 |
variant="stop",
|
| 308 |
)
|
| 309 |
|
| 310 |
+
with gr.Column(elem_id="chat-column"):
|
| 311 |
with gr.Row():
|
| 312 |
clear_btn = gr.Button(
|
| 313 |
value="Clear History",
|
|
|
|
| 333 |
"Send",
|
| 334 |
variant="primary",
|
| 335 |
scale=0,
|
| 336 |
+
min_width=40,
|
| 337 |
+
elem_id="send-btn",
|
| 338 |
)
|
| 339 |
stop_btn = gr.Button(
|
| 340 |
"Stop",
|
| 341 |
variant="stop",
|
| 342 |
scale=0,
|
| 343 |
+
min_width=40,
|
| 344 |
visible=False,
|
| 345 |
+
elem_id="stop-btn",
|
| 346 |
)
|
| 347 |
|
| 348 |
new_chat_btn.click(
|