Spaces:
Sleeping
Sleeping
updated ui with theme toggle
Browse files- chatbot-gradio.py +86 -5
chatbot-gradio.py
CHANGED
|
@@ -290,6 +290,17 @@ minimal_css = """
|
|
| 290 |
z-index: 10001;
|
| 291 |
}
|
| 292 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
#chatbot-panel {
|
| 294 |
position: fixed;
|
| 295 |
bottom: 5vh;
|
|
@@ -337,9 +348,14 @@ minimal_css = """
|
|
| 337 |
bottom: 0;
|
| 338 |
}
|
| 339 |
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
|
|
|
| 343 |
def format_faq_question(question):
|
| 344 |
"""Format FAQ questions with proper capitalization and punctuation"""
|
| 345 |
# Remove extra whitespace
|
|
@@ -387,7 +403,6 @@ async def get_chatbot_examples():
|
|
| 387 |
examples = []
|
| 388 |
for faq in faqs:
|
| 389 |
faq = format_faq_question(faq)
|
| 390 |
-
print(faq)
|
| 391 |
examples.append({
|
| 392 |
"text": faq,
|
| 393 |
"display_text": faq
|
|
@@ -398,7 +413,6 @@ async def get_chatbot_examples():
|
|
| 398 |
logger.error(f"Failed to load FAQ examples: {str(e)}")
|
| 399 |
return []
|
| 400 |
|
| 401 |
-
|
| 402 |
def get_examples_sync():
|
| 403 |
try:
|
| 404 |
loop = asyncio.get_event_loop()
|
|
@@ -425,6 +439,14 @@ with gr.Blocks(theme=custom_theme, css=minimal_css) as demo:
|
|
| 425 |
variant="primary",
|
| 426 |
size="sm"
|
| 427 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 428 |
|
| 429 |
# Chat panel
|
| 430 |
chat_panel = gr.Column(visible=panel_visible, elem_id="chatbot-panel")
|
|
@@ -447,7 +469,7 @@ with gr.Blocks(theme=custom_theme, css=minimal_css) as demo:
|
|
| 447 |
Lofty the TAL Bot
|
| 448 |
</div>
|
| 449 |
""")
|
| 450 |
-
|
| 451 |
# Chatbot with theme styling
|
| 452 |
chatbot = gr.Chatbot(
|
| 453 |
type="messages",
|
|
@@ -471,6 +493,65 @@ with gr.Blocks(theme=custom_theme, css=minimal_css) as demo:
|
|
| 471 |
variant="primary",
|
| 472 |
scale=1
|
| 473 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
|
| 475 |
def handle_example_select(evt: gr.SelectData):
|
| 476 |
"""Handle when user clicks on an example"""
|
|
|
|
| 290 |
z-index: 10001;
|
| 291 |
}
|
| 292 |
|
| 293 |
+
#theme-toggle-btn {
|
| 294 |
+
position: fixed !important;
|
| 295 |
+
top: 20px;
|
| 296 |
+
right: 20px;
|
| 297 |
+
z-index: 10002;
|
| 298 |
+
border-radius: 50% !important;
|
| 299 |
+
width: 50px !important;
|
| 300 |
+
height: 50px !important;
|
| 301 |
+
min-width: 50px !important;
|
| 302 |
+
}
|
| 303 |
+
|
| 304 |
#chatbot-panel {
|
| 305 |
position: fixed;
|
| 306 |
bottom: 5vh;
|
|
|
|
| 348 |
bottom: 0;
|
| 349 |
}
|
| 350 |
|
| 351 |
+
#theme-toggle-btn {
|
| 352 |
+
top: 15px;
|
| 353 |
+
right: 15px;
|
| 354 |
+
width: 45px !important;
|
| 355 |
+
height: 45px !important;
|
| 356 |
+
}
|
| 357 |
|
| 358 |
+
"""
|
| 359 |
def format_faq_question(question):
|
| 360 |
"""Format FAQ questions with proper capitalization and punctuation"""
|
| 361 |
# Remove extra whitespace
|
|
|
|
| 403 |
examples = []
|
| 404 |
for faq in faqs:
|
| 405 |
faq = format_faq_question(faq)
|
|
|
|
| 406 |
examples.append({
|
| 407 |
"text": faq,
|
| 408 |
"display_text": faq
|
|
|
|
| 413 |
logger.error(f"Failed to load FAQ examples: {str(e)}")
|
| 414 |
return []
|
| 415 |
|
|
|
|
| 416 |
def get_examples_sync():
|
| 417 |
try:
|
| 418 |
loop = asyncio.get_event_loop()
|
|
|
|
| 439 |
variant="primary",
|
| 440 |
size="sm"
|
| 441 |
)
|
| 442 |
+
|
| 443 |
+
theme_toggle = gr.Button(
|
| 444 |
+
"๐",
|
| 445 |
+
elem_id="theme-toggle-btn",
|
| 446 |
+
variant="secondary",
|
| 447 |
+
scale=0,
|
| 448 |
+
min_width=50
|
| 449 |
+
)
|
| 450 |
|
| 451 |
# Chat panel
|
| 452 |
chat_panel = gr.Column(visible=panel_visible, elem_id="chatbot-panel")
|
|
|
|
| 469 |
Lofty the TAL Bot
|
| 470 |
</div>
|
| 471 |
""")
|
| 472 |
+
|
| 473 |
# Chatbot with theme styling
|
| 474 |
chatbot = gr.Chatbot(
|
| 475 |
type="messages",
|
|
|
|
| 493 |
variant="primary",
|
| 494 |
scale=1
|
| 495 |
)
|
| 496 |
+
def toggle_theme():
|
| 497 |
+
# This uses Gradio's built-in JavaScript functionality
|
| 498 |
+
return gr.update(), gr.update()
|
| 499 |
+
|
| 500 |
+
theme_toggle.click(
|
| 501 |
+
fn=None,
|
| 502 |
+
js="""
|
| 503 |
+
() => {
|
| 504 |
+
// Toggle dark mode using Gradio's built-in functionality
|
| 505 |
+
const isDark = document.body.classList.contains('dark');
|
| 506 |
+
if (isDark) {
|
| 507 |
+
document.body.classList.remove('dark');
|
| 508 |
+
// Update button text
|
| 509 |
+
const buttons = document.querySelectorAll('button');
|
| 510 |
+
buttons.forEach(btn => {
|
| 511 |
+
if (btn.textContent.trim() === 'โ๏ธ') {
|
| 512 |
+
btn.textContent = '๐';
|
| 513 |
+
}
|
| 514 |
+
});
|
| 515 |
+
localStorage.setItem('gradio-theme', 'light');
|
| 516 |
+
} else {
|
| 517 |
+
document.body.classList.add('dark');
|
| 518 |
+
// Update button text
|
| 519 |
+
const buttons = document.querySelectorAll('button');
|
| 520 |
+
buttons.forEach(btn => {
|
| 521 |
+
if (btn.textContent.trim() === '๐') {
|
| 522 |
+
btn.textContent = 'โ๏ธ';
|
| 523 |
+
}
|
| 524 |
+
});
|
| 525 |
+
localStorage.setItem('gradio-theme', 'dark');
|
| 526 |
+
}
|
| 527 |
+
}
|
| 528 |
+
"""
|
| 529 |
+
)
|
| 530 |
+
|
| 531 |
+
# Initialize theme on load
|
| 532 |
+
demo.load(
|
| 533 |
+
fn=None,
|
| 534 |
+
js="""
|
| 535 |
+
() => {
|
| 536 |
+
// Check saved theme preference
|
| 537 |
+
const savedTheme = localStorage.getItem('gradio-theme');
|
| 538 |
+
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
|
| 539 |
+
|
| 540 |
+
if (savedTheme === 'dark' || (!savedTheme && prefersDark)) {
|
| 541 |
+
document.body.classList.add('dark');
|
| 542 |
+
// Update button text
|
| 543 |
+
setTimeout(() => {
|
| 544 |
+
const buttons = document.querySelectorAll('button');
|
| 545 |
+
buttons.forEach(btn => {
|
| 546 |
+
if (btn.textContent.trim() === '๐') {
|
| 547 |
+
btn.textContent = 'โ๏ธ';
|
| 548 |
+
}
|
| 549 |
+
});
|
| 550 |
+
}, 100);
|
| 551 |
+
}
|
| 552 |
+
}
|
| 553 |
+
"""
|
| 554 |
+
)
|
| 555 |
|
| 556 |
def handle_example_select(evt: gr.SelectData):
|
| 557 |
"""Handle when user clicks on an example"""
|