akhildarge01 commited on
Commit
9cde9c0
·
1 Parent(s): 7f41c25

Legal Query chatbot intial commit

Browse files
Files changed (5) hide show
  1. app.py +134 -58
  2. constitution_of_india.pdf +0 -0
  3. ipc.pdf +0 -0
  4. property_law.pdf +0 -0
  5. requirements.txt +5 -1
app.py CHANGED
@@ -1,63 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
8
-
9
-
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
-
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
-
26
- messages.append({"role": "user", "content": message})
27
-
28
- response = ""
29
-
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
- """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  )
60
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  if __name__ == "__main__":
63
- demo.launch()
 
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
- huggingface_hub==0.22.2
 
 
 
 
 
1
+ openai
2
+ langchain
3
+ langchain_community
4
+ gradio
5
+ PyPDF2