File size: 28,629 Bytes
b9756ef
 
 
bfee845
b9756ef
bfee845
 
ea0c3e1
b9756ef
 
 
bfee845
 
ea0c3e1
7c7cb71
 
 
 
 
ea0c3e1
b9756ef
 
 
bfee845
b9756ef
 
 
bfee845
 
 
7c7cb71
b9756ef
bfee845
 
 
 
 
 
 
 
7c7cb71
b9756ef
 
7c7cb71
b9756ef
bfee845
b9756ef
bfee845
b9756ef
bfee845
 
b9756ef
bfee845
 
 
b9756ef
bfee845
ea0c3e1
b9756ef
bfee845
b9756ef
bfee845
b9756ef
bfee845
 
 
 
 
 
 
 
 
 
b9756ef
bfee845
b9756ef
 
 
 
bfee845
 
b9756ef
bfee845
 
b9756ef
bfee845
b9756ef
bfee845
 
 
 
 
ea0c3e1
7c7cb71
b9756ef
7c7cb71
 
b9756ef
bfee845
 
 
 
 
 
 
 
 
 
7c7cb71
bfee845
7c7cb71
bfee845
 
 
 
 
 
 
7c7cb71
bfee845
7c7cb71
bfee845
 
 
ea0c3e1
bfee845
 
 
 
b9756ef
 
bfee845
7c7cb71
bfee845
 
b9756ef
bfee845
 
 
 
 
 
 
b9756ef
bfee845
7c7cb71
b9756ef
 
bfee845
b9756ef
bfee845
7c7cb71
b9756ef
bfee845
 
b9756ef
bfee845
 
b9756ef
bfee845
 
 
 
b9756ef
bfee845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9756ef
bfee845
 
b9756ef
 
 
7c7cb71
bfee845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9756ef
bfee845
 
 
 
 
 
 
 
 
b9756ef
bfee845
 
b9756ef
7c7cb71
b9756ef
bfee845
b9756ef
 
bfee845
 
 
 
 
 
b9756ef
bfee845
 
ea0c3e1
b9756ef
bfee845
 
 
 
b9756ef
bfee845
b9756ef
bfee845
 
 
 
b9756ef
 
bfee845
 
 
 
b9756ef
7c7cb71
bfee845
 
 
b9756ef
bfee845
 
 
 
 
 
 
 
7c7cb71
bfee845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9756ef
4c5479b
 
 
 
 
b9756ef
 
bfee845
4c5479b
bfee845
 
 
 
 
 
 
 
 
 
 
 
 
 
4c5479b
bfee845
 
 
 
 
 
4c5479b
bfee845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c5479b
 
bfee845
 
7c7cb71
 
bfee845
 
 
 
 
 
 
 
 
7c7cb71
 
 
bfee845
 
7c7cb71
bfee845
b9756ef
bfee845
 
 
7c7cb71
 
bfee845
 
 
 
 
7c7cb71
 
bfee845
 
 
4c5479b
bfee845
 
b9756ef
7c7cb71
 
bfee845
 
7c7cb71
 
bfee845
 
 
7c7cb71
 
bfee845
 
7c7cb71
 
bfee845
 
 
 
 
7c7cb71
 
 
bfee845
229d1b2
 
bfee845
 
 
 
 
 
229d1b2
bfee845
229d1b2
bfee845
7c7cb71
 
 
bfee845
 
7c7cb71
bfee845
7c7cb71
bfee845
 
 
 
 
 
 
7c7cb71
 
 
bfee845
 
 
ea0c3e1
bfee845
ea0c3e1
bfee845
b9756ef
bfee845
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9756ef
bfee845
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
import os
import json
import logging
from typing import Dict, List, Optional
from functools import lru_cache
import re

import gradio as gr
from langchain_openai import ChatOpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
# Make sure vector_db.py is in the same directory or accessible via PYTHONPATH
from vector_db import VectorDatabase # <-- This now imports the placeholder

# Enhanced logging for better debugging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s'
)

class RAGSystem:
    def __init__(self, vector_db: Optional[VectorDatabase] = None):
        logging.info("Initializing RAGSystem")
        # If no vector_db instance is passed, create one (uses placeholder for now)
        self.vector_db = vector_db if vector_db else VectorDatabase()
        self.llm = None
        self.chain = None

        # Using f-string for potentially better readability/maintenance if template gets complex
        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 initialize_llm(self, openai_api_key: str):
        """Initializes the LLM and the processing chain."""
        if not openai_api_key:
            logging.error("Attempted to initialize LLM without API key.")
            raise ValueError("OpenAI API key is required.")

        # Avoid re-initializing if already done with the same key implicitly
        # Note: This simple check doesn't handle key changes well.
        # A more robust approach might involve checking if self.llm's key matches.
        if self.llm and self.chain:
            # Check if the key is the same (conceptually - can't directly read from ChatOpenAI instance easily)
            # If key changes are expected, need a more complex re-initialization logic.
             logging.info("LLM and Chain already initialized.")
             return

        try:
            logging.info("Initializing OpenAI LLM...")
            self.llm = ChatOpenAI(
                temperature=0.2,
                openai_api_key=openai_api_key,
                model_name="gpt-3.5-turbo",
                max_tokens=1500, # Max response tokens
                request_timeout=45 # Increased timeout
            )
            logging.info("OpenAI LLM initialized successfully.")

            self.chain = LLMChain(llm=self.llm, prompt=self.prompt_template)
            logging.info("LLMChain created successfully.")
        except Exception as e:
            logging.error(f"Failed to initialize OpenAI LLM or Chain: {str(e)}")
            # Reset llm/chain if initialization failed partially
            self.llm = None
            self.chain = None
            raise  # Re-raise the exception to be caught by the caller

    def extract_statutes(self, text: str) -> str:
        """
        Extract statute citations from the given text using a refined regex pattern.
        Returns a string of valid statutes, one per line, or a message if none are found.
        """
        # Refined Regex: Aims to capture common US statute formats. May need tuning.
        # - Allows state abbreviations (e.g., CA, NY) or full names (e.g, California)
        # - Looks for common terms like Code, Laws, Statutes, CCP, USC, ILCS
        # - Requires section symbol (§) followed by numbers/hyphens/parenthesized parts
        # - Tries to avoid matching simple parenthetical remarks like '(a)' or '(found here)'
        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)\s+§\s*[\d\-]+(?:\.\d+)?(?:\([\w\.]+\))?|Title\s+\d+\s+USC\s+§\s*\d+(?:-\d+)?\b'

        # Use finditer for more control if needed later, findall is simpler for now
        statutes = re.findall(statute_pattern, text, re.IGNORECASE)

        valid_statutes = []
        # Basic filtering (can be improved)
        for statute in statutes:
             # Remove potential leading/trailing spaces and ensure it looks like a statute
             statute = statute.strip()
             if '§' in statute and any(char.isdigit() for char in statute):
                 # Avoid things that might just be section references like '(a)' or URLs mistakenly caught
                 if not re.match(r'^\([\w\.]+\)$', statute) and 'http' not in statute:
                     valid_statutes.append(statute)

        if valid_statutes:
            # Deduplicate while preserving order
            seen = set()
            unique_statutes = [s for s in valid_statutes if not (s in seen or seen.add(s))]
            logging.info(f"Extracted {len(unique_statutes)} unique statutes.")
            return "\n".join(f"- {s}" for s in unique_statutes) # Format as list

        logging.info("No statutes found matching the pattern in the context.")
        return "No specific statutes found in the provided context."

    # Cache results for the same query, state, and key (key isn't explicitly cached but influences init)
    @lru_cache(maxsize=100)
    def process_query(self, query: str, state: str, openai_api_key: str, n_results: int = 5) -> Dict[str, any]:
        """Processes the user query using RAG."""
        logging.info(f"Processing query: '{query}' for state: '{state}' with n_results={n_results}")

        # 1. Input Validation
        if not state:
            logging.warning("No state provided for query.")
            return {
                "answer": "**Error:** Please select a state to proceed with your query.",
                "context_used": "N/A"
            }
        if not query:
            logging.warning("No query provided.")
            return {
                "answer": "**Error:** Please enter your question in the Query box.",
                "context_used": "N/A"
            }
        if not openai_api_key:
            logging.warning("No OpenAI API key provided.")
            return {
                "answer": "**Error:** Please provide an OpenAI API key to proceed.",
                "context_used": "N/A"
            }

        # 2. Initialize LLM (if needed)
        try:
            # Initialize LLM here, ensuring it's ready before DB query or LLM call
            self.initialize_llm(openai_api_key)
        except Exception as e:
            logging.error(f"LLM Initialization failed: {str(e)}")
            return {
                "answer": f"**Error:** Failed to initialize the AI model. Please check your API key and network connection. ({str(e)})",
                "context_used": "N/A"
            }

        # Ensure chain is initialized after initialize_llm() call succeeds
        if not self.chain:
             logging.error("LLM Chain is not initialized after attempting LLM initialization.")
             return {
                  "answer": "**Error:** Internal system error. Failed to prepare the processing chain.",
                  "context_used": "N/A"
             }


        # 3. Query Vector Database
        context = "No relevant context found." # Default context
        try:
            results = self.vector_db.query(query, state=state, n_results=n_results)
            logging.info(f"Vector database query successful for state '{state}'.")
            # logging.debug(f"Raw query results: {json.dumps(results, indent=2)}")

            context_parts = []
            # Process document results carefully, checking list structure
            doc_results = results.get("document_results", {})
            docs = doc_results.get("documents", [[]])[0] # Safely access first list
            metadatas = doc_results.get("metadatas", [[]])[0] # Safely access first list

            if docs and metadatas and len(docs) == len(metadatas):
                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"[{state_label} - Chunk {chunk_id}] {doc_content}")
            else:
                logging.warning("No document results or mismatch in docs/metadata lengths.")

            # Process state summary results
            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):
                 for i, state_doc_content in enumerate(state_docs):
                    metadata = state_metadatas[i]
                    state_label = metadata.get('state', state) # Use provided state if not in metadata
                    context_parts.append(f"[{state_label} - Summary] {state_doc_content}")
            else:
                 logging.warning("No state summary results found.")

            if context_parts:
                context = "\n\n---\n\n".join(context_parts)
                logging.info(f"Constructed context with {len(context_parts)} parts.")
                # Limit context length if necessary (though max_tokens helps)
                # max_context_len = 5000 # Example limit
                # if len(context) > max_context_len:
                #     context = context[:max_context_len] + "\n... [Context Truncated]"
                #     logging.warning("Context truncated due to length.")
            else:
                logging.warning("No relevant context parts found after processing DB results.")
                context = "No relevant context could be retrieved from the available documents for your query and selected state."


        except Exception as e:
            logging.error(f"Vector database query or context processing failed: {str(e)}", exc_info=True)
            # Fallback to general knowledge if DB fails, but inform the user
            context = f"An error occurred while retrieving specific legal documents ({str(e)}). I will attempt to answer based on general knowledge, but it may lack state-specific details."
            statutes_from_context = "Statute retrieval skipped due to context error."


        # 4. Extract Statutes from Retrieved Context (if context retrieval succeeded)
        statutes_from_context = "No specific statutes found in the provided context."
        if "An error occurred while retrieving" not in context and "No relevant context found" not in context:
             try:
                statutes_from_context = self.extract_statutes(context)
                logging.info(f"Statutes extracted: {statutes_from_context}")
             except Exception as e:
                 logging.error(f"Error extracting statutes: {e}")
                 statutes_from_context = "Error occurred during statute extraction."


        # 5. Generate Answer using LLM
        try:
            logging.info("Invoking LLMChain...")
            llm_input = {
                "query": query,
                "context": context,
                "state": state,
                "statutes": statutes_from_context
            }
            # logging.debug(f"Input to LLMChain: {json.dumps(llm_input, indent=2)}") # Be careful logging sensitive data
            answer_dict = self.chain.invoke(llm_input)
            answer_text = answer_dict.get('text', '').strip()

            if not answer_text:
                 logging.warning("LLM returned an empty answer.")
                 answer_text = "I received an empty response from the AI model. This might be a temporary issue. Please try rephrasing your question or try again later."

            logging.info("LLM generated answer successfully.")
            # logging.debug(f"Raw answer text from LLM: {answer_text}")

            return {
                "answer": answer_text,
                "context_used": context # Return the context for potential display or debugging
            }
        except Exception as e:
            logging.error(f"LLM processing failed: {str(e)}", exc_info=True)
            # Provide a more informative error message
            error_message = f"**Error:** An error occurred while generating the answer. This could be due to issues with the AI model connection, API key limits, or the complexity of the request. Please try again later.\n\nDetails: {str(e)}"
            # Check for common API errors
            if "authentication" in str(e).lower():
                 error_message = "**Error:** Authentication failed. Please check if your OpenAI API key is correct and active."
            elif "rate limit" in str(e).lower():
                 error_message = "**Error:** You've exceeded your OpenAI API usage limit. Please check your plan or try again later."

            return {
                "answer": error_message,
                "context_used": context # Still return context for debugging
            }

    def get_states(self) -> List[str]:
        """Retrieves the list of available states from the VectorDatabase."""
        try:
            states = self.vector_db.get_states()
            if not states:
                 logging.warning("No states returned from vector_db.get_states(). Using default list.")
                 # Provide a fallback list if needed
                 return ["California", "New York", "Texas", "Florida", "Oregon", "Alabama", "Select State..."]
            logging.info(f"Retrieved {len(states)} states from VectorDatabase.")
            return sorted(list(set(states))) # Ensure uniqueness and sort
        except Exception as e:
            logging.error(f"Failed to get states from VectorDatabase: {str(e)}")
            return ["Error fetching states"] # Indicate error in the dropdown

    def load_pdf(self, pdf_path: str) -> int:
        """Loads and processes the PDF using the VectorDatabase."""
        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 PDF: {pdf_path}")
            num_states = self.vector_db.process_and_load_pdf(pdf_path)
            if num_states > 0:
                logging.info(f"Successfully processed PDF. Found data for {num_states} states.")
            else:
                logging.warning(f"Processed PDF, but found no state-specific data according to vector_db implementation.")
            return num_states
        except Exception as e:
            logging.error(f"Failed to load or process PDF '{pdf_path}': {str(e)}", exc_info=True)
            # Depending on vector_db, this might leave the DB in a partial state.
            return 0 # Indicate failure


    def gradio_interface(self):
        """Creates and returns the Gradio interface."""

        # Define the core function that Gradio will call
        def query_interface(api_key: str, query: str, state: str) -> str:
            # Clear cache for each new request if desired, or rely on LRU cache parameters
            # self.process_query.cache_clear()
            logging.info(f"Gradio interface received query: '{query}', state: '{state}'")
            result = self.process_query(query=query, state=state, openai_api_key=api_key)

            # Format the response clearly using Markdown
            answer = result.get("answer", "**Error:** No answer generated.")
            # context_used = result.get("context_used", "N/A") # Optional: show context

            # Simple formatting for now, can be enhanced
            formatted_response = f"### Answer for {state}:\n\n{answer}"

            # Optional: Include context for debugging/transparency (can be long)
            # formatted_response += f"\n\n---\n<details><summary>Context Used (Debug)</summary>\n\n```\n{context_used}\n```\n</details>"

            return formatted_response

        # Get states for the dropdown
        available_states = self.get_states()
        if not available_states or "Error" in available_states[0]:
             logging.error("Could not load states for dropdown. Interface might be unusable.")
             # Handle case where states couldn't be loaded
             available_states = ["Error: Could not load states"]


        # Define example queries (only query and state needed for examples UI)
        example_queries = [
            ["What is the rent due date law?", "California"],
            ["What are the rules for security deposit returns?", "New York"],
            ["Can a landlord enter without notice?", "Texas"],
            ["What are the eviction notice requirements?", "Florida"],
            ["Are there rent control laws?", "Oregon"]
        ]

        # Custom CSS (minor adjustments for clarity if needed, your CSS is quite comprehensive)
        custom_css = """
            /* Your existing CSS here */
            .gr-form { max-width: 900px; margin: 0 auto; padding: 30px; /* ... */ }
            .gr-title { font-size: 2.5em; font-weight: 700; color: #1a3c34; /* ... */ }
            /* ... rest of your CSS ... */
             .output-markdown {
                 background: #f9fafb; /* Light mode bg */
                 color: #1f2937; /* Light mode text */
                 padding: 20px;
                 border-radius: 12px;
                 border: 1px solid #e5e7eb;
                 font-size: 1.05em; /* Slightly larger text */
                 line-height: 1.7; /* More spacing */
                 box-shadow: 0 2px 12px rgba(0, 0, 0, 0.05);
                 margin-top: 20px; /* Add space above output */
            }
            .output-markdown h3 { /* Style the 'Answer for STATE:' heading */
                 margin-top: 0;
                 margin-bottom: 15px;
                 color: #1a3c34; /* Match title color */
                 border-bottom: 1px solid #e5e7eb;
                 padding-bottom: 8px;
            }
            .output-markdown p { margin-bottom: 1em; }
            .output-markdown ul, .output-markdown ol { margin-left: 20px; margin-bottom: 1em; }
            .output-markdown li { margin-bottom: 0.5em; }

             /* Dark mode adjustments */
             @media (prefers-color-scheme: dark) {
                 .output-markdown {
                     background: #374151 !important; /* Dark mode bg */
                     color: #f3f4f6 !important; /* Dark mode text */
                     border-color: #4b5563 !important;
                 }
                  .output-markdown h3 {
                       color: #f3f4f6; /* Dark title */
                       border-bottom: 1px solid #4b5563;
                  }
             }
        """

        # Build the Gradio Blocks interface
        with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo: # Use a slightly different theme
            gr.Markdown(
                """
                <div style="text-align: center;">
                    <img src="https://img.icons8.com/plasticine/100/000000/document.png" alt="Icon" style="vertical-align: middle; height: 50px;">
                    <h1 class='gr-title' style='display: inline-block; margin-bottom: 0; vertical-align: middle; margin-left: 10px;'>Landlord-Tenant Rights Bot</h1>
                </div>
                <p class='gr-description'>
                Ask questions about tenant rights and landlord-tenant laws based on state-specific legal documents.
                Provide your OpenAI API key, select a state, and enter your question.
                Get your key from <a href='https://platform.openai.com/api-keys' target='_blank'>OpenAI</a>.
                </p>
                """
            )

            with gr.Column(elem_classes="gr-form"): # Use elem_classes if defined in CSS, otherwise elem_id
                # Input Components defined *within* gr.Blocks()
                api_key_input = gr.Textbox(
                    label="OpenAI API Key",
                    type="password",
                    placeholder="Enter your OpenAI API key (e.g., sk-...)",
                    info="Required to process your query.", # Use info parameter
                    #elem_classes="input-field" # Use if defined in CSS
                )
                query_input = gr.Textbox(
                    label="Your Question",
                    placeholder="e.g., What are the rules for security deposit returns?",
                    lines=4, # Increased lines slightly
                    info="Enter your question about landlord-tenant law here.",
                    #elem_classes="input-field"
                )
                state_input = gr.Dropdown(
                    label="Select State",
                    choices=available_states,
                    value=available_states[0] if available_states else None, # Default to first state or None
                    allow_custom_value=False,
                    info="Select the state your question applies to.",
                    #elem_classes="input-field"
                )

                with gr.Row():
                    clear_button = gr.Button("Clear Inputs", variant="secondary")
                    submit_button = gr.Button("Submit Query", variant="primary")

                output = gr.Markdown(
                    label="Answer",
                    value="Your answer will appear here...", # Initial placeholder text
                    elem_classes="output-markdown" # Apply custom class for styling
                )

                gr.Markdown("---") # Separator
                gr.Markdown("### Examples")
                gr.Examples(
                    examples=example_queries,
                    inputs=[query_input, state_input], # Examples only fill these two
                    # outputs=output, # Output is handled by the main submit button click
                    # fn=query_interface, # Don't run the function directly on example click
                    # cache_examples=False, # Don't cache example runs if fn were used
                    label="Click an example to load it",
                    examples_per_page=5
                )


                gr.Markdown(
                    """
                    <div class='footnote' style='margin-top: 30px; padding-top: 15px; border-top: 1px solid #e5e7eb; text-align: center; font-size: 0.9em; color: #6c757d;'>
                    Developed by Nischal Subedi. Follow on
                    <a href='https://www.linkedin.com/in/nischal1/' target='_blank' style='color: #007bff; text-decoration: none;'>LinkedIn</a> |
                    Read insights on <a href='https://datascientistinsights.substack.com/' target='_blank' style='color: #007bff; text-decoration: none;'>Substack</a>.
                    <br>Disclaimer: This bot provides informational summaries based on AI interpretation and retrieved data. It is not a substitute for professional legal advice.
                    </div>
                    """
                 , elem_classes="footnote") # Apply footnote class if defined in CSS

            # Connect Actions to Functions
            submit_button.click(
                fn=query_interface,
                inputs=[api_key_input, query_input, state_input],
                outputs=output,
                api_name="submit_query" # Add API name for potential programmatic use
            )
            # Clear button clears all inputs and the output
            clear_button.click(
                fn=lambda: ( # Return empty values for each output component
                        "", # api_key_input
                        "", # query_input
                        available_states[0] if available_states else None, # state_input (reset to default)
                        "Cleared. Ready for new query..." # output
                        ),
                inputs=[], # No inputs needed for clear
                outputs=[api_key_input, query_input, state_input, output]
            )

        logging.info("Gradio interface created successfully.")
        return demo # Corrected return variable name


# Main execution block
if __name__ == "__main__":
    logging.info("Starting application setup...")
    try:
        # --- Configuration ---
        PDF_PATH = os.getenv("PDF_PATH", "data/tenant-landlord.pdf") # Use env var or default
        VECTOR_DB_PATH = os.getenv("VECTOR_DB_PATH", "./vector_db_store") # For persistent DBs

        # Check if PDF exists
        if not os.path.exists(PDF_PATH):
             logging.error(f"FATAL: PDF file not found at the specified path: {PDF_PATH}")
             logging.error("Please ensure the PDF file exists or set the PDF_PATH environment variable correctly.")
             # Exit if the core data file is missing
             exit(1) # Or raise an exception


        # --- Initialization ---
        # Initialize VectorDatabase (using placeholder for now)
        # Pass path if your implementation uses it (like ChromaDB)
        vector_db_instance = VectorDatabase(persist_directory=VECTOR_DB_PATH)

        # Initialize RAGSystem with the database instance
        rag = RAGSystem(vector_db=vector_db_instance)

        # --- Data Loading ---
        # Load the PDF data into the vector database
        # This step is crucial and needs the *real* vector_db.py
        logging.info(f"Loading data from PDF: {PDF_PATH}")
        states_loaded = rag.load_pdf(PDF_PATH)
        if states_loaded == 0 and not isinstance(vector_db_instance, VectorDatabase): # Check if using placeholder
              logging.warning("PDF loading reported 0 states. Check PDF content and vector_db implementation.")
              # Decide if you want to proceed without data or halt

        # --- Interface Setup & Launch ---
        logging.info("Setting up Gradio interface...")
        app_interface = rag.gradio_interface()

        logging.info("Launching Gradio app...")
        # Launch the Gradio app
        # share=True generates a public link (use with caution)
        # server_name="0.0.0.0" makes it accessible on the network
        app_interface.launch(server_name="0.0.0.0", server_port=7860, share=False)
        # For cloud deployments (like Hugging Face Spaces), you might not need server_name/port

    except FileNotFoundError as fnf_error:
         logging.error(f"Initialization failed: {str(fnf_error)}")
         # No need to raise again, already logged. Maybe print a message.
         print(f"Error: {str(fnf_error)}. Please check file paths.")
    except ImportError as import_error:
         logging.error(f"Import error: {str(import_error)}. Please ensure all required libraries (gradio, langchain, openai, etc.) and the 'vector_db.py' file are installed and accessible.")
         print(f"Import Error: {str(import_error)}. Check your dependencies.")
    except Exception as e:
        # Catch any other unexpected errors during setup or launch
        logging.error(f"An unexpected error occurred during application startup: {str(e)}", exc_info=True)
        print(f"Fatal Error: {str(e)}. Check logs for details.")