Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,659 +1,932 @@
|
|
| 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 |
-
#
|
| 396 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 588 |
-
#
|
| 589 |
-
#
|
| 590 |
-
#
|
| 591 |
-
#
|
| 592 |
-
#
|
| 593 |
-
#
|
| 594 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 595 |
|
| 596 |
-
#
|
| 597 |
-
#
|
| 598 |
-
#
|
| 599 |
-
#
|
| 600 |
-
#
|
| 601 |
-
#
|
| 602 |
-
#
|
| 603 |
-
# inputs=msg
|
| 604 |
-
# )
|
| 605 |
-
|
| 606 |
-
# # Footer with version info
|
| 607 |
-
# gr.Markdown(f"{APP_NAME} {APP_VERSION} © 2025")
|
| 608 |
|
| 609 |
-
#
|
| 610 |
-
#
|
| 611 |
-
|
| 612 |
-
#
|
| 613 |
-
#
|
| 614 |
-
#
|
| 615 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 616 |
|
| 617 |
-
#
|
| 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 |
-
#
|
| 630 |
-
# """Launch the Gradio interface."""
|
| 631 |
-
# ui = create_ui()
|
| 632 |
-
# ui.launch(share=True)
|
| 633 |
|
| 634 |
-
|
| 635 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 636 |
# try:
|
| 637 |
-
# #
|
| 638 |
-
#
|
| 639 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 640 |
# except Exception as e:
|
| 641 |
-
#
|
| 642 |
-
#
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
#
|
| 646 |
-
#
|
| 647 |
-
#
|
| 648 |
-
#
|
| 649 |
-
#
|
| 650 |
-
|
| 651 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 652 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
|
| 654 |
|
| 655 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 656 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 657 |
|
| 658 |
import os
|
| 659 |
from langchain_groq import ChatGroq
|
|
@@ -669,9 +942,14 @@ from langchain_core.prompts import ChatPromptTemplate
|
|
| 669 |
import gradio as gr
|
| 670 |
from PyPDF2 import PdfReader
|
| 671 |
from langchain_huggingface import HuggingFaceEmbeddings
|
|
|
|
|
|
|
|
|
|
| 672 |
|
| 673 |
-
|
|
|
|
| 674 |
|
|
|
|
| 675 |
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
| 676 |
|
| 677 |
def scrape_websites(base_urls):
|
|
@@ -764,12 +1042,12 @@ def extract_pdf_text(pdf_url):
|
|
| 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()
|
|
@@ -777,54 +1055,91 @@ def clean_body_content(html_content):
|
|
| 777 |
return cleaned_content
|
| 778 |
|
| 779 |
|
| 780 |
-
if __name__ == "__main__":
|
| 781 |
-
website = ["https://haguruka.org.rw/"
|
| 782 |
-
|
| 783 |
-
]
|
| 784 |
-
all_content = scrape_websites(website)
|
| 785 |
-
|
| 786 |
-
temp_list = []
|
| 787 |
-
for url, content in all_content.items():
|
| 788 |
-
temp_list.append((url, content))
|
| 789 |
-
|
| 790 |
-
|
| 791 |
-
processed_texts = []
|
| 792 |
-
|
| 793 |
-
|
| 794 |
-
for element in temp_list:
|
| 795 |
-
if isinstance(element, tuple):
|
| 796 |
-
url, content = element
|
| 797 |
-
processed_texts.append(f"url: {url}, content: {content}")
|
| 798 |
-
elif isinstance(element, str):
|
| 799 |
-
processed_texts.append(element)
|
| 800 |
-
else:
|
| 801 |
-
processed_texts.append(str(element))
|
| 802 |
-
|
| 803 |
def chunk_string(s, chunk_size=1000):
|
| 804 |
return [s[i:i+chunk_size] for i in range(0, len(s), chunk_size)]
|
| 805 |
|
| 806 |
-
chunked_texts = []
|
| 807 |
|
| 808 |
-
|
| 809 |
-
|
|
|
|
|
|
|
|
|
|
| 810 |
|
|
|
|
|
|
|
|
|
|
| 811 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 812 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 813 |
|
| 814 |
|
| 815 |
-
|
| 816 |
-
|
| 817 |
-
embedding_function=embed_model,
|
| 818 |
-
persist_directory="./",
|
| 819 |
-
)
|
| 820 |
-
|
| 821 |
-
vectorstore.get().keys()
|
| 822 |
-
|
| 823 |
-
vectorstore.add_texts(chunked_texts)
|
| 824 |
-
|
| 825 |
|
| 826 |
-
template
|
| 827 |
-
|
|
|
|
| 828 |
|
| 829 |
1. **Warm & Natural Interaction**
|
| 830 |
- If the user greets you (e.g., "Hello," "Hi," "Good morning"), respond warmly and acknowledge them.
|
|
@@ -833,7 +1148,7 @@ template = ("""
|
|
| 833 |
- "Hello! What can I do for you? 🚀"
|
| 834 |
|
| 835 |
2. **Precise Information Extraction**
|
| 836 |
-
- Provide only the relevant details from the given context
|
| 837 |
- Do not generate extra content or assumptions beyond the provided information.
|
| 838 |
|
| 839 |
3. **Conversational & Engaging Tone**
|
|
@@ -848,49 +1163,134 @@ template = ("""
|
|
| 848 |
- "I don't have that information at the moment, but I'm happy to help with something else! 😊"
|
| 849 |
|
| 850 |
6. **Personalized Interaction**
|
| 851 |
-
-
|
| 852 |
|
| 853 |
7. **Direct, Concise Responses**
|
| 854 |
- If the user requests specific data, provide only the requested details without unnecessary explanations unless asked.
|
| 855 |
|
| 856 |
8. **Extracting Relevant Links**
|
| 857 |
-
- If the user asks for a link related to their request
|
| 858 |
- Example response:
|
| 859 |
- "Here is the link you requested: [URL]"
|
| 860 |
|
| 861 |
-
**Context:** {context}
|
| 862 |
-
|
| 863 |
-
**
|
| 864 |
-
|
| 865 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 866 |
|
|
|
|
| 867 |
rag_prompt = PromptTemplate.from_template(template)
|
| 868 |
|
| 869 |
-
|
| 870 |
-
|
| 871 |
-
from langchain_core.output_parsers import StrOutputParser
|
| 872 |
-
from langchain_core.runnables import RunnablePassthrough
|
| 873 |
|
| 874 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 875 |
|
| 876 |
-
|
| 877 |
-
|
| 878 |
-
| rag_prompt
|
| 879 |
-
| llm
|
| 880 |
-
| StrOutputParser()
|
| 881 |
-
)
|
| 882 |
|
|
|
|
|
|
|
| 883 |
|
| 884 |
-
# Define the
|
| 885 |
-
def rag_memory_stream(message, history):
|
| 886 |
-
|
| 887 |
-
|
| 888 |
-
|
| 889 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 890 |
|
| 891 |
# Title with emojis
|
| 892 |
-
title = "GBVR Chatbot"
|
| 893 |
-
|
| 894 |
|
| 895 |
# Custom CSS for styling the interface
|
| 896 |
custom_css = """
|
|
@@ -912,18 +1312,9 @@ body {
|
|
| 912 |
.gr-textbox:focus, .gr-button:focus {
|
| 913 |
outline: none; /* Remove outline focus for a cleaner look */
|
| 914 |
}
|
| 915 |
-
|
| 916 |
"""
|
| 917 |
|
| 918 |
-
# Create the Chat Interface
|
| 919 |
-
demo = gr.ChatInterface(
|
| 920 |
-
fn=rag_memory_stream,
|
| 921 |
-
title=title,
|
| 922 |
-
fill_height=True,
|
| 923 |
-
theme="soft",
|
| 924 |
-
css=custom_css, # Apply the custom CSS
|
| 925 |
-
)
|
| 926 |
-
|
| 927 |
# Launch the app
|
| 928 |
if __name__ == "__main__":
|
|
|
|
| 929 |
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://haguruka.org.rw/"
|
| 782 |
+
|
| 783 |
+
# ]
|
| 784 |
+
# all_content = scrape_websites(website)
|
| 785 |
+
|
| 786 |
+
# temp_list = []
|
| 787 |
+
# for url, content in all_content.items():
|
| 788 |
+
# temp_list.append((url, content))
|
| 789 |
+
|
| 790 |
+
|
| 791 |
+
# processed_texts = []
|
| 792 |
+
|
| 793 |
+
|
| 794 |
+
# for element in temp_list:
|
| 795 |
+
# if isinstance(element, tuple):
|
| 796 |
+
# url, content = element
|
| 797 |
+
# processed_texts.append(f"url: {url}, content: {content}")
|
| 798 |
+
# elif isinstance(element, str):
|
| 799 |
+
# processed_texts.append(element)
|
| 800 |
+
# else:
|
| 801 |
+
# processed_texts.append(str(element))
|
| 802 |
+
|
| 803 |
+
# def chunk_string(s, chunk_size=1000):
|
| 804 |
+
# return [s[i:i+chunk_size] for i in range(0, len(s), chunk_size)]
|
| 805 |
+
|
| 806 |
+
# chunked_texts = []
|
| 807 |
+
|
| 808 |
+
# for text in processed_texts:
|
| 809 |
+
# chunked_texts.extend(chunk_string(text))
|
| 810 |
+
|
| 811 |
+
|
| 812 |
+
|
| 813 |
+
|
| 814 |
+
|
| 815 |
+
# vectorstore = Chroma(
|
| 816 |
+
# collection_name="GBVR_Dataset",
|
| 817 |
+
# embedding_function=embed_model,
|
| 818 |
+
# persist_directory="./",
|
| 819 |
+
# )
|
| 820 |
+
|
| 821 |
+
# vectorstore.get().keys()
|
| 822 |
+
|
| 823 |
+
# vectorstore.add_texts(chunked_texts)
|
| 824 |
+
|
| 825 |
+
|
| 826 |
+
# template = ("""
|
| 827 |
+
# 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:
|
| 828 |
+
|
| 829 |
+
# 1. **Warm & Natural Interaction**
|
| 830 |
+
# - If the user greets you (e.g., "Hello," "Hi," "Good morning"), respond warmly and acknowledge them.
|
| 831 |
+
# - Example responses:
|
| 832 |
+
# - "😊 Good morning! How can I assist you today?"
|
| 833 |
+
# - "Hello! What can I do for you? 🚀"
|
| 834 |
+
|
| 835 |
+
# 2. **Precise Information Extraction**
|
| 836 |
+
# - Provide only the relevant details from the given context: {context}.
|
| 837 |
+
# - Do not generate extra content or assumptions beyond the provided information.
|
| 838 |
+
|
| 839 |
+
# 3. **Conversational & Engaging Tone**
|
| 840 |
+
# - Keep responses friendly, natural, and engaging.
|
| 841 |
+
# - Use occasional emojis (e.g., 😊, 🚀) to make interactions more lively.
|
| 842 |
+
|
| 843 |
+
# 4. **Awareness of Real-Time Context**
|
| 844 |
+
# - If necessary, acknowledge the current date and time to show awareness of real-world updates.
|
| 845 |
+
|
| 846 |
+
# 5. **Handling Missing Information**
|
| 847 |
+
# - If no relevant information exists in the context, respond politely:
|
| 848 |
+
# - "I don't have that information at the moment, but I'm happy to help with something else! 😊"
|
| 849 |
+
|
| 850 |
+
# 6. **Personalized Interaction**
|
| 851 |
+
# - If user history is available, tailor responses based on their previous interactions for a more natural and engaging conversation.
|
| 852 |
+
|
| 853 |
+
# 7. **Direct, Concise Responses**
|
| 854 |
+
# - If the user requests specific data, provide only the requested details without unnecessary explanations unless asked.
|
| 855 |
+
|
| 856 |
+
# 8. **Extracting Relevant Links**
|
| 857 |
+
# - If the user asks for a link related to their request `{question}`, extract the most relevant URL from `{context}` and provide it directly.
|
| 858 |
+
# - Example response:
|
| 859 |
+
# - "Here is the link you requested: [URL]"
|
| 860 |
+
|
| 861 |
+
# **Context:** {context}
|
| 862 |
+
# **User's Question:** {question}
|
| 863 |
+
# **Your Response:**
|
| 864 |
+
# """)
|
| 865 |
+
|
| 866 |
+
|
| 867 |
+
# rag_prompt = PromptTemplate.from_template(template)
|
| 868 |
+
|
| 869 |
+
# retriever = vectorstore.as_retriever()
|
| 870 |
+
|
| 871 |
+
# from langchain_core.output_parsers import StrOutputParser
|
| 872 |
+
# from langchain_core.runnables import RunnablePassthrough
|
| 873 |
+
|
| 874 |
+
# llm = ChatGroq(model="llama-3.3-70b-versatile", api_key=groq_api_key )
|
| 875 |
+
|
| 876 |
+
# rag_chain = (
|
| 877 |
+
# {"context": retriever, "question": RunnablePassthrough()}
|
| 878 |
+
# | rag_prompt
|
| 879 |
+
# | llm
|
| 880 |
+
# | StrOutputParser()
|
| 881 |
+
# )
|
| 882 |
+
|
| 883 |
+
|
| 884 |
+
# # Define the RAG memory stream function
|
| 885 |
+
# def rag_memory_stream(message, history):
|
| 886 |
+
# partial_text = ""
|
| 887 |
+
# for new_text in rag_chain.stream(message): # Replace with actual streaming logic
|
| 888 |
+
# partial_text += new_text
|
| 889 |
+
# yield partial_text
|
| 890 |
+
|
| 891 |
+
# # Title with emojis
|
| 892 |
+
# title = "GBVR Chatbot"
|
| 893 |
+
|
| 894 |
+
|
| 895 |
+
# # Custom CSS for styling the interface
|
| 896 |
+
# custom_css = """
|
| 897 |
+
# body {
|
| 898 |
+
# font-family: "Arial", serif;
|
| 899 |
+
# }
|
| 900 |
+
# .gradio-container {
|
| 901 |
+
# font-family: "Times New Roman", serif;
|
| 902 |
+
# }
|
| 903 |
+
# .gr-button {
|
| 904 |
+
# background-color: #007bff; /* Blue button */
|
| 905 |
+
# color: white;
|
| 906 |
+
# border: none;
|
| 907 |
+
# border-radius: 5px;
|
| 908 |
+
# font-size: 16px;
|
| 909 |
+
# padding: 10px 20px;
|
| 910 |
+
# cursor: pointer;
|
| 911 |
+
# }
|
| 912 |
+
# .gr-textbox:focus, .gr-button:focus {
|
| 913 |
+
# outline: none; /* Remove outline focus for a cleaner look */
|
| 914 |
+
# }
|
| 915 |
+
|
| 916 |
+
# """
|
| 917 |
+
|
| 918 |
+
# # Create the Chat Interface
|
| 919 |
+
# demo = gr.ChatInterface(
|
| 920 |
+
# fn=rag_memory_stream,
|
| 921 |
+
# title=title,
|
| 922 |
+
# fill_height=True,
|
| 923 |
+
# theme="soft",
|
| 924 |
+
# css=custom_css, # Apply the custom CSS
|
| 925 |
+
# )
|
| 926 |
+
|
| 927 |
+
# # Launch the app
|
| 928 |
+
# if __name__ == "__main__":
|
| 929 |
+
# demo.launch(share=True, inbrowser=True, debug=True)
|
| 930 |
|
| 931 |
import os
|
| 932 |
from langchain_groq import ChatGroq
|
|
|
|
| 942 |
import gradio as gr
|
| 943 |
from PyPDF2 import PdfReader
|
| 944 |
from langchain_huggingface import HuggingFaceEmbeddings
|
| 945 |
+
from langchain_core.messages import HumanMessage, AIMessage
|
| 946 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 947 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 948 |
|
| 949 |
+
# Set up environment variables
|
| 950 |
+
groq_api_key = os.environ.get('GBV')
|
| 951 |
|
| 952 |
+
# Initialize embedding model
|
| 953 |
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
| 954 |
|
| 955 |
def scrape_websites(base_urls):
|
|
|
|
| 1042 |
|
| 1043 |
def clean_body_content(html_content):
|
| 1044 |
soup = BeautifulSoup(html_content, "html.parser")
|
|
|
|
| 1045 |
|
| 1046 |
+
# Remove scripts and styles
|
| 1047 |
for script_or_style in soup(["script", "style"]):
|
| 1048 |
script_or_style.extract()
|
|
|
|
| 1049 |
|
| 1050 |
+
# Get cleaned text
|
| 1051 |
cleaned_content = soup.get_text(separator="\n")
|
| 1052 |
cleaned_content = "\n".join(
|
| 1053 |
line.strip() for line in cleaned_content.splitlines() if line.strip()
|
|
|
|
| 1055 |
return cleaned_content
|
| 1056 |
|
| 1057 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1058 |
def chunk_string(s, chunk_size=1000):
|
| 1059 |
return [s[i:i+chunk_size] for i in range(0, len(s), chunk_size)]
|
| 1060 |
|
|
|
|
| 1061 |
|
| 1062 |
+
# Setup vectorstore for RAG
|
| 1063 |
+
def setup_vectorstore():
|
| 1064 |
+
if __name__ == "__main__":
|
| 1065 |
+
website = ["https://haguruka.org.rw/"]
|
| 1066 |
+
all_content = scrape_websites(website)
|
| 1067 |
|
| 1068 |
+
temp_list = []
|
| 1069 |
+
for url, content in all_content.items():
|
| 1070 |
+
temp_list.append((url, content))
|
| 1071 |
|
| 1072 |
+
processed_texts = []
|
| 1073 |
+
|
| 1074 |
+
for element in temp_list:
|
| 1075 |
+
if isinstance(element, tuple):
|
| 1076 |
+
url, content = element
|
| 1077 |
+
processed_texts.append(f"url: {url}, content: {content}")
|
| 1078 |
+
elif isinstance(element, str):
|
| 1079 |
+
processed_texts.append(element)
|
| 1080 |
+
else:
|
| 1081 |
+
processed_texts.append(str(element))
|
| 1082 |
+
|
| 1083 |
+
chunked_texts = []
|
| 1084 |
+
for text in processed_texts:
|
| 1085 |
+
chunked_texts.extend(chunk_string(text))
|
| 1086 |
+
|
| 1087 |
+
vectorstore = Chroma(
|
| 1088 |
+
collection_name="GBVR_Dataset",
|
| 1089 |
+
embedding_function=embed_model,
|
| 1090 |
+
persist_directory="./",
|
| 1091 |
+
)
|
| 1092 |
+
|
| 1093 |
+
vectorstore.add_texts(chunked_texts)
|
| 1094 |
+
return vectorstore
|
| 1095 |
+
else:
|
| 1096 |
+
# If imported as a module, just load the existing vectorstore
|
| 1097 |
+
vectorstore = Chroma(
|
| 1098 |
+
collection_name="GBVR_Dataset",
|
| 1099 |
+
embedding_function=embed_model,
|
| 1100 |
+
persist_directory="./",
|
| 1101 |
+
)
|
| 1102 |
+
return vectorstore
|
| 1103 |
+
|
| 1104 |
+
|
| 1105 |
+
# Session Manager class to handle conversation history
|
| 1106 |
+
class SessionManager:
|
| 1107 |
+
def __init__(self):
|
| 1108 |
+
self.sessions = {}
|
| 1109 |
|
| 1110 |
+
def get_session(self, session_id):
|
| 1111 |
+
if session_id not in self.sessions:
|
| 1112 |
+
self.sessions[session_id] = []
|
| 1113 |
+
return self.sessions[session_id]
|
| 1114 |
+
|
| 1115 |
+
def add_message(self, session_id, role, content):
|
| 1116 |
+
session = self.get_session(session_id)
|
| 1117 |
+
if role == "human":
|
| 1118 |
+
session.append(HumanMessage(content=content))
|
| 1119 |
+
elif role == "ai":
|
| 1120 |
+
session.append(AIMessage(content=content))
|
| 1121 |
+
|
| 1122 |
+
def get_history_as_string(self, session_id, max_turns=5):
|
| 1123 |
+
"""Convert recent conversation history to string format for context"""
|
| 1124 |
+
session = self.get_session(session_id)
|
| 1125 |
+
|
| 1126 |
+
# Get the most recent conversations (limited to max_turns)
|
| 1127 |
+
recent_messages = session[-max_turns*2:] if len(session) > max_turns*2 else session
|
| 1128 |
+
|
| 1129 |
+
history_str = ""
|
| 1130 |
+
for msg in recent_messages:
|
| 1131 |
+
role = "User" if isinstance(msg, HumanMessage) else "Assistant"
|
| 1132 |
+
history_str += f"{role}: {msg.content}\n"
|
| 1133 |
+
|
| 1134 |
+
return history_str.strip()
|
| 1135 |
|
| 1136 |
|
| 1137 |
+
# Initialize session manager
|
| 1138 |
+
session_manager = SessionManager()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1139 |
|
| 1140 |
+
# Modified template to include conversation history
|
| 1141 |
+
template = """
|
| 1142 |
+
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 and assist the user effectively. Follow these guidelines:
|
| 1143 |
|
| 1144 |
1. **Warm & Natural Interaction**
|
| 1145 |
- If the user greets you (e.g., "Hello," "Hi," "Good morning"), respond warmly and acknowledge them.
|
|
|
|
| 1148 |
- "Hello! What can I do for you? 🚀"
|
| 1149 |
|
| 1150 |
2. **Precise Information Extraction**
|
| 1151 |
+
- Provide only the relevant details from the given context.
|
| 1152 |
- Do not generate extra content or assumptions beyond the provided information.
|
| 1153 |
|
| 1154 |
3. **Conversational & Engaging Tone**
|
|
|
|
| 1163 |
- "I don't have that information at the moment, but I'm happy to help with something else! 😊"
|
| 1164 |
|
| 1165 |
6. **Personalized Interaction**
|
| 1166 |
+
- Use the conversation history to provide more personalized and contextually relevant responses.
|
| 1167 |
|
| 1168 |
7. **Direct, Concise Responses**
|
| 1169 |
- If the user requests specific data, provide only the requested details without unnecessary explanations unless asked.
|
| 1170 |
|
| 1171 |
8. **Extracting Relevant Links**
|
| 1172 |
+
- If the user asks for a link related to their request, extract the most relevant URL from the context and provide it directly.
|
| 1173 |
- Example response:
|
| 1174 |
- "Here is the link you requested: [URL]"
|
| 1175 |
|
| 1176 |
+
**Context from knowledge base:** {context}
|
| 1177 |
+
|
| 1178 |
+
**Previous conversation history:**
|
| 1179 |
+
{history}
|
| 1180 |
+
|
| 1181 |
+
**Current User's Question:** {question}
|
| 1182 |
+
|
| 1183 |
+
**Your Response:**
|
| 1184 |
+
"""
|
| 1185 |
|
| 1186 |
+
# Create prompt template with history
|
| 1187 |
rag_prompt = PromptTemplate.from_template(template)
|
| 1188 |
|
| 1189 |
+
# Initialize Groq LLM
|
| 1190 |
+
llm = ChatGroq(model="llama-3.3-70b-versatile", api_key=groq_api_key)
|
|
|
|
|
|
|
| 1191 |
|
| 1192 |
+
# Define the RAG chain with session history
|
| 1193 |
+
def get_rag_chain(vectorstore):
|
| 1194 |
+
retriever = vectorstore.as_retriever()
|
| 1195 |
+
|
| 1196 |
+
def rag_chain_with_history(query, session_id):
|
| 1197 |
+
# Get conversation history
|
| 1198 |
+
history = session_manager.get_history_as_string(session_id)
|
| 1199 |
+
|
| 1200 |
+
# Get relevant documents from retriever
|
| 1201 |
+
retrieved_docs = retriever.invoke(query)
|
| 1202 |
+
context = "\n".join([doc.page_content for doc in retrieved_docs])
|
| 1203 |
+
|
| 1204 |
+
# Create the prompt with context and history
|
| 1205 |
+
prompt = rag_prompt.format(
|
| 1206 |
+
context=context,
|
| 1207 |
+
history=history,
|
| 1208 |
+
question=query
|
| 1209 |
+
)
|
| 1210 |
+
|
| 1211 |
+
# Generate response
|
| 1212 |
+
response = llm.invoke(prompt)
|
| 1213 |
+
|
| 1214 |
+
# Add to session history
|
| 1215 |
+
session_manager.add_message(session_id, "human", query)
|
| 1216 |
+
session_manager.add_message(session_id, "ai", response.content)
|
| 1217 |
+
|
| 1218 |
+
return response.content
|
| 1219 |
+
|
| 1220 |
+
return rag_chain_with_history
|
| 1221 |
|
| 1222 |
+
# Initialize the vectorstore
|
| 1223 |
+
vectorstore = setup_vectorstore()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1224 |
|
| 1225 |
+
# Get the RAG chain
|
| 1226 |
+
rag_chain_fn = get_rag_chain(vectorstore)
|
| 1227 |
|
| 1228 |
+
# Define the streaming function for Gradio
|
| 1229 |
+
def rag_memory_stream(message, history, session_id=None):
|
| 1230 |
+
if session_id is None:
|
| 1231 |
+
# Generate a simple session ID if none provided
|
| 1232 |
+
# In a production app, you would use something more sophisticated
|
| 1233 |
+
session_id = "default_session"
|
| 1234 |
+
|
| 1235 |
+
# Process the message and get the response
|
| 1236 |
+
response = rag_chain_fn(message, session_id)
|
| 1237 |
+
|
| 1238 |
+
# Stream the response word by word
|
| 1239 |
+
words = response.split()
|
| 1240 |
+
partial_response = ""
|
| 1241 |
+
|
| 1242 |
+
for word in words:
|
| 1243 |
+
partial_response += word + " "
|
| 1244 |
+
yield partial_response.strip()
|
| 1245 |
+
|
| 1246 |
+
# Create the Chat Interface with session management
|
| 1247 |
+
def create_chat_interface():
|
| 1248 |
+
with gr.Blocks(theme="soft", css=custom_css) as demo:
|
| 1249 |
+
gr.Markdown(f"# {title}")
|
| 1250 |
+
|
| 1251 |
+
# Hidden session ID - in a real app, this would be managed by authentication
|
| 1252 |
+
session_id = gr.State(value="default_session")
|
| 1253 |
+
|
| 1254 |
+
chatbot = gr.Chatbot(height=600)
|
| 1255 |
+
msg = gr.Textbox(
|
| 1256 |
+
placeholder="Ask me anything about GBV resources...",
|
| 1257 |
+
container=False,
|
| 1258 |
+
scale=7
|
| 1259 |
+
)
|
| 1260 |
+
|
| 1261 |
+
def user_input(message, chat_history, session_id_val):
|
| 1262 |
+
if message.strip() == "":
|
| 1263 |
+
return "", chat_history
|
| 1264 |
+
|
| 1265 |
+
chat_history.append([message, None])
|
| 1266 |
+
return "", chat_history
|
| 1267 |
+
|
| 1268 |
+
def bot_response(chat_history, session_id_val):
|
| 1269 |
+
if chat_history and chat_history[-1][1] is None:
|
| 1270 |
+
user_message = chat_history[-1][0]
|
| 1271 |
+
bot_message = ""
|
| 1272 |
+
|
| 1273 |
+
for chunk in rag_memory_stream(user_message, chat_history, session_id_val):
|
| 1274 |
+
bot_message = chunk
|
| 1275 |
+
chat_history[-1][1] = bot_message
|
| 1276 |
+
yield chat_history
|
| 1277 |
+
|
| 1278 |
+
send = gr.Button("Send", variant="primary", scale=1)
|
| 1279 |
+
clear = gr.Button("Clear Chat", variant="secondary")
|
| 1280 |
+
|
| 1281 |
+
# Event handlers
|
| 1282 |
+
send_event = msg.submit(user_input, [msg, chatbot, session_id], [msg, chatbot]).then(
|
| 1283 |
+
bot_response, [chatbot, session_id], chatbot
|
| 1284 |
+
)
|
| 1285 |
+
send.click(user_input, [msg, chatbot, session_id], [msg, chatbot]).then(
|
| 1286 |
+
bot_response, [chatbot, session_id], chatbot
|
| 1287 |
+
)
|
| 1288 |
+
clear.click(lambda: [], outputs=[chatbot])
|
| 1289 |
+
|
| 1290 |
+
return demo
|
| 1291 |
|
| 1292 |
# Title with emojis
|
| 1293 |
+
title = "🤖 GBVR Chatbot"
|
|
|
|
| 1294 |
|
| 1295 |
# Custom CSS for styling the interface
|
| 1296 |
custom_css = """
|
|
|
|
| 1312 |
.gr-textbox:focus, .gr-button:focus {
|
| 1313 |
outline: none; /* Remove outline focus for a cleaner look */
|
| 1314 |
}
|
|
|
|
| 1315 |
"""
|
| 1316 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1317 |
# Launch the app
|
| 1318 |
if __name__ == "__main__":
|
| 1319 |
+
demo = create_chat_interface()
|
| 1320 |
demo.launch(share=True, inbrowser=True, debug=True)
|