Spaces:
Sleeping
Sleeping
Update app_gradio_messenger.py
Browse files- app_gradio_messenger.py +87 -50
app_gradio_messenger.py
CHANGED
|
@@ -250,45 +250,75 @@ body, .gradio-container {
|
|
| 250 |
|
| 251 |
.chat-history {
|
| 252 |
padding: 18px 18px 8px;
|
|
|
|
| 253 |
min-height: 0;
|
|
|
|
|
|
|
| 254 |
}
|
| 255 |
|
| 256 |
-
.
|
| 257 |
-
|
| 258 |
-
|
|
|
|
| 259 |
}
|
| 260 |
|
| 261 |
-
.
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
background: var(--user-bubble) !important;
|
| 265 |
-
color: #2f1e1a !important;
|
| 266 |
}
|
| 267 |
|
| 268 |
-
.
|
| 269 |
-
|
| 270 |
-
.chat-history .message-row.bot .bubble,
|
| 271 |
-
.chat-history .message-row.assistant .bubble {
|
| 272 |
-
background: var(--assistant-bubble) !important;
|
| 273 |
-
color: var(--assistant-text) !important;
|
| 274 |
-
border: 1px solid rgba(31, 24, 32, 0.08) !important;
|
| 275 |
}
|
| 276 |
|
| 277 |
-
.
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
overflow: hidden;
|
| 280 |
}
|
| 281 |
|
| 282 |
-
.
|
| 283 |
-
.
|
| 284 |
-
|
| 285 |
-
.chatbot-host .bubble-wrap {
|
| 286 |
height: 100%;
|
|
|
|
|
|
|
| 287 |
}
|
| 288 |
|
| 289 |
-
.
|
| 290 |
-
|
| 291 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
}
|
| 293 |
|
| 294 |
.chat-compose {
|
|
@@ -413,15 +443,31 @@ def render_sidebar(history: list[dict[str, str]]) -> str:
|
|
| 413 |
""".strip()
|
| 414 |
|
| 415 |
|
| 416 |
-
def
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
|
| 426 |
|
| 427 |
def begin_request(
|
|
@@ -430,9 +476,9 @@ def begin_request(
|
|
| 430 |
session_id: str | None,
|
| 431 |
):
|
| 432 |
if not message.strip():
|
| 433 |
-
return render_sidebar(history),
|
| 434 |
status_text = "서비스 준비 중..." if SERVICES is None else "답변 생성 중..."
|
| 435 |
-
return render_sidebar(history),
|
| 436 |
|
| 437 |
|
| 438 |
async def respond(
|
|
@@ -441,7 +487,7 @@ async def respond(
|
|
| 441 |
session_id: str | None,
|
| 442 |
):
|
| 443 |
if not message.strip():
|
| 444 |
-
yield render_sidebar(history),
|
| 445 |
return
|
| 446 |
|
| 447 |
updated_history = list(history)
|
|
@@ -449,7 +495,7 @@ async def respond(
|
|
| 449 |
updated_history.append({"role": "assistant", "content": ""})
|
| 450 |
yield (
|
| 451 |
render_sidebar(updated_history),
|
| 452 |
-
|
| 453 |
updated_history,
|
| 454 |
session_id,
|
| 455 |
"",
|
|
@@ -467,7 +513,7 @@ async def respond(
|
|
| 467 |
updated_history[-1] = {"role": "assistant", "content": partial_text}
|
| 468 |
yield (
|
| 469 |
render_sidebar(updated_history),
|
| 470 |
-
|
| 471 |
updated_history,
|
| 472 |
active_session_id,
|
| 473 |
"",
|
|
@@ -482,7 +528,7 @@ async def respond(
|
|
| 482 |
|
| 483 |
yield (
|
| 484 |
render_sidebar(updated_history),
|
| 485 |
-
|
| 486 |
updated_history,
|
| 487 |
active_session_id,
|
| 488 |
"",
|
|
@@ -514,16 +560,7 @@ with gr.Blocks(title="Megumin Messenger", fill_height=True) as demo:
|
|
| 514 |
</div>
|
| 515 |
""".strip()
|
| 516 |
)
|
| 517 |
-
|
| 518 |
-
messages_html = gr.Chatbot(
|
| 519 |
-
value=render_chatbot_history(initial_history()),
|
| 520 |
-
elem_classes=["chat-history"],
|
| 521 |
-
avatar_images=(MEGUMIN_IMAGE_URL, None),
|
| 522 |
-
height=560,
|
| 523 |
-
label="Megumin Messenger",
|
| 524 |
-
render_markdown=True,
|
| 525 |
-
show_label=False,
|
| 526 |
-
)
|
| 527 |
with gr.Column(elem_classes=["chat-compose"]):
|
| 528 |
runtime_status = gr.Markdown("", elem_classes=["compose-status"])
|
| 529 |
with gr.Row(elem_classes=["compose-row"]):
|
|
@@ -562,7 +599,7 @@ with gr.Blocks(title="Megumin Messenger", fill_height=True) as demo:
|
|
| 562 |
clear_button.click(
|
| 563 |
fn=lambda: (
|
| 564 |
render_sidebar(initial_history()),
|
| 565 |
-
|
| 566 |
initial_history(),
|
| 567 |
None,
|
| 568 |
"",
|
|
|
|
| 250 |
|
| 251 |
.chat-history {
|
| 252 |
padding: 18px 18px 8px;
|
| 253 |
+
overflow-y: auto;
|
| 254 |
min-height: 0;
|
| 255 |
+
max-height: 100%;
|
| 256 |
+
height: 100%;
|
| 257 |
}
|
| 258 |
|
| 259 |
+
.message-col {
|
| 260 |
+
display: flex;
|
| 261 |
+
flex-direction: column;
|
| 262 |
+
gap: 12px;
|
| 263 |
}
|
| 264 |
|
| 265 |
+
.message-row {
|
| 266 |
+
display: flex;
|
| 267 |
+
width: 100%;
|
|
|
|
|
|
|
| 268 |
}
|
| 269 |
|
| 270 |
+
.message-row.assistant {
|
| 271 |
+
justify-content: flex-start;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
}
|
| 273 |
|
| 274 |
+
.message-row.user {
|
| 275 |
+
justify-content: flex-end;
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
.assistant-wrap {
|
| 279 |
+
display: grid;
|
| 280 |
+
grid-template-columns: 42px minmax(0, 1fr);
|
| 281 |
+
gap: 10px;
|
| 282 |
+
max-width: 78%;
|
| 283 |
+
align-items: start;
|
| 284 |
+
}
|
| 285 |
+
|
| 286 |
+
.assistant-avatar {
|
| 287 |
+
width: 42px;
|
| 288 |
+
height: 42px;
|
| 289 |
+
border-radius: 12px;
|
| 290 |
overflow: hidden;
|
| 291 |
}
|
| 292 |
|
| 293 |
+
.assistant-avatar img,
|
| 294 |
+
.assistant-avatar .fallback {
|
| 295 |
+
width: 100%;
|
|
|
|
| 296 |
height: 100%;
|
| 297 |
+
display: block;
|
| 298 |
+
object-fit: cover;
|
| 299 |
}
|
| 300 |
|
| 301 |
+
.bubble {
|
| 302 |
+
padding: 12px 14px;
|
| 303 |
+
border-radius: 18px;
|
| 304 |
+
line-height: 1.6;
|
| 305 |
+
font-size: 0.95rem;
|
| 306 |
+
white-space: pre-wrap;
|
| 307 |
+
word-break: break-word;
|
| 308 |
+
backdrop-filter: blur(8px);
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
.assistant-bubble {
|
| 312 |
+
background: var(--assistant-bubble);
|
| 313 |
+
color: var(--assistant-text);
|
| 314 |
+
border-top-left-radius: 8px;
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
.user-bubble {
|
| 318 |
+
max-width: 72%;
|
| 319 |
+
background: var(--user-bubble);
|
| 320 |
+
color: #2f1e1a;
|
| 321 |
+
border-top-right-radius: 8px;
|
| 322 |
}
|
| 323 |
|
| 324 |
.chat-compose {
|
|
|
|
| 443 |
""".strip()
|
| 444 |
|
| 445 |
|
| 446 |
+
def render_messages(history: list[dict[str, str]]) -> str:
|
| 447 |
+
rows: list[str] = []
|
| 448 |
+
for item in history:
|
| 449 |
+
role = item.get("role", "")
|
| 450 |
+
content = html.escape(item.get("content", ""))
|
| 451 |
+
if role == "assistant":
|
| 452 |
+
rows.append(
|
| 453 |
+
f"""
|
| 454 |
+
<div class="message-row assistant">
|
| 455 |
+
<div class="assistant-wrap">
|
| 456 |
+
{render_avatar("assistant-avatar")}
|
| 457 |
+
<div class="bubble assistant-bubble">{content}</div>
|
| 458 |
+
</div>
|
| 459 |
+
</div>
|
| 460 |
+
""".strip()
|
| 461 |
+
)
|
| 462 |
+
else:
|
| 463 |
+
rows.append(
|
| 464 |
+
f"""
|
| 465 |
+
<div class="message-row user">
|
| 466 |
+
<div class="bubble user-bubble">{content}</div>
|
| 467 |
+
</div>
|
| 468 |
+
""".strip()
|
| 469 |
+
)
|
| 470 |
+
return f'<div class="message-col">{"".join(rows)}</div>'
|
| 471 |
|
| 472 |
|
| 473 |
def begin_request(
|
|
|
|
| 476 |
session_id: str | None,
|
| 477 |
):
|
| 478 |
if not message.strip():
|
| 479 |
+
return render_sidebar(history), render_messages(history), history, session_id, "", ""
|
| 480 |
status_text = "서비스 준비 중..." if SERVICES is None else "답변 생성 중..."
|
| 481 |
+
return render_sidebar(history), render_messages(history), history, session_id, message, status_text
|
| 482 |
|
| 483 |
|
| 484 |
async def respond(
|
|
|
|
| 487 |
session_id: str | None,
|
| 488 |
):
|
| 489 |
if not message.strip():
|
| 490 |
+
yield render_sidebar(history), render_messages(history), history, session_id, "", ""
|
| 491 |
return
|
| 492 |
|
| 493 |
updated_history = list(history)
|
|
|
|
| 495 |
updated_history.append({"role": "assistant", "content": ""})
|
| 496 |
yield (
|
| 497 |
render_sidebar(updated_history),
|
| 498 |
+
render_messages(updated_history),
|
| 499 |
updated_history,
|
| 500 |
session_id,
|
| 501 |
"",
|
|
|
|
| 513 |
updated_history[-1] = {"role": "assistant", "content": partial_text}
|
| 514 |
yield (
|
| 515 |
render_sidebar(updated_history),
|
| 516 |
+
render_messages(updated_history),
|
| 517 |
updated_history,
|
| 518 |
active_session_id,
|
| 519 |
"",
|
|
|
|
| 528 |
|
| 529 |
yield (
|
| 530 |
render_sidebar(updated_history),
|
| 531 |
+
render_messages(updated_history),
|
| 532 |
updated_history,
|
| 533 |
active_session_id,
|
| 534 |
"",
|
|
|
|
| 560 |
</div>
|
| 561 |
""".strip()
|
| 562 |
)
|
| 563 |
+
messages_html = gr.HTML(render_messages(initial_history()), elem_classes=["chat-history"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 564 |
with gr.Column(elem_classes=["chat-compose"]):
|
| 565 |
runtime_status = gr.Markdown("", elem_classes=["compose-status"])
|
| 566 |
with gr.Row(elem_classes=["compose-row"]):
|
|
|
|
| 599 |
clear_button.click(
|
| 600 |
fn=lambda: (
|
| 601 |
render_sidebar(initial_history()),
|
| 602 |
+
render_messages(initial_history()),
|
| 603 |
initial_history(),
|
| 604 |
None,
|
| 605 |
"",
|