Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -487,143 +487,77 @@ def rag_memory_stream(message, history):
|
|
487 |
user_session.add_to_history("assistant", full_response)
|
488 |
|
489 |
|
490 |
-
|
491 |
|
492 |
# Gradio Interface Setup with improved UX
|
493 |
def chatbot_interface():
|
494 |
# Get API key (in a real application, handle this more securely)
|
495 |
api_key = api # This should be properly defined or imported elsewhere
|
496 |
|
497 |
-
|
498 |
# Update the template to include conversation history
|
499 |
-
global template
|
500 |
template = """
|
501 |
-
You are a compassionate and supportive AI assistant specializing in helping individuals affected by Gender-Based Violence (GBV).
|
502 |
|
503 |
-
|
504 |
-
|
505 |
|
506 |
-
|
507 |
-
|
508 |
|
509 |
-
|
510 |
|
511 |
-
|
512 |
-
"""
|
513 |
|
514 |
-
|
515 |
-
|
516 |
-
|
517 |
|
|
|
|
|
|
|
|
|
|
|
518 |
|
|
|
|
|
|
|
|
|
|
|
519 |
|
520 |
-
|
521 |
-
|
522 |
-
--background: #000000;
|
523 |
-
--text: #FFFFFF;
|
524 |
-
}
|
525 |
-
|
526 |
-
body {
|
527 |
-
background: var(--background) !important;
|
528 |
-
color: var(--text) !important;
|
529 |
-
font-family: 'Inter', system-ui, sans-serif;
|
530 |
-
margin: 0 !important;
|
531 |
-
padding: 0 !important;
|
532 |
-
width: 100vw !important;
|
533 |
-
height: 100vh !important;
|
534 |
-
display: flex;
|
535 |
-
flex-direction: column;
|
536 |
-
}
|
537 |
-
|
538 |
-
.gradio-container {
|
539 |
-
max-width: 100% !important;
|
540 |
-
width: 100vw !important;
|
541 |
-
height: 100vh !important;
|
542 |
-
margin: 0 !important;
|
543 |
-
padding: 20px !important;
|
544 |
-
display: flex;
|
545 |
-
flex-direction: column;
|
546 |
-
}
|
547 |
-
|
548 |
-
.welcome-box, .chat-container, .gr-textbox, .bot {
|
549 |
-
background: var(--background) !important;
|
550 |
-
color: var(--text) !important;
|
551 |
-
border-radius: 12px !important;
|
552 |
-
padding: 2rem !important;
|
553 |
-
border: 1px solid rgba(255, 255, 255, 0.1) !important;
|
554 |
-
box-shadow: 0 4px 6px rgba(255, 255, 255, 0.05) !important;
|
555 |
-
}
|
556 |
-
|
557 |
-
.gr-button-primary {
|
558 |
-
background: var(--background) !important;
|
559 |
-
color: var(--text) !important;
|
560 |
-
padding: 12px 24px !important;
|
561 |
-
border-radius: 8px !important;
|
562 |
-
transition: all 0.3s ease !important;
|
563 |
-
border: 1px solid rgba(255, 255, 255, 0.1) !important;
|
564 |
-
}
|
565 |
-
|
566 |
-
.gr-button-primary:hover {
|
567 |
-
transform: translateY(-1px);
|
568 |
-
box-shadow: 0 4px 12px rgba(255, 255, 255, 0.2) !important;
|
569 |
-
}
|
570 |
-
|
571 |
-
footer {
|
572 |
-
text-align: center !important;
|
573 |
-
color: var(--text) !important;
|
574 |
-
opacity: 0.7 !important;
|
575 |
-
padding: 1rem !important;
|
576 |
-
font-size: 0.9em !important;
|
577 |
-
}
|
578 |
-
|
579 |
-
.gr-markdown h3 {
|
580 |
-
color: var(--text) !important;
|
581 |
-
margin-bottom: 1rem !important;
|
582 |
-
}
|
583 |
-
|
584 |
|
585 |
-
|
586 |
-
|
|
|
|
|
587 |
# User registration section
|
588 |
-
|
589 |
-
|
590 |
-
gr.Markdown("### Your privacy is our concern, please provide your nickname. ")
|
591 |
-
# with registration_container:
|
592 |
-
# gr.Markdown(
|
593 |
-
# "### Your privacy is our concern, please provide your nickname.",
|
594 |
-
# elem_id="registration-markdown"
|
595 |
-
# )
|
596 |
|
597 |
with gr.Row():
|
598 |
first_name = gr.Textbox(
|
599 |
label="Nickname",
|
600 |
placeholder="Enter your Nickname",
|
601 |
-
scale=1
|
|
|
602 |
)
|
603 |
|
604 |
with gr.Row():
|
605 |
submit_btn = gr.Button("Start Chatting", variant="primary", scale=2)
|
606 |
|
607 |
-
response_message = gr.Markdown(
|
608 |
|
609 |
# Chatbot section (initially hidden)
|
610 |
-
|
611 |
-
with chatbot_container:
|
612 |
chat_interface = gr.ChatInterface(
|
613 |
fn=rag_memory_stream,
|
614 |
title="Chat with GBVR",
|
615 |
fill_height=True
|
616 |
)
|
617 |
-
# with chatbot_container:
|
618 |
-
# chat_interface = gr.ChatInterface(
|
619 |
-
# fn=rag_memory_stream,
|
620 |
-
# title="Chat with GBVR",
|
621 |
-
# fill_height=True,
|
622 |
-
# elem_id="chat-title"
|
623 |
-
# )
|
624 |
|
625 |
# Footer with version info
|
626 |
-
gr.Markdown("Ijwi ry'Ubufasha v1.0.0 © 2025"
|
627 |
|
628 |
# Handle user registration
|
629 |
submit_btn.click(
|
@@ -632,9 +566,73 @@ def chatbot_interface():
|
|
632 |
outputs=[response_message, chatbot_container, registration_container, chat_interface.chatbot]
|
633 |
)
|
634 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
635 |
return demo
|
636 |
|
637 |
# Launch the interface
|
638 |
if __name__ == "__main__":
|
639 |
-
# Launch the interface
|
640 |
chatbot_interface().launch(share=True)
|
|
|
487 |
user_session.add_to_history("assistant", full_response)
|
488 |
|
489 |
|
490 |
+
import gradio as gr
|
491 |
|
492 |
# Gradio Interface Setup with improved UX
|
493 |
def chatbot_interface():
|
494 |
# Get API key (in a real application, handle this more securely)
|
495 |
api_key = api # This should be properly defined or imported elsewhere
|
496 |
|
|
|
497 |
# Update the template to include conversation history
|
|
|
498 |
template = """
|
499 |
+
You are a compassionate and supportive AI assistant specializing in helping individuals affected by Gender-Based Violence (GBV) with emotional awareness.
|
500 |
|
501 |
+
**Previous conversation:**
|
502 |
+
{conversation_history}
|
503 |
|
504 |
+
**Context information:**
|
505 |
+
{context}
|
506 |
|
507 |
+
**User's Question:** {question}
|
508 |
|
509 |
+
Respond to {first_name} with these guidelines:
|
|
|
510 |
|
511 |
+
1. **Emotional Awareness**
|
512 |
+
- Acknowledge emotions with empathy (e.g., "I understand how you feel")
|
513 |
+
- Offer comfort for negative emotions
|
514 |
|
515 |
+
2. **Communication Approach**
|
516 |
+
- Occasionaly use a warm welcome or check-in (e.g., "Hello {first_name}! How are you feeling today?")
|
517 |
+
- Use occasional appropriate emojis (😊, 🤗, ❤️)
|
518 |
+
- Keep responses concise unless details are requested
|
519 |
+
- Maintain warm but professional tone
|
520 |
|
521 |
+
3. **Information Handling**
|
522 |
+
- Extract relevant details from context
|
523 |
+
- Provide links when specifically requested
|
524 |
+
- If information isn't available, respond with: "I don't have that information at the moment, {first_name}. 😊"
|
525 |
+
- Never generate unsupported content or speculate
|
526 |
|
527 |
+
Your response should be supportive, accurate, and centered on the user's immediate needs.
|
528 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
529 |
|
530 |
+
# Create the RAG chain with user context
|
531 |
+
rag_chain = create_rag_chain(retriever, template, api_key)
|
532 |
+
|
533 |
+
with gr.Blocks() as demo:
|
534 |
# User registration section
|
535 |
+
with gr.Column(visible=True, elem_id="registration_container") as registration_container:
|
536 |
+
gr.Markdown("### Your privacy is our concern, please provide your nickname.")
|
|
|
|
|
|
|
|
|
|
|
|
|
537 |
|
538 |
with gr.Row():
|
539 |
first_name = gr.Textbox(
|
540 |
label="Nickname",
|
541 |
placeholder="Enter your Nickname",
|
542 |
+
scale=1,
|
543 |
+
elem_id="input_nickname"
|
544 |
)
|
545 |
|
546 |
with gr.Row():
|
547 |
submit_btn = gr.Button("Start Chatting", variant="primary", scale=2)
|
548 |
|
549 |
+
response_message = gr.Markdown()
|
550 |
|
551 |
# Chatbot section (initially hidden)
|
552 |
+
with gr.Column(visible=False, elem_id="chatbot_container") as chatbot_container:
|
|
|
553 |
chat_interface = gr.ChatInterface(
|
554 |
fn=rag_memory_stream,
|
555 |
title="Chat with GBVR",
|
556 |
fill_height=True
|
557 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
558 |
|
559 |
# Footer with version info
|
560 |
+
gr.Markdown("Ijwi ry'Ubufasha v1.0.0 © 2025")
|
561 |
|
562 |
# Handle user registration
|
563 |
submit_btn.click(
|
|
|
566 |
outputs=[response_message, chatbot_container, registration_container, chat_interface.chatbot]
|
567 |
)
|
568 |
|
569 |
+
demo.css = """
|
570 |
+
:root {
|
571 |
+
--background: #f0f0f0;
|
572 |
+
--text: #000000;
|
573 |
+
}
|
574 |
+
|
575 |
+
body, .gradio-container {
|
576 |
+
margin: 0;
|
577 |
+
padding: 0;
|
578 |
+
width: 100vw;
|
579 |
+
height: 100vh;
|
580 |
+
display: flex;
|
581 |
+
flex-direction: column;
|
582 |
+
justify-content: center;
|
583 |
+
align-items: center;
|
584 |
+
background: var(--background);
|
585 |
+
color: var(--text);
|
586 |
+
}
|
587 |
+
|
588 |
+
.gradio-container {
|
589 |
+
max-width: 100%;
|
590 |
+
max-height: 100%;
|
591 |
+
}
|
592 |
+
|
593 |
+
.gr-box {
|
594 |
+
background: var(--background);
|
595 |
+
color: var(--text);
|
596 |
+
border-radius: 12px;
|
597 |
+
padding: 2rem;
|
598 |
+
border: 1px solid rgba(0, 0, 0, 0.1);
|
599 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
|
600 |
+
}
|
601 |
+
|
602 |
+
.gr-button-primary {
|
603 |
+
background: var(--background);
|
604 |
+
color: var(--text);
|
605 |
+
padding: 12px 24px;
|
606 |
+
border-radius: 8px;
|
607 |
+
transition: all 0.3s ease;
|
608 |
+
border: 1px solid rgba(0, 0, 0, 0.1);
|
609 |
+
}
|
610 |
+
|
611 |
+
.gr-button-primary:hover {
|
612 |
+
transform: translateY(-1px);
|
613 |
+
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
|
614 |
+
}
|
615 |
+
|
616 |
+
footer {
|
617 |
+
text-align: center;
|
618 |
+
color: var(--text);
|
619 |
+
opacity: 0.7;
|
620 |
+
padding: 1rem;
|
621 |
+
font-size: 0.9em;
|
622 |
+
}
|
623 |
+
|
624 |
+
.gr-markdown h3 {
|
625 |
+
color: var(--text);
|
626 |
+
margin-bottom: 1rem;
|
627 |
+
}
|
628 |
+
|
629 |
+
.registration-markdown, .chat-title h1 {
|
630 |
+
color: var(--text);
|
631 |
+
}
|
632 |
+
"""
|
633 |
+
|
634 |
return demo
|
635 |
|
636 |
# Launch the interface
|
637 |
if __name__ == "__main__":
|
|
|
638 |
chatbot_interface().launch(share=True)
|