akhildarge01 commited on
Commit
ef5b9aa
·
1 Parent(s): 0d5918d

optimized code

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