UKURIKIYEYEZU commited on
Commit
4880aa4
Β·
verified Β·
1 Parent(s): acf09e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -101
app.py CHANGED
@@ -267,8 +267,10 @@ import gradio as gr
267
  from typing import Iterator
268
  import time
269
 
 
270
  # Refined Template with Emotional Awareness
271
  template = ("""
 
272
  You are a friendly and empathetic chatbot designed to assist users in a conversational and human-like manner. Your goal is to provide accurate, helpful, and emotionally supportive responses based on the provided context: {context}. Follow these guidelines:
273
 
274
  1. **Emotional Awareness**
@@ -279,18 +281,19 @@ template = ("""
279
  2. **Contextual Interaction**
280
  - Begin with a warm and empathetic welcome message.
281
  - Extract precise details from the provided context: {context}.
282
- - Respond directly to the user's question: {question}.
283
- - Remember the user's name is {first_name}. some time you can address it occasionally
 
284
 
285
  3. **Communication Guidelines**
286
- - Maintain a warm, conversational tone.
287
- - Use occasional emojis for engagement (e.g., 😊, πŸ‘,πŸ‘‹, ❀️).
288
  - Provide clear, concise, and emotionally supportive information.
289
 
290
  4. **Response Strategies**
291
  - Greet users naturally and ask about their wellbeing (e.g., "Welcome, {first_name}! 😊 How are you feeling today?", "Hello {first_name}! πŸ€— What's on your mind?").
292
  - Always start with a check-in about the user's wellbeing or current situation.
293
- - Deliver only relevant information.
294
  - Avoid generating content beyond the context.
295
  - Handle missing information transparently.
296
 
@@ -309,12 +312,11 @@ template = ("""
309
  7. **Real-Time Awareness**
310
  - Acknowledge the current context when appropriate.
311
  - Stay focused on the user's immediate needs.
312
- - If this is the first message, always ask how the user is feeling and what they would like help with today.
313
 
314
-
315
  **Context:** {context}
316
  **User's Question:** {question}
317
- **Your Response:**
318
  """)
319
 
320
  rag_prompt = PromptTemplate.from_template(template)
@@ -489,11 +491,14 @@ def rag_memory_stream(message, history):
489
  user_session.add_to_history("assistant", full_response)
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
  # Update the template to include conversation history
498
  global template
499
  template = """
@@ -507,103 +512,115 @@ def chatbot_interface():
507
 
508
  User {first_name} asks: {question}
509
 
510
- Respond with empathy and provide support and resources based on the conversation and context. Always maintain a warm, supportive tone.
511
  """
512
 
513
  # Create the RAG chain with user context
514
  global rag_chain
515
  rag_chain = create_rag_chain(retriever, template, api_key)
516
 
 
 
517
  with gr.Blocks(css="""
518
- :root {
519
- --background: #343541;
520
- --primary-text: #ececf1;
521
- --input-bg: #40414f;
522
- --border: #555766;
523
- --button-bg: #19c37d;
524
- --button-hover: #15a46a;
525
- --user-bubble: #343541;
526
- --bot-bubble: #444654;
527
- }
528
-
529
- body {
530
- background: var(--background) !important;
531
- font-family: 'Segoe UI', system-ui, sans-serif !important;
532
- color: var(--primary-text) !important;
533
- }
534
-
535
- .welcome-container {
536
- max-width: 500px;
537
- margin: 2rem auto;
538
- padding: 2rem;
539
- background: var(--input-bg);
540
- border-radius: 8px;
541
- box-shadow: 0 4px 6px rgba(0,0,0,0.1);
542
- }
543
-
544
- .chatbot {
545
- border: none !important;
546
- background: transparent !important;
547
- box-shadow: none !important;
548
- min-height: 70vh;
549
- }
550
-
551
- .user, .bot {
552
- padding: 12px 16px !important;
553
- margin: 8px 0 !important;
554
- border-radius: 8px !important;
555
- max-width: 80%;
556
- }
557
-
558
- .user {
559
- background: var(--user-bubble) !important;
560
- margin-left: auto !important;
561
- border: 1px solid var(--border);
562
- }
563
-
564
- .bot {
565
- background: var(--bot-bubble) !important;
566
- margin-right: auto !important;
567
- }
568
-
569
- .textbox {
570
- background: var(--input-bg) !important;
571
- border: 1px solid var(--border) !important;
572
- border-radius: 8px !important;
573
- color: var(--primary-text) !important;
574
- padding: 12px !important;
575
- }
576
-
577
- .feedback-btn {
578
- background: var(--button-bg) !important;
579
- color: white !important;
580
- border: none !important;
581
- border-radius: 6px !important;
582
- padding: 8px 16px !important;
583
- transition: background 0.3s ease;
584
- }
585
-
586
- .feedback-btn:hover {
587
- background: var(--button-hover) !important;
588
- }
589
-
590
- footer {
591
- color: #888 !important;
592
- font-size: 0.9em;
593
- padding: 1rem;
594
- }
595
-
596
- #component-0 {
597
- max-width: 1200px;
598
- margin: 0 auto;
599
- padding: 20px;
600
- }
601
- """) as demo:
 
 
 
 
 
 
 
 
602
 
 
 
603
  # User registration section
604
  registration_container = gr.Column(visible=True)
605
  with registration_container:
606
- gr.Markdown("### To prioritize your privacy, please provide your nickname. ")
607
 
608
  with gr.Row():
609
  first_name = gr.Textbox(
@@ -622,10 +639,8 @@ def chatbot_interface():
622
  with chatbot_container:
623
  chat_interface = gr.ChatInterface(
624
  fn=rag_memory_stream,
625
- title=gr.Markdown("# Ijwi ry'Ubufasha", elem_classes="chat-title"),
626
- fill_height=True,
627
- textbox=gr.Textbox(placeholder="Type your message here...", show_label=False),
628
- submit_btn=gr.Button("Send", variant="primary")
629
  )
630
 
631
  # Footer with version info
@@ -642,6 +657,5 @@ def chatbot_interface():
642
 
643
  # Launch the interface
644
  if __name__ == "__main__":
645
-
646
- chatbot_interface().launch(share=True, inbrowser=True)
647
-
 
267
  from typing import Iterator
268
  import time
269
 
270
+
271
  # Refined Template with Emotional Awareness
272
  template = ("""
273
+ **Role**: Compassionate Regal Assistance and GBV Support Specialist with Emotional Awareness.
274
  You are a friendly and empathetic chatbot designed to assist users in a conversational and human-like manner. Your goal is to provide accurate, helpful, and emotionally supportive responses based on the provided context: {context}. Follow these guidelines:
275
 
276
  1. **Emotional Awareness**
 
281
  2. **Contextual Interaction**
282
  - Begin with a warm and empathetic welcome message.
283
  - Extract precise details from the provided context: {context}.
284
+ - Respond directly to the user's question: {question}.\
285
+ - Only provide detailed information if user requests it.
286
+ - Remember the user's name is {first_name}.
287
 
288
  3. **Communication Guidelines**
289
+ - Maintain a warm, conversational tone (avoid over-familiarity).
290
+ - Use occasional emojis for engagement (e.g., 😊, πŸ€—, ❀️).
291
  - Provide clear, concise, and emotionally supportive information.
292
 
293
  4. **Response Strategies**
294
  - Greet users naturally and ask about their wellbeing (e.g., "Welcome, {first_name}! 😊 How are you feeling today?", "Hello {first_name}! πŸ€— What's on your mind?").
295
  - Always start with a check-in about the user's wellbeing or current situation.
296
+ - Provide a concise summary with only relevant information.
297
  - Avoid generating content beyond the context.
298
  - Handle missing information transparently.
299
 
 
312
  7. **Real-Time Awareness**
313
  - Acknowledge the current context when appropriate.
314
  - Stay focused on the user's immediate needs.
 
315
 
316
+
317
  **Context:** {context}
318
  **User's Question:** {question}
319
+ **Your Response:**
320
  """)
321
 
322
  rag_prompt = PromptTemplate.from_template(template)
 
491
  user_session.add_to_history("assistant", full_response)
492
 
493
 
494
+
495
+
496
  # Gradio Interface Setup with improved UX
497
  def chatbot_interface():
498
  # Get API key (in a real application, handle this more securely)
499
  api_key = api # This should be properly defined or imported elsewhere
500
 
501
+
502
  # Update the template to include conversation history
503
  global template
504
  template = """
 
512
 
513
  User {first_name} asks: {question}
514
 
515
+ Respond with empathy, providing support and resources based on the conversation. Keep answers short unless the user asks for more details, while maintaining a warm, supportive tone.
516
  """
517
 
518
  # Create the RAG chain with user context
519
  global rag_chain
520
  rag_chain = create_rag_chain(retriever, template, api_key)
521
 
522
+
523
+
524
  with gr.Blocks(css="""
525
+ :root {
526
+ --primary: #10A37F;
527
+ --secondary: #19C99D;
528
+ --accent: #FF6B6B;
529
+ --background: #000000;
530
+ --text: #FFFFFF;
531
+ --card-bg: #1E1E1E;
532
+ }
533
+
534
+ body {
535
+ background: var(--background) !important;
536
+ color: var(--text) !important;
537
+ font-family: 'Inter', system-ui, sans-serif;
538
+ margin: 0 !important;
539
+ padding: 0 !important;
540
+ width: 100vw !important;
541
+ height: 100vh !important;
542
+ display: flex;
543
+ flex-direction: column;
544
+ }
545
+
546
+ .gradio-container {
547
+ max-width: 100% !important;
548
+ width: 100vw !important;
549
+ height: 100vh !important;
550
+ margin: 0 !important;
551
+ padding: 20px !important;
552
+ display: flex;
553
+ flex-direction: column;
554
+ }
555
+
556
+ .welcome-box {
557
+ background: var(--card-bg) !important;
558
+ border-radius: 12px !important;
559
+ padding: 2rem !important;
560
+ box-shadow: 0 4px 6px rgba(255, 255, 255, 0.05) !important;
561
+ border: 1px solid rgba(255, 255, 255, 0.1) !important;
562
+ }
563
+
564
+ .chat-container {
565
+ border-radius: 12px !important;
566
+ background: var(--card-bg) !important;
567
+ box-shadow: 0 4px 6px rgba(255, 255, 255, 0.05) !important;
568
+ border: 1px solid rgba(255, 255, 255, 0.1) !important;
569
+ flex-grow: 1;
570
+ min-height: 600px;
571
+ }
572
+
573
+ .gr-button-primary {
574
+ background: var(--primary) !important;
575
+ color: white !important;
576
+ padding: 12px 24px !important;
577
+ border-radius: 8px !important;
578
+ transition: all 0.3s ease !important;
579
+ }
580
+
581
+ .gr-button-primary:hover {
582
+ transform: translateY(-1px);
583
+ box-shadow: 0 4px 12px rgba(16, 163, 127, 0.2) !important;
584
+ }
585
+
586
+ .gr-textbox {
587
+ background: var(--card-bg) !important;
588
+ color: var(--text) !important;
589
+ border-radius: 8px !important;
590
+ padding: 12px 16px !important;
591
+ border: 1px solid rgba(255, 255, 255, 0.1) !important;
592
+ }
593
+
594
+ .chatbot {
595
+ background: transparent !important;
596
+ border: none !important;
597
+ }
598
+
599
+ .bot {
600
+ background: var(--card-bg) !important;
601
+ border: 1px solid rgba(255, 255, 255, 0.1) !important;
602
+ margin-right: auto !important;
603
+ }
604
+
605
+ footer {
606
+ text-align: center !important;
607
+ color: var(--text) !important;
608
+ opacity: 0.7 !important;
609
+ padding: 1rem !important;
610
+ font-size: 0.9em !important;
611
+ }
612
+
613
+ .gr-markdown h3 {
614
+ color: var(--primary) !important;
615
+ margin-bottom: 1rem !important;
616
+ }
617
 
618
+
619
+ """) as demo:
620
  # User registration section
621
  registration_container = gr.Column(visible=True)
622
  with registration_container:
623
+ gr.Markdown("### Your privacy is our concern, please provide your nickname. ")
624
 
625
  with gr.Row():
626
  first_name = gr.Textbox(
 
639
  with chatbot_container:
640
  chat_interface = gr.ChatInterface(
641
  fn=rag_memory_stream,
642
+ title="Chat with GBVR",
643
+ fill_height=True
 
 
644
  )
645
 
646
  # Footer with version info
 
657
 
658
  # Launch the interface
659
  if __name__ == "__main__":
660
+ # Launch the interface
661
+ chatbot_interface().launch(share=True)