Rabbit-Innotech commited on
Commit
762cba4
Β·
verified Β·
1 Parent(s): 0a00b33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +892 -570
app.py CHANGED
@@ -1,651 +1,973 @@
1
- import os
2
- import time
3
- import pandas as pd
4
- import gradio as gr
5
- from langchain_groq import ChatGroq
6
- from langchain_huggingface import HuggingFaceEmbeddings
7
- from langchain_community.vectorstores import Chroma
8
- from langchain_core.prompts import PromptTemplate
9
- from langchain_core.output_parsers import StrOutputParser
10
- from langchain_core.runnables import RunnablePassthrough
11
- from PyPDF2 import PdfReader
12
-
13
-
14
- # Configuration constants
15
- COLLECTION_NAME = "GBVRS"
16
- DATA_FOLDER = "./"
17
- APP_VERSION = "v1.0.0"
18
- APP_NAME = "Ijwi ry'Ubufasha"
19
- MAX_HISTORY_MESSAGES = 8 # Limit history to avoid token limits
20
-
21
- # Global variables for application state
22
- llm = None
23
- embed_model = None
24
- vectorstore = None
25
- retriever = None
26
- rag_chain = None
27
-
28
- # User session management
29
- class UserSession:
30
- def __init__(self, session_id, llm):
31
- """Initialize a user session with unique ID and language model."""
32
- self.session_id = session_id
33
- self.user_info = {"Nickname": "Guest"}
34
- self.conversation_history = []
35
- self.llm = llm
36
- self.welcome_message = None
37
- self.last_activity = time.time()
38
 
39
- def set_user(self, user_info):
40
- """Set user information and generate welcome message."""
41
- self.user_info = user_info
42
- self.generate_welcome_message()
43
 
44
- # Initialize conversation history with welcome message
45
- welcome = self.get_welcome_message()
46
- self.conversation_history = [
47
- {"role": "assistant", "content": welcome},
48
- ]
49
 
50
- def get_user(self):
51
- """Get current user information."""
52
- return self.user_info
53
 
54
- def generate_welcome_message(self):
55
- """Generate a dynamic welcome message using the LLM."""
56
- try:
57
- nickname = self.user_info.get("Nickname", "Guest")
58
 
59
- # Use the LLM to generate the message
60
- prompt = (
61
- f"Create a brief and warm welcome message for {nickname} that's about 1-2 sentences. "
62
- f"Emphasize this is a safe space for discussing gender-based violence issues "
63
- f"and that we provide support and resources. Keep it warm and reassuring."
64
- )
65
 
66
- response = self.llm.invoke(prompt)
67
- welcome = response.content.strip()
68
 
69
- # Format the message with HTML styling
70
- self.welcome_message = (
71
- f"<div style='font-size: 18px; color: #4E6BBF;'>"
72
- f"{welcome}"
73
- f"</div>"
74
- )
75
- except Exception as e:
76
- # Fallback welcome message
77
- nickname = self.user_info.get("Nickname", "Guest")
78
- self.welcome_message = (
79
- f"<div style='font-size: 18px; color: #4E6BBF;'>"
80
- f"Welcome, {nickname}! You're in a safe space. We're here to provide support with "
81
- f"gender-based violence issues and connect you with resources that can help."
82
- f"</div>"
83
- )
84
 
85
- def get_welcome_message(self):
86
- """Get the formatted welcome message."""
87
- if not self.welcome_message:
88
- self.generate_welcome_message()
89
- return self.welcome_message
90
 
91
- def add_to_history(self, role, message):
92
- """Add a message to the conversation history."""
93
- self.conversation_history.append({"role": role, "content": message})
94
- self.last_activity = time.time()
95
 
96
- # Trim history if it gets too long
97
- if len(self.conversation_history) > MAX_HISTORY_MESSAGES * 2: # Keep pairs of messages
98
- # Keep the first message (welcome) and the most recent messages
99
- self.conversation_history = [self.conversation_history[0]] + self.conversation_history[-MAX_HISTORY_MESSAGES*2+1:]
100
 
101
- def get_conversation_history(self):
102
- """Get the full conversation history."""
103
- return self.conversation_history
104
 
105
- def get_formatted_history(self):
106
- """Get conversation history formatted as a string for the LLM."""
107
- # Skip the welcome message and only include the last few exchanges
108
- recent_history = self.conversation_history[1:] if len(self.conversation_history) > 1 else []
109
 
110
- # Limit to last MAX_HISTORY_MESSAGES exchanges
111
- if len(recent_history) > MAX_HISTORY_MESSAGES * 2:
112
- recent_history = recent_history[-MAX_HISTORY_MESSAGES*2:]
113
 
114
- formatted_history = ""
115
- for entry in recent_history:
116
- role = "User" if entry["role"] == "user" else "Assistant"
117
- # Truncate very long messages to avoid token limits
118
- content = entry["content"]
119
- if len(content) > 500: # Limit message length
120
- content = content[:500] + "..."
121
- formatted_history += f"{role}: {content}\n\n"
122
 
123
- return formatted_history
124
 
125
- def is_expired(self, timeout_seconds=3600):
126
- """Check if the session has been inactive for too long."""
127
- return (time.time() - self.last_activity) > timeout_seconds
128
-
129
- # Session manager to handle multiple users
130
- class SessionManager:
131
- def __init__(self):
132
- """Initialize the session manager."""
133
- self.sessions = {}
134
- self.session_timeout = 3600 # 1 hour timeout
135
 
136
- def get_session(self, session_id):
137
- """Get an existing session or create a new one."""
138
- # Clean expired sessions first
139
- self._clean_expired_sessions()
140
 
141
- # Create new session if needed
142
- if session_id not in self.sessions:
143
- self.sessions[session_id] = UserSession(session_id, llm)
144
 
145
- return self.sessions[session_id]
146
 
147
- def _clean_expired_sessions(self):
148
- """Remove expired sessions to free up memory."""
149
- expired_keys = []
150
- for key, session in self.sessions.items():
151
- if session.is_expired(self.session_timeout):
152
- expired_keys.append(key)
153
 
154
- for key in expired_keys:
155
- del self.sessions[key]
156
 
157
- # Initialize the session manager
158
- session_manager = SessionManager()
159
 
160
- def initialize_assistant():
161
- """Initialize the assistant with necessary components and configurations."""
162
- global llm, embed_model, vectorstore, retriever, rag_chain
163
 
164
- # Initialize API key - try both possible key names
165
- groq_api_key = os.environ.get('GBV') or os.environ.get('GBV')
166
- if not groq_api_key:
167
- print("WARNING: No GROQ API key found in userdata.")
168
 
169
- # Initialize LLM - Default to Llama model which is more widely available
170
- llm = ChatGroq(
171
- model="llama-3.3-70b-versatile", # More reliable than whisper model
172
- api_key=groq_api_key
173
- )
174
 
175
- # Set up embedding model
176
- try:
177
- embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
178
- except Exception as e:
179
- # Fallback to smaller model
180
- embed_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
181
 
182
- # Process data and create vector store
183
- print("Processing data files...")
184
- data = process_data_files()
185
 
186
- print("Creating vector store...")
187
- vectorstore = create_vectorstore(data)
188
- retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
189
 
190
- # Create RAG chain
191
- print("Setting up RAG chain...")
192
- rag_chain = create_rag_chain()
193
 
194
- print(f"βœ… {APP_NAME} initialized successfully")
195
 
196
- def process_data_files():
197
- """Process all data files from the specified folder."""
198
- context_data = []
199
 
200
- try:
201
- if not os.path.exists(DATA_FOLDER):
202
- print(f"WARNING: Data folder does not exist: {DATA_FOLDER}")
203
- return context_data
204
 
205
- # Get list of data files
206
- all_files = os.listdir(DATA_FOLDER)
207
- data_files = [f for f in all_files if f.lower().endswith(('.csv', '.xlsx', '.xls'))]
208
 
209
- if not data_files:
210
- print(f"WARNING: No data files found in: {DATA_FOLDER}")
211
- return context_data
212
 
213
- # Process each file
214
- for index, file_name in enumerate(data_files, 1):
215
- print(f"Processing file {index}/{len(data_files)}: {file_name}")
216
- file_path = os.path.join(DATA_FOLDER, file_name)
217
 
218
- try:
219
- # Read file based on extension
220
- if file_name.lower().endswith('.csv'):
221
- df = pd.read_csv(file_path)
222
- else:
223
- df = pd.read_excel(file_path)
224
 
225
- # Check if column 3 exists (source data is in third column)
226
- if df.shape[1] > 2:
227
- column_data = df.iloc[:, 2].dropna().astype(str).tolist()
228
 
229
- # Each row becomes one chunk with metadata
230
- for i, text in enumerate(column_data):
231
- if text and len(text.strip()) > 0:
232
- context_data.append({
233
- "page_content": text,
234
- "metadata": {
235
- "source": file_name,
236
- "row": i+1
237
- }
238
- })
239
- else:
240
- print(f"WARNING: File {file_name} has fewer than 3 columns.")
241
 
242
- except Exception as e:
243
- print(f"ERROR processing file {file_name}: {e}")
244
 
245
- print(f"βœ… Created {len(context_data)} chunks from {len(data_files)} files.")
246
 
247
- except Exception as e:
248
- print(f"ERROR accessing data folder: {e}")
249
 
250
- return context_data
251
 
252
- def create_vectorstore(data):
253
- """
254
- Creates and returns a Chroma vector store populated with the provided data.
255
-
256
- Parameters:
257
- data (list): A list of dictionaries, each containing 'page_content' and 'metadata'.
258
-
259
- Returns:
260
- Chroma: The populated Chroma vector store instance.
261
- """
262
- # Initialize the vector store
263
- vectorstore = Chroma(
264
- collection_name=COLLECTION_NAME,
265
- embedding_function=embed_model,
266
- persist_directory="./"
267
- )
268
-
269
- if not data:
270
- print("⚠️ No data provided. Returning an empty vector store.")
271
- return vectorstore
272
-
273
- try:
274
- # Extract text and metadata from the data
275
- texts = [doc["page_content"] for doc in data]
276
-
277
- # Add the texts and metadata to the vector store
278
- vectorstore.add_texts(texts)
279
- except Exception as e:
280
- print(f"❌ Failed to add documents to vector store: {e}")
281
-
282
- # Fix: Return vectorstore instead of vs
283
- return vectorstore # Changed from 'return vs' to 'return vectorstore'
284
-
285
-
286
- def create_rag_chain():
287
- """Create the RAG chain for processing user queries."""
288
- # Define the prompt template
289
- template = """
290
- You are a compassionate and supportive AI assistant specializing in helping individuals affected by Gender-Based Violence (GBV). Your responses must be based EXCLUSIVELY on the information provided in the context. Your primary goal is to provide emotionally intelligent support while maintaining appropriate boundaries.
291
 
292
- **Previous conversation:** {conversation_history}
293
- **Context information:** {context}
294
- **User's Question:** {question}
295
 
296
- When responding follow these guidelines:
297
 
298
- 1. **Strict Context Adherence**
299
- - Only use information that appears in the provided {context}
300
- - If the answer is not found in the context, state "I don't have that information in my available resources" rather than generating a response
301
 
302
- 2. **Personalized Communication**
303
- - Avoid contractions (e.g., use I am instead of I'm)
304
- - Incorporate thoughtful pauses or reflective questions when the conversation involves difficult topics
305
- - Use selective emojis (😊, πŸ€—, ❀️) only when tone-appropriate and not during crisis discussions
306
- - Balance warmth with professionalism
307
 
308
- 3. **Emotional Intelligence**
309
- - Validate feelings without judgment
310
- - Offer reassurance when appropriate, always centered on empowerment
311
- - Adjust your tone based on the emotional state conveyed
312
 
313
- 4. **Conversation Management**
314
- - Refer to {conversation_history} to maintain continuity and avoid repetition
315
- - Use clear paragraph breaks for readability
316
 
317
- 5. **Information Delivery**
318
- - Extract only relevant information from {context} that directly addresses the question
319
- - Present information in accessible, non-technical language
320
- - When information is unavailable, respond with: "I don't have that specific information right now, {first_name}. Would it be helpful if I focus on [alternative support option]?"
321
 
322
- 6. **Safety and Ethics**
323
- - Do not generate any speculative content or advice not supported by the context
324
- - If the context contains safety information, prioritize sharing that information
325
 
326
- Your response must come entirely from the provided context, maintaining the supportive tone while never introducing information from outside the provided materials.
327
- **Context:** {context}
328
- **User's Question:** {question}
329
- **Your Response:**
330
- """
331
 
332
 
333
- rag_prompt = PromptTemplate.from_template(template)
334
 
335
- def get_context_and_question(query_with_session):
336
- # Extract query and session_id
337
- query = query_with_session["query"]
338
- session_id = query_with_session["session_id"]
339
 
340
- # Get the user session
341
- session = session_manager.get_session(session_id)
342
- user_info = session.get_user()
343
- first_name = user_info.get("Nickname", "User")
344
- conversation_hist = session.get_formatted_history()
345
 
346
- try:
347
- # Retrieve relevant documents
348
- retrieved_docs = retriever.invoke(query)
349
- context_str = format_context(retrieved_docs)
350
- except Exception as e:
351
- print(f"ERROR retrieving documents: {e}")
352
- context_str = "No relevant information found."
353
 
354
- # Return the combined inputs for the prompt
355
- return {
356
- "context": context_str,
357
- "question": query,
358
- "first_name": first_name,
359
- "conversation_history": conversation_hist
360
- }
361
 
362
- # Build the chain
363
- try:
364
- chain = (
365
- RunnablePassthrough()
366
- | get_context_and_question
367
- | rag_prompt
368
- | llm
369
- | StrOutputParser()
370
- )
371
- return chain
372
- except Exception as e:
373
- print(f"ERROR creating RAG chain: {e}")
374
 
375
- # Return a simple function as fallback
376
- def fallback_chain(query_with_session):
377
- session_id = query_with_session["session_id"]
378
- session = session_manager.get_session(session_id)
379
- nickname = session.get_user().get("Nickname", "there")
380
- return f"I'm here to help you, {nickname}, but I'm experiencing some technical difficulties right now. Please try again shortly."
381
 
382
- return fallback_chain
383
-
384
- def format_context(retrieved_docs):
385
- """Format retrieved documents into a string context."""
386
- if not retrieved_docs:
387
- return "No relevant information available."
388
- return "\n\n".join([doc.page_content for doc in retrieved_docs])
389
-
390
- def rag_memory_stream(message, history, session_id):
391
- """Process user message and generate response with memory."""
392
- # Get the user session
393
- session = session_manager.get_session(session_id)
394
 
395
- # Add user message to history
396
- session.add_to_history("user", message)
397
 
398
- try:
399
- # Get response from RAG chain
400
- print(f"Processing message for session {session_id}: {message[:50]}...")
401
 
402
- # Pass both query and session_id to the chain
403
- response = rag_chain.invoke({
404
- "query": message,
405
- "session_id": session_id
406
- })
407
 
408
- print(f"Generated response: {response[:50]}...")
409
 
410
- # Add assistant response to history
411
- session.add_to_history("assistant", response)
412
 
413
- # Yield the response
414
- yield response
415
 
416
- except Exception as e:
417
- import traceback
418
- print(f"ERROR in rag_memory_stream: {e}")
419
- print(f"Detailed error: {traceback.format_exc()}")
420
 
421
- nickname = session.get_user().get("Nickname", "there")
422
- error_msg = f"I'm sorry, {nickname}. I encountered an error processing your request. Let's try a different question."
423
- session.add_to_history("assistant", error_msg)
424
- yield error_msg
425
-
426
- def collect_user_info(nickname, session_id):
427
- """Store user details and initialize session."""
428
- if not nickname or nickname.strip() == "":
429
- return "Nickname is required to proceed.", gr.update(visible=False), gr.update(visible=True), []
430
-
431
- # Store user info for chat session
432
- user_info = {
433
- "Nickname": nickname.strip(),
434
- "timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
435
- }
436
-
437
- # Get the session and set user info
438
- session = session_manager.get_session(session_id)
439
- session.set_user(user_info)
440
-
441
- # Generate welcome message
442
- welcome_message = session.get_welcome_message()
443
-
444
- # Return welcome message and update UI
445
- return welcome_message, gr.update(visible=True), gr.update(visible=False), [(None, welcome_message)]
446
-
447
- def get_css():
448
- """Define CSS for the UI."""
449
- return """
450
- :root {
451
- --primary: #4E6BBF;
452
- --primary-light: #697BBF;
453
- --text-primary: #333333;
454
- --text-secondary: #666666;
455
- --background: #F9FAFC;
456
- --card-bg: #FFFFFF;
457
- --border: #E1E5F0;
458
- --shadow: rgba(0, 0, 0, 0.05);
459
- }
460
-
461
- body, .gradio-container {
462
- margin: 0;
463
- padding: 0;
464
- width: 100vw;
465
- height: 100vh;
466
- display: flex;
467
- flex-direction: column;
468
- justify-content: center;
469
- align-items: center;
470
- background: var(--background);
471
- color: var(--text-primary);
472
- font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
473
- }
474
-
475
- .gradio-container {
476
- max-width: 100%;
477
- max-height: 100%;
478
- }
479
-
480
- .gr-box {
481
- background: var(--card-bg);
482
- color: var(--text-primary);
483
- border-radius: 12px;
484
- padding: 2rem;
485
- border: 1px solid var(--border);
486
- box-shadow: 0 4px 12px var(--shadow);
487
- }
488
-
489
- .gr-button-primary {
490
- background: var(--primary);
491
- color: white;
492
- padding: 12px 24px;
493
- border-radius: 8px;
494
- transition: all 0.3s ease;
495
- border: none;
496
- font-weight: bold;
497
- }
498
-
499
- .gr-button-primary:hover {
500
- transform: translateY(-1px);
501
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
502
- background: var(--primary-light);
503
- }
504
-
505
- footer {
506
- text-align: center;
507
- color: var(--text-secondary);
508
- padding: 1rem;
509
- font-size: 0.9em;
510
- }
511
-
512
- .gr-markdown h2 {
513
- color: var(--primary);
514
- margin-bottom: 0.5rem;
515
- font-size: 1.8em;
516
- }
517
-
518
- .gr-markdown h3 {
519
- color: var(--text-secondary);
520
- margin-bottom: 1.5rem;
521
- font-weight: normal;
522
- }
523
-
524
- #chatbot_container .chat-title h1,
525
- #chatbot_container .empty-chatbot {
526
- color: var(--primary);
527
- }
528
-
529
- #input_nickname {
530
- padding: 12px;
531
- border-radius: 8px;
532
- border: 1px solid var(--border);
533
- background: var(--card-bg);
534
- transition: all 0.3s ease;
535
- }
536
 
537
- #input_nickname:focus {
538
- border-color: var(--primary);
539
- box-shadow: 0 0 0 2px rgba(78, 107, 191, 0.2);
540
- outline: none;
541
- }
542
-
543
- .chatbot-container .message.user {
544
- background: #E8F0FE;
545
- border-radius: 12px 12px 0 12px;
546
- }
547
-
548
- .chatbot-container .message.bot {
549
- background: #F5F7FF;
550
- border-radius: 12px 12px 12px 0;
551
- }
552
- """
553
-
554
- def create_ui():
555
- """Create and configure the Gradio UI."""
556
- with gr.Blocks(css=get_css(), theme=gr.themes.Soft()) as demo:
557
- # Create a unique session ID for this browser tab
558
- session_id = gr.State(value=f"session_{int(time.time())}_{os.urandom(4).hex()}")
559
 
560
- # Registration section
561
- with gr.Column(visible=True, elem_id="registration_container") as registration_container:
562
- gr.Markdown(f"## Welcome to {APP_NAME}")
563
- gr.Markdown("### Your privacy is important to us. Please provide a nickname to continue.")
564
-
565
- with gr.Row():
566
- first_name = gr.Textbox(
567
- label="Nickname",
568
- placeholder="Enter your nickname",
569
- scale=1,
570
- elem_id="input_nickname"
571
- )
572
-
573
- with gr.Row():
574
- submit_btn = gr.Button("Start Chatting", variant="primary", scale=2)
575
-
576
- response_message = gr.Markdown()
577
-
578
- # Chatbot section (initially hidden)
579
- with gr.Column(visible=False, elem_id="chatbot_container") as chatbot_container:
580
- # Create a custom chat interface to pass session_id to our function
581
- chatbot = gr.Chatbot(
582
- elem_id="chatbot",
583
- height=500,
584
- show_label=False
585
- )
586
 
587
- with gr.Row():
588
- msg = gr.Textbox(
589
- placeholder="Type your message here...",
590
- show_label=False,
591
- container=False,
592
- scale=9
593
- )
594
- submit = gr.Button("Send", scale=1, variant="primary")
595
 
596
- examples = gr.Examples(
597
- examples=[
598
- "What resources are available for GBV victims?",
599
- "How can I report an incident?",
600
- "What are my legal rights?",
601
- "I need help, what should I do first?"
602
- ],
603
- inputs=msg
604
- )
605
-
606
- # Footer with version info
607
- gr.Markdown(f"{APP_NAME} {APP_VERSION} Β© 2025")
608
 
609
- # Handle chat message submission
610
- def respond(message, chat_history, session_id):
611
- bot_message = ""
612
- for chunk in rag_memory_stream(message, chat_history, session_id):
613
- bot_message += chunk
614
- chat_history.append((message, bot_message))
615
- return "", chat_history
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
616
 
617
- msg.submit(respond, [msg, chatbot, session_id], [msg, chatbot])
618
- submit.click(respond, [msg, chatbot, session_id], [msg, chatbot])
619
 
620
- # Handle user registration
621
- submit_btn.click(
622
- collect_user_info,
623
- inputs=[first_name, session_id],
624
- outputs=[response_message, chatbot_container, registration_container, chatbot]
625
- )
626
 
627
- return demo
628
 
629
- def launch_app():
630
- """Launch the Gradio interface."""
631
- ui = create_ui()
632
- ui.launch(share=True)
633
 
634
- # Main execution
635
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
636
  try:
637
- # Initialize and launch the assistant
638
- initialize_assistant()
639
- launch_app()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
640
  except Exception as e:
641
- import traceback
642
- print(f"❌ Fatal error initializing GBV Assistant: {e}")
643
- print(traceback.format_exc())
644
-
645
- # Create a minimal emergency UI to display the error
646
- with gr.Blocks() as error_demo:
647
- gr.Markdown("## System Error")
648
- gr.Markdown(f"An error occurred while initializing the application: {str(e)}")
649
- gr.Markdown("Please check your configuration and try again.")
650
-
651
- error_demo.launch(share=True, inbrowser=True, debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import os
2
+ # import time
3
+ # import pandas as pd
4
+ # import gradio as gr
5
+ # from langchain_groq import ChatGroq
6
+ # from langchain_huggingface import HuggingFaceEmbeddings
7
+ # from langchain_community.vectorstores import Chroma
8
+ # from langchain_core.prompts import PromptTemplate
9
+ # from langchain_core.output_parsers import StrOutputParser
10
+ # from langchain_core.runnables import RunnablePassthrough
11
+ # from PyPDF2 import PdfReader
12
+
13
+
14
+ # # Configuration constants
15
+ # COLLECTION_NAME = "GBVRS"
16
+ # DATA_FOLDER = "./"
17
+ # APP_VERSION = "v1.0.0"
18
+ # APP_NAME = "Ijwi ry'Ubufasha"
19
+ # MAX_HISTORY_MESSAGES = 8 # Limit history to avoid token limits
20
+
21
+ # # Global variables for application state
22
+ # llm = None
23
+ # embed_model = None
24
+ # vectorstore = None
25
+ # retriever = None
26
+ # rag_chain = None
27
+
28
+ # # User session management
29
+ # class UserSession:
30
+ # def __init__(self, session_id, llm):
31
+ # """Initialize a user session with unique ID and language model."""
32
+ # self.session_id = session_id
33
+ # self.user_info = {"Nickname": "Guest"}
34
+ # self.conversation_history = []
35
+ # self.llm = llm
36
+ # self.welcome_message = None
37
+ # self.last_activity = time.time()
38
 
39
+ # def set_user(self, user_info):
40
+ # """Set user information and generate welcome message."""
41
+ # self.user_info = user_info
42
+ # self.generate_welcome_message()
43
 
44
+ # # Initialize conversation history with welcome message
45
+ # welcome = self.get_welcome_message()
46
+ # self.conversation_history = [
47
+ # {"role": "assistant", "content": welcome},
48
+ # ]
49
 
50
+ # def get_user(self):
51
+ # """Get current user information."""
52
+ # return self.user_info
53
 
54
+ # def generate_welcome_message(self):
55
+ # """Generate a dynamic welcome message using the LLM."""
56
+ # try:
57
+ # nickname = self.user_info.get("Nickname", "Guest")
58
 
59
+ # # Use the LLM to generate the message
60
+ # prompt = (
61
+ # f"Create a brief and warm welcome message for {nickname} that's about 1-2 sentences. "
62
+ # f"Emphasize this is a safe space for discussing gender-based violence issues "
63
+ # f"and that we provide support and resources. Keep it warm and reassuring."
64
+ # )
65
 
66
+ # response = self.llm.invoke(prompt)
67
+ # welcome = response.content.strip()
68
 
69
+ # # Format the message with HTML styling
70
+ # self.welcome_message = (
71
+ # f"<div style='font-size: 18px; color: #4E6BBF;'>"
72
+ # f"{welcome}"
73
+ # f"</div>"
74
+ # )
75
+ # except Exception as e:
76
+ # # Fallback welcome message
77
+ # nickname = self.user_info.get("Nickname", "Guest")
78
+ # self.welcome_message = (
79
+ # f"<div style='font-size: 18px; color: #4E6BBF;'>"
80
+ # f"Welcome, {nickname}! You're in a safe space. We're here to provide support with "
81
+ # f"gender-based violence issues and connect you with resources that can help."
82
+ # f"</div>"
83
+ # )
84
 
85
+ # def get_welcome_message(self):
86
+ # """Get the formatted welcome message."""
87
+ # if not self.welcome_message:
88
+ # self.generate_welcome_message()
89
+ # return self.welcome_message
90
 
91
+ # def add_to_history(self, role, message):
92
+ # """Add a message to the conversation history."""
93
+ # self.conversation_history.append({"role": role, "content": message})
94
+ # self.last_activity = time.time()
95
 
96
+ # # Trim history if it gets too long
97
+ # if len(self.conversation_history) > MAX_HISTORY_MESSAGES * 2: # Keep pairs of messages
98
+ # # Keep the first message (welcome) and the most recent messages
99
+ # self.conversation_history = [self.conversation_history[0]] + self.conversation_history[-MAX_HISTORY_MESSAGES*2+1:]
100
 
101
+ # def get_conversation_history(self):
102
+ # """Get the full conversation history."""
103
+ # return self.conversation_history
104
 
105
+ # def get_formatted_history(self):
106
+ # """Get conversation history formatted as a string for the LLM."""
107
+ # # Skip the welcome message and only include the last few exchanges
108
+ # recent_history = self.conversation_history[1:] if len(self.conversation_history) > 1 else []
109
 
110
+ # # Limit to last MAX_HISTORY_MESSAGES exchanges
111
+ # if len(recent_history) > MAX_HISTORY_MESSAGES * 2:
112
+ # recent_history = recent_history[-MAX_HISTORY_MESSAGES*2:]
113
 
114
+ # formatted_history = ""
115
+ # for entry in recent_history:
116
+ # role = "User" if entry["role"] == "user" else "Assistant"
117
+ # # Truncate very long messages to avoid token limits
118
+ # content = entry["content"]
119
+ # if len(content) > 500: # Limit message length
120
+ # content = content[:500] + "..."
121
+ # formatted_history += f"{role}: {content}\n\n"
122
 
123
+ # return formatted_history
124
 
125
+ # def is_expired(self, timeout_seconds=3600):
126
+ # """Check if the session has been inactive for too long."""
127
+ # return (time.time() - self.last_activity) > timeout_seconds
128
+
129
+ # # Session manager to handle multiple users
130
+ # class SessionManager:
131
+ # def __init__(self):
132
+ # """Initialize the session manager."""
133
+ # self.sessions = {}
134
+ # self.session_timeout = 3600 # 1 hour timeout
135
 
136
+ # def get_session(self, session_id):
137
+ # """Get an existing session or create a new one."""
138
+ # # Clean expired sessions first
139
+ # self._clean_expired_sessions()
140
 
141
+ # # Create new session if needed
142
+ # if session_id not in self.sessions:
143
+ # self.sessions[session_id] = UserSession(session_id, llm)
144
 
145
+ # return self.sessions[session_id]
146
 
147
+ # def _clean_expired_sessions(self):
148
+ # """Remove expired sessions to free up memory."""
149
+ # expired_keys = []
150
+ # for key, session in self.sessions.items():
151
+ # if session.is_expired(self.session_timeout):
152
+ # expired_keys.append(key)
153
 
154
+ # for key in expired_keys:
155
+ # del self.sessions[key]
156
 
157
+ # # Initialize the session manager
158
+ # session_manager = SessionManager()
159
 
160
+ # def initialize_assistant():
161
+ # """Initialize the assistant with necessary components and configurations."""
162
+ # global llm, embed_model, vectorstore, retriever, rag_chain
163
 
164
+ # # Initialize API key - try both possible key names
165
+ # groq_api_key = os.environ.get('GBV') or os.environ.get('GBV')
166
+ # if not groq_api_key:
167
+ # print("WARNING: No GROQ API key found in userdata.")
168
 
169
+ # # Initialize LLM - Default to Llama model which is more widely available
170
+ # llm = ChatGroq(
171
+ # model="llama-3.3-70b-versatile", # More reliable than whisper model
172
+ # api_key=groq_api_key
173
+ # )
174
 
175
+ # # Set up embedding model
176
+ # try:
177
+ # embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
178
+ # except Exception as e:
179
+ # # Fallback to smaller model
180
+ # embed_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
181
 
182
+ # # Process data and create vector store
183
+ # print("Processing data files...")
184
+ # data = process_data_files()
185
 
186
+ # print("Creating vector store...")
187
+ # vectorstore = create_vectorstore(data)
188
+ # retriever = vectorstore.as_retriever(search_kwargs={"k": 3})
189
 
190
+ # # Create RAG chain
191
+ # print("Setting up RAG chain...")
192
+ # rag_chain = create_rag_chain()
193
 
194
+ # print(f"βœ… {APP_NAME} initialized successfully")
195
 
196
+ # def process_data_files():
197
+ # """Process all data files from the specified folder."""
198
+ # context_data = []
199
 
200
+ # try:
201
+ # if not os.path.exists(DATA_FOLDER):
202
+ # print(f"WARNING: Data folder does not exist: {DATA_FOLDER}")
203
+ # return context_data
204
 
205
+ # # Get list of data files
206
+ # all_files = os.listdir(DATA_FOLDER)
207
+ # data_files = [f for f in all_files if f.lower().endswith(('.csv', '.xlsx', '.xls'))]
208
 
209
+ # if not data_files:
210
+ # print(f"WARNING: No data files found in: {DATA_FOLDER}")
211
+ # return context_data
212
 
213
+ # # Process each file
214
+ # for index, file_name in enumerate(data_files, 1):
215
+ # print(f"Processing file {index}/{len(data_files)}: {file_name}")
216
+ # file_path = os.path.join(DATA_FOLDER, file_name)
217
 
218
+ # try:
219
+ # # Read file based on extension
220
+ # if file_name.lower().endswith('.csv'):
221
+ # df = pd.read_csv(file_path)
222
+ # else:
223
+ # df = pd.read_excel(file_path)
224
 
225
+ # # Check if column 3 exists (source data is in third column)
226
+ # if df.shape[1] > 2:
227
+ # column_data = df.iloc[:, 2].dropna().astype(str).tolist()
228
 
229
+ # # Each row becomes one chunk with metadata
230
+ # for i, text in enumerate(column_data):
231
+ # if text and len(text.strip()) > 0:
232
+ # context_data.append({
233
+ # "page_content": text,
234
+ # "metadata": {
235
+ # "source": file_name,
236
+ # "row": i+1
237
+ # }
238
+ # })
239
+ # else:
240
+ # print(f"WARNING: File {file_name} has fewer than 3 columns.")
241
 
242
+ # except Exception as e:
243
+ # print(f"ERROR processing file {file_name}: {e}")
244
 
245
+ # print(f"βœ… Created {len(context_data)} chunks from {len(data_files)} files.")
246
 
247
+ # except Exception as e:
248
+ # print(f"ERROR accessing data folder: {e}")
249
 
250
+ # return context_data
251
 
252
+ # def create_vectorstore(data):
253
+ # """
254
+ # Creates and returns a Chroma vector store populated with the provided data.
255
+
256
+ # Parameters:
257
+ # data (list): A list of dictionaries, each containing 'page_content' and 'metadata'.
258
+
259
+ # Returns:
260
+ # Chroma: The populated Chroma vector store instance.
261
+ # """
262
+ # # Initialize the vector store
263
+ # vectorstore = Chroma(
264
+ # collection_name=COLLECTION_NAME,
265
+ # embedding_function=embed_model,
266
+ # persist_directory="./"
267
+ # )
268
+
269
+ # if not data:
270
+ # print("⚠️ No data provided. Returning an empty vector store.")
271
+ # return vectorstore
272
+
273
+ # try:
274
+ # # Extract text and metadata from the data
275
+ # texts = [doc["page_content"] for doc in data]
276
+
277
+ # # Add the texts and metadata to the vector store
278
+ # vectorstore.add_texts(texts)
279
+ # except Exception as e:
280
+ # print(f"❌ Failed to add documents to vector store: {e}")
281
+
282
+ # # Fix: Return vectorstore instead of vs
283
+ # return vectorstore # Changed from 'return vs' to 'return vectorstore'
284
+
285
+
286
+ # def create_rag_chain():
287
+ # """Create the RAG chain for processing user queries."""
288
+ # # Define the prompt template
289
+ # template = """
290
+ # You are a compassionate and supportive AI assistant specializing in helping individuals affected by Gender-Based Violence (GBV). Your responses must be based EXCLUSIVELY on the information provided in the context. Your primary goal is to provide emotionally intelligent support while maintaining appropriate boundaries.
291
 
292
+ # **Previous conversation:** {conversation_history}
293
+ # **Context information:** {context}
294
+ # **User's Question:** {question}
295
 
296
+ # When responding follow these guidelines:
297
 
298
+ # 1. **Strict Context Adherence**
299
+ # - Only use information that appears in the provided {context}
300
+ # - If the answer is not found in the context, state "I don't have that information in my available resources" rather than generating a response
301
 
302
+ # 2. **Personalized Communication**
303
+ # - Avoid contractions (e.g., use I am instead of I'm)
304
+ # - Incorporate thoughtful pauses or reflective questions when the conversation involves difficult topics
305
+ # - Use selective emojis (😊, πŸ€—, ❀️) only when tone-appropriate and not during crisis discussions
306
+ # - Balance warmth with professionalism
307
 
308
+ # 3. **Emotional Intelligence**
309
+ # - Validate feelings without judgment
310
+ # - Offer reassurance when appropriate, always centered on empowerment
311
+ # - Adjust your tone based on the emotional state conveyed
312
 
313
+ # 4. **Conversation Management**
314
+ # - Refer to {conversation_history} to maintain continuity and avoid repetition
315
+ # - Use clear paragraph breaks for readability
316
 
317
+ # 5. **Information Delivery**
318
+ # - Extract only relevant information from {context} that directly addresses the question
319
+ # - Present information in accessible, non-technical language
320
+ # - When information is unavailable, respond with: "I don't have that specific information right now, {first_name}. Would it be helpful if I focus on [alternative support option]?"
321
 
322
+ # 6. **Safety and Ethics**
323
+ # - Do not generate any speculative content or advice not supported by the context
324
+ # - If the context contains safety information, prioritize sharing that information
325
 
326
+ # Your response must come entirely from the provided context, maintaining the supportive tone while never introducing information from outside the provided materials.
327
+ # **Context:** {context}
328
+ # **User's Question:** {question}
329
+ # **Your Response:**
330
+ # """
331
 
332
 
333
+ # rag_prompt = PromptTemplate.from_template(template)
334
 
335
+ # def get_context_and_question(query_with_session):
336
+ # # Extract query and session_id
337
+ # query = query_with_session["query"]
338
+ # session_id = query_with_session["session_id"]
339
 
340
+ # # Get the user session
341
+ # session = session_manager.get_session(session_id)
342
+ # user_info = session.get_user()
343
+ # first_name = user_info.get("Nickname", "User")
344
+ # conversation_hist = session.get_formatted_history()
345
 
346
+ # try:
347
+ # # Retrieve relevant documents
348
+ # retrieved_docs = retriever.invoke(query)
349
+ # context_str = format_context(retrieved_docs)
350
+ # except Exception as e:
351
+ # print(f"ERROR retrieving documents: {e}")
352
+ # context_str = "No relevant information found."
353
 
354
+ # # Return the combined inputs for the prompt
355
+ # return {
356
+ # "context": context_str,
357
+ # "question": query,
358
+ # "first_name": first_name,
359
+ # "conversation_history": conversation_hist
360
+ # }
361
 
362
+ # # Build the chain
363
+ # try:
364
+ # chain = (
365
+ # RunnablePassthrough()
366
+ # | get_context_and_question
367
+ # | rag_prompt
368
+ # | llm
369
+ # | StrOutputParser()
370
+ # )
371
+ # return chain
372
+ # except Exception as e:
373
+ # print(f"ERROR creating RAG chain: {e}")
374
 
375
+ # # Return a simple function as fallback
376
+ # def fallback_chain(query_with_session):
377
+ # session_id = query_with_session["session_id"]
378
+ # session = session_manager.get_session(session_id)
379
+ # nickname = session.get_user().get("Nickname", "there")
380
+ # return f"I'm here to help you, {nickname}, but I'm experiencing some technical difficulties right now. Please try again shortly."
381
 
382
+ # return fallback_chain
383
+
384
+ # def format_context(retrieved_docs):
385
+ # """Format retrieved documents into a string context."""
386
+ # if not retrieved_docs:
387
+ # return "No relevant information available."
388
+ # return "\n\n".join([doc.page_content for doc in retrieved_docs])
389
+
390
+ # def rag_memory_stream(message, history, session_id):
391
+ # """Process user message and generate response with memory."""
392
+ # # Get the user session
393
+ # session = session_manager.get_session(session_id)
394
 
395
+ # # Add user message to history
396
+ # session.add_to_history("user", message)
397
 
398
+ # try:
399
+ # # Get response from RAG chain
400
+ # print(f"Processing message for session {session_id}: {message[:50]}...")
401
 
402
+ # # Pass both query and session_id to the chain
403
+ # response = rag_chain.invoke({
404
+ # "query": message,
405
+ # "session_id": session_id
406
+ # })
407
 
408
+ # print(f"Generated response: {response[:50]}...")
409
 
410
+ # # Add assistant response to history
411
+ # session.add_to_history("assistant", response)
412
 
413
+ # # Yield the response
414
+ # yield response
415
 
416
+ # except Exception as e:
417
+ # import traceback
418
+ # print(f"ERROR in rag_memory_stream: {e}")
419
+ # print(f"Detailed error: {traceback.format_exc()}")
420
 
421
+ # nickname = session.get_user().get("Nickname", "there")
422
+ # error_msg = f"I'm sorry, {nickname}. I encountered an error processing your request. Let's try a different question."
423
+ # session.add_to_history("assistant", error_msg)
424
+ # yield error_msg
425
+
426
+ # def collect_user_info(nickname, session_id):
427
+ # """Store user details and initialize session."""
428
+ # if not nickname or nickname.strip() == "":
429
+ # return "Nickname is required to proceed.", gr.update(visible=False), gr.update(visible=True), []
430
+
431
+ # # Store user info for chat session
432
+ # user_info = {
433
+ # "Nickname": nickname.strip(),
434
+ # "timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
435
+ # }
436
+
437
+ # # Get the session and set user info
438
+ # session = session_manager.get_session(session_id)
439
+ # session.set_user(user_info)
440
+
441
+ # # Generate welcome message
442
+ # welcome_message = session.get_welcome_message()
443
+
444
+ # # Return welcome message and update UI
445
+ # return welcome_message, gr.update(visible=True), gr.update(visible=False), [(None, welcome_message)]
446
+
447
+ # def get_css():
448
+ # """Define CSS for the UI."""
449
+ # return """
450
+ # :root {
451
+ # --primary: #4E6BBF;
452
+ # --primary-light: #697BBF;
453
+ # --text-primary: #333333;
454
+ # --text-secondary: #666666;
455
+ # --background: #F9FAFC;
456
+ # --card-bg: #FFFFFF;
457
+ # --border: #E1E5F0;
458
+ # --shadow: rgba(0, 0, 0, 0.05);
459
+ # }
460
+
461
+ # body, .gradio-container {
462
+ # margin: 0;
463
+ # padding: 0;
464
+ # width: 100vw;
465
+ # height: 100vh;
466
+ # display: flex;
467
+ # flex-direction: column;
468
+ # justify-content: center;
469
+ # align-items: center;
470
+ # background: var(--background);
471
+ # color: var(--text-primary);
472
+ # font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
473
+ # }
474
+
475
+ # .gradio-container {
476
+ # max-width: 100%;
477
+ # max-height: 100%;
478
+ # }
479
+
480
+ # .gr-box {
481
+ # background: var(--card-bg);
482
+ # color: var(--text-primary);
483
+ # border-radius: 12px;
484
+ # padding: 2rem;
485
+ # border: 1px solid var(--border);
486
+ # box-shadow: 0 4px 12px var(--shadow);
487
+ # }
488
+
489
+ # .gr-button-primary {
490
+ # background: var(--primary);
491
+ # color: white;
492
+ # padding: 12px 24px;
493
+ # border-radius: 8px;
494
+ # transition: all 0.3s ease;
495
+ # border: none;
496
+ # font-weight: bold;
497
+ # }
498
+
499
+ # .gr-button-primary:hover {
500
+ # transform: translateY(-1px);
501
+ # box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
502
+ # background: var(--primary-light);
503
+ # }
504
+
505
+ # footer {
506
+ # text-align: center;
507
+ # color: var(--text-secondary);
508
+ # padding: 1rem;
509
+ # font-size: 0.9em;
510
+ # }
511
+
512
+ # .gr-markdown h2 {
513
+ # color: var(--primary);
514
+ # margin-bottom: 0.5rem;
515
+ # font-size: 1.8em;
516
+ # }
517
+
518
+ # .gr-markdown h3 {
519
+ # color: var(--text-secondary);
520
+ # margin-bottom: 1.5rem;
521
+ # font-weight: normal;
522
+ # }
523
+
524
+ # #chatbot_container .chat-title h1,
525
+ # #chatbot_container .empty-chatbot {
526
+ # color: var(--primary);
527
+ # }
528
+
529
+ # #input_nickname {
530
+ # padding: 12px;
531
+ # border-radius: 8px;
532
+ # border: 1px solid var(--border);
533
+ # background: var(--card-bg);
534
+ # transition: all 0.3s ease;
535
+ # }
536
 
537
+ # #input_nickname:focus {
538
+ # border-color: var(--primary);
539
+ # box-shadow: 0 0 0 2px rgba(78, 107, 191, 0.2);
540
+ # outline: none;
541
+ # }
542
+
543
+ # .chatbot-container .message.user {
544
+ # background: #E8F0FE;
545
+ # border-radius: 12px 12px 0 12px;
546
+ # }
547
+
548
+ # .chatbot-container .message.bot {
549
+ # background: #F5F7FF;
550
+ # border-radius: 12px 12px 12px 0;
551
+ # }
552
+ # """
553
+
554
+ # def create_ui():
555
+ # """Create and configure the Gradio UI."""
556
+ # with gr.Blocks(css=get_css(), theme=gr.themes.Soft()) as demo:
557
+ # # Create a unique session ID for this browser tab
558
+ # session_id = gr.State(value=f"session_{int(time.time())}_{os.urandom(4).hex()}")
559
 
560
+ # # Registration section
561
+ # with gr.Column(visible=True, elem_id="registration_container") as registration_container:
562
+ # gr.Markdown(f"## Welcome to {APP_NAME}")
563
+ # gr.Markdown("### Your privacy is important to us. Please provide a nickname to continue.")
564
+
565
+ # with gr.Row():
566
+ # first_name = gr.Textbox(
567
+ # label="Nickname",
568
+ # placeholder="Enter your nickname",
569
+ # scale=1,
570
+ # elem_id="input_nickname"
571
+ # )
572
+
573
+ # with gr.Row():
574
+ # submit_btn = gr.Button("Start Chatting", variant="primary", scale=2)
575
+
576
+ # response_message = gr.Markdown()
577
+
578
+ # # Chatbot section (initially hidden)
579
+ # with gr.Column(visible=False, elem_id="chatbot_container") as chatbot_container:
580
+ # # Create a custom chat interface to pass session_id to our function
581
+ # chatbot = gr.Chatbot(
582
+ # elem_id="chatbot",
583
+ # height=500,
584
+ # show_label=False
585
+ # )
586
 
587
+ # with gr.Row():
588
+ # msg = gr.Textbox(
589
+ # placeholder="Type your message here...",
590
+ # show_label=False,
591
+ # container=False,
592
+ # scale=9
593
+ # )
594
+ # submit = gr.Button("Send", scale=1, variant="primary")
595
 
596
+ # examples = gr.Examples(
597
+ # examples=[
598
+ # "What resources are available for GBV victims?",
599
+ # "How can I report an incident?",
600
+ # "What are my legal rights?",
601
+ # "I need help, what should I do first?"
602
+ # ],
603
+ # inputs=msg
604
+ # )
605
+
606
+ # # Footer with version info
607
+ # gr.Markdown(f"{APP_NAME} {APP_VERSION} Β© 2025")
608
 
609
+ # # Handle chat message submission
610
+ # def respond(message, chat_history, session_id):
611
+ # bot_message = ""
612
+ # for chunk in rag_memory_stream(message, chat_history, session_id):
613
+ # bot_message += chunk
614
+ # chat_history.append((message, bot_message))
615
+ # return "", chat_history
616
+
617
+ # msg.submit(respond, [msg, chatbot, session_id], [msg, chatbot])
618
+ # submit.click(respond, [msg, chatbot, session_id], [msg, chatbot])
619
+
620
+ # # Handle user registration
621
+ # submit_btn.click(
622
+ # collect_user_info,
623
+ # inputs=[first_name, session_id],
624
+ # outputs=[response_message, chatbot_container, registration_container, chatbot]
625
+ # )
626
+
627
+ # return demo
628
+
629
+ # def launch_app():
630
+ # """Launch the Gradio interface."""
631
+ # ui = create_ui()
632
+ # ui.launch(share=True)
633
+
634
+ # # Main execution
635
+ # if __name__ == "__main__":
636
+ # try:
637
+ # # Initialize and launch the assistant
638
+ # initialize_assistant()
639
+ # launch_app()
640
+ # except Exception as e:
641
+ # import traceback
642
+ # print(f"❌ Fatal error initializing GBV Assistant: {e}")
643
+ # print(traceback.format_exc())
644
+
645
+ # # Create a minimal emergency UI to display the error
646
+ # with gr.Blocks() as error_demo:
647
+ # gr.Markdown("## System Error")
648
+ # gr.Markdown(f"An error occurred while initializing the application: {str(e)}")
649
+ # gr.Markdown("Please check your configuration and try again.")
650
 
651
+ # error_demo.launch(share=True, inbrowser=True, debug=True)
 
652
 
 
 
 
 
 
 
653
 
 
654
 
655
+ ############################################################################################################
 
 
 
656
 
657
+
658
+ import os
659
+ from langchain_groq import ChatGroq
660
+ from langchain.prompts import ChatPromptTemplate, PromptTemplate
661
+ from langchain.output_parsers import ResponseSchema, StructuredOutputParser
662
+ from urllib.parse import urljoin, urlparse
663
+ import requests
664
+ from io import BytesIO
665
+ from langchain_chroma import Chroma
666
+ import requests
667
+ from bs4 import BeautifulSoup
668
+ from langchain_core.prompts import ChatPromptTemplate
669
+ import gradio as gr
670
+ from PyPDF2 import PdfReader
671
+ from langchain_huggingface import HuggingFaceEmbeddings
672
+
673
+ groq_api_key= os.environ.get('GBV')
674
+
675
+ embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
676
+
677
+ def scrape_websites(base_urls):
678
  try:
679
+ visited_links = set() # To avoid revisiting the same link
680
+ content_by_url = {} # Store content from each URL
681
+
682
+ for base_url in base_urls:
683
+ if not base_url.strip():
684
+ continue # Skip empty or invalid URLs
685
+
686
+ print(f"Scraping base URL: {base_url}")
687
+ html_content = fetch_page_content(base_url)
688
+ if html_content:
689
+ cleaned_content = clean_body_content(html_content)
690
+ content_by_url[base_url] = cleaned_content
691
+ visited_links.add(base_url)
692
+
693
+ # Extract and process all internal links
694
+ soup = BeautifulSoup(html_content, "html.parser")
695
+ links = extract_internal_links(base_url, soup)
696
+
697
+ for link in links:
698
+ if link not in visited_links:
699
+ print(f"Scraping link: {link}")
700
+ page_content = fetch_page_content(link)
701
+ if page_content:
702
+ cleaned_content = clean_body_content(page_content)
703
+ content_by_url[link] = cleaned_content
704
+ visited_links.add(link)
705
+
706
+ # If the link is a PDF file, extract its content
707
+ if link.lower().endswith('.pdf'):
708
+ print(f"Extracting PDF content from: {link}")
709
+ pdf_content = extract_pdf_text(link)
710
+ if pdf_content:
711
+ content_by_url[link] = pdf_content
712
+
713
+ return content_by_url
714
+
715
  except Exception as e:
716
+ print(f"Error during scraping: {e}")
717
+ return {}
718
+
719
+
720
+ def fetch_page_content(url):
721
+ try:
722
+ response = requests.get(url, timeout=10)
723
+ response.raise_for_status()
724
+ return response.text
725
+ except requests.exceptions.RequestException as e:
726
+ print(f"Error fetching {url}: {e}")
727
+ return None
728
+
729
+
730
+ def extract_internal_links(base_url, soup):
731
+ links = set()
732
+ for anchor in soup.find_all("a", href=True):
733
+ href = anchor["href"]
734
+ full_url = urljoin(base_url, href)
735
+ if is_internal_link(base_url, full_url):
736
+ links.add(full_url)
737
+ return links
738
+
739
+
740
+ def is_internal_link(base_url, link_url):
741
+ base_netloc = urlparse(base_url).netloc
742
+ link_netloc = urlparse(link_url).netloc
743
+ return base_netloc == link_netloc
744
+
745
+
746
+ def extract_pdf_text(pdf_url):
747
+ try:
748
+ response = requests.get(pdf_url)
749
+ response.raise_for_status()
750
+ with BytesIO(response.content) as file:
751
+ reader = PdfReader(file)
752
+ pdf_text = ""
753
+ for page in reader.pages:
754
+ pdf_text += page.extract_text()
755
+
756
+ return pdf_text if pdf_text else None
757
+ except requests.exceptions.RequestException as e:
758
+ print(f"Error fetching PDF {pdf_url}: {e}")
759
+ return None
760
+ except Exception as e:
761
+ print(f"Error reading PDF {pdf_url}: {e}")
762
+ return None
763
+
764
+
765
+ def clean_body_content(html_content):
766
+ soup = BeautifulSoup(html_content, "html.parser")
767
+
768
+
769
+ for script_or_style in soup(["script", "style"]):
770
+ script_or_style.extract()
771
+
772
+
773
+ cleaned_content = soup.get_text(separator="\n")
774
+ cleaned_content = "\n".join(
775
+ line.strip() for line in cleaned_content.splitlines() if line.strip()
776
+ )
777
+ return cleaned_content
778
+
779
+
780
+ if __name__ == "__main__":
781
+ website = ["https://www.rra.gov.rw/en/publications",
782
+ "https://www.rra.gov.rw/en/customs-services",
783
+
784
+
785
+ ]
786
+ all_content = scrape_websites(website)
787
+
788
+ temp_list = []
789
+ for url, content in all_content.items():
790
+ temp_list.append((url, content))
791
+
792
+
793
+ processed_texts = []
794
+
795
+
796
+ for element in temp_list:
797
+ if isinstance(element, tuple):
798
+ url, content = element
799
+ processed_texts.append(f"url: {url}, content: {content}")
800
+ elif isinstance(element, str):
801
+ processed_texts.append(element)
802
+ else:
803
+ processed_texts.append(str(element))
804
+
805
+ def chunk_string(s, chunk_size=1000):
806
+ return [s[i:i+chunk_size] for i in range(0, len(s), chunk_size)]
807
+
808
+ chunked_texts = []
809
+
810
+ for text in processed_texts:
811
+ chunked_texts.extend(chunk_string(text))
812
+
813
+
814
+
815
+
816
+
817
+ vectorstore = Chroma(
818
+ collection_name="R_R_A",
819
+ embedding_function=embed_model,
820
+ persist_directory="./",
821
+ )
822
+
823
+ vectorstore.get().keys()
824
+
825
+ vectorstore.add_texts(chunked_texts)
826
+
827
+
828
+ # template = ("""
829
+ # You are a friendly and intelligent chatbot designed to assist users in a conversational and human-like manner. Your goal is to provide accurate, helpful, and engaging responses from the provided context: {context} while maintaining a natural tone. Follow these guidelines:
830
+
831
+ # 1. **Greetings:** If the user greets you (e.g., "Morning," "Hello," "Hi"), respond warmly and acknowledge the greeting. For example:
832
+ # - "😊 Good morning! How can I assist you today?"
833
+ # - "Hello! What can I do for you? πŸš€"
834
+ # 2. **Extract Information:** If the user asks for specific information, extract only the relevant details from the provided context: {context}.
835
+ # 3. **Human-like Interaction:** Respond in a warm, conversational tone. Use emojis occasionally to make the interaction more engaging (e.g., 😊, πŸš€).
836
+ # 4. **Stay Updated:** Acknowledge the current date and time to show you are aware of real-time updates.
837
+ # 5. **No Extra Content:** If no information matches the user's request, respond politely: "I don't have that information at the moment, but I'm happy to help with something else! 😊"
838
+ # 6. **Personalized Interaction:** Use the user's historical interactions (if available) to tailor your responses and make the conversation more personalized.
839
+ # 7. **Direct Data Only:** If the user requests specific data, provide only the requested information without additional explanations unless asked.
840
+
841
+ # Context: {context}
842
+ # User's Question: {question}
843
+ # Your Response:
844
+ # """)
845
+
846
+ template = ("""
847
+ You are a friendly, intelligent, and conversational AI assistant designed to provide accurate, engaging, and human-like responses based on the given context. Your goal is to extract relevant details from the provided context: {context} and assist the user effectively. Follow these guidelines:
848
+
849
+ 1. **Warm & Natural Interaction**
850
+ - If the user greets you (e.g., "Hello," "Hi," "Good morning"), respond warmly and acknowledge them.
851
+ - Example responses:
852
+ - "😊 Good morning! How can I assist you today?"
853
+ - "Hello! What can I do for you? πŸš€"
854
+
855
+ 2. **Precise Information Extraction**
856
+ - Provide only the relevant details from the given context: {context}.
857
+ - Do not generate extra content or assumptions beyond the provided information.
858
+
859
+ 3. **Conversational & Engaging Tone**
860
+ - Keep responses friendly, natural, and engaging.
861
+ - Use occasional emojis (e.g., 😊, πŸš€) to make interactions more lively.
862
+
863
+ 4. **Awareness of Real-Time Context**
864
+ - If necessary, acknowledge the current date and time to show awareness of real-world updates.
865
+
866
+ 5. **Handling Missing Information**
867
+ - If no relevant information exists in the context, respond politely:
868
+ - "I don't have that information at the moment, but I'm happy to help with something else! 😊"
869
+
870
+ 6. **Personalized Interaction**
871
+ - If user history is available, tailor responses based on their previous interactions for a more natural and engaging conversation.
872
+
873
+ 7. **Direct, Concise Responses**
874
+ - If the user requests specific data, provide only the requested details without unnecessary explanations unless asked.
875
+
876
+ 8. **Extracting Relevant Links**
877
+ - If the user asks for a link related to their request `{question}`, extract the most relevant URL from `{context}` and provide it directly.
878
+ - Example response:
879
+ - "Here is the link you requested: [URL]"
880
+
881
+ **Context:** {context}
882
+ **User's Question:** {question}
883
+ **Your Response:**
884
+ """)
885
+
886
+
887
+ rag_prompt = PromptTemplate.from_template(template)
888
+
889
+ retriever = vectorstore.as_retriever()
890
+
891
+ from langchain_core.output_parsers import StrOutputParser
892
+ from langchain_core.runnables import RunnablePassthrough
893
+
894
+ llm = ChatGroq(model="llama-3.3-70b-versatile", api_key=groq_api_key )
895
+
896
+ rag_chain = (
897
+ {"context": retriever, "question": RunnablePassthrough()}
898
+ | rag_prompt
899
+ | llm
900
+ | StrOutputParser()
901
+ )
902
+
903
+
904
+ # Define the RAG memory stream function
905
+ def rag_memory_stream(message, history):
906
+ partial_text = ""
907
+ for new_text in rag_chain.stream(message): # Replace with actual streaming logic
908
+ partial_text += new_text
909
+ yield partial_text
910
+
911
+ # Title with emojis
912
+ title = "RRA Chatbot"
913
+
914
+ # Short description for the examples section
915
+ examples = [
916
+ " Can you help me with the tax rates on vehicle importation?",
917
+ " What is TIN deregistration? What about Tax account deactivation?",
918
+ "When do I receive my registration certificate?"
919
+ ]
920
+
921
+
922
+ # Custom CSS for styling the interface
923
+ custom_css = """
924
+ body {
925
+ font-family: "Arial", serif;
926
+ }
927
+ .gradio-container {
928
+ font-family: "Times New Roman", serif;
929
+ }
930
+ .gr-button {
931
+ background-color: #007bff; /* Blue button */
932
+ color: white;
933
+ border: none;
934
+ border-radius: 5px;
935
+ font-size: 16px;
936
+ padding: 10px 20px;
937
+ cursor: pointer;
938
+ }
939
+ .gr-textbox:focus, .gr-button:focus {
940
+ outline: none; /* Remove outline focus for a cleaner look */
941
+ }
942
+
943
+ /* Custom CSS for the examples section */
944
+ .gr-examples {
945
+ font-size: 30px; /* Increase font size of examples */
946
+ background-color: #f9f9f9; /* Light background color */
947
+ border-radius: 30px; /* Rounded corners */
948
+ }
949
+
950
+ .gr-examples .example {
951
+ background-color: white; /* White background for each example */
952
+ cursor: pointer; /* Change cursor to pointer on hover */
953
+ transition: background-color 0.3s ease; /* Smooth hover effect */
954
+ }
955
+
956
+ .gr-examples .example:hover {
957
+ background-color: #f1f1f1; /* Light gray background on hover */
958
+ }
959
+ """
960
+
961
+ # Create the Chat Interface
962
+ demo = gr.ChatInterface(
963
+ fn=rag_memory_stream,
964
+ title=title,
965
+ examples=examples, # Display the short description and example questions
966
+ fill_height=True,
967
+ theme="soft",
968
+ css=custom_css, # Apply the custom CSS
969
+ )
970
+
971
+ # Launch the app
972
+ if __name__ == "__main__":
973
+ demo.launch(share=True, inbrowser=True, debug=True)