usmanyousaf commited on
Commit
cb4d732
·
verified ·
1 Parent(s): 1ed40d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -127
app.py CHANGED
@@ -140,8 +140,8 @@ st.set_page_config(
140
 
141
  # Constants
142
  DEFAULT_GROQ_API_KEY = os.getenv("GROQ_API_KEY")
143
- MODEL_NAME = "llama-3.3-70b-versatile"
144
- DEFAULT_DOCUMENT_PATH = "lawbook.pdf" # Path to your hardcoded Pakistan laws PDF
145
  DEFAULT_COLLECTION_NAME = "pakistan_laws_default"
146
  CHROMA_PERSIST_DIR = "./chroma_db"
147
 
@@ -179,49 +179,41 @@ def setup_llm():
179
 
180
  def check_default_db_exists():
181
  """Check if the default document database already exists"""
182
- if os.path.exists(os.path.join(CHROMA_PERSIST_DIR, DEFAULT_COLLECTION_NAME)):
183
- return True
184
- return False
185
 
186
  def load_existing_vectordb(collection_name):
187
  """Load an existing vector database from disk"""
188
  embeddings = setup_embeddings()
189
  try:
190
- db = Chroma(
191
  persist_directory=CHROMA_PERSIST_DIR,
192
  embedding_function=embeddings,
193
  collection_name=collection_name
194
  )
195
- return db
196
  except Exception as e:
197
  st.error(f"Error loading existing database: {str(e)}")
198
  return None
199
 
200
  def process_default_document(force_rebuild=False):
201
- """Process the default Pakistan laws document or load from disk if available"""
202
- # Check if database already exists
203
  if check_default_db_exists() and not force_rebuild:
204
  st.info("Loading existing Pakistan law database...")
205
  db = load_existing_vectordb(DEFAULT_COLLECTION_NAME)
206
- if db is not None:
207
  st.session_state.vectordb = db
208
  setup_qa_chain()
209
  st.session_state.using_custom_docs = False
210
  return True
211
 
212
- # If database doesn't exist or force rebuild, create it
213
  if not os.path.exists(DEFAULT_DOCUMENT_PATH):
214
- st.error(f"Default document {DEFAULT_DOCUMENT_PATH} not found. Please make sure it exists.")
215
  return False
216
 
217
- embeddings = setup_embeddings()
218
-
219
  try:
220
- with st.spinner("Building Pakistan law database (this may take a few minutes)..."):
221
  loader = PyPDFLoader(DEFAULT_DOCUMENT_PATH)
222
  documents = loader.load()
223
 
224
- # Add source filename to metadata
225
  for doc in documents:
226
  doc.metadata["source"] = "Pakistan Laws (Official)"
227
 
@@ -231,61 +223,44 @@ def process_default_document(force_rebuild=False):
231
  )
232
  chunks = text_splitter.split_documents(documents)
233
 
234
- # Create vector store
235
  db = Chroma.from_documents(
236
  documents=chunks,
237
- embedding=embeddings,
238
  collection_name=DEFAULT_COLLECTION_NAME,
239
  persist_directory=CHROMA_PERSIST_DIR
240
  )
241
 
242
- # Explicitly persist to disk
243
  db.persist()
244
-
245
  st.session_state.vectordb = db
246
  setup_qa_chain()
247
  st.session_state.using_custom_docs = False
248
-
249
  return True
250
  except Exception as e:
251
  st.error(f"Error processing default document: {str(e)}")
252
  return False
253
 
254
- def check_custom_db_exists(collection_name):
255
- """Check if a custom document database already exists"""
256
- if os.path.exists(os.path.join(CHROMA_PERSIST_DIR, collection_name)):
257
- return True
258
- return False
259
-
260
  def process_custom_documents(uploaded_files):
261
  """Process user-uploaded PDF documents"""
262
  embeddings = setup_embeddings()
263
  collection_name = st.session_state.custom_collection_name
264
-
265
  documents = []
266
 
267
  for uploaded_file in uploaded_files:
268
- # Save file temporarily
269
  with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
270
  tmp_file.write(uploaded_file.getvalue())
271
  tmp_path = tmp_file.name
272
 
273
- # Load and split the document
274
  try:
275
  loader = PyPDFLoader(tmp_path)
276
  file_docs = loader.load()
277
 
278
- # Add source filename to metadata
279
  for doc in file_docs:
280
  doc.metadata["source"] = uploaded_file.name
281
 
282
  documents.extend(file_docs)
283
-
284
- # Clean up temp file
285
  os.unlink(tmp_path)
286
  except Exception as e:
287
  st.error(f"Error processing {uploaded_file.name}: {str(e)}")
288
- continue
289
 
290
  if documents:
291
  text_splitter = RecursiveCharacterTextSplitter(
@@ -294,19 +269,18 @@ def process_custom_documents(uploaded_files):
294
  )
295
  chunks = text_splitter.split_documents(documents)
296
 
297
- # Create vector store
298
  with st.spinner("Building custom document database..."):
299
- # If a previous custom DB exists for this user, delete it first
300
- if check_custom_db_exists(collection_name):
301
- # We need to recreate the vectorstore to delete the old collection
302
- temp_db = Chroma(
 
 
303
  persist_directory=CHROMA_PERSIST_DIR,
304
  embedding_function=embeddings,
305
  collection_name=collection_name
306
- )
307
- temp_db.delete_collection()
308
 
309
- # Create new vector store
310
  db = Chroma.from_documents(
311
  documents=chunks,
312
  embedding=embeddings,
@@ -314,25 +288,18 @@ def process_custom_documents(uploaded_files):
314
  persist_directory=CHROMA_PERSIST_DIR
315
  )
316
 
317
- # Explicitly persist to disk
318
  db.persist()
319
-
320
  st.session_state.vectordb = db
321
  setup_qa_chain()
322
  st.session_state.using_custom_docs = True
323
-
324
  return True
325
  return False
326
 
327
  def setup_qa_chain():
328
  """Set up the QA chain with the RAG system"""
329
  if st.session_state.vectordb:
330
- llm = setup_llm()
331
-
332
- # Create prompt template
333
  template = """You are a helpful legal assistant specializing in Pakistani law.
334
- Use the following context to answer the question. If you don't know the answer based on the context,
335
- say that you don't have enough information, but provide general legal information if possible.
336
 
337
  Context: {context}
338
 
@@ -340,52 +307,33 @@ def setup_qa_chain():
340
 
341
  Answer:"""
342
 
343
- prompt = ChatPromptTemplate.from_template(template)
344
-
345
- # Create the QA chain
346
  st.session_state.qa_chain = RetrievalQA.from_chain_type(
347
- llm=llm,
348
  chain_type="stuff",
349
  retriever=st.session_state.vectordb.as_retriever(search_kwargs={"k": 3}),
350
- chain_type_kwargs={"prompt": prompt},
351
  return_source_documents=True
352
  )
353
 
354
  def generate_similar_questions(question, docs):
355
  """Generate similar questions based on retrieved documents"""
356
  llm = setup_llm()
357
-
358
- # Extract key content from docs
359
  context = "\n".join([doc.page_content for doc in docs[:2]])
360
 
361
- # Prompt to generate similar questions
362
- prompt = f"""Based on the following user question and legal context, generate 3 similar questions that the user might also be interested in.
363
- Make the questions specific, related to Pakistani law, and directly relevant to the original question.
364
-
365
  Original Question: {question}
366
-
367
  Legal Context: {context}
368
-
369
  Generate exactly 3 similar questions:"""
370
 
371
  try:
372
  response = llm.invoke(prompt)
373
- # Extract questions from response using regex
374
  questions = re.findall(r"\d+\.\s+(.*?)(?=\d+\.|$)", response.content, re.DOTALL)
375
- if not questions:
376
- questions = response.content.split("\n")
377
- questions = [q.strip() for q in questions if q.strip() and not q.startswith("Similar") and "?" in q]
378
-
379
- # Clean and limit to 3 questions
380
- questions = [q.strip().replace("\n", " ") for q in questions if "?" in q]
381
- return questions[:3]
382
  except Exception as e:
383
- print(f"Error generating similar questions: {e}")
384
  return []
385
 
386
  def get_answer(question):
387
  """Get answer from QA chain"""
388
- # If default documents haven't been processed yet, try to load them
389
  if not st.session_state.vectordb:
390
  with st.spinner("Loading Pakistan law database..."):
391
  process_default_document()
@@ -393,71 +341,56 @@ def get_answer(question):
393
  if st.session_state.qa_chain:
394
  result = st.session_state.qa_chain({"query": question})
395
  answer = result["result"]
396
-
397
- # Generate similar questions
398
- source_docs = result.get("source_documents", [])
399
- st.session_state.similar_questions = generate_similar_questions(question, source_docs)
400
-
401
- # Add source information
402
  sources = set()
403
- for doc in source_docs:
 
404
  if "source" in doc.metadata:
405
  sources.add(doc.metadata["source"])
406
 
407
  if sources:
408
  answer += f"\n\nSources: {', '.join(sources)}"
409
 
 
 
 
410
  return answer
411
- else:
412
- return "Initializing the knowledge base. Please try again in a moment."
413
 
414
  def main():
415
- st.title("Pakistan Law AI Agent")
 
416
 
417
- # Determine current mode
418
  if st.session_state.using_custom_docs:
419
  st.subheader("Training on your personal resources")
420
  else:
421
- st.subheader("Powered by Pakistan law database")
422
 
423
- # Sidebar for uploading documents and switching modes
424
  with st.sidebar:
425
  st.header("Resource Management")
426
 
427
- # Option to return to default documents
428
  if st.session_state.using_custom_docs:
429
  if st.button("Return to Official Database"):
430
- with st.spinner("Loading official Pakistan law database..."):
431
  process_default_document()
432
- st.success("Switched to official Pakistan law database!")
433
- st.session_state.messages.append(AIMessage(content="Switched to official Pakistan law database. You can now ask legal questions."))
434
  st.rerun()
435
 
436
- # Option to rebuild the default database
437
  if not st.session_state.using_custom_docs:
438
  if st.button("Rebuild Official Database"):
439
- with st.spinner("Rebuilding official Pakistan law database..."):
440
  process_default_document(force_rebuild=True)
441
- st.success("Official database rebuilt successfully!")
442
  st.rerun()
443
 
444
- # Option to upload custom documents
445
- st.header("Upload Custom Legal Documents")
446
  uploaded_files = st.file_uploader(
447
- "Upload PDF files containing legal documents",
448
- type=["pdf"],
449
- accept_multiple_files=True
450
- )
451
 
452
  if st.button("Train on Uploaded Documents") and uploaded_files:
453
- with st.spinner("Processing your documents..."):
454
- success = process_custom_documents(uploaded_files)
455
- if success:
456
- st.success("Your documents processed successfully!")
457
- st.session_state.messages.append(AIMessage(content="Custom legal documents loaded successfully. You are now training on your personal resources."))
458
  st.rerun()
459
-
460
- # Display chat messages
461
  for message in st.session_state.messages:
462
  if isinstance(message, HumanMessage):
463
  with st.chat_message("user"):
@@ -465,42 +398,29 @@ def main():
465
  else:
466
  with st.chat_message("assistant", avatar="⚖️"):
467
  st.write(message.content)
468
-
469
- # Display similar questions if available
470
  if st.session_state.similar_questions:
471
  st.markdown("#### Related Questions:")
472
  cols = st.columns(len(st.session_state.similar_questions))
473
- for i, question in enumerate(st.session_state.similar_questions):
474
- if cols[i].button(question, key=f"similar_q_{i}"):
475
- # Add selected question as user input
476
- st.session_state.messages.append(HumanMessage(content=question))
477
-
478
- # Generate and display assistant response
479
- with st.chat_message("assistant", avatar="⚖️"):
480
- with st.spinner("Thinking..."):
481
- response = get_answer(question)
482
- st.write(response)
483
-
484
- # Add assistant response to chat history
485
- st.session_state.messages.append(AIMessage(content=response))
486
  st.rerun()
487
-
488
- # Input for new question
489
  if user_input := st.chat_input("Ask a legal question..."):
490
- # Add user message to chat history
491
  st.session_state.messages.append(HumanMessage(content=user_input))
492
 
493
- # Display user message
494
  with st.chat_message("user"):
495
  st.write(user_input)
496
 
497
- # Generate and display assistant response
498
  with st.chat_message("assistant", avatar="⚖️"):
499
  with st.spinner("Thinking..."):
500
  response = get_answer(user_input)
501
  st.write(response)
502
 
503
- # Add assistant response to chat history
504
  st.session_state.messages.append(AIMessage(content=response))
505
  st.rerun()
506
 
 
140
 
141
  # Constants
142
  DEFAULT_GROQ_API_KEY = os.getenv("GROQ_API_KEY")
143
+ MODEL_NAME = "llama3-70b-8192"
144
+ DEFAULT_DOCUMENT_PATH = "lawbook.pdf"
145
  DEFAULT_COLLECTION_NAME = "pakistan_laws_default"
146
  CHROMA_PERSIST_DIR = "./chroma_db"
147
 
 
179
 
180
  def check_default_db_exists():
181
  """Check if the default document database already exists"""
182
+ return os.path.exists(os.path.join(CHROMA_PERSIST_DIR, DEFAULT_COLLECTION_NAME))
 
 
183
 
184
  def load_existing_vectordb(collection_name):
185
  """Load an existing vector database from disk"""
186
  embeddings = setup_embeddings()
187
  try:
188
+ return Chroma(
189
  persist_directory=CHROMA_PERSIST_DIR,
190
  embedding_function=embeddings,
191
  collection_name=collection_name
192
  )
 
193
  except Exception as e:
194
  st.error(f"Error loading existing database: {str(e)}")
195
  return None
196
 
197
  def process_default_document(force_rebuild=False):
198
+ """Process the default Pakistan laws document"""
 
199
  if check_default_db_exists() and not force_rebuild:
200
  st.info("Loading existing Pakistan law database...")
201
  db = load_existing_vectordb(DEFAULT_COLLECTION_NAME)
202
+ if db:
203
  st.session_state.vectordb = db
204
  setup_qa_chain()
205
  st.session_state.using_custom_docs = False
206
  return True
207
 
 
208
  if not os.path.exists(DEFAULT_DOCUMENT_PATH):
209
+ st.error(f"Default document {DEFAULT_DOCUMENT_PATH} not found.")
210
  return False
211
 
 
 
212
  try:
213
+ with st.spinner("Building Pakistan law database..."):
214
  loader = PyPDFLoader(DEFAULT_DOCUMENT_PATH)
215
  documents = loader.load()
216
 
 
217
  for doc in documents:
218
  doc.metadata["source"] = "Pakistan Laws (Official)"
219
 
 
223
  )
224
  chunks = text_splitter.split_documents(documents)
225
 
 
226
  db = Chroma.from_documents(
227
  documents=chunks,
228
+ embedding=setup_embeddings(),
229
  collection_name=DEFAULT_COLLECTION_NAME,
230
  persist_directory=CHROMA_PERSIST_DIR
231
  )
232
 
 
233
  db.persist()
 
234
  st.session_state.vectordb = db
235
  setup_qa_chain()
236
  st.session_state.using_custom_docs = False
 
237
  return True
238
  except Exception as e:
239
  st.error(f"Error processing default document: {str(e)}")
240
  return False
241
 
 
 
 
 
 
 
242
  def process_custom_documents(uploaded_files):
243
  """Process user-uploaded PDF documents"""
244
  embeddings = setup_embeddings()
245
  collection_name = st.session_state.custom_collection_name
 
246
  documents = []
247
 
248
  for uploaded_file in uploaded_files:
 
249
  with tempfile.NamedTemporaryFile(delete=False, suffix='.pdf') as tmp_file:
250
  tmp_file.write(uploaded_file.getvalue())
251
  tmp_path = tmp_file.name
252
 
 
253
  try:
254
  loader = PyPDFLoader(tmp_path)
255
  file_docs = loader.load()
256
 
 
257
  for doc in file_docs:
258
  doc.metadata["source"] = uploaded_file.name
259
 
260
  documents.extend(file_docs)
 
 
261
  os.unlink(tmp_path)
262
  except Exception as e:
263
  st.error(f"Error processing {uploaded_file.name}: {str(e)}")
 
264
 
265
  if documents:
266
  text_splitter = RecursiveCharacterTextSplitter(
 
269
  )
270
  chunks = text_splitter.split_documents(documents)
271
 
 
272
  with st.spinner("Building custom document database..."):
273
+ if Chroma(
274
+ persist_directory=CHROMA_PERSIST_DIR,
275
+ embedding_function=embeddings,
276
+ collection_name=collection_name
277
+ ).get():
278
+ Chroma(
279
  persist_directory=CHROMA_PERSIST_DIR,
280
  embedding_function=embeddings,
281
  collection_name=collection_name
282
+ ).delete_collection()
 
283
 
 
284
  db = Chroma.from_documents(
285
  documents=chunks,
286
  embedding=embeddings,
 
288
  persist_directory=CHROMA_PERSIST_DIR
289
  )
290
 
 
291
  db.persist()
 
292
  st.session_state.vectordb = db
293
  setup_qa_chain()
294
  st.session_state.using_custom_docs = True
 
295
  return True
296
  return False
297
 
298
  def setup_qa_chain():
299
  """Set up the QA chain with the RAG system"""
300
  if st.session_state.vectordb:
 
 
 
301
  template = """You are a helpful legal assistant specializing in Pakistani law.
302
+ Use the context to answer. If unsure, say so but provide general info.
 
303
 
304
  Context: {context}
305
 
 
307
 
308
  Answer:"""
309
 
 
 
 
310
  st.session_state.qa_chain = RetrievalQA.from_chain_type(
311
+ llm=setup_llm(),
312
  chain_type="stuff",
313
  retriever=st.session_state.vectordb.as_retriever(search_kwargs={"k": 3}),
314
+ chain_type_kwargs={"prompt": ChatPromptTemplate.from_template(template)},
315
  return_source_documents=True
316
  )
317
 
318
  def generate_similar_questions(question, docs):
319
  """Generate similar questions based on retrieved documents"""
320
  llm = setup_llm()
 
 
321
  context = "\n".join([doc.page_content for doc in docs[:2]])
322
 
323
+ prompt = f"""Generate 3 similar questions based on:
 
 
 
324
  Original Question: {question}
 
325
  Legal Context: {context}
 
326
  Generate exactly 3 similar questions:"""
327
 
328
  try:
329
  response = llm.invoke(prompt)
 
330
  questions = re.findall(r"\d+\.\s+(.*?)(?=\d+\.|$)", response.content, re.DOTALL)
331
+ return [q.strip() for q in questions[:3] if "?" in q]
 
 
 
 
 
 
332
  except Exception as e:
 
333
  return []
334
 
335
  def get_answer(question):
336
  """Get answer from QA chain"""
 
337
  if not st.session_state.vectordb:
338
  with st.spinner("Loading Pakistan law database..."):
339
  process_default_document()
 
341
  if st.session_state.qa_chain:
342
  result = st.session_state.qa_chain({"query": question})
343
  answer = result["result"]
 
 
 
 
 
 
344
  sources = set()
345
+
346
+ for doc in result.get("source_documents", []):
347
  if "source" in doc.metadata:
348
  sources.add(doc.metadata["source"])
349
 
350
  if sources:
351
  answer += f"\n\nSources: {', '.join(sources)}"
352
 
353
+ st.session_state.similar_questions = generate_similar_questions(
354
+ question, result.get("source_documents", [])
355
+ )
356
  return answer
357
+ return "Initializing knowledge base..."
 
358
 
359
  def main():
360
+ inject_custom_css() # CSS injection added here
361
+ st.title("Pakistan Law AI Agent ⚖️")
362
 
 
363
  if st.session_state.using_custom_docs:
364
  st.subheader("Training on your personal resources")
365
  else:
366
+ st.subheader("Powered by Pakistan law database")
367
 
 
368
  with st.sidebar:
369
  st.header("Resource Management")
370
 
 
371
  if st.session_state.using_custom_docs:
372
  if st.button("Return to Official Database"):
373
+ with st.spinner("Loading official database..."):
374
  process_default_document()
375
+ st.session_state.messages.append(AIMessage(content="Switched to official database!"))
 
376
  st.rerun()
377
 
 
378
  if not st.session_state.using_custom_docs:
379
  if st.button("Rebuild Official Database"):
380
+ with st.spinner("Rebuilding..."):
381
  process_default_document(force_rebuild=True)
 
382
  st.rerun()
383
 
384
+ st.header("Upload Custom Documents")
 
385
  uploaded_files = st.file_uploader(
386
+ "Upload PDFs", type=["pdf"], accept_multiple_files=True)
 
 
 
387
 
388
  if st.button("Train on Uploaded Documents") and uploaded_files:
389
+ with st.spinner("Processing..."):
390
+ if process_custom_documents(uploaded_files):
391
+ st.session_state.messages.append(AIMessage(content="Custom documents loaded!"))
 
 
392
  st.rerun()
393
+
 
394
  for message in st.session_state.messages:
395
  if isinstance(message, HumanMessage):
396
  with st.chat_message("user"):
 
398
  else:
399
  with st.chat_message("assistant", avatar="⚖️"):
400
  st.write(message.content)
401
+
 
402
  if st.session_state.similar_questions:
403
  st.markdown("#### Related Questions:")
404
  cols = st.columns(len(st.session_state.similar_questions))
405
+ for i, q in enumerate(st.session_state.similar_questions):
406
+ if cols[i].button(q, key=f"similar_q_{i}"):
407
+ st.session_state.messages.extend([
408
+ HumanMessage(content=q),
409
+ AIMessage(content=get_answer(q))
410
+ ])
 
 
 
 
 
 
 
411
  st.rerun()
412
+
 
413
  if user_input := st.chat_input("Ask a legal question..."):
 
414
  st.session_state.messages.append(HumanMessage(content=user_input))
415
 
 
416
  with st.chat_message("user"):
417
  st.write(user_input)
418
 
 
419
  with st.chat_message("assistant", avatar="⚖️"):
420
  with st.spinner("Thinking..."):
421
  response = get_answer(user_input)
422
  st.write(response)
423
 
 
424
  st.session_state.messages.append(AIMessage(content=response))
425
  st.rerun()
426