Update app.py
Browse files
app.py
CHANGED
|
@@ -10,30 +10,70 @@ import os
|
|
| 10 |
import pytesseract
|
| 11 |
from PIL import Image
|
| 12 |
pytesseract.pytesseract.tesseract_cmd = r"tesseract.exe"
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
ipc_tool = Tool(
|
| 31 |
name="IPC Information Retrieval",
|
| 32 |
func=retrieve_info,
|
| 33 |
-
description="
|
| 34 |
)
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
llm = ChatGoogleGenerativeAI(
|
| 38 |
model="gemini-1.5-pro",
|
| 39 |
temperature=0.25,
|
|
@@ -56,7 +96,7 @@ llm = ChatGoogleGenerativeAI(
|
|
| 56 |
""",
|
| 57 |
)
|
| 58 |
|
| 59 |
-
agent_tools = [ipc_tool]
|
| 60 |
|
| 61 |
agent = initialize_agent(
|
| 62 |
tools=agent_tools,
|
|
@@ -145,6 +185,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
| 145 |
|
| 146 |
|
| 147 |
iface.launch(
|
| 148 |
-
show_error=True
|
| 149 |
-
prevent_thread_lock=True
|
| 150 |
)
|
|
|
|
| 10 |
import pytesseract
|
| 11 |
from PIL import Image
|
| 12 |
pytesseract.pytesseract.tesseract_cmd = r"tesseract.exe"
|
| 13 |
+
model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
|
| 14 |
+
index = faiss.read_index('IPC_index.faiss')
|
| 15 |
+
index2 = faiss.read_index('CrpC_index.faiss')
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Step 3: Retrieval with Citations using PDF filename
|
| 19 |
+
def retrieve_info_with_citation(query, top_k=5):
|
| 20 |
+
query_embedding = model.encode([query])
|
| 21 |
+
D, I = index.search(query_embedding, k=top_k)
|
| 22 |
+
|
| 23 |
+
results = []
|
| 24 |
+
for i in range(min(top_k, len(I[0]))):
|
| 25 |
+
if D[0][i] < 1.0: # Relevance threshold
|
| 26 |
+
chunk_index = I[0][i]
|
| 27 |
+
citation = f"Source: IPC"
|
| 28 |
+
results.append((match, citation))
|
| 29 |
+
else:
|
| 30 |
+
break
|
| 31 |
+
|
| 32 |
+
if results:
|
| 33 |
+
return results
|
| 34 |
+
else:
|
| 35 |
+
return [("I'm sorry, I couldn't find relevant information.", "Source: N/A")]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def retrieve_info_with_citation2(query, top_k=5):
|
| 39 |
+
query_embedding = model.encode([query])
|
| 40 |
+
D, I = index.search(query_embedding, k=top_k)
|
| 41 |
+
|
| 42 |
+
results = []
|
| 43 |
+
for i in range(min(top_k, len(I[0]))):
|
| 44 |
+
if D[0][i] < 1.0: # Relevance threshold
|
| 45 |
+
chunk_index = I[0][i]
|
| 46 |
+
citation = f"Source: CrPC"
|
| 47 |
+
results.append((match, citation))
|
| 48 |
+
else:
|
| 49 |
+
break
|
| 50 |
|
| 51 |
+
if results:
|
| 52 |
+
return results
|
| 53 |
+
else:
|
| 54 |
+
return [("I'm sorry, I couldn't find relevant information.", "Source: N/A")]
|
| 55 |
+
|
| 56 |
+
def retrieve_info(query):
|
| 57 |
+
results = retrieve_info_with_citation(query)
|
| 58 |
+
formatted_results = "\n\n".join([f"{i+1}. {match}\n{citation}" for i, (match, citation) in enumerate(results)])
|
| 59 |
+
return formatted_results
|
| 60 |
|
| 61 |
+
def retrieve_info2(query):
|
| 62 |
+
results = retrieve_info_with_citation2(query)
|
| 63 |
+
formatted_results = "\n\n".join([f"{i+1}. {match}\n{citation}" for i, (match, citation) in enumerate(results)])
|
| 64 |
+
return formatted_results
|
| 65 |
|
| 66 |
ipc_tool = Tool(
|
| 67 |
name="IPC Information Retrieval",
|
| 68 |
func=retrieve_info,
|
| 69 |
+
description="Retrieve information from the Indian Penal Code Related to query keyword(s)."
|
| 70 |
)
|
| 71 |
|
| 72 |
+
crpc_tool=Tool(
|
| 73 |
+
name="CrPC Information Retrieval",
|
| 74 |
+
func=retrieve_info2,
|
| 75 |
+
description="Retrieve information from the Code of Criminal Procedure(CrPC) Related to query keyword(s)."
|
| 76 |
+
)
|
| 77 |
llm = ChatGoogleGenerativeAI(
|
| 78 |
model="gemini-1.5-pro",
|
| 79 |
temperature=0.25,
|
|
|
|
| 96 |
""",
|
| 97 |
)
|
| 98 |
|
| 99 |
+
agent_tools = [ipc_tool,crpc_tool]
|
| 100 |
|
| 101 |
agent = initialize_agent(
|
| 102 |
tools=agent_tools,
|
|
|
|
| 185 |
|
| 186 |
|
| 187 |
iface.launch(
|
| 188 |
+
show_error=True
|
|
|
|
| 189 |
)
|