Spaces:
Sleeping
Sleeping
Commit
·
9cde9c0
1
Parent(s):
7f41c25
Legal Query chatbot intial commit
Browse files- app.py +134 -58
- constitution_of_india.pdf +0 -0
- ipc.pdf +0 -0
- property_law.pdf +0 -0
- requirements.txt +5 -1
app.py
CHANGED
|
@@ -1,63 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 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 |
if __name__ == "__main__":
|
| 63 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import openai
|
| 3 |
+
from langchain_community.embeddings import OpenAIEmbeddings
|
| 4 |
+
from langchain_community.vectorstores import FAISS
|
| 5 |
+
from langchain_community.llms import OpenAI
|
| 6 |
+
from langchain.chains import ConversationChain
|
| 7 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 8 |
+
from langchain.memory import ConversationBufferMemory
|
| 9 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 10 |
+
from langchain_openai import ChatOpenAI
|
| 11 |
+
from langchain.memory import ConversationSummaryMemory
|
| 12 |
+
|
| 13 |
import gradio as gr
|
| 14 |
+
from PyPDF2 import PdfReader
|
| 15 |
+
from langchain.agents import initialize_agent, Tool
|
| 16 |
+
from langchain.schema import HumanMessage, AIMessage
|
| 17 |
+
from langchain_core.exceptions import OutputParserException
|
| 18 |
+
|
| 19 |
+
# Set up OpenAI API key
|
| 20 |
+
#openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 21 |
+
|
| 22 |
+
#TODO: Need to remove
|
| 23 |
+
apiKey = open("key.txt", "r").readline().strip('\n')
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Load PDF
|
| 28 |
+
def read_pdf(file_paths):
|
| 29 |
+
combined_text = ""
|
| 30 |
+
for file_path in file_paths:
|
| 31 |
+
with open(file_path, "rb") as file:
|
| 32 |
+
reader = PdfReader(file)
|
| 33 |
+
text = ""
|
| 34 |
+
for page in reader.pages:
|
| 35 |
+
text += page.extract_text()
|
| 36 |
+
combined_text += text + "\n\n" # Add a newline for separation between files
|
| 37 |
+
return combined_text
|
| 38 |
+
|
| 39 |
+
# Load legal document (Constitution of India)
|
| 40 |
+
pdf_file_path = ["property_law.pdf","ipc.pdf","constitution_of_india.pdf"]
|
| 41 |
+
document_text = read_pdf(pdf_file_path)
|
| 42 |
+
|
| 43 |
+
# Split the text into smaller chunks (e.g., 1,000 characters each)
|
| 44 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 45 |
+
chunk_size=1000, # Adjust this size based on your needs
|
| 46 |
+
chunk_overlap=100 # To keep some overlap between chunks for better context
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Split the document text
|
| 50 |
+
chunks = text_splitter.split_text(document_text)
|
| 51 |
+
|
| 52 |
+
# Initialize embeddings and FAISS vector store
|
| 53 |
+
embeddings = OpenAIEmbeddings(openai_api_key=apiKey)
|
| 54 |
+
vector_db = FAISS.from_texts(chunks, embeddings)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# Function to retrieve relevant content from vector DB
|
| 60 |
+
def retrieve_from_db(query):
|
| 61 |
+
results = vector_db.similarity_search(query, k=1)
|
| 62 |
+
return results[0].page_content
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# Initialize OpenAI LLM
|
| 66 |
+
llm = ChatOpenAI(openai_api_key=apiKey)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# Define agent tools
|
| 70 |
+
tools = [
|
| 71 |
+
Tool(
|
| 72 |
+
name="DocumentRetriever",
|
| 73 |
+
func=retrieve_from_db,
|
| 74 |
+
description="Retrieves relevant legal information from pre-loaded documents"
|
| 75 |
+
)
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
# Initialize memory and agent
|
| 80 |
+
memory = ConversationSummaryMemory(llm=llm)
|
| 81 |
+
agent = initialize_agent(
|
| 82 |
+
tools=tools,
|
| 83 |
+
agent_type="zero-shot-react-description",
|
| 84 |
+
llm=llm,
|
| 85 |
+
memory=memory,
|
| 86 |
+
handle_parsing_errors=True
|
| 87 |
)
|
| 88 |
|
| 89 |
|
| 90 |
+
# Function to interact with the agent and store conversation
|
| 91 |
+
def chatbot(input_text, chat_history):
|
| 92 |
+
try:
|
| 93 |
+
|
| 94 |
+
# Run the agent with the input and memory history
|
| 95 |
+
response = agent.run(input_text)
|
| 96 |
+
|
| 97 |
+
# Check if the response is "N/A" and replace with a custom message
|
| 98 |
+
if response == "N/A":
|
| 99 |
+
response = "Sorry, I couldn't understand your question. Please ask a specific question regarding IPC, Transfer of Property and Constitution of India."
|
| 100 |
+
|
| 101 |
+
# Store the assistant's response in memory
|
| 102 |
+
memory.save_context({"user": input_text}, {"assistant": response})
|
| 103 |
+
|
| 104 |
+
# Update chat history with the new response
|
| 105 |
+
chat_history.append([input_text, response])
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
return chat_history
|
| 109 |
+
|
| 110 |
+
except OutputParserException as e:
|
| 111 |
+
# Handle the exception and notify the user
|
| 112 |
+
error_message = "Sorry, I couldn't understand your question. Please ask a specific question regarding IPC, Transfer of Property and Constitution of India."
|
| 113 |
+
# Append the error message to chat history
|
| 114 |
+
chat_history.append([error_message, input_text])
|
| 115 |
+
print("Error:", str(e))
|
| 116 |
+
|
| 117 |
+
return chat_history
|
| 118 |
+
|
| 119 |
+
# Gradio UI
|
| 120 |
+
def gradio_interface():
|
| 121 |
+
with gr.Blocks() as demo:
|
| 122 |
+
gr.Markdown("# Legal Query Chatbot")
|
| 123 |
+
|
| 124 |
+
# Create chat UI with custom class
|
| 125 |
+
with gr.Column():
|
| 126 |
+
chatbot_ui = gr.Chatbot()
|
| 127 |
+
user_input = gr.Textbox(show_label=True, placeholder="Enter your INDIAN PENAL CODE, TRANSFER OF PROPERTY, CONSTITUTION OF INDIA query here...")
|
| 128 |
+
submit_button = gr.Button("Submit")
|
| 129 |
+
submit_button.click(fn=chatbot, inputs=[user_input, chatbot_ui], outputs=chatbot_ui)
|
| 130 |
+
user_input.submit(fn=chatbot, inputs=[user_input, chatbot_ui], outputs=chatbot_ui)
|
| 131 |
+
|
| 132 |
+
return demo
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
# Run the app
|
| 136 |
+
app = gradio_interface()
|
| 137 |
+
|
| 138 |
if __name__ == "__main__":
|
| 139 |
+
app.launch()
|
constitution_of_india.pdf
ADDED
|
Binary file (655 kB). View file
|
|
|
ipc.pdf
ADDED
|
Binary file (842 kB). View file
|
|
|
property_law.pdf
ADDED
|
Binary file (644 kB). View file
|
|
|
requirements.txt
CHANGED
|
@@ -1 +1,5 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
openai
|
| 2 |
+
langchain
|
| 3 |
+
langchain_community
|
| 4 |
+
gradio
|
| 5 |
+
PyPDF2
|