Nischal Subedi
commited on
Commit
·
0634f1a
1
Parent(s):
26e5b39
much enhanced UI
Browse files- .gitattributes +0 -1
- app.py +636 -391
- requirements.txt +1 -1
- vector_db.py +2 -2
.gitattributes
CHANGED
@@ -33,5 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
-
data/tenant-landlord.pdf filter=lfs diff=lfs merge=lfs -text
|
37 |
tenant-landlord.pdf filter=lfs diff=lfs merge=lfs -text
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
36 |
tenant-landlord.pdf filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
@@ -1,43 +1,63 @@
|
|
1 |
import os
|
2 |
-
import json
|
3 |
import logging
|
4 |
from typing import Dict, List, Optional
|
5 |
from functools import lru_cache
|
6 |
import re
|
7 |
|
8 |
import gradio as gr
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
from langchain.prompts import PromptTemplate
|
11 |
from langchain.chains import LLMChain
|
12 |
-
# Make sure vector_db.py is in the same directory or accessible via PYTHONPATH
|
13 |
-
from vector_db import VectorDatabase # <-- This now imports the placeholder
|
14 |
|
15 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
logging.basicConfig(
|
17 |
level=logging.INFO,
|
18 |
format='%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s'
|
19 |
)
|
20 |
|
|
|
21 |
class RAGSystem:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
def __init__(self, vector_db: Optional[VectorDatabase] = None):
|
23 |
logging.info("Initializing RAGSystem")
|
24 |
-
# If no vector_db instance is passed, create one (uses placeholder for now)
|
25 |
self.vector_db = vector_db if vector_db else VectorDatabase()
|
26 |
self.llm = None
|
27 |
self.chain = None
|
28 |
-
|
29 |
-
# Using f-string for potentially better readability/maintenance if template gets complex
|
30 |
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.
|
31 |
|
32 |
Instructions:
|
33 |
-
*
|
34 |
-
*
|
35 |
-
*
|
36 |
-
*
|
37 |
-
*
|
38 |
-
*
|
39 |
-
*
|
40 |
-
*
|
41 |
|
42 |
Question: {query}
|
43 |
State: {state}
|
@@ -56,491 +76,716 @@ Answer:"""
|
|
56 |
)
|
57 |
logging.info("RAGSystem initialized.")
|
58 |
|
59 |
-
def initialize_llm(self, openai_api_key: str):
|
60 |
-
"""Initializes the LLM and the processing chain."""
|
61 |
-
if not openai_api_key:
|
62 |
-
logging.error("Attempted to initialize LLM without API key.")
|
63 |
-
raise ValueError("OpenAI API key is required.")
|
64 |
-
|
65 |
-
# Avoid re-initializing if already done with the same key implicitly
|
66 |
-
# Note: This simple check doesn't handle key changes well.
|
67 |
-
# A more robust approach might involve checking if self.llm's key matches.
|
68 |
-
if self.llm and self.chain:
|
69 |
-
# Check if the key is the same (conceptually - can't directly read from ChatOpenAI instance easily)
|
70 |
-
# If key changes are expected, need a more complex re-initialization logic.
|
71 |
-
logging.info("LLM and Chain already initialized.")
|
72 |
-
return
|
73 |
-
|
74 |
-
try:
|
75 |
-
logging.info("Initializing OpenAI LLM...")
|
76 |
-
self.llm = ChatOpenAI(
|
77 |
-
temperature=0.2,
|
78 |
-
openai_api_key=openai_api_key,
|
79 |
-
model_name="gpt-3.5-turbo",
|
80 |
-
max_tokens=1500, # Max response tokens
|
81 |
-
request_timeout=45 # Increased timeout
|
82 |
-
)
|
83 |
-
logging.info("OpenAI LLM initialized successfully.")
|
84 |
-
|
85 |
-
self.chain = LLMChain(llm=self.llm, prompt=self.prompt_template)
|
86 |
-
logging.info("LLMChain created successfully.")
|
87 |
-
except Exception as e:
|
88 |
-
logging.error(f"Failed to initialize OpenAI LLM or Chain: {str(e)}")
|
89 |
-
# Reset llm/chain if initialization failed partially
|
90 |
-
self.llm = None
|
91 |
-
self.chain = None
|
92 |
-
raise # Re-raise the exception to be caught by the caller
|
93 |
-
|
94 |
def extract_statutes(self, text: str) -> str:
|
95 |
-
|
96 |
-
Extract statute citations from the given text using a refined regex pattern.
|
97 |
-
Returns a string of valid statutes, one per line, or a message if none are found.
|
98 |
-
"""
|
99 |
-
# Refined Regex: Aims to capture common US statute formats. May need tuning.
|
100 |
-
# - Allows state abbreviations (e.g., CA, NY) or full names (e.g, California)
|
101 |
-
# - Looks for common terms like Code, Laws, Statutes, CCP, USC, ILCS
|
102 |
-
# - Requires section symbol (§) followed by numbers/hyphens/parenthesized parts
|
103 |
-
# - Tries to avoid matching simple parenthetical remarks like '(a)' or '(found here)'
|
104 |
-
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'
|
105 |
-
|
106 |
-
# Use finditer for more control if needed later, findall is simpler for now
|
107 |
statutes = re.findall(statute_pattern, text, re.IGNORECASE)
|
108 |
-
|
109 |
valid_statutes = []
|
110 |
-
# Basic filtering (can be improved)
|
111 |
for statute in statutes:
|
112 |
-
|
113 |
-
|
114 |
-
if '§' in statute and any(char.isdigit() for char in statute):
|
115 |
-
# Avoid things that might just be section references like '(a)' or URLs mistakenly caught
|
116 |
if not re.match(r'^\([\w\.]+\)$', statute) and 'http' not in statute:
|
117 |
-
|
|
|
118 |
|
119 |
if valid_statutes:
|
120 |
-
# Deduplicate while preserving order
|
121 |
seen = set()
|
122 |
-
unique_statutes = [s for s in valid_statutes if not (s in seen or seen.add(s))]
|
123 |
logging.info(f"Extracted {len(unique_statutes)} unique statutes.")
|
124 |
-
return "\n".join(f"- {s}" for s in unique_statutes)
|
125 |
|
126 |
logging.info("No statutes found matching the pattern in the context.")
|
127 |
return "No specific statutes found in the provided context."
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
if not
|
137 |
-
logging.warning("No state provided for query.")
|
138 |
-
return {
|
139 |
-
"answer": "**Error:** Please select a state to proceed with your query.",
|
140 |
-
"context_used": "N/A"
|
141 |
-
}
|
142 |
-
if not query:
|
143 |
logging.warning("No query provided.")
|
144 |
-
return {
|
145 |
-
|
146 |
-
|
147 |
-
}
|
148 |
-
|
149 |
-
logging.warning("No OpenAI API key provided.")
|
150 |
-
return {
|
151 |
-
"answer": "**Error:** Please provide an OpenAI API key to proceed.",
|
152 |
-
"context_used": "N/A"
|
153 |
-
}
|
154 |
-
|
155 |
-
# 2. Initialize LLM (if needed)
|
156 |
try:
|
157 |
-
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
159 |
except Exception as e:
|
160 |
-
logging.error(f"LLM Initialization failed: {str(e)}")
|
161 |
-
|
162 |
-
|
163 |
-
"
|
164 |
-
}
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
logging.error("LLM Chain is not initialized after attempting LLM initialization.")
|
169 |
-
return {
|
170 |
-
"answer": "**Error:** Internal system error. Failed to prepare the processing chain.",
|
171 |
-
"context_used": "N/A"
|
172 |
-
}
|
173 |
-
|
174 |
-
|
175 |
-
# 3. Query Vector Database
|
176 |
-
context = "No relevant context found." # Default context
|
177 |
try:
|
|
|
178 |
results = self.vector_db.query(query, state=state, n_results=n_results)
|
179 |
-
logging.info(f"Vector
|
180 |
-
# logging.debug(f"Raw query results: {json.dumps(results, indent=2)}")
|
181 |
|
182 |
context_parts = []
|
183 |
-
# Process document results carefully, checking list structure
|
184 |
doc_results = results.get("document_results", {})
|
185 |
-
docs = doc_results.get("documents", [[]])[0]
|
186 |
-
metadatas = doc_results.get("metadatas", [[]])[0]
|
187 |
-
|
188 |
if docs and metadatas and len(docs) == len(metadatas):
|
|
|
189 |
for i, doc_content in enumerate(docs):
|
190 |
metadata = metadatas[i]
|
191 |
state_label = metadata.get('state', 'Unknown State')
|
192 |
chunk_id = metadata.get('chunk_id', 'N/A')
|
193 |
-
context_parts.append(f"
|
194 |
-
else:
|
195 |
-
logging.warning("No document results or mismatch in docs/metadata lengths.")
|
196 |
|
197 |
-
# Process state summary results
|
198 |
state_results_data = results.get("state_results", {})
|
199 |
state_docs = state_results_data.get("documents", [[]])[0]
|
200 |
state_metadatas = state_results_data.get("metadatas", [[]])[0]
|
201 |
-
|
202 |
if state_docs and state_metadatas and len(state_docs) == len(state_metadatas):
|
203 |
-
|
|
|
204 |
metadata = state_metadatas[i]
|
205 |
-
state_label = metadata.get('state', state)
|
206 |
-
context_parts.append(f"
|
207 |
-
else:
|
208 |
-
logging.warning("No state summary results found.")
|
209 |
|
210 |
if context_parts:
|
211 |
context = "\n\n---\n\n".join(context_parts)
|
212 |
-
logging.info(f"Constructed context with {len(context_parts)} parts.")
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
else:
|
219 |
-
logging.warning("No relevant context parts found
|
220 |
-
context = "No relevant context could be retrieved from the
|
221 |
-
|
222 |
|
223 |
except Exception as e:
|
224 |
-
logging.error(f"Vector
|
225 |
-
|
226 |
-
|
227 |
-
statutes_from_context = "Statute retrieval skipped due to context error."
|
228 |
-
|
229 |
-
|
230 |
-
# 4. Extract Statutes from Retrieved Context (if context retrieval succeeded)
|
231 |
-
statutes_from_context = "No specific statutes found in the provided context."
|
232 |
-
if "An error occurred while retrieving" not in context and "No relevant context found" not in context:
|
233 |
-
try:
|
234 |
-
statutes_from_context = self.extract_statutes(context)
|
235 |
-
logging.info(f"Statutes extracted: {statutes_from_context}")
|
236 |
-
except Exception as e:
|
237 |
-
logging.error(f"Error extracting statutes: {e}")
|
238 |
-
statutes_from_context = "Error occurred during statute extraction."
|
239 |
-
|
240 |
|
241 |
-
# 5. Generate Answer using LLM
|
242 |
try:
|
243 |
-
logging.info("Invoking LLMChain...")
|
244 |
-
llm_input = {
|
245 |
-
|
246 |
-
"context": context,
|
247 |
-
"state": state,
|
248 |
-
"statutes": statutes_from_context
|
249 |
-
}
|
250 |
-
# logging.debug(f"Input to LLMChain: {json.dumps(llm_input, indent=2)}") # Be careful logging sensitive data
|
251 |
-
answer_dict = self.chain.invoke(llm_input)
|
252 |
answer_text = answer_dict.get('text', '').strip()
|
253 |
|
254 |
if not answer_text:
|
255 |
-
|
256 |
-
|
|
|
|
|
257 |
|
258 |
-
|
259 |
-
# logging.debug(f"Raw answer text from LLM: {answer_text}")
|
260 |
|
261 |
-
return {
|
262 |
-
"answer": answer_text,
|
263 |
-
"context_used": context # Return the context for potential display or debugging
|
264 |
-
}
|
265 |
except Exception as e:
|
266 |
logging.error(f"LLM processing failed: {str(e)}", exc_info=True)
|
267 |
-
|
268 |
-
|
269 |
-
# Check for common API errors
|
270 |
if "authentication" in str(e).lower():
|
271 |
-
|
|
|
272 |
elif "rate limit" in str(e).lower():
|
273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
274 |
|
275 |
-
|
276 |
-
|
277 |
-
"context_used": context # Still return context for debugging
|
278 |
-
}
|
279 |
|
280 |
def get_states(self) -> List[str]:
|
281 |
-
"""Retrieves the list of available states from the VectorDatabase."""
|
282 |
try:
|
283 |
states = self.vector_db.get_states()
|
284 |
if not states:
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
logging.info(f"Retrieved {len(
|
289 |
-
return
|
290 |
except Exception as e:
|
291 |
-
logging.error(f"Failed to get states from VectorDatabase: {str(e)}")
|
292 |
-
return ["Error
|
293 |
|
294 |
def load_pdf(self, pdf_path: str) -> int:
|
295 |
-
"""Loads and processes the PDF using the VectorDatabase."""
|
296 |
if not os.path.exists(pdf_path):
|
297 |
-
|
298 |
-
|
299 |
try:
|
300 |
-
logging.info(f"Attempting to load PDF: {pdf_path}")
|
301 |
-
|
302 |
-
|
303 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
304 |
else:
|
305 |
-
logging.warning(f"
|
306 |
-
|
|
|
307 |
except Exception as e:
|
308 |
logging.error(f"Failed to load or process PDF '{pdf_path}': {str(e)}", exc_info=True)
|
309 |
-
|
310 |
-
return 0 # Indicate failure
|
311 |
|
312 |
|
|
|
313 |
def gradio_interface(self):
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
318 |
-
#
|
319 |
-
|
320 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
321 |
result = self.process_query(query=query, state=state, openai_api_key=api_key)
|
322 |
|
323 |
-
# Format the response
|
324 |
-
answer = result.get("answer", "
|
325 |
-
# context_used = result.get("context_used", "N/A") # Optional: show context
|
326 |
|
327 |
-
#
|
328 |
-
|
|
|
|
|
|
|
329 |
|
330 |
-
#
|
331 |
-
|
|
|
|
|
|
|
|
|
332 |
|
333 |
return formatted_response
|
334 |
|
335 |
-
# Get
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
["
|
348 |
-
|
349 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
350 |
]
|
351 |
-
|
352 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
353 |
custom_css = """
|
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 |
-
with gr.Blocks(css=custom_css,
|
396 |
-
|
397 |
-
"""
|
398 |
-
<div style="text-align: center;">
|
399 |
-
<img src="https://img.icons8.com/plasticine/100/000000/document.png" alt="Icon" style="vertical-align: middle; height: 50px;">
|
400 |
-
<h1 class='gr-title' style='display: inline-block; margin-bottom: 0; vertical-align: middle; margin-left: 10px;'>Landlord-Tenant Rights Bot</h1>
|
401 |
-
</div>
|
402 |
-
<p class='gr-description'>
|
403 |
-
Ask questions about tenant rights and landlord-tenant laws based on state-specific legal documents.
|
404 |
-
Provide your OpenAI API key, select a state, and enter your question.
|
405 |
-
Get your key from <a href='https://platform.openai.com/api-keys' target='_blank'>OpenAI</a>.
|
406 |
-
</p>
|
407 |
-
"""
|
408 |
-
)
|
409 |
|
410 |
-
|
411 |
-
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
)
|
419 |
-
query_input = gr.Textbox(
|
420 |
-
label="Your Question",
|
421 |
-
placeholder="e.g., What are the rules for security deposit returns?",
|
422 |
-
lines=4, # Increased lines slightly
|
423 |
-
info="Enter your question about landlord-tenant law here.",
|
424 |
-
#elem_classes="input-field"
|
425 |
-
)
|
426 |
-
state_input = gr.Dropdown(
|
427 |
-
label="Select State",
|
428 |
-
choices=available_states,
|
429 |
-
value=available_states[0] if available_states else None, # Default to first state or None
|
430 |
-
allow_custom_value=False,
|
431 |
-
info="Select the state your question applies to.",
|
432 |
-
#elem_classes="input-field"
|
433 |
)
|
434 |
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
label="Answer",
|
441 |
-
value="Your answer will appear here...", # Initial placeholder text
|
442 |
-
elem_classes="output-markdown" # Apply custom class for styling
|
443 |
-
)
|
444 |
|
445 |
-
|
446 |
-
|
447 |
-
|
448 |
-
|
449 |
-
|
450 |
-
# outputs=output, # Output is handled by the main submit button click
|
451 |
-
# fn=query_interface, # Don't run the function directly on example click
|
452 |
-
# cache_examples=False, # Don't cache example runs if fn were used
|
453 |
-
label="Click an example to load it",
|
454 |
-
examples_per_page=5
|
455 |
)
|
456 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
457 |
|
|
|
|
|
458 |
gr.Markdown(
|
459 |
"""
|
460 |
-
|
461 |
-
|
462 |
-
<a href=
|
463 |
-
|
464 |
-
|
465 |
-
</div>
|
466 |
-
"""
|
467 |
-
, elem_classes="footnote") # Apply footnote class if defined in CSS
|
468 |
|
469 |
-
#
|
470 |
submit_button.click(
|
471 |
-
fn=
|
472 |
inputs=[api_key_input, query_input, state_input],
|
473 |
outputs=output,
|
474 |
-
api_name="submit_query"
|
475 |
)
|
476 |
-
|
477 |
clear_button.click(
|
478 |
-
fn=lambda: (
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
),
|
484 |
-
inputs=[], # No inputs needed for clear
|
485 |
outputs=[api_key_input, query_input, state_input, output]
|
486 |
)
|
487 |
|
488 |
-
logging.info("Gradio interface created successfully.")
|
489 |
-
return demo
|
490 |
|
491 |
-
|
492 |
-
# Main execution block
|
493 |
if __name__ == "__main__":
|
494 |
-
logging.info("Starting application
|
495 |
try:
|
496 |
-
|
497 |
-
|
498 |
-
|
|
|
499 |
|
500 |
-
|
501 |
-
|
502 |
-
logging.error(f"FATAL: PDF file not found at the specified path: {PDF_PATH}")
|
503 |
-
logging.error("Please ensure the PDF file exists or set the PDF_PATH environment variable correctly.")
|
504 |
-
# Exit if the core data file is missing
|
505 |
-
exit(1) # Or raise an exception
|
506 |
|
|
|
|
|
507 |
|
508 |
-
|
509 |
-
|
510 |
-
# Pass path if your implementation uses it (like ChromaDB)
|
511 |
-
vector_db_instance = VectorDatabase(persist_directory=VECTOR_DB_PATH)
|
512 |
|
513 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
514 |
rag = RAGSystem(vector_db=vector_db_instance)
|
515 |
|
516 |
-
|
517 |
-
|
518 |
-
|
519 |
-
|
520 |
-
|
521 |
-
|
522 |
-
|
523 |
-
|
|
|
|
|
|
|
524 |
|
525 |
-
# --- Interface Setup & Launch ---
|
526 |
logging.info("Setting up Gradio interface...")
|
527 |
app_interface = rag.gradio_interface()
|
528 |
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
|
|
|
|
|
|
|
|
535 |
|
536 |
except FileNotFoundError as fnf_error:
|
537 |
-
|
538 |
-
|
539 |
-
|
|
|
|
|
540 |
except ImportError as import_error:
|
541 |
-
|
542 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
543 |
except Exception as e:
|
544 |
-
# Catch any other unexpected errors during setup or launch
|
545 |
logging.error(f"An unexpected error occurred during application startup: {str(e)}", exc_info=True)
|
546 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
|
|
2 |
import logging
|
3 |
from typing import Dict, List, Optional
|
4 |
from functools import lru_cache
|
5 |
import re
|
6 |
|
7 |
import gradio as gr
|
8 |
+
try:
|
9 |
+
from vector_db import VectorDatabase
|
10 |
+
except ImportError:
|
11 |
+
print("Error: Could not import VectorDatabase from vector_db.py.")
|
12 |
+
print("Please ensure vector_db.py exists in the same directory and is correctly defined.")
|
13 |
+
exit(1)
|
14 |
+
|
15 |
+
try:
|
16 |
+
from langchain_openai import ChatOpenAI
|
17 |
+
except ImportError:
|
18 |
+
print("Error: langchain-openai not found. Please install it: pip install langchain-openai")
|
19 |
+
exit(1)
|
20 |
+
|
21 |
from langchain.prompts import PromptTemplate
|
22 |
from langchain.chains import LLMChain
|
|
|
|
|
23 |
|
24 |
+
# Suppress warnings
|
25 |
+
import warnings
|
26 |
+
warnings.filterwarnings("ignore", category=SyntaxWarning)
|
27 |
+
warnings.filterwarnings("ignore", category=UserWarning, message=".*You are using gradio version.*")
|
28 |
+
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
29 |
+
|
30 |
+
# Enhanced logging
|
31 |
logging.basicConfig(
|
32 |
level=logging.INFO,
|
33 |
format='%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s'
|
34 |
)
|
35 |
|
36 |
+
# --- RAGSystem Class ---
|
37 |
class RAGSystem:
|
38 |
+
# (Keep the RAGSystem class exactly the same as in the previous version)
|
39 |
+
# ... __init__ ...
|
40 |
+
# ... extract_statutes ...
|
41 |
+
# ... process_query_cached ...
|
42 |
+
# ... process_query ...
|
43 |
+
# ... get_states ...
|
44 |
+
# ... load_pdf ...
|
45 |
def __init__(self, vector_db: Optional[VectorDatabase] = None):
|
46 |
logging.info("Initializing RAGSystem")
|
|
|
47 |
self.vector_db = vector_db if vector_db else VectorDatabase()
|
48 |
self.llm = None
|
49 |
self.chain = None
|
|
|
|
|
50 |
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.
|
51 |
|
52 |
Instructions:
|
53 |
+
* Use the context and statutes as the primary basis for your answer when available.
|
54 |
+
* For state-specific queries, prioritize statutes or legal principles from the specified state over federal laws.
|
55 |
+
* Cite relevant statutes (e.g., (AS § 34.03.220(a)(2))) explicitly in your answer when applicable.
|
56 |
+
* If multiple statutes apply, list all relevant ones.
|
57 |
+
* 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.
|
58 |
+
* Include practical examples or scenarios to enhance clarity and usefulness.
|
59 |
+
* Use bullet points or numbered lists for readability when appropriate.
|
60 |
+
* Maintain a professional and neutral tone.
|
61 |
|
62 |
Question: {query}
|
63 |
State: {state}
|
|
|
76 |
)
|
77 |
logging.info("RAGSystem initialized.")
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
def extract_statutes(self, text: str) -> str:
|
80 |
+
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'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
statutes = re.findall(statute_pattern, text, re.IGNORECASE)
|
|
|
82 |
valid_statutes = []
|
|
|
83 |
for statute in statutes:
|
84 |
+
statute = statute.strip()
|
85 |
+
if '§' in statute and any(char.isdigit() for char in statute):
|
|
|
|
|
86 |
if not re.match(r'^\([\w\.]+\)$', statute) and 'http' not in statute:
|
87 |
+
if len(statute) > 5:
|
88 |
+
valid_statutes.append(statute)
|
89 |
|
90 |
if valid_statutes:
|
|
|
91 |
seen = set()
|
92 |
+
unique_statutes = [s for s in valid_statutes if not (s.rstrip('.,;') in seen or seen.add(s.rstrip('.,;')))]
|
93 |
logging.info(f"Extracted {len(unique_statutes)} unique statutes.")
|
94 |
+
return "\n".join(f"- {s}" for s in unique_statutes)
|
95 |
|
96 |
logging.info("No statutes found matching the pattern in the context.")
|
97 |
return "No specific statutes found in the provided context."
|
98 |
|
99 |
+
@lru_cache(maxsize=50)
|
100 |
+
def process_query_cached(self, query: str, state: str, openai_api_key: str, n_results: int = 5) -> Dict[str, any]:
|
101 |
+
logging.info(f"Processing query (cache key: '{query}'|'{state}'|key_hidden) with n_results={n_results}")
|
102 |
+
|
103 |
+
if not state or state == "Select a state..." or "Error" in state:
|
104 |
+
logging.warning("No valid state provided for query.")
|
105 |
+
return {"answer": "<div class='error-message'>Error: Please select a valid state.</div>", "context_used": "N/A - Invalid Input"}
|
106 |
+
if not query or not query.strip():
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
logging.warning("No query provided.")
|
108 |
+
return {"answer": "<div class='error-message'>Error: Please enter your question.</div>", "context_used": "N/A - Invalid Input"}
|
109 |
+
if not openai_api_key or not openai_api_key.strip() or not openai_api_key.startswith("sk-"):
|
110 |
+
logging.warning("No valid OpenAI API key provided.")
|
111 |
+
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"}
|
112 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
try:
|
114 |
+
logging.info("Initializing temporary LLM and Chain for this query...")
|
115 |
+
temp_llm = ChatOpenAI(
|
116 |
+
temperature=0.2, openai_api_key=openai_api_key, model_name="gpt-3.5-turbo",
|
117 |
+
max_tokens=1500, request_timeout=45
|
118 |
+
)
|
119 |
+
temp_chain = LLMChain(llm=temp_llm, prompt=self.prompt_template)
|
120 |
+
logging.info("Temporary LLM and Chain initialized successfully.")
|
121 |
except Exception as e:
|
122 |
+
logging.error(f"LLM Initialization failed: {str(e)}", exc_info=True)
|
123 |
+
error_msg = "Error: Failed to initialize AI model. Please check your network connection and API key validity."
|
124 |
+
if "authentication" in str(e).lower():
|
125 |
+
error_msg = "Error: OpenAI API Key is invalid or expired. Please check your key."
|
126 |
+
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"}
|
127 |
+
|
128 |
+
context = "No relevant context found."
|
129 |
+
statutes_from_context = "Statute retrieval skipped due to context issues."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
try:
|
131 |
+
logging.info(f"Querying Vector DB for query: '{query[:50]}...' in state '{state}'...")
|
132 |
results = self.vector_db.query(query, state=state, n_results=n_results)
|
133 |
+
logging.info(f"Vector DB query successful for state '{state}'. Processing results...")
|
|
|
134 |
|
135 |
context_parts = []
|
|
|
136 |
doc_results = results.get("document_results", {})
|
137 |
+
docs = doc_results.get("documents", [[]])[0]
|
138 |
+
metadatas = doc_results.get("metadatas", [[]])[0]
|
|
|
139 |
if docs and metadatas and len(docs) == len(metadatas):
|
140 |
+
logging.info(f"Found {len(docs)} document chunks.")
|
141 |
for i, doc_content in enumerate(docs):
|
142 |
metadata = metadatas[i]
|
143 |
state_label = metadata.get('state', 'Unknown State')
|
144 |
chunk_id = metadata.get('chunk_id', 'N/A')
|
145 |
+
context_parts.append(f"**Source: Document Chunk {chunk_id} (State: {state_label})**\n{doc_content}")
|
|
|
|
|
146 |
|
|
|
147 |
state_results_data = results.get("state_results", {})
|
148 |
state_docs = state_results_data.get("documents", [[]])[0]
|
149 |
state_metadatas = state_results_data.get("metadatas", [[]])[0]
|
|
|
150 |
if state_docs and state_metadatas and len(state_docs) == len(state_metadatas):
|
151 |
+
logging.info(f"Found {len(state_docs)} state summary documents.")
|
152 |
+
for i, state_doc_content in enumerate(state_docs):
|
153 |
metadata = state_metadatas[i]
|
154 |
+
state_label = metadata.get('state', state)
|
155 |
+
context_parts.append(f"**Source: State Summary (State: {state_label})**\n{state_doc_content}")
|
|
|
|
|
156 |
|
157 |
if context_parts:
|
158 |
context = "\n\n---\n\n".join(context_parts)
|
159 |
+
logging.info(f"Constructed context with {len(context_parts)} parts. Length: {len(context)} chars.")
|
160 |
+
try:
|
161 |
+
statutes_from_context = self.extract_statutes(context)
|
162 |
+
except Exception as e:
|
163 |
+
logging.error(f"Error extracting statutes: {e}", exc_info=True)
|
164 |
+
statutes_from_context = "Error extracting statutes from context."
|
165 |
else:
|
166 |
+
logging.warning("No relevant context parts found from vector DB query.")
|
167 |
+
context = "No relevant context could be retrieved from the knowledge base for this query and state. The AI will answer from its general knowledge."
|
168 |
+
statutes_from_context = "No specific statutes found as no context was retrieved."
|
169 |
|
170 |
except Exception as e:
|
171 |
+
logging.error(f"Vector DB query/context processing failed: {str(e)}", exc_info=True)
|
172 |
+
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."
|
173 |
+
statutes_from_context = "Statute retrieval skipped due to error retrieving context."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
|
|
175 |
try:
|
176 |
+
logging.info("Invoking LLMChain with constructed input...")
|
177 |
+
llm_input = {"query": query, "context": context, "state": state, "statutes": statutes_from_context}
|
178 |
+
answer_dict = temp_chain.invoke(llm_input)
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
answer_text = answer_dict.get('text', '').strip()
|
180 |
|
181 |
if not answer_text:
|
182 |
+
logging.warning("LLM returned an empty answer.")
|
183 |
+
answer_text = "<div class='error-message'>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>"
|
184 |
+
else:
|
185 |
+
logging.info("LLM generated answer successfully.")
|
186 |
|
187 |
+
return {"answer": answer_text, "context_used": context}
|
|
|
188 |
|
|
|
|
|
|
|
|
|
189 |
except Exception as e:
|
190 |
logging.error(f"LLM processing failed: {str(e)}", exc_info=True)
|
191 |
+
error_message = "Error: AI answer generation failed."
|
192 |
+
details = f"Details: {str(e)}"
|
|
|
193 |
if "authentication" in str(e).lower():
|
194 |
+
error_message = "Error: Authentication failed. Please double-check your OpenAI API key."
|
195 |
+
details = ""
|
196 |
elif "rate limit" in str(e).lower():
|
197 |
+
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."
|
198 |
+
details = ""
|
199 |
+
elif "context length" in str(e).lower():
|
200 |
+
error_message = "Error: The request was too long for the AI model. This can happen with very complex questions or extensive retrieved context."
|
201 |
+
details = "Try simplifying your question or asking about a more specific aspect."
|
202 |
+
elif "timeout" in str(e).lower():
|
203 |
+
error_message = "Error: The request to the AI model timed out. The service might be busy."
|
204 |
+
details = "Please try again in a few moments."
|
205 |
+
|
206 |
+
formatted_error = f"<div class='error-message'>{error_message}</div>"
|
207 |
+
if details:
|
208 |
+
formatted_error += f"<div class='error-details'>{details}</div>"
|
209 |
+
|
210 |
+
return {"answer": formatted_error, "context_used": context}
|
211 |
|
212 |
+
def process_query(self, query: str, state: str, openai_api_key: str, n_results: int = 5) -> Dict[str, any]:
|
213 |
+
return self.process_query_cached(query.strip(), state, openai_api_key.strip(), n_results)
|
|
|
|
|
214 |
|
215 |
def get_states(self) -> List[str]:
|
|
|
216 |
try:
|
217 |
states = self.vector_db.get_states()
|
218 |
if not states:
|
219 |
+
logging.warning("No states retrieved from vector_db. Returning empty list.")
|
220 |
+
return []
|
221 |
+
valid_states = sorted(list(set(s for s in states if s and isinstance(s, str) and s != "Select a state...")))
|
222 |
+
logging.info(f"Retrieved {len(valid_states)} unique, valid states from VectorDatabase.")
|
223 |
+
return valid_states
|
224 |
except Exception as e:
|
225 |
+
logging.error(f"Failed to get states from VectorDatabase: {str(e)}", exc_info=True)
|
226 |
+
return ["Error: Could not load states"]
|
227 |
|
228 |
def load_pdf(self, pdf_path: str) -> int:
|
|
|
229 |
if not os.path.exists(pdf_path):
|
230 |
+
logging.error(f"PDF file not found at path: {pdf_path}")
|
231 |
+
raise FileNotFoundError(f"PDF file not found: {pdf_path}")
|
232 |
try:
|
233 |
+
logging.info(f"Attempting to load/verify data from PDF: {pdf_path}")
|
234 |
+
num_states_processed = self.vector_db.process_and_load_pdf(pdf_path)
|
235 |
+
doc_count = self.vector_db.document_collection.count()
|
236 |
+
state_count = self.vector_db.state_collection.count()
|
237 |
+
total_items = doc_count + state_count
|
238 |
+
|
239 |
+
if total_items > 0:
|
240 |
+
logging.info(f"Vector DB contains {total_items} items ({doc_count} docs, {state_count} states). PDF processed or data already existed.")
|
241 |
+
current_states = self.get_states()
|
242 |
+
return len(current_states) if current_states and "Error" not in current_states[0] else 0
|
243 |
else:
|
244 |
+
logging.warning(f"PDF processing completed, but the vector database appears empty. Check PDF content and processing logs.")
|
245 |
+
return 0
|
246 |
+
|
247 |
except Exception as e:
|
248 |
logging.error(f"Failed to load or process PDF '{pdf_path}': {str(e)}", exc_info=True)
|
249 |
+
raise RuntimeError(f"Failed to process PDF '{pdf_path}': {e}") from e
|
|
|
250 |
|
251 |
|
252 |
+
# --- GRADIO INTERFACE ---
|
253 |
def gradio_interface(self):
|
254 |
+
# Wrapper function for the Gradio interface logic
|
255 |
+
def query_interface_wrapper(api_key: str, query: str, state: str) -> str:
|
256 |
+
logging.info(f"Gradio interface received query: '{query[:50]}...', state: '{state}'")
|
257 |
+
|
258 |
+
# Re-validate inputs robustly
|
259 |
+
if not api_key or not api_key.strip() or not api_key.startswith("sk-"):
|
260 |
+
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>"
|
261 |
+
if not state or state == "Select a state..." or "Error" in state:
|
262 |
+
return "<div class='error-message'>Please select a valid state from the dropdown.</div>"
|
263 |
+
if not query or not query.strip():
|
264 |
+
return "<div class='error-message'>Please enter your question in the text box.</div>"
|
265 |
+
|
266 |
+
# Call the core processing logic
|
267 |
result = self.process_query(query=query, state=state, openai_api_key=api_key)
|
268 |
|
269 |
+
# Format the response for display
|
270 |
+
answer = result.get("answer", "<div class='error-message'>An unexpected error occurred, and no answer was generated. Please check the logs or try again.</div>")
|
|
|
271 |
|
272 |
+
# Add a header *only* if the answer is not an error message itself
|
273 |
+
if not "<div class='error-message'>" in answer:
|
274 |
+
formatted_response = f"<h3 class='response-header'>Response for {state}</h3><hr class='divider'>{answer}"
|
275 |
+
else:
|
276 |
+
formatted_response = answer # Pass through error messages directly
|
277 |
|
278 |
+
# Log context length for debugging (optional)
|
279 |
+
context_used = result.get("context_used", "N/A")
|
280 |
+
if isinstance(context_used, str) and "N/A" not in context_used:
|
281 |
+
logging.debug(f"Context length used for query: {len(context_used)} characters.")
|
282 |
+
else:
|
283 |
+
logging.debug(f"No context was used or available for this query ({context_used}).")
|
284 |
|
285 |
return formatted_response
|
286 |
|
287 |
+
# --- Get Available States for Dropdown ---
|
288 |
+
try:
|
289 |
+
available_states_list = self.get_states()
|
290 |
+
if not available_states_list or "Error" in available_states_list[0]:
|
291 |
+
dropdown_choices = ["Error: Could not load states"]
|
292 |
+
initial_value = dropdown_choices[0]
|
293 |
+
logging.error("Could not load states for dropdown. UI will show error.")
|
294 |
+
else:
|
295 |
+
dropdown_choices = ["Select a state..."] + available_states_list
|
296 |
+
initial_value = dropdown_choices[0]
|
297 |
+
except Exception as e:
|
298 |
+
logging.error(f"Unexpected critical error getting states: {e}", exc_info=True)
|
299 |
+
dropdown_choices = ["Error: Critical failure loading states"]
|
300 |
+
initial_value = dropdown_choices[0]
|
301 |
+
|
302 |
+
# --- Prepare Example Queries ---
|
303 |
+
example_queries_base = [
|
304 |
+
["What are the rules for security deposit returns?", "California"],
|
305 |
+
["Can a landlord enter my apartment without notice?", "New York"],
|
306 |
+
["My landlord hasn't made necessary repairs. What can I do?", "Texas"],
|
307 |
+
["What are the limits on rent increases in my state?", "Florida"],
|
308 |
+
["Is my lease automatically renewed if I don't move out?", "Illinois"],
|
309 |
+
["What happens if I break my lease early?", "Washington"]
|
310 |
]
|
311 |
+
example_queries = []
|
312 |
+
if available_states_list and "Error" not in available_states_list[0]:
|
313 |
+
loaded_states_set = set(available_states_list)
|
314 |
+
example_queries = [ex for ex in example_queries_base if ex[1] in loaded_states_set]
|
315 |
+
if not example_queries:
|
316 |
+
fallback_state = available_states_list[0] if available_states_list and "Error" not in available_states_list[0] else "California"
|
317 |
+
example_queries.append(["What basic rights do tenants have?", fallback_state])
|
318 |
+
|
319 |
+
# --- Refined Custom CSS ---
|
320 |
+
# Focus: Unified background, distinct white cards, proper centering, refined examples table
|
321 |
custom_css = """
|
322 |
+
@import url('https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap');
|
323 |
+
|
324 |
+
/* --- Base & Body --- */
|
325 |
+
body, .gradio-container {
|
326 |
+
font-family: 'Roboto', sans-serif !important;
|
327 |
+
background-color: #F5F7FA !important; /* Light grey base background */
|
328 |
+
color: #1F2A44;
|
329 |
+
margin: 0;
|
330 |
+
padding: 0;
|
331 |
+
min-height: 100vh;
|
332 |
+
font-size: 16px; /* Base font size */
|
333 |
+
-webkit-font-smoothing: antialiased;
|
334 |
+
-moz-osx-font-smoothing: grayscale;
|
335 |
+
}
|
336 |
+
* {
|
337 |
+
box-sizing: border-box;
|
338 |
+
}
|
339 |
+
|
340 |
+
/* --- Main Content Container --- */
|
341 |
+
.gradio-container > .flex.flex-col { /* Target the main content column */
|
342 |
+
max-width: 960px; /* Slightly wider max-width */
|
343 |
+
margin: 0 auto !important; /* Center the column */
|
344 |
+
padding: 3rem 1.5rem !important; /* More vertical padding */
|
345 |
+
gap: 2.5rem !important; /* Consistent gap between sections */
|
346 |
+
background-color: transparent !important; /* Ensure container itself is transparent */
|
347 |
+
}
|
348 |
+
|
349 |
+
/* --- Card Styling (Applied to Groups) --- */
|
350 |
+
.card-style {
|
351 |
+
background-color: #FFFFFF !important; /* White background for cards */
|
352 |
+
border: 1px solid #E5E7EB !important; /* Subtle border */
|
353 |
+
border-radius: 12px !important;
|
354 |
+
padding: 2rem !important; /* Consistent padding inside cards */
|
355 |
+
box-shadow: 0 4px 12px rgba(101, 119, 134, 0.08) !important; /* Refined shadow */
|
356 |
+
overflow: hidden; /* Prevent content spill */
|
357 |
+
}
|
358 |
+
/* Remove default Gradio Group padding if using custom padding */
|
359 |
+
.gradio-group {
|
360 |
+
padding: 0 !important;
|
361 |
+
border: none !important;
|
362 |
+
background: none !important;
|
363 |
+
box-shadow: none !important;
|
364 |
+
}
|
365 |
+
|
366 |
+
/* --- Header Section --- */
|
367 |
+
.header-section {
|
368 |
+
background-color: transparent !important; /* Header blends */
|
369 |
+
padding: 1rem 0 !important;
|
370 |
+
text-align: center !important; /* Center align all content */
|
371 |
+
border: none !important;
|
372 |
+
box-shadow: none !important;
|
373 |
+
}
|
374 |
+
.header-logo {
|
375 |
+
font-size: 2.8rem;
|
376 |
+
color: #2563EB;
|
377 |
+
margin-bottom: 0.75rem;
|
378 |
+
display: block; /* Ensure centering */
|
379 |
+
}
|
380 |
+
.header-title {
|
381 |
+
font-size: 2rem; /* Larger title */
|
382 |
+
font-weight: 700;
|
383 |
+
color: #111827; /* Darker title */
|
384 |
+
margin: 0 0 0.25rem 0;
|
385 |
+
}
|
386 |
+
.header-tagline {
|
387 |
+
font-size: 1.1rem;
|
388 |
+
color: #4B5563;
|
389 |
+
margin: 0;
|
390 |
+
}
|
391 |
+
|
392 |
+
/* --- Introduction Section --- */
|
393 |
+
/* Uses card-style defined above */
|
394 |
+
.intro-card h3 {
|
395 |
+
font-size: 1.5rem;
|
396 |
+
font-weight: 600;
|
397 |
+
color: #0369A1; /* Blue heading */
|
398 |
+
margin: 0 0 1rem 0;
|
399 |
+
padding-bottom: 0.5rem;
|
400 |
+
border-bottom: 1px solid #E0F2FE; /* Light blue underline */
|
401 |
+
}
|
402 |
+
.intro-card p {
|
403 |
+
font-size: 1rem;
|
404 |
+
line-height: 1.6;
|
405 |
+
color: #374151; /* Standard text color */
|
406 |
+
margin: 0 0 0.75rem 0;
|
407 |
+
}
|
408 |
+
.intro-card a {
|
409 |
+
color: #0369A1;
|
410 |
+
text-decoration: underline;
|
411 |
+
font-weight: 500;
|
412 |
+
}
|
413 |
+
.intro-card a:hover { color: #0284C7; }
|
414 |
+
.intro-card strong { font-weight: 600; color: #1F2A44; }
|
415 |
+
|
416 |
+
/* --- Input Form Section --- */
|
417 |
+
/* Uses card-style */
|
418 |
+
.input-form-card h3 {
|
419 |
+
font-size: 1.4rem;
|
420 |
+
font-weight: 600;
|
421 |
+
color: #1F2A44;
|
422 |
+
margin: 0 0 1.75rem 0;
|
423 |
+
padding-bottom: 0.75rem;
|
424 |
+
border-bottom: 1px solid #E5E7EB;
|
425 |
+
}
|
426 |
+
.input-field-group { margin-bottom: 1.5rem; }
|
427 |
+
.input-row { display: flex; gap: 1.5rem; flex-wrap: wrap; margin-bottom: 1.5rem; }
|
428 |
+
.input-field { flex: 1; min-width: 220px; }
|
429 |
+
|
430 |
+
/* Input Elements */
|
431 |
+
.gradio-textbox textarea, .gradio-dropdown select, .gradio-textbox input[type=password] {
|
432 |
+
border: 1px solid #D1D5DB !important;
|
433 |
+
border-radius: 8px !important;
|
434 |
+
padding: 0.8rem 1rem !important;
|
435 |
+
font-size: 1rem !important; /* Make inputs slightly larger */
|
436 |
+
background-color: #F9FAFB !important;
|
437 |
+
color: #1F2A44 !important;
|
438 |
+
transition: border-color 0.2s ease, box-shadow 0.2s ease;
|
439 |
+
width: 100% !important;
|
440 |
+
}
|
441 |
+
.gradio-textbox textarea { min-height: 90px; }
|
442 |
+
.gradio-textbox textarea:focus, .gradio-dropdown select:focus, .gradio-textbox input[type=password]:focus {
|
443 |
+
border-color: #2563EB !important;
|
444 |
+
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15) !important;
|
445 |
+
outline: none !important;
|
446 |
+
background-color: #FFFFFF !important;
|
447 |
+
}
|
448 |
+
.gradio-input-label, .gradio-output-label { /* Label styling */
|
449 |
+
font-size: 0.9rem !important;
|
450 |
+
font-weight: 500 !important;
|
451 |
+
color: #374151 !important;
|
452 |
+
margin-bottom: 0.5rem !important;
|
453 |
+
display: block !important;
|
454 |
+
}
|
455 |
+
.gradio-input-info { /* Info text */
|
456 |
+
font-size: 0.85rem !important;
|
457 |
+
color: #6B7280 !important;
|
458 |
+
margin-top: 0.3rem;
|
459 |
+
}
|
460 |
+
|
461 |
+
/* Buttons */
|
462 |
+
.button-row { display: flex; gap: 1rem; margin-top: 1.5rem; flex-wrap: wrap; justify-content: flex-end; }
|
463 |
+
.gradio-button {
|
464 |
+
border-radius: 8px !important; padding: 0.75rem 1.5rem !important; font-size: 0.95rem !important;
|
465 |
+
font-weight: 500 !important; border: none !important; cursor: pointer;
|
466 |
+
transition: background-color 0.2s ease, transform 0.1s ease, box-shadow 0.2s ease;
|
467 |
+
}
|
468 |
+
.gradio-button:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); }
|
469 |
+
.gradio-button:active:not(:disabled) { transform: scale(0.98); box-shadow: none; }
|
470 |
+
.gradio-button:disabled { background: #E5E7EB !important; color: #9CA3AF !important; cursor: not-allowed; }
|
471 |
+
.gr-button-primary { background-color: #2563EB !important; color: #FFFFFF !important; }
|
472 |
+
.gr-button-primary:hover:not(:disabled) { background-color: #1D4ED8 !important; }
|
473 |
+
.gr-button-secondary { background-color: #F3F4F6 !important; color: #374151 !important; border: 1px solid #D1D5DB !important; }
|
474 |
+
.gr-button-secondary:hover:not(:disabled) { background-color: #E5E7EB !important; border-color: #9CA3AF !important; }
|
475 |
+
|
476 |
+
/* --- Output Section --- */
|
477 |
+
/* Uses card-style */
|
478 |
+
.output-card .response-header { /* Style the H3 we add in Python */
|
479 |
+
font-size: 1.3rem;
|
480 |
+
font-weight: 600;
|
481 |
+
color: #1F2A44;
|
482 |
+
margin: 0 0 0.75rem 0;
|
483 |
+
}
|
484 |
+
.output-card .divider { /* Style the HR we add */
|
485 |
+
border: none; border-top: 1px solid #E5E7EB; margin: 1rem 0 1.5rem 0;
|
486 |
+
}
|
487 |
+
.output-card .output-content-wrapper { /* Wrapper for the markdown content */
|
488 |
+
font-size: 1rem; line-height: 1.7; color: #374151;
|
489 |
+
}
|
490 |
+
.output-card .output-content-wrapper p { margin-bottom: 1rem; }
|
491 |
+
.output-card .output-content-wrapper ul, .output-card .output-content-wrapper ol { margin-left: 1.5rem; margin-bottom: 1rem; padding-left: 1rem; }
|
492 |
+
.output-card .output-content-wrapper li { margin-bottom: 0.5rem; }
|
493 |
+
.output-card .output-content-wrapper strong, .output-card .output-content-wrapper b { font-weight: 600; color: #111827; }
|
494 |
+
.output-card .output-content-wrapper a { color: #2563EB; text-decoration: underline; }
|
495 |
+
.output-card .output-content-wrapper a:hover { color: #1D4ED8; }
|
496 |
+
|
497 |
+
/* Error message styling */
|
498 |
+
.output-card .error-message {
|
499 |
+
background-color: #FEF2F2; border: 1px solid #FECACA; border-left: 4px solid #F87171;
|
500 |
+
border-radius: 8px; padding: 1rem 1.25rem; color: #B91C1C; font-weight: 500; margin-top: 0.5rem;
|
501 |
+
}
|
502 |
+
.output-card .error-details { font-size: 0.9rem; color: #991B1B; margin-top: 0.5rem; font-style: italic; }
|
503 |
+
/* Placeholder text */
|
504 |
+
.output-card .placeholder { color: #9CA3AF; font-style: italic; text-align: center; padding: 2rem 1rem; display: block; }
|
505 |
+
|
506 |
+
/* --- Examples Section --- */
|
507 |
+
/* Uses card-style */
|
508 |
+
.examples-card .gr-examples-header { /* Style the header Gradio adds */
|
509 |
+
font-size: 1.3rem !important; font-weight: 600 !important; color: #1F2A44 !important;
|
510 |
+
margin: 0 0 1.5rem 0 !important; padding-bottom: 0.75rem !important; border-bottom: 1px solid #E5E7EB !important;
|
511 |
+
}
|
512 |
+
/* Style the TABLE generated by gr.Examples */
|
513 |
+
.examples-card .gr-examples-table { border-collapse: collapse !important; width: 100% !important; }
|
514 |
+
.examples-card .gr-examples-table th,
|
515 |
+
.examples-card .gr-examples-table td {
|
516 |
+
text-align: left !important; padding: 0.75rem 1rem !important;
|
517 |
+
border: 1px solid #E5E7EB !important; font-size: 0.95rem !important;
|
518 |
+
color: #374151 !important; background-color: transparent !important;
|
519 |
+
}
|
520 |
+
.examples-card .gr-examples-table th {
|
521 |
+
font-weight: 500 !important; background-color: #F9FAFB !important; color: #1F2A44 !important;
|
522 |
+
}
|
523 |
+
/* Style the example *rows* when clickable */
|
524 |
+
.examples-card .gr-examples-table tr { cursor: pointer; transition: background-color 0.2s ease; }
|
525 |
+
.examples-card .gr-examples-table tr:hover td { background-color: #F3F4F6 !important; }
|
526 |
+
|
527 |
+
/* --- Footer Section --- */
|
528 |
+
.footer-section {
|
529 |
+
background-color: transparent !important;
|
530 |
+
border-top: 1px solid #E5E7EB !important;
|
531 |
+
padding: 2rem 1rem !important;
|
532 |
+
margin-top: 1rem !important; /* Space above footer */
|
533 |
+
text-align: center !important;
|
534 |
+
color: #6B7280 !important;
|
535 |
+
font-size: 0.9rem !important;
|
536 |
+
line-height: 1.6 !important;
|
537 |
+
box-shadow: none !important; border-radius: 0 !important;
|
538 |
+
}
|
539 |
+
.footer-section strong { color: #374151; font-weight: 500; }
|
540 |
+
.footer-section a { color: #2563EB; text-decoration: none; font-weight: 500; }
|
541 |
+
.footer-section a:hover { color: #1D4ED8; text-decoration: underline; }
|
542 |
+
|
543 |
+
/* --- Accessibility & Focus --- */
|
544 |
+
:focus-visible {
|
545 |
+
outline: 2px solid #2563EB !important;
|
546 |
+
outline-offset: 2px;
|
547 |
+
box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.2) !important;
|
548 |
+
}
|
549 |
+
/* Remove default Gradio focus on button internal span */
|
550 |
+
.gradio-button span:focus { outline: none !important; }
|
551 |
+
|
552 |
+
/* --- Responsive Adjustments --- */
|
553 |
+
@media (max-width: 768px) {
|
554 |
+
.gradio-container > .flex.flex-col { padding: 2rem 1rem !important; gap: 2rem !important; }
|
555 |
+
.card-style { padding: 1.5rem !important; }
|
556 |
+
.header-title { font-size: 1.8rem; }
|
557 |
+
.header-tagline { font-size: 1rem; }
|
558 |
+
.input-row { flex-direction: column; gap: 1rem; margin-bottom: 1rem; }
|
559 |
+
.button-row { justify-content: center; }
|
560 |
+
}
|
561 |
+
@media (max-width: 480px) {
|
562 |
+
body { font-size: 15px; }
|
563 |
+
.gradio-container > .flex.flex-col { padding: 1.5rem 1rem !important; gap: 1.5rem !important; }
|
564 |
+
.card-style { padding: 1.25rem !important; border-radius: 10px !important;}
|
565 |
+
.header-logo { font-size: 2.5rem; margin-bottom: 0.5rem;}
|
566 |
+
.header-title { font-size: 1.5rem; }
|
567 |
+
.header-tagline { font-size: 0.95rem; }
|
568 |
+
.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; }
|
569 |
+
.gradio-textbox textarea, .gradio-dropdown select, .gradio-textbox input[type=password] { font-size: 0.95rem !important; padding: 0.75rem !important; }
|
570 |
+
.gradio-button { width: 100%; padding: 0.7rem 1.2rem !important; font-size: 0.9rem !important; }
|
571 |
+
.button-row { flex-direction: column; gap: 0.75rem; }
|
572 |
+
.footer-section { font-size: 0.85rem; padding: 1.5rem 1rem !important; }
|
573 |
+
.examples-card .gr-examples-table th, .examples-card .gr-examples-table td { padding: 0.6rem 0.8rem !important; font-size: 0.9rem !important;}
|
574 |
+
}
|
575 |
+
|
576 |
+
/* Gradio Specific Overrides (Use sparingly) */
|
577 |
+
/* Force main container gap */
|
578 |
+
.gradio-container > .flex { gap: 2.5rem !important; }
|
579 |
+
/* Ensure no weird margins collapse */
|
580 |
+
.gradio-markdown > *:first-child { margin-top: 0; }
|
581 |
+
.gradio-markdown > *:last-child { margin-bottom: 0; }
|
582 |
+
/* Remove border from dropdown wrapper if needed */
|
583 |
+
.gradio-dropdown { border: none !important; padding: 0 !important; }
|
584 |
+
/* Remove border from textbox wrapper */
|
585 |
+
.gradio-textbox { border: none !important; padding: 0 !important; }
|
586 |
"""
|
587 |
|
588 |
+
# --- Gradio Blocks Layout ---
|
589 |
+
with gr.Blocks(css=custom_css, title="Landlord-Tenant Rights Assistant") as demo:
|
590 |
+
# The main container class is applied implicitly by Gradio, CSS targets it
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
591 |
|
592 |
+
# Header Section (No Card Style)
|
593 |
+
with gr.Group(elem_classes="header-section"): # Use Group for structure, styled via class
|
594 |
+
gr.Markdown(
|
595 |
+
"""
|
596 |
+
<span class="header-logo">⚖️</span>
|
597 |
+
<h1 class="header-title">Landlord-Tenant Rights Assistant</h1>
|
598 |
+
<p class="header-tagline">Your AI-powered guide to U.S. landlord-tenant laws</p>
|
599 |
+
""", elem_id="app-title"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
600 |
)
|
601 |
|
602 |
+
# Introduction Section (Card Style)
|
603 |
+
with gr.Group(elem_classes="card-style intro-card"):
|
604 |
+
gr.Markdown(
|
605 |
+
"""
|
606 |
+
<h3 style="text-align: center;">Discover Your Rights</h3>
|
|
|
|
|
|
|
|
|
607 |
|
608 |
+
<p>Get accurate, AI-powered answers to your questions about landlord-tenant laws. Select your state, provide an <strong>OpenAI API key</strong>, and ask your question below.</p>
|
609 |
+
<p>Need an API key? <a href='https://platform.openai.com/api-keys' target='_blank'>Get one free here</a> from OpenAI.</p>
|
610 |
+
<p><strong>Note:</strong> This tool is for informational purposes only. Always consult a licensed attorney for legal advice specific to your situation.</p>
|
611 |
+
""",
|
612 |
+
elem_id="app-description"
|
|
|
|
|
|
|
|
|
|
|
613 |
)
|
614 |
|
615 |
+
# Examples Section (Card Style)
|
616 |
+
|
617 |
+
# Input Form Section (Card Style)
|
618 |
+
with gr.Group(elem_classes="card-style input-form-card"):
|
619 |
+
gr.Markdown("<h3>Query Section</h3>", elem_id="form-heading")
|
620 |
+
|
621 |
+
with gr.Column(elem_classes="input-field-group"):
|
622 |
+
api_key_input = gr.Textbox(
|
623 |
+
label="OpenAI API Key", type="password",
|
624 |
+
placeholder="Enter your API key (e.g., sk-...)",
|
625 |
+
info="Required to process your question. Securely used per request, not stored.",
|
626 |
+
elem_id="api-key-input", lines=1
|
627 |
+
)
|
628 |
+
|
629 |
+
with gr.Row(elem_classes="input-row"):
|
630 |
+
with gr.Column(elem_classes="input-field"):
|
631 |
+
query_input = gr.Textbox(
|
632 |
+
label="Curious about landlord-tenant laws in your state? Ask away!",
|
633 |
+
placeholder="E.g., What are the rules for security deposit returns in my state?",
|
634 |
+
lines=4, max_lines=8, elem_id="query-input"
|
635 |
+
)
|
636 |
+
with gr.Column(elem_classes="input-field"):
|
637 |
+
state_input = gr.Dropdown(
|
638 |
+
label="Select State", choices=dropdown_choices, value=initial_value,
|
639 |
+
allow_custom_value=False, elem_id="state-dropdown"
|
640 |
+
)
|
641 |
+
|
642 |
+
with gr.Row(elem_classes="button-row"):
|
643 |
+
clear_button = gr.Button(
|
644 |
+
"Clear Inputs", variant="secondary", elem_id="clear-button",
|
645 |
+
elem_classes=["gr-button-secondary"]
|
646 |
+
)
|
647 |
+
submit_button = gr.Button(
|
648 |
+
"Submit Question", variant="primary", elem_id="submit-button",
|
649 |
+
elem_classes=["gr-button-primary"]
|
650 |
+
)
|
651 |
+
|
652 |
+
# Output Section (Card Style)
|
653 |
+
with gr.Group(elem_classes="card-style output-card"):
|
654 |
+
# Wrap the output markdown for better targeting if needed
|
655 |
+
with gr.Column(): # Add column wrapper if needed for spacing/styling
|
656 |
+
output = gr.Markdown(
|
657 |
+
value="<div class='placeholder'>The response will appear here after submitting a question.</div>",
|
658 |
+
elem_id="output-content",
|
659 |
+
elem_classes="output-content-wrapper" # Apply styling to this wrapper
|
660 |
+
)
|
661 |
+
|
662 |
+
# Example Questions Section (Card Style)
|
663 |
+
if example_queries:
|
664 |
+
with gr.Group(elem_classes="card-style examples-card"):
|
665 |
+
gr.Examples(
|
666 |
+
examples=example_queries,
|
667 |
+
inputs=[query_input, state_input],
|
668 |
+
label="Example Sample Questions", # Uses .gr-examples-header class
|
669 |
+
examples_per_page=6
|
670 |
+
)
|
671 |
+
else:
|
672 |
+
with gr.Group(elem_classes="card-style examples-card"): # Still use card style for consistency
|
673 |
+
gr.Markdown(
|
674 |
+
"<div class='placeholder'>Sample questions could not be loaded. Please ensure states are available.</div>"
|
675 |
+
)
|
676 |
|
677 |
+
# Footer Section (No Card Style)
|
678 |
+
with gr.Group(elem_classes="footer-section"):
|
679 |
gr.Markdown(
|
680 |
"""
|
681 |
+
**Disclaimer**: This tool is for informational purposes only and does not constitute legal advice.
|
682 |
+
<br><br>
|
683 |
+
Developed by **Nischal Subedi**. Connect on <a href="https://www.linkedin.com/in/nischal1/" target="_blank">LinkedIn</a> or explore insights at <a href="https://datascientistinsights.substack.com/" target="_blank">Substack</a>.
|
684 |
+
""", elem_id="app-footer"
|
685 |
+
)
|
|
|
|
|
|
|
686 |
|
687 |
+
# --- Event Listeners ---
|
688 |
submit_button.click(
|
689 |
+
fn=query_interface_wrapper,
|
690 |
inputs=[api_key_input, query_input, state_input],
|
691 |
outputs=output,
|
692 |
+
api_name="submit_query"
|
693 |
)
|
694 |
+
|
695 |
clear_button.click(
|
696 |
+
fn=lambda: (
|
697 |
+
"", "", initial_value,
|
698 |
+
"<div class='placeholder'>Inputs cleared. Ready for your next question.</div>"
|
699 |
+
),
|
700 |
+
inputs=[],
|
|
|
|
|
701 |
outputs=[api_key_input, query_input, state_input, output]
|
702 |
)
|
703 |
|
704 |
+
logging.info("Refined Gradio interface created successfully.")
|
705 |
+
return demo
|
706 |
|
707 |
+
# --- Main Execution Block ---
|
|
|
708 |
if __name__ == "__main__":
|
709 |
+
logging.info("Starting Landlord-Tenant Rights Bot application...")
|
710 |
try:
|
711 |
+
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
|
712 |
+
DEFAULT_DATA_DIR = os.path.join(SCRIPT_DIR, "data")
|
713 |
+
DEFAULT_PDF_PATH = os.path.join(DEFAULT_DATA_DIR, "tenant-landlord.pdf")
|
714 |
+
DEFAULT_DB_PATH = os.path.join(DEFAULT_DATA_DIR, "chroma_db")
|
715 |
|
716 |
+
PDF_PATH = os.getenv("PDF_PATH", DEFAULT_PDF_PATH)
|
717 |
+
VECTOR_DB_PATH = os.getenv("VECTOR_DB_PATH", DEFAULT_DB_PATH)
|
|
|
|
|
|
|
|
|
718 |
|
719 |
+
os.makedirs(os.path.dirname(VECTOR_DB_PATH), exist_ok=True)
|
720 |
+
os.makedirs(os.path.dirname(PDF_PATH), exist_ok=True)
|
721 |
|
722 |
+
logging.info(f"Using PDF path: {PDF_PATH}")
|
723 |
+
logging.info(f"Using Vector DB path: {VECTOR_DB_PATH}")
|
|
|
|
|
724 |
|
725 |
+
if not os.path.exists(PDF_PATH):
|
726 |
+
logging.error(f"FATAL: PDF file not found at the specified path: {PDF_PATH}")
|
727 |
+
print(f"\n--- CONFIGURATION ERROR ---")
|
728 |
+
print(f"The required PDF file ('{os.path.basename(PDF_PATH)}') was not found at:")
|
729 |
+
print(f" {PDF_PATH}")
|
730 |
+
print(f"Please ensure the file exists or set 'PDF_PATH' environment variable.")
|
731 |
+
print(f"---------------------------\n")
|
732 |
+
exit(1)
|
733 |
+
|
734 |
+
logging.info("Initializing Vector Database...")
|
735 |
+
vector_db_instance = VectorDatabase(persist_directory=VECTOR_DB_PATH)
|
736 |
+
logging.info("Initializing RAG System...")
|
737 |
rag = RAGSystem(vector_db=vector_db_instance)
|
738 |
|
739 |
+
logging.info(f"Loading/Verifying data from PDF: {PDF_PATH}")
|
740 |
+
states_loaded_count = rag.load_pdf(PDF_PATH)
|
741 |
+
doc_count = vector_db_instance.document_collection.count() if vector_db_instance.document_collection else 0
|
742 |
+
state_count = vector_db_instance.state_collection.count() if vector_db_instance.state_collection else 0
|
743 |
+
total_items = doc_count + state_count
|
744 |
+
|
745 |
+
if total_items > 0:
|
746 |
+
logging.info(f"Data loading/verification complete. Vector DB contains {total_items} items. Found {states_loaded_count} distinct states.")
|
747 |
+
else:
|
748 |
+
logging.warning("Potential issue: PDF processed but Vector DB appears empty. Check PDF content/format and logs.")
|
749 |
+
print("\nWarning: No data loaded from PDF or found in DB. Application might not function correctly.\n")
|
750 |
|
|
|
751 |
logging.info("Setting up Gradio interface...")
|
752 |
app_interface = rag.gradio_interface()
|
753 |
|
754 |
+
SERVER_PORT = 7861
|
755 |
+
logging.info(f"Launching Gradio app on http://0.0.0.0:{SERVER_PORT}")
|
756 |
+
print("\n--- Gradio App Running ---")
|
757 |
+
print(f"Access the interface in your browser at: http://localhost:{SERVER_PORT} or http://<your-ip-address>:{SERVER_PORT}")
|
758 |
+
print("--------------------------\n")
|
759 |
+
app_interface.launch(
|
760 |
+
server_name="0.0.0.0", server_port=SERVER_PORT,
|
761 |
+
share=False,
|
762 |
+
# enable_queue=True # Consider for higher traffic
|
763 |
+
)
|
764 |
|
765 |
except FileNotFoundError as fnf_error:
|
766 |
+
logging.error(f"Initialization failed due to a missing file: {str(fnf_error)}", exc_info=True)
|
767 |
+
print(f"\n--- STARTUP ERROR: File Not Found ---")
|
768 |
+
print(f"{str(fnf_error)}")
|
769 |
+
print(f"---------------------------------------\n")
|
770 |
+
exit(1)
|
771 |
except ImportError as import_error:
|
772 |
+
logging.error(f"Import error: {str(import_error)}. Check dependencies.", exc_info=True)
|
773 |
+
print(f"\n--- STARTUP ERROR: Missing Dependency ---")
|
774 |
+
print(f"Import Error: {str(import_error)}")
|
775 |
+
print(f"Please ensure required libraries are installed (e.g., pip install -r requirements.txt).")
|
776 |
+
print(f"-----------------------------------------\n")
|
777 |
+
exit(1)
|
778 |
+
except RuntimeError as runtime_error:
|
779 |
+
logging.error(f"A runtime error occurred during setup: {str(runtime_error)}", exc_info=True)
|
780 |
+
print(f"\n--- STARTUP ERROR: Runtime Problem ---")
|
781 |
+
print(f"Runtime Error: {str(runtime_error)}")
|
782 |
+
print(f"Check logs for details, often related to data loading or DB setup.")
|
783 |
+
print(f"--------------------------------------\n")
|
784 |
+
exit(1)
|
785 |
except Exception as e:
|
|
|
786 |
logging.error(f"An unexpected error occurred during application startup: {str(e)}", exc_info=True)
|
787 |
+
print(f"\n--- FATAL STARTUP ERROR ---")
|
788 |
+
print(f"An unexpected error stopped the application: {str(e)}")
|
789 |
+
print(f"Check logs for detailed traceback.")
|
790 |
+
print(f"---------------------------\n")
|
791 |
+
exit(1)
|
requirements.txt
CHANGED
@@ -11,4 +11,4 @@ pandas==2.2.2
|
|
11 |
huggingface_hub==0.23.4
|
12 |
pymupdf==1.24.9
|
13 |
langchain_community
|
14 |
-
# force rebuild
|
|
|
11 |
huggingface_hub==0.23.4
|
12 |
pymupdf==1.24.9
|
13 |
langchain_community
|
14 |
+
# force rebuild #1
|
vector_db.py
CHANGED
@@ -13,7 +13,7 @@ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(
|
|
13 |
class VectorDatabase:
|
14 |
"""Vector database for storing and retrieving tenant rights information from PDF."""
|
15 |
|
16 |
-
def __init__(self, persist_directory="./
|
17 |
"""Initialize the vector database."""
|
18 |
logging.info("Initializing VectorDatabase")
|
19 |
logging.info(f"NumPy version: {np.__version__}")
|
@@ -182,7 +182,7 @@ class VectorDatabase:
|
|
182 |
if __name__ == "__main__":
|
183 |
try:
|
184 |
db = VectorDatabase()
|
185 |
-
pdf_path = "
|
186 |
db.process_and_load_pdf(pdf_path)
|
187 |
states = db.get_states()
|
188 |
print(f"Available states: {states}")
|
|
|
13 |
class VectorDatabase:
|
14 |
"""Vector database for storing and retrieving tenant rights information from PDF."""
|
15 |
|
16 |
+
def __init__(self, persist_directory="./chroma_db"):
|
17 |
"""Initialize the vector database."""
|
18 |
logging.info("Initializing VectorDatabase")
|
19 |
logging.info(f"NumPy version: {np.__version__}")
|
|
|
182 |
if __name__ == "__main__":
|
183 |
try:
|
184 |
db = VectorDatabase()
|
185 |
+
pdf_path = "tenant-landlord.pdf"
|
186 |
db.process_and_load_pdf(pdf_path)
|
187 |
states = db.get_states()
|
188 |
print(f"Available states: {states}")
|