import time import os import streamlit as st from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas from googleapiclient.discovery import build from google.oauth2.service_account import Credentials from langchain_community.vectorstores import FAISS from langchain_community.embeddings import HuggingFaceEmbeddings from langchain.prompts import PromptTemplate from langchain.memory import ConversationBufferWindowMemory from langchain.chains import ConversationalRetrievalChain from langchain_together import Together from footer import footer # Google Drive API setup SCOPES = ["https://www.googleapis.com/auth/drive.readonly"] SERVICE_ACCOUNT_FILE = "data/credentials.json" # Path to your Google API credentials file FOLDER_ID = "1LZIx-1tt_GormpU8nF_I2WL88Oxa9juU" # Replace with your Google Drive folder ID def authenticate_drive(): """Authenticate and return the Google Drive API service.""" creds = Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES) return build("drive", "v3", credentials=creds) from fuzzywuzzy import process def search_drive_file(file_name): """Search for a file by name in the specified Google Drive folder using fuzzy matching.""" service = authenticate_drive() try: # Get all files in the folder query = f"'{FOLDER_ID}' in parents and trashed=false" results = service.files().list(q=query, fields="files(id, name)").execute() files = results.get("files", []) # Debug: Print all file names for inspection st.write("Available files:", [f['name'] for f in files]) # Perform fuzzy matching to find the best match file_names = [f['name'] for f in files] best_match, score = process.extractOne(file_name, file_names) if score >= 75: # Threshold for a match matched_file = next(f for f in files if f['name'] == best_match) st.write(f"Match found: {matched_file['name']} (Score: {score})") return [matched_file] else: st.warning(f"No close matches found for '{file_name}'. Try rephrasing or checking the folder manually.") return [] except Exception as e: st.error(f"An error occurred: {e}") return [] # Set the Streamlit page configuration and theme st.set_page_config(page_title="In-Legal-IPC", layout="wide") # Display the logo image with blur shadow col1, col2, col3 = st.columns([1, 30, 1]) with col2: st.markdown( """
If you need any application form, please mention 'form' at the end.
", unsafe_allow_html=True) # Add CSS for the button styling st.markdown(""" """, unsafe_allow_html=True) # Display previous messages for message in st.session_state.messages: with st.chat_message(message["role"]): st.write(message["content"]) # Initialize session state variables if "show_reset" not in st.session_state: st.session_state.show_reset = False # Chat input area input_prompt = st.chat_input("Say something...") if input_prompt: with st.chat_message("user"): st.markdown(f"**You:** {input_prompt}") # Enable the reset button after receiving input st.session_state.show_reset = True if "form" in input_prompt.lower() or "document" in input_prompt.lower(): with st.spinner("Searching Google Drive..."): # Call the updated search function search_results = search_drive_file(input_prompt) if search_results: # Generate response for found files response = "🔍 Document(s) found! Click below to view:" for file in search_results: response += f"\n- [{file['name']}](https://drive.google.com/file/d/{file['id']}/view)" st.session_state.messages.append({"role": "assistant", "content": response}) st.write(response) else: # If no results, provide an alternative message response = ( "⚠️ No matching documents found. " "Please check the spelling or explore the folder directly: " f"[Google Drive Folder](https://drive.google.com/drive/folders/{FOLDER_ID})" ) st.session_state.messages.append({"role": "assistant", "content": response}) st.write(response) else: # Handle general questions with st.chat_message("assistant"): with st.spinner("Thinking 💡..."): try: # Validate the input before invoking the QA chain if not input_prompt.strip(): st.warning("⚠️ Input cannot be empty!") else: result = qa.invoke(input=input_prompt) answer = result["answer"].strip() # Simulate typing effect for the response message_placeholder = st.empty() full_response = ( "⚠️ **_Gentle reminder: We strive for precision, but please double-check._**\n\n" ) for chunk in answer.split(): full_response += chunk + " " time.sleep(0.02) # Simulating typing message_placeholder.markdown(full_response + " |", unsafe_allow_html=True) st.session_state.messages.append({"role": "assistant", "content": answer}) except Exception as e: # Handle unexpected errors during QA invocation error_message = f"⚠️ **_Error: An unexpected issue occurred: {str(e)}._**" st.error(error_message) st.session_state.messages.append({"role": "assistant", "content": error_message}) # Reset button if st.session_state.show_reset: if st.button('🗑️ Reset All Chat', on_click=reset_conversation): st.rerun() # Updated from st.experimental_rerun footer()