Spaces:
Sleeping
Sleeping
Commit ·
ed596e9
1
Parent(s): ef5b9aa
Added more relevant data
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ def read_pdf(file_paths):
|
|
| 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(
|
|
@@ -37,7 +37,7 @@ chunks = text_splitter.split_text(document_text)
|
|
| 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):
|
|
@@ -50,7 +50,7 @@ llm = ChatOpenAI(openai_api_key=apiKey)
|
|
| 50 |
|
| 51 |
tools = [
|
| 52 |
Tool(
|
| 53 |
-
name="
|
| 54 |
func=retrieve_from_db,
|
| 55 |
description="Retrieves relevant legal information from pre-loaded documents"
|
| 56 |
)
|
|
@@ -73,7 +73,7 @@ def chatbot(input_text, chat_history):
|
|
| 73 |
response = agent.run(input_text)
|
| 74 |
|
| 75 |
if response == "N/A":
|
| 76 |
-
response =
|
| 77 |
|
| 78 |
memory.save_context({"user": input_text}, {"assistant": response})
|
| 79 |
|
|
@@ -83,11 +83,14 @@ def chatbot(input_text, chat_history):
|
|
| 83 |
return chat_history
|
| 84 |
|
| 85 |
except OutputParserException as e:
|
| 86 |
-
error_message =
|
| 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:
|
|
@@ -98,7 +101,9 @@ def gradio_interface():
|
|
| 98 |
user_input = gr.Textbox(show_label=True, placeholder="Enter your INDIAN PENAL CODE, TRANSFER OF PROPERTY, CONSTITUTION OF INDIA query here...")
|
| 99 |
submit_button = gr.Button("Submit")
|
| 100 |
submit_button.click(fn=chatbot, inputs=[user_input, chatbot_ui], outputs=chatbot_ui)
|
|
|
|
| 101 |
user_input.submit(fn=chatbot, inputs=[user_input, chatbot_ui], outputs=chatbot_ui)
|
|
|
|
| 102 |
|
| 103 |
return demo
|
| 104 |
|
|
|
|
| 24 |
combined_text += text + "\n\n"
|
| 25 |
return combined_text
|
| 26 |
|
| 27 |
+
pdf_file_path = ["property_law.pdf","ipc.pdf","constitution_of_india.pdf","ipc_2.pdf","cn_2.pdf","pl_2.pdf"]
|
| 28 |
document_text = read_pdf(pdf_file_path)
|
| 29 |
|
| 30 |
text_splitter = RecursiveCharacterTextSplitter(
|
|
|
|
| 37 |
embeddings = OpenAIEmbeddings(openai_api_key=apiKey)
|
| 38 |
vector_db = FAISS.from_texts(chunks, embeddings)
|
| 39 |
|
| 40 |
+
exceptionMsg = "Sorry, I couldn't understand your question. Please ask a specific question regarding IPC, Transfer of Property and Constitution of India."
|
| 41 |
|
| 42 |
|
| 43 |
def retrieve_from_db(query):
|
|
|
|
| 50 |
|
| 51 |
tools = [
|
| 52 |
Tool(
|
| 53 |
+
name="Legal-Library",
|
| 54 |
func=retrieve_from_db,
|
| 55 |
description="Retrieves relevant legal information from pre-loaded documents"
|
| 56 |
)
|
|
|
|
| 73 |
response = agent.run(input_text)
|
| 74 |
|
| 75 |
if response == "N/A":
|
| 76 |
+
response = exceptionMsg
|
| 77 |
|
| 78 |
memory.save_context({"user": input_text}, {"assistant": response})
|
| 79 |
|
|
|
|
| 83 |
return chat_history
|
| 84 |
|
| 85 |
except OutputParserException as e:
|
| 86 |
+
error_message = exceptionMsg
|
| 87 |
chat_history.append([error_message, input_text])
|
| 88 |
print("Error:", str(e))
|
| 89 |
|
| 90 |
return chat_history
|
| 91 |
+
|
| 92 |
+
def clear_input():
|
| 93 |
+
return gr.update(value="")
|
| 94 |
|
| 95 |
def gradio_interface():
|
| 96 |
with gr.Blocks() as demo:
|
|
|
|
| 101 |
user_input = gr.Textbox(show_label=True, placeholder="Enter your INDIAN PENAL CODE, TRANSFER OF PROPERTY, CONSTITUTION OF INDIA query here...")
|
| 102 |
submit_button = gr.Button("Submit")
|
| 103 |
submit_button.click(fn=chatbot, inputs=[user_input, chatbot_ui], outputs=chatbot_ui)
|
| 104 |
+
submit_button.click(fn=clear_input, inputs=None, outputs=user_input)
|
| 105 |
user_input.submit(fn=chatbot, inputs=[user_input, chatbot_ui], outputs=chatbot_ui)
|
| 106 |
+
user_input.submit(fn=clear_input, inputs=None, outputs=user_input)
|
| 107 |
|
| 108 |
return demo
|
| 109 |
|
cn_2.pdf
ADDED
|
Binary file (881 kB). View file
|
|
|
ipc_2.pdf
ADDED
|
Binary file (910 kB). View file
|
|
|
pl_2.pdf
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|