Nischal Subedi
UI update
792a7aa
raw
history blame
30 kB
import os
import logging
from typing import Dict, List, Optional
from functools import lru_cache
import re
import gradio as gr
try:
# Assuming vector_db.py exists in the same directory or is installed
from vector_db import VectorDatabase
except ImportError:
print("Error: Could not import VectorDatabase from vector_db.py.")
print("Please ensure vector_db.py exists in the same directory and is correctly defined.")
# Exit if critical dependency is missing at import time
exit(1)
try:
from langchain_openai import ChatOpenAI
except ImportError:
print("Error: langchain-openai not found. Please install it: pip install langchain-openai")
# Exit if critical dependency is missing at import time
exit(1)
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
# Suppress warnings for cleaner console output
import warnings
warnings.filterwarnings("ignore", category=SyntaxWarning)
warnings.filterwarnings("ignore", category=UserWarning, message=".*You are using gradio version.*")
warnings.filterwarnings("ignore", category=DeprecationWarning)
# Enhanced logging configuration
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s'
)
# --- RAGSystem Class (Processing Logic - KEPT INTACT AS REQUESTED) ---
class RAGSystem:
def __init__(self, vector_db: Optional[VectorDatabase] = None):
logging.info("Initializing RAGSystem")
self.vector_db = vector_db if vector_db else VectorDatabase()
self.llm = None
self.chain = None
self.prompt_template_str = """You are a legal assistant specializing in tenant rights and landlord-tenant laws. Your goal is to provide accurate, detailed, and helpful answers grounded in legal authority. Use the provided statutes as the primary source when available. If no relevant statutes are found in the context, rely on your general knowledge to provide a pertinent and practical response, clearly indicating when you are doing so and prioritizing state-specific information over federal laws for state-specific queries.
Instructions:
* Use the context and statutes as the primary basis for your answer when available.
* For state-specific queries, prioritize statutes or legal principles from the specified state over federal laws.
* Cite relevant statutes (e.g., (AS § 34.03.220(a)(2))) explicitly in your answer when applicable.
* If multiple statutes apply, list all relevant ones.
* If no specific statute is found in the context, state this clearly (e.g., 'No specific statute was found in the provided context'), then provide a general answer based on common legal principles or practices, marked as such.
* Include practical examples or scenarios to enhance clarity and usefulness.
* Use bullet points or numbered lists for readability when appropriate.
* Maintain a professional and neutral tone.
Question: {query}
State: {state}
Statutes from context:
{statutes}
Context information:
--- START CONTEXT ---
{context}
--- END CONCONTEXT ---
Answer:"""
self.prompt_template = PromptTemplate(
input_variables=["query", "context", "state", "statutes"],
template=self.prompt_template_str
)
logging.info("RAGSystem initialized.")
def extract_statutes(self, text: str) -> str:
statute_pattern = r'\b(?:[A-Z]{2,}\.?\s+(?:Rev\.\s+)?Stat\.?|Code(?:\s+Ann\.?)?|Ann\.?\s+Laws|Statutes|CCP|USC|ILCS|Civ\.\s+Code|Penal\s+Code|Gen\.\s+Oblig\.\s+Law|R\.?S\.?|P\.?L\.?)\s+§\s*[\d\-]+(?:\.\d+)?(?:[\(\w\.\)]+)?|Title\s+\d+\s+USC\s+§\s*\d+(?:-\d+)?\b'
statutes = re.findall(statute_pattern, text, re.IGNORECASE)
valid_statutes = []
for statute in statutes:
statute = statute.strip()
if '§' in statute and any(char.isdigit() for char in statute):
if not re.match(r'^\([\w\.]+\)$', statute) and 'http' not in statute:
if len(statute) > 5:
valid_statutes.append(statute)
if valid_statutes:
seen = set()
unique_statutes = [s for s in valid_statutes if not (s.rstrip('.,;') in seen or seen.add(s.rstrip('.,;')))]
logging.info(f"Extracted {len(unique_statutes)} unique statutes.")
return "\n".join(f"- {s}" for s in unique_statutes)
logging.info("No statutes found matching the pattern in the context.")
return "No specific statutes found in the provided context."
@lru_cache(maxsize=50)
def process_query_cached(self, query: str, state: str, openai_api_key: str, n_results: int = 5) -> Dict[str, any]:
logging.info(f"Processing query (cache key: '{query}'|'{state}'|key_hidden) with n_results={n_results}")
if not state or state is None: # Removed "Select a state..." from error check as it's not a choice
logging.warning("No valid state provided for query.")
return {"answer": "<div class='error-message'>Error: Please select a valid state.</div>", "context_used": "N/A - Invalid Input"}
if not query or not query.strip():
logging.warning("No query provided.")
return {"answer": "<div class='error-message'>Error: Please enter your question.</div>", "context_used": "N/A - Invalid Input"}
if not openai_api_key or not openai_api_key.strip() or not openai_api_key.startswith("sk-"):
logging.warning("No valid OpenAI API key provided.")
return {"answer": "<div class='error-message'>Error: Please provide a valid OpenAI API key (starting with 'sk-'). Get one from <a href='https://platform.openai.com/api-keys' target='_blank'>OpenAI</a>.</div>", "context_used": "N/A - Invalid Input"}
try:
logging.info("Initializing temporary LLM and Chain for this query...")
temp_llm = ChatOpenAI(
temperature=0.2, openai_api_key=openai_api_key, model_name="gpt-3.5-turbo",
max_tokens=1500, request_timeout=45
)
temp_chain = LLMChain(llm=temp_llm, prompt=self.prompt_template)
logging.info("Temporary LLM and Chain initialized successfully.")
except Exception as e:
logging.error(f"LLM Initialization failed: {str(e)}", exc_info=True)
error_msg = "Error: Failed to initialize AI model. Please check your network connection and API key validity."
if "authentication" in str(e).lower():
error_msg = "Error: OpenAI API Key is invalid or expired. Please check your key."
return {"answer": f"<div class='error-message'>{error_msg}</div><div class='error-details'>Details: {str(e)}</div>", "context_used": "N/A - LLM Init Failed"}
context = "No relevant context found."
statutes_from_context = "Statute retrieval skipped due to context issues."
try:
logging.info(f"Querying Vector DB for query: '{query[:50]}...' in state '{state}'...")
results = self.vector_db.query(query, state=state, n_results=n_results)
logging.info(f"Vector DB query successful for state '{state}'. Processing results...")
context_parts = []
doc_results = results.get("document_results", {})
docs = doc_results.get("documents", [[]])[0]
metadatas = doc_results.get("metadatas", [[]])[0]
if docs and metadatas and len(docs) == len(metadatas):
logging.info(f"Found {len(docs)} document chunks.")
for i, doc_content in enumerate(docs):
metadata = metadatas[i]
state_label = metadata.get('state', 'Unknown State')
chunk_id = metadata.get('chunk_id', 'N/A')
context_parts.append(f"**Source: Document Chunk {chunk_id} (State: {state_label})**\n{doc_content}")
state_results_data = results.get("state_results", {})
state_docs = state_results_data.get("documents", [[]])[0]
state_metadatas = state_results_data.get("metadatas", [[]])[0]
if state_docs and state_metadatas and len(state_docs) == len(state_metadatas):
logging.info(f"Found {len(state_docs)} state summary documents.")
for i, state_doc_content in enumerate(state_docs):
metadata = state_metadatas[i]
state_label = metadata.get('state', state)
context_parts.append(f"**Source: State Summary (State: {state_label})**\n{state_doc_content}")
if context_parts:
context = "\n\n---\n\n".join(context_parts)
logging.info(f"Constructed context with {len(context)} chars.")
try:
statutes_from_context = self.extract_statutes(context)
except Exception as e:
logging.error(f"Error extracting statutes: {e}", exc_info=True)
statutes_from_context = "Error extracting statutes from context."
else:
logging.warning("No relevant context parts found from vector DB query.")
context = "No relevant context could be retrieved from the knowledge base for this query and state. The AI will answer from its general knowledge."
statutes_from_context = "No specific statutes found as no context was retrieved."
except Exception as e:
logging.error(f"Vector DB query/context processing failed: {str(e)}", exc_info=True)
context = f"Warning: Error retrieving documents from the knowledge base ({str(e)}). The AI will attempt to answer from its general knowledge, which may be less specific or accurate."
statutes_from_context = "Statute retrieval skipped due to error retrieving context."
try:
logging.info("Invoking LLMChain with constructed input...")
llm_input = {"query": query, "context": context, "state": state, "statutes": statutes_from_context}
answer_dict = temp_chain.invoke(llm_input)
answer_text = answer_dict.get('text', '').strip()
if not answer_text:
logging.warning("LLM returned an empty answer.")
answer_text = "<div class='error-message'><span class='error-icon'>⚠️</span>The AI model returned an empty response. This might be due to the query, context limitations, or temporary issues. Please try rephrasing your question or try again later.</div>"
else:
logging.info("LLM generated answer successfully.")
return {"answer": answer_text, "context_used": context}
except Exception as e:
logging.error(f"LLM processing failed: {str(e)}", exc_info=True)
error_message = "Error: AI answer generation failed."
details = f"Details: {str(e)}"
if "authentication" in str(e).lower():
error_message = "Error: Authentication failed. Please double-check your OpenAI API key."
details = ""
elif "rate limit" in str(e).lower():
error_message = "Error: You've exceeded your OpenAI API rate limit or quota. Please check your usage and plan limits, or wait and try again."
details = ""
elif "context length" in str(e).lower():
error_message = "Error: The request was too long for the AI model. This can happen with very complex questions or extensive retrieved context."
details = "Try simplifying your question or asking about a more specific aspect."
elif "timeout" in str(e).lower():
error_message = "Error: The request to the AI model timed out. The service might be busy."
details = "Please try again in a few moments."
formatted_error = f"<div class='error-message'><span class='error-icon'>❌</span>{error_message}</div>"
if details:
formatted_error += f"<div class='error-details'>{details}</div>"
return {"answer": formatted_error, "context_used": context}
def process_query(self, query: str, state: str, openai_api_key: str, n_results: int = 5) -> Dict[str, any]:
return self.process_query_cached(query.strip(), state, openai_api_key.strip(), n_results)
def get_states(self) -> List[str]:
try:
states = self.vector_db.get_states()
if not states:
logging.warning("No states retrieved from vector_db. Returning empty list.")
return []
valid_states = sorted(list(set(s for s in states if s and isinstance(s, str) and s != "Select a state...")))
logging.info(f"Retrieved {len(valid_states)} unique, valid states from VectorDatabase.")
return valid_states
except Exception as e:
logging.error(f"Failed to get states from VectorDatabase: {str(e)}", exc_info=True)
return ["Error: Could not load states"]
def load_pdf(self, pdf_path: str) -> int:
if not os.path.exists(pdf_path):
logging.error(f"PDF file not found at path: {pdf_path}")
raise FileNotFoundError(f"PDF file not found: {pdf_path}")
try:
logging.info(f"Attempting to load/verify data from PDF: {pdf_path}")
# Assuming process_and_load_pdf is part of VectorDatabase and correctly implemented
num_states_processed = self.vector_db.process_and_load_pdf(pdf_path)
doc_count = self.vector_db.document_collection.count()
state_count = self.vector_db.state_collection.count()
total_items = doc_count + state_count
if total_items > 0:
logging.info(f"Vector DB contains {total_items} items ({doc_count} docs, {state_count} states). PDF processed or data already existed.")
current_states = self.get_states()
return len(current_states) if current_states and "Error" not in current_states[0] else 0
else:
logging.warning(f"PDF processing completed, but the vector database appears empty. Check PDF content and processing logs.")
return 0
except Exception as e:
logging.error(f"Failed to load or process PDF '{pdf_path}': {str(e)}", exc_info=True)
raise RuntimeError(f"Failed to process PDF '{pdf_path}': {e}") from e
# --- GRADIO INTERFACE (NEW UI DESIGN) ---
def gradio_interface(self):
def query_interface_wrapper(api_key: str, query: str, state: str, mode: str) -> str:
if not api_key or not api_key.strip() or not api_key.startswith("sk-"):
return "<div class='error-message'>Please provide a valid OpenAI API key (starting with 'sk-'). <a href='https://platform.openai.com/api-keys' target='_blank'>Get one here</a>.</div>"
if not state or state is None:
return "<div class='error-message'>Please select a valid state from the list.</div>"
if not query or not query.strip():
return "<div class='error-message'>Please enter your question in the text box.</div>"
result = self.process_query(query=query, state=state, openai_api_key=api_key)
answer = result.get("answer", "<div class='error-message'>An unexpected error occurred.</div>")
if "<div class='error-message'>" in answer:
return answer
else:
formatted_response = f"<div class='response-title'>Response for {state}</div><div class='response-content'>{answer}</div>"
return f"<div class='output-box'>{formatted_response}</div>"
def toggle_theme():
is_dark = "dark-mode" in document.body.className
document.body.className = "dark-mode" if not is_dark else ""
return gr.update(visible=not is_dark), gr.update(visible=is_dark)
try:
available_states = self.get_states()
print(f"DEBUG: States loaded: {available_states}")
states = available_states if available_states and "Error" not in available_states[0] else ["Error: States unavailable"]
initial_state = states[0] if states and "Error" not in states[0] else None
except Exception as e:
print(f"DEBUG: Error loading states: {e}")
states = ["Error: Critical failure loading states"]
initial_state = None
example_queries = [
["What are the rules for security deposit returns?", "California"],
["Can a landlord enter without notice?", "New York"],
["What to do about unrepaired issues?", "Texas"],
]
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap');
body {
font-family: 'Roboto', sans-serif !important;
margin: 0;
padding: 0;
background-color: #F5F5F5;
color: #333;
transition: all 0.3s ease;
}
.dark-mode {
background-color: #1E1E1E;
color: #E0E0E0;
}
.container {
max-width: 800px;
margin: 20px auto;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.dark-mode .container {
box-shadow: 0 2px 5px rgba(0,0,0,0.3);
}
.header {
text-align: center;
padding: 20px;
border-bottom: 2px solid #DDD;
}
.dark-mode .header {
border-bottom: 2px solid #444;
}
.header h1 {
margin: 0;
font-size: 2.5rem;
color: #FF5722;
}
.dark-mode .header h1 {
color: #FF7043;
}
.theme-toggle {
margin: 10px 0;
text-align: center;
}
.theme-toggle button {
padding: 10px 20px;
font-size: 1rem;
cursor: pointer;
border: none;
border-radius: 5px;
background-color: #FF5722;
color: white;
transition: background-color 0.3s;
}
.theme-toggle button:hover {
background-color: #E64A19;
}
.dark-mode .theme-toggle button {
background-color: #FF7043;
}
.dark-mode .theme-toggle button:hover {
background-color: #F4511E;
}
.section {
margin: 20px 0;
padding: 15px;
background-color: white;
border-radius: 5px;
}
.dark-mode .section {
background-color: #2C2C2C;
}
.section label {
font-size: 1.2rem;
font-weight: 500;
margin-bottom: 10px;
display: block;
}
.input-area {
width: 100%;
padding: 12px;
font-size: 1rem;
border: 2px solid #CCC;
border-radius: 5px;
box-sizing: border-box;
}
.dark-mode .input-area {
background-color: #333;
border-color: #555;
color: #E0E0E0;
}
.radio-group {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 10px;
margin-top: 10px;
}
.radio-group label {
padding: 10px;
background-color: #F0F0F0;
border: 2px solid #DDD;
border-radius: 5px;
text-align: center;
font-size: 1rem;
cursor: pointer;
}
.dark-mode .radio-group label {
background-color: #3A3A3A;
border-color: #555;
color: #E0E0E0;
}
.radio-group input[type="radio"]:checked + label {
background-color: #FF5722;
color: white;
border-color: #E64A19;
}
.dark-mode .radio-group input[type="radio"]:checked + label {
background-color: #FF7043;
border-color: #F4511E;
}
.button-group {
margin-top: 15px;
text-align: right;
}
.button-group button {
padding: 10px 20px;
font-size: 1rem;
cursor: pointer;
border: none;
border-radius: 5px;
margin-left: 10px;
}
.submit-btn {
background-color: #FF5722;
color: white;
}
.submit-btn:hover {
background-color: #E64A19;
}
.clear-btn {
background-color: #B0BEC5;
color: white;
}
.clear-btn:hover {
background-color: #90A4AE;
}
.dark-mode .submit-btn {
background-color: #FF7043;
}
.dark-mode .submit-btn:hover {
background-color: #F4511E;
}
.dark-mode .clear-btn {
background-color: #78909C;
}
.dark-mode .clear-btn:hover {
background-color: #607D8B;
}
.output-box {
margin-top: 20px;
padding: 15px;
border: 2px solid #DDD;
border-radius: 5px;
min-height: 100px;
}
.dark-mode .output-box {
border-color: #555;
background-color: #2C2C2C;
}
.response-title {
font-size: 1.3rem;
font-weight: 700;
margin-bottom: 10px;
color: #FF5722;
}
.dark-mode .response-title {
color: #FF7043;
}
.response-content {
font-size: 1rem;
line-height: 1.6;
}
.error-message {
background-color: #FFE0E0;
border: 2px solid #F44336;
color: #D32F2F;
padding: 10px;
border-radius: 5px;
margin-top: 10px;
}
.dark-mode .error-message {
background-color: #400000;
border-color: #B71C1C;
color: #EF5350;
}
.examples {
margin-top: 20px;
}
.examples button {
padding: 8px 15px;
margin: 5px;
font-size: 1rem;
cursor: pointer;
border: 2px solid #DDD;
border-radius: 5px;
background-color: white;
}
.dark-mode .examples button {
border-color: #555;
background-color: #3A3A3A;
color: #E0E0E0;
}
.examples button:hover {
background-color: #F5F5F5;
}
.dark-mode .examples button:hover {
background-color: #444;
}
.footer {
text-align: center;
padding: 20px;
font-size: 0.9rem;
color: #666;
border-top: 2px solid #DDD;
}
.dark-mode .footer {
color: #A0A0A0;
border-top: 2px solid #444;
}
"""
with gr.Blocks(css=custom_css, title="Landlord-Tenant Rights Assistant") as demo:
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("<div class='header'><h1>Landlord-Tenant Rights Assistant</h1></div>")
theme_toggle_btn = gr.Button("Toggle Dark Mode", elem_classes="theme-toggle")
with gr.Column(elem_classes="container"):
with gr.Column():
gr.Markdown("This AI-powered assistant helps navigate complex landlord-tenant laws. Ask a question about your state's regulations for detailed, legally-grounded insights.")
with gr.Column(elem_classes="section"):
api_key_input = gr.Textbox(label="API Key", type="password", placeholder="Enter your OpenAI API key (e.g., sk-...)", elem_classes="input-area")
with gr.Column(elem_classes="section"):
query_input = gr.Textbox(label="Your Question", placeholder="E.g., What are the rules for security deposit returns in my state?", lines=6, elem_classes="input-area")
state_input = gr.Radio(label="Select State", choices=states, value=initial_state, elem_classes="radio-group")
with gr.Row(elem_classes="button-group"):
clear_button = gr.Button("Clear", elem_classes="clear-btn")
submit_button = gr.Button("Submit Query", elem_classes="submit-btn")
with gr.Column(elem_classes="section"):
output = gr.HTML(value="<div class='output-box'><div class='placeholder'>The answer will appear here after submitting your query.</div></div>")
with gr.Column(elem_classes="examples"):
gr.Examples(examples=example_queries, inputs=[query_input, state_input], label="Example Questions")
with gr.Column():
gr.Markdown("<div class='footer'><p><strong>Disclaimer:</strong> This tool is for informational purposes only and does not constitute legal advice. Consult a licensed attorney for specific guidance. Developed by Nischal Subedi.</p></div>")
theme_toggle_btn.click(
fn=toggle_theme,
outputs=[]
)
submit_button.click(
fn=query_interface_wrapper,
inputs=[api_key_input, query_input, state_input],
outputs=output
)
clear_button.click(
fn=lambda: ["", "", initial_state, "<div class='output-box'><div class='placeholder'>Inputs cleared. Ready for your next question.</div></div>"],
inputs=[],
outputs=[api_key_input, query_input, state_input, output]
)
return demo
# --- Main Execution Block (UNCHANGED from original logic) ---
if __name__ == "__main__":
logging.info("Starting Landlord-Tenant Rights Bot application...")
try:
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
DEFAULT_PDF_PATH = os.path.join(SCRIPT_DIR, "tenant-landlord.pdf")
DEFAULT_DB_PATH = os.path.join(SCRIPT_DIR, "chroma_db")
PDF_PATH = os.getenv("PDF_PATH", DEFAULT_PDF_PATH)
VECTOR_DB_PATH = os.getenv("VECTOR_DB_PATH", DEFAULT_DB_PATH)
# Ensure vector DB directory exists before initialization
os.makedirs(os.path.dirname(VECTOR_DB_PATH), exist_ok=True)
logging.info(f"Attempting to load PDF from: {PDF_PATH}")
if not os.path.exists(PDF_PATH):
logging.error(f"FATAL: PDF file not found at the specified path: {PDF_PATH}")
print(f"\n--- CONFIGURATION ERROR ---\nPDF file ('{os.path.basename(PDF_PATH)}') not found at: {PDF_PATH}.\nPlease ensure it exists or set 'PDF_PATH' environment variable.\n---------------------------\n")
exit(1) # Correctly exits if PDF is not found
if not os.access(PDF_PATH, os.R_OK):
logging.error(f"FATAL: PDF file at '{PDF_PATH}' exists but is not readable. Check file permissions.")
print(f"\n--- PERMISSION ERROR ---\nPDF file ('{os.path.basename(PDF_PATH)}') found but not readable at: {PDF_PATH}\nPlease check file permissions (e.g., using 'chmod +r' in terminal).\n---------------------------\n")
exit(1) # Correctly exits if PDF is unreadable
logging.info(f"PDF file '{os.path.basename(PDF_PATH)}' found and is readable.")
# Initialize VectorDatabase and RAGSystem
vector_db_instance = VectorDatabase(persist_directory=VECTOR_DB_PATH)
rag = RAGSystem(vector_db=vector_db_instance)
# Load PDF data into the vector DB (or verify it's already loaded)
rag.load_pdf(PDF_PATH)
# Get the Gradio interface object
app_interface = rag.gradio_interface()
SERVER_PORT = int(os.getenv("PORT", 7860)) # Use PORT env var for Hugging Face Spaces
logging.info(f"Launching Gradio app on http://0.0.0.0:{SERVER_PORT}")
print(f"\n--- Gradio App Running ---\nAccess at: http://localhost:{SERVER_PORT} or your public Spaces URL\n--------------------------\n")
app_interface.launch(server_name="0.0.0.0", server_port=SERVER_PORT, share=False) # share=False is typical for Spaces
except ModuleNotFoundError as e:
if "vector_db" in str(e):
logging.error(f"FATAL: Could not import VectorDatabase. Ensure 'vector_db.py' is in the same directory and 'chromadb', 'langchain', 'pypdf', 'sentence-transformers' are installed.", exc_info=True)
print(f"\n--- MISSING DEPENDENCY OR FILE ---\nCould not find/import 'vector_db.py' or one of its dependencies.\nError: {e}\nPlease ensure 'vector_db.py' is present and all required packages (chromadb, langchain, pypdf, sentence-transformers, etc.) are in your requirements.txt and installed.\n---------------------------\n")
else:
logging.error(f"Application startup failed due to a missing module: {str(e)}", exc_info=True)
print(f"\n--- FATAL STARTUP ERROR - MISSING MODULE ---\n{str(e)}\nPlease ensure all dependencies are installed.\nCheck logs for more details.\n---------------------------\n")
exit(1)
except FileNotFoundError as e:
logging.error(f"Application startup failed due to a missing file: {str(e)}", exc_info=True)
print(f"\n--- FATAL STARTUP ERROR - FILE NOT FOUND ---\n{str(e)}\nPlease ensure the file exists at the specified path.\nCheck logs for more details.\n---------------------------\n")
exit(1)
except Exception as e:
logging.error(f"Application startup failed: {str(e)}", exc_info=True)
print(f"\n--- FATAL STARTUP ERROR ---\n{str(e)}\nCheck logs for more details.\n---------------------------\n")
exit(1)