import os
import logging
from typing import Dict, List, Optional
from functools import lru_cache
import re
import gradio as gr
try:
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(1)
try:
from langchain_openai import ChatOpenAI
except ImportError:
print("Error: langchain-openai not found. Please install it: pip install langchain-openai")
exit(1)
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
# Suppress warnings
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
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s'
)
# --- RAGSystem Class ---
class RAGSystem:
# (Keep the RAGSystem class exactly the same as in the previous version)
# ... __init__ ...
# ... extract_statutes ...
# ... process_query_cached ...
# ... process_query ...
# ... get_states ...
# ... load_pdf ...
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 CONTEXT ---
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 == "Select a state..." or "Error" in state:
logging.warning("No valid state provided for query.")
return {"answer": "
Error: Please select a valid state.
", "context_used": "N/A - Invalid Input"}
if not query or not query.strip():
logging.warning("No query provided.")
return {"answer": "Error: Please enter your question.
", "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": "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.
"
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"Please select a valid state from the dropdown.
"
if not query or not query.strip():
return "Please enter your question in the text box.
"
# Call the core processing logic
result = self.process_query(query=query, state=state, openai_api_key=api_key)
# Format the response for display
answer = result.get("answer", "An unexpected error occurred, and no answer was generated. Please check the logs or try again.
")
# Add a header *only* if the answer is not an error message itself
if not "" in answer:
formatted_response = f"
{answer}"
else:
formatted_response = answer # Pass through error messages directly
# Log context length for debugging (optional)
context_used = result.get("context_used", "N/A")
if isinstance(context_used, str) and "N/A" not in context_used:
logging.debug(f"Context length used for query: {len(context_used)} characters.")
else:
logging.debug(f"No context was used or available for this query ({context_used}).")
return formatted_response
# --- Get Available States for Dropdown ---
try:
available_states_list = self.get_states()
if not available_states_list or "Error" in available_states_list[0]:
dropdown_choices = ["Error: Could not load states"]
initial_value = dropdown_choices[0]
logging.error("Could not load states for dropdown. UI will show error.")
else:
dropdown_choices = ["Select a state..."] + available_states_list
initial_value = dropdown_choices[0]
except Exception as e:
logging.error(f"Unexpected critical error getting states: {e}", exc_info=True)
dropdown_choices = ["Error: Critical failure loading states"]
initial_value = dropdown_choices[0]
# --- Prepare Example Queries ---
example_queries_base = [
["What are the rules for security deposit returns?", "California"],
["Can a landlord enter my apartment without notice?", "New York"],
["My landlord hasn't made necessary repairs. What can I do?", "Texas"],
["What are the limits on rent increases in my state?", "Florida"],
["Is my lease automatically renewed if I don't move out?", "Illinois"],
["What happens if I break my lease early?", "Washington"]
]
example_queries = []
if available_states_list and "Error" not in available_states_list[0]:
loaded_states_set = set(available_states_list)
example_queries = [ex for ex in example_queries_base if ex[1] in loaded_states_set]
if not example_queries:
fallback_state = available_states_list[0] if available_states_list and "Error" not in available_states_list[0] else "California"
example_queries.append(["What basic rights do tenants have?", fallback_state])
# --- Refined Custom CSS ---
# Focus: Unified background, distinct white cards, proper centering, refined examples table
custom_css = """
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap');
/* --- Base & Body --- */
body, .gradio-container {
font-family: 'Roboto', sans-serif !important;
background-color: #F5F7FA !important; /* Light grey base background */
color: #1F2A44;
margin: 0;
padding: 0;
min-height: 100vh;
font-size: 16px; /* Base font size */
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
* {
box-sizing: border-box;
}
/* --- Main Content Container --- */
.gradio-container > .flex.flex-col { /* Target the main content column */
max-width: 960px; /* Slightly wider max-width */
margin: 0 auto !important; /* Center the column */
padding: 3rem 1.5rem !important; /* More vertical padding */
gap: 2.5rem !important; /* Consistent gap between sections */
background-color: transparent !important; /* Ensure container itself is transparent */
}
/* --- Card Styling (Applied to Groups) --- */
.card-style {
background-color: #FFFFFF !important; /* White background for cards */
border: 1px solid #E5E7EB !important; /* Subtle border */
border-radius: 12px !important;
padding: 2rem !important; /* Consistent padding inside cards */
box-shadow: 0 4px 12px rgba(101, 119, 134, 0.08) !important; /* Refined shadow */
overflow: hidden; /* Prevent content spill */
}
/* Remove default Gradio Group padding if using custom padding */
.gradio-group {
padding: 0 !important;
border: none !important;
background: none !important;
box-shadow: none !important;
}
/* --- Header Section --- */
.header-section {
background-color: transparent !important; /* Header blends */
padding: 1rem 0 !important;
text-align: center !important; /* Center align all content */
border: none !important;
box-shadow: none !important;
}
.header-logo {
font-size: 2.8rem;
color: #2563EB;
margin-bottom: 0.75rem;
display: block; /* Ensure centering */
}
.header-title {
font-size: 2rem; /* Larger title */
font-weight: 700;
color: #111827; /* Darker title */
margin: 0 0 0.25rem 0;
}
.header-tagline {
font-size: 1.1rem;
color: #4B5563;
margin: 0;
}
/* --- Introduction Section --- */
/* Uses card-style defined above */
.intro-card h3 {
font-size: 1.5rem;
font-weight: 600;
color: #0369A1; /* Blue heading */
margin: 0 0 1rem 0;
padding-bottom: 0.5rem;
border-bottom: 1px solid #E0F2FE; /* Light blue underline */
}
.intro-card p {
font-size: 1rem;
line-height: 1.6;
color: #374151; /* Standard text color */
margin: 0 0 0.75rem 0;
}
.intro-card a {
color: #0369A1;
text-decoration: underline;
font-weight: 500;
}
.intro-card a:hover { color: #0284C7; }
.intro-card strong { font-weight: 600; color: #1F2A44; }
/* --- Input Form Section --- */
/* Uses card-style */
.input-form-card h3 {
font-size: 1.4rem;
font-weight: 600;
color: #1F2A44;
margin: 0 0 1.75rem 0;
padding-bottom: 0.75rem;
border-bottom: 1px solid #E5E7EB;
}
.input-field-group { margin-bottom: 1.5rem; }
.input-row { display: flex; gap: 1.5rem; flex-wrap: wrap; margin-bottom: 1.5rem; }
.input-field { flex: 1; min-width: 220px; }
/* Input Elements */
.gradio-textbox textarea, .gradio-dropdown select, .gradio-textbox input[type=password] {
border: 1px solid #D1D5DB !important;
border-radius: 8px !important;
padding: 0.8rem 1rem !important;
font-size: 1rem !important; /* Make inputs slightly larger */
background-color: #F9FAFB !important;
color: #1F2A44 !important;
transition: border-color 0.2s ease, box-shadow 0.2s ease;
width: 100% !important;
}
.gradio-textbox textarea { min-height: 90px; }
.gradio-textbox textarea:focus, .gradio-dropdown select:focus, .gradio-textbox input[type=password]:focus {
border-color: #2563EB !important;
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15) !important;
outline: none !important;
background-color: #FFFFFF !important;
}
.gradio-input-label, .gradio-output-label { /* Label styling */
font-size: 0.9rem !important;
font-weight: 500 !important;
color: #374151 !important;
margin-bottom: 0.5rem !important;
display: block !important;
}
.gradio-input-info { /* Info text */
font-size: 0.85rem !important;
color: #6B7280 !important;
margin-top: 0.3rem;
}
/* Buttons */
.button-row { display: flex; gap: 1rem; margin-top: 1.5rem; flex-wrap: wrap; justify-content: flex-end; }
.gradio-button {
border-radius: 8px !important; padding: 0.75rem 1.5rem !important; font-size: 0.95rem !important;
font-weight: 500 !important; border: none !important; cursor: pointer;
transition: background-color 0.2s ease, transform 0.1s ease, box-shadow 0.2s ease;
}
.gradio-button:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); }
.gradio-button:active:not(:disabled) { transform: scale(0.98); box-shadow: none; }
.gradio-button:disabled { background: #E5E7EB !important; color: #9CA3AF !important; cursor: not-allowed; }
.gr-button-primary { background-color: #2563EB !important; color: #FFFFFF !important; }
.gr-button-primary:hover:not(:disabled) { background-color: #1D4ED8 !important; }
.gr-button-secondary { background-color: #F3F4F6 !important; color: #374151 !important; border: 1px solid #D1D5DB !important; }
.gr-button-secondary:hover:not(:disabled) { background-color: #E5E7EB !important; border-color: #9CA3AF !important; }
/* --- Output Section --- */
/* Uses card-style */
.output-card .response-header { /* Style the H3 we add in Python */
font-size: 1.3rem;
font-weight: 600;
color: #1F2A44;
margin: 0 0 0.75rem 0;
}
.output-card .divider { /* Style the HR we add */
border: none; border-top: 1px solid #E5E7EB; margin: 1rem 0 1.5rem 0;
}
.output-card .output-content-wrapper { /* Wrapper for the markdown content */
font-size: 1rem; line-height: 1.7; color: #374151;
}
.output-card .output-content-wrapper p { margin-bottom: 1rem; }
.output-card .output-content-wrapper ul, .output-card .output-content-wrapper ol { margin-left: 1.5rem; margin-bottom: 1rem; padding-left: 1rem; }
.output-card .output-content-wrapper li { margin-bottom: 0.5rem; }
.output-card .output-content-wrapper strong, .output-card .output-content-wrapper b { font-weight: 600; color: #111827; }
.output-card .output-content-wrapper a { color: #2563EB; text-decoration: underline; }
.output-card .output-content-wrapper a:hover { color: #1D4ED8; }
/* Error message styling */
.output-card .error-message {
background-color: #FEF2F2; border: 1px solid #FECACA; border-left: 4px solid #F87171;
border-radius: 8px; padding: 1rem 1.25rem; color: #B91C1C; font-weight: 500; margin-top: 0.5rem;
}
.output-card .error-details { font-size: 0.9rem; color: #991B1B; margin-top: 0.5rem; font-style: italic; }
/* Placeholder text */
.output-card .placeholder { color: #9CA3AF; font-style: italic; text-align: center; padding: 2rem 1rem; display: block; }
/* --- Examples Section --- */
/* Uses card-style */
.examples-card .gr-examples-header { /* Style the header Gradio adds */
font-size: 1.3rem !important; font-weight: 600 !important; color: #1F2A44 !important;
margin: 0 0 1.5rem 0 !important; padding-bottom: 0.75rem !important; border-bottom: 1px solid #E5E7EB !important;
}
/* Style the TABLE generated by gr.Examples */
.examples-card .gr-examples-table { border-collapse: collapse !important; width: 100% !important; }
.examples-card .gr-examples-table th,
.examples-card .gr-examples-table td {
text-align: left !important; padding: 0.75rem 1rem !important;
border: 1px solid #E5E7EB !important; font-size: 0.95rem !important;
color: #374151 !important; background-color: transparent !important;
}
.examples-card .gr-examples-table th {
font-weight: 500 !important; background-color: #F9FAFB !important; color: #1F2A44 !important;
}
/* Style the example *rows* when clickable */
.examples-card .gr-examples-table tr { cursor: pointer; transition: background-color 0.2s ease; }
.examples-card .gr-examples-table tr:hover td { background-color: #F3F4F6 !important; }
/* --- Footer Section --- */
.footer-section {
background-color: transparent !important;
border-top: 1px solid #E5E7EB !important;
padding: 2rem 1rem !important;
margin-top: 1rem !important; /* Space above footer */
text-align: center !important;
color: #6B7280 !important;
font-size: 0.9rem !important;
line-height: 1.6 !important;
box-shadow: none !important; border-radius: 0 !important;
}
.footer-section strong { color: #374151; font-weight: 500; }
.footer-section a { color: #2563EB; text-decoration: none; font-weight: 500; }
.footer-section a:hover { color: #1D4ED8; text-decoration: underline; }
/* --- Accessibility & Focus --- */
:focus-visible {
outline: 2px solid #2563EB !important;
outline-offset: 2px;
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2) !important;
}
/* Remove default Gradio focus on button internal span */
.gradio-button span:focus { outline: none !important; }
/* --- Responsive Adjustments --- */
@media (max-width: 768px) {
.gradio-container > .flex.flex-col { padding: 2rem 1rem !important; gap: 2rem !important; }
.card-style { padding: 1.5rem !important; }
.header-title { font-size: 1.8rem; }
.header-tagline { font-size: 1rem; }
.input-row { flex-direction: column; gap: 1rem; margin-bottom: 1rem; }
.button-row { justify-content: center; }
}
@media (max-width: 480px) {
body { font-size: 15px; }
.gradio-container > .flex.flex-col { padding: 1.5rem 1rem !important; gap: 1.5rem !important; }
.card-style { padding: 1.25rem !important; border-radius: 10px !important;}
.header-logo { font-size: 2.5rem; margin-bottom: 0.5rem;}
.header-title { font-size: 1.5rem; }
.header-tagline { font-size: 0.95rem; }
.intro-card h3, .input-form-card h3, .output-card .response-header, .examples-card .gr-examples-header { font-size: 1.2rem !important; margin-bottom: 1rem !important; }
.gradio-textbox textarea, .gradio-dropdown select, .gradio-textbox input[type=password] { font-size: 0.95rem !important; padding: 0.75rem !important; }
.gradio-button { width: 100%; padding: 0.7rem 1.2rem !important; font-size: 0.9rem !important; }
.button-row { flex-direction: column; gap: 0.75rem; }
.footer-section { font-size: 0.85rem; padding: 1.5rem 1rem !important; }
.examples-card .gr-examples-table th, .examples-card .gr-examples-table td { padding: 0.6rem 0.8rem !important; font-size: 0.9rem !important;}
}
/* Gradio Specific Overrides (Use sparingly) */
/* Force main container gap */
.gradio-container > .flex { gap: 2.5rem !important; }
/* Ensure no weird margins collapse */
.gradio-markdown > *:first-child { margin-top: 0; }
.gradio-markdown > *:last-child { margin-bottom: 0; }
/* Remove border from dropdown wrapper if needed */
.gradio-dropdown { border: none !important; padding: 0 !important; }
/* Remove border from textbox wrapper */
.gradio-textbox { border: none !important; padding: 0 !important; }
"""
# --- Gradio Blocks Layout ---
with gr.Blocks(css=custom_css, title="Landlord-Tenant Rights Assistant") as demo:
# The main container class is applied implicitly by Gradio, CSS targets it
# Header Section (No Card Style)
with gr.Group(elem_classes="header-section"): # Use Group for structure, styled via class
gr.Markdown(
"""
""", elem_id="app-title"
)
# Introduction Section (Card Style)
with gr.Group(elem_classes="card-style intro-card"):
gr.Markdown(
"""
Discover Your Rights
Get accurate, AI-powered answers to your questions about landlord-tenant laws. Select your state, provide an OpenAI API key, and ask your question below.
Need an API key? Get one free here from OpenAI.
Note: This tool is for informational purposes only. Always consult a licensed attorney for legal advice specific to your situation.
""",
elem_id="app-description"
)
# Examples Section (Card Style)
# Input Form Section (Card Style)
with gr.Group(elem_classes="card-style input-form-card"):
gr.Markdown("
Query Section
", elem_id="form-heading")
with gr.Column(elem_classes="input-field-group"):
api_key_input = gr.Textbox(
label="OpenAI API Key", type="password",
placeholder="Enter your API key (e.g., sk-...)",
info="Required to process your question. Securely used per request, not stored.",
elem_id="api-key-input", lines=1
)
with gr.Row(elem_classes="input-row"):
with gr.Column(elem_classes="input-field"):
query_input = gr.Textbox(
label="Curious about landlord-tenant laws in your state? Ask away!",
placeholder="E.g., What are the rules for security deposit returns in my state?",
lines=4, max_lines=8, elem_id="query-input"
)
with gr.Column(elem_classes="input-field"):
state_input = gr.Dropdown(
label="Select State", choices=dropdown_choices, value=initial_value,
allow_custom_value=False, elem_id="state-dropdown"
)
with gr.Row(elem_classes="button-row"):
clear_button = gr.Button(
"Clear Inputs", variant="secondary", elem_id="clear-button",
elem_classes=["gr-button-secondary"]
)
submit_button = gr.Button(
"Submit Question", variant="primary", elem_id="submit-button",
elem_classes=["gr-button-primary"]
)
# Output Section (Card Style)
with gr.Group(elem_classes="card-style output-card"):
# Wrap the output markdown for better targeting if needed
with gr.Column(): # Add column wrapper if needed for spacing/styling
output = gr.Markdown(
value="
The response will appear here after submitting a question.
",
elem_id="output-content",
elem_classes="output-content-wrapper" # Apply styling to this wrapper
)
# Example Questions Section (Card Style)
if example_queries:
with gr.Group(elem_classes="card-style examples-card"):
gr.Examples(
examples=example_queries,
inputs=[query_input, state_input],
label="Example Sample Questions", # Uses .gr-examples-header class
examples_per_page=6
)
else:
with gr.Group(elem_classes="card-style examples-card"): # Still use card style for consistency
gr.Markdown(
"
Sample questions could not be loaded. Please ensure states are available.
"
)
# Footer Section (No Card Style)
with gr.Group(elem_classes="footer-section"):
gr.Markdown(
"""
**Disclaimer**: This tool is for informational purposes only and does not constitute legal advice.
Developed by **Nischal Subedi**. Connect on
LinkedIn or explore insights at
Substack.
""", elem_id="app-footer"
)
# --- Event Listeners ---
submit_button.click(
fn=query_interface_wrapper,
inputs=[api_key_input, query_input, state_input],
outputs=output,
api_name="submit_query"
)
clear_button.click(
fn=lambda: (
"", "", initial_value,
"
Inputs cleared. Ready for your next question.
"
),
inputs=[],
outputs=[api_key_input, query_input, state_input, output]
)
logging.info("Refined Gradio interface created successfully.")
return demo
# --- Main Execution Block ---
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)
os.makedirs(os.path.dirname(VECTOR_DB_PATH), exist_ok=True)
os.makedirs(os.path.dirname(PDF_PATH), exist_ok=True)
logging.info(f"Using PDF path: {PDF_PATH}")
logging.info(f"Using Vector DB path: {VECTOR_DB_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 ---")
print(f"The required PDF file ('{os.path.basename(PDF_PATH)}') was not found at:")
print(f" {PDF_PATH}")
print(f"Please ensure the file exists or set 'PDF_PATH' environment variable.")
print(f"---------------------------\n")
exit(1)
logging.info("Initializing Vector Database...")
vector_db_instance = VectorDatabase(persist_directory=VECTOR_DB_PATH)
logging.info("Initializing RAG System...")
rag = RAGSystem(vector_db=vector_db_instance)
logging.info(f"Loading/Verifying data from PDF: {PDF_PATH}")
states_loaded_count = rag.load_pdf(PDF_PATH)
doc_count = vector_db_instance.document_collection.count() if vector_db_instance.document_collection else 0
state_count = vector_db_instance.state_collection.count() if vector_db_instance.state_collection else 0
total_items = doc_count + state_count
if total_items > 0:
logging.info(f"Data loading/verification complete. Vector DB contains {total_items} items. Found {states_loaded_count} distinct states.")
else:
logging.warning("Potential issue: PDF processed but Vector DB appears empty. Check PDF content/format and logs.")
print("\nWarning: No data loaded from PDF or found in DB. Application might not function correctly.\n")
logging.info("Setting up Gradio interface...")
app_interface = rag.gradio_interface()
SERVER_PORT = 7860
logging.info(f"Launching Gradio app on http://0.0.0.0:{SERVER_PORT}")
print("\n--- Gradio App Running ---")
print(f"Access the interface in your browser at: http://localhost:{SERVER_PORT} or http://
:{SERVER_PORT}")
print("--------------------------\n")
app_interface.launch(
server_name="0.0.0.0", server_port=SERVER_PORT,
share=False,
# enable_queue=True # Consider for higher traffic
)
except FileNotFoundError as fnf_error:
logging.error(f"Initialization failed due to a missing file: {str(fnf_error)}", exc_info=True)
print(f"\n--- STARTUP ERROR: File Not Found ---")
print(f"{str(fnf_error)}")
print(f"---------------------------------------\n")
exit(1)
except ImportError as import_error:
logging.error(f"Import error: {str(import_error)}. Check dependencies.", exc_info=True)
print(f"\n--- STARTUP ERROR: Missing Dependency ---")
print(f"Import Error: {str(import_error)}")
print(f"Please ensure required libraries are installed (e.g., pip install -r requirements.txt).")
print(f"-----------------------------------------\n")
exit(1)
except RuntimeError as runtime_error:
logging.error(f"A runtime error occurred during setup: {str(runtime_error)}", exc_info=True)
print(f"\n--- STARTUP ERROR: Runtime Problem ---")
print(f"Runtime Error: {str(runtime_error)}")
print(f"Check logs for details, often related to data loading or DB setup.")
print(f"--------------------------------------\n")
exit(1)
except Exception as e:
logging.error(f"An unexpected error occurred during application startup: {str(e)}", exc_info=True)
print(f"\n--- FATAL STARTUP ERROR ---")
print(f"An unexpected error stopped the application: {str(e)}")
print(f"Check logs for detailed traceback.")
print(f"---------------------------\n")
exit(1)