RoAr777 commited on
Commit
d1dcb6a
Β·
verified Β·
1 Parent(s): 9b42243

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -19
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
- def retrieve_info():
14
- return """IPC in gist:
15
- Title and Scope: The Indian Penal Code applies to the whole of India, except for the State of Jammu and Kashmir. It dictates punishments for acts or omissions that violate its provisions.●
16
- General Explanations: The document provides definitions for various terms used throughout the code, such as "public," "servant of Government," "Government," "India," "Judge," "Court of Justice," and "moveable property."●
17
- Punishments: The Indian Penal Code outlines six categories of punishment: death, imprisonment for life, imprisonment (rigorous or simple), forfeiture of property, and fine.●
18
- Offenses Against the State: These include waging war against the Government of India, sedition, and offenses relating to the Army, Navy, and Air Force.●
19
- Offenses Against Public Tranquility: This category encompasses unlawful assembly, rioting, and affray (fighting in a public place).●
20
- Offenses By or Relating to Public Servants: This section covers offenses such as public servants disobeying the law, engaging in unlawful trade, and framing incorrect documents.●
21
- Offenses Relating to Documents and Property Marks: This part deals with forgery, using forged documents, counterfeiting, and offenses related to property marks.●
22
- Criminal Breach of Contracts of Service: The code addresses breaches of contract related to attending to helpless individuals.●
23
- Offenses Relating to Marriage: This section covers offenses such as causing a woman to cohabit based on deceitful marriage, marrying again while a spouse is alive, and concealing a previous marriage.●
24
- Defamation: The code defines defamation and includes exceptions for publications made in good faith, such as court reports, opinions on decided cases, and cautions conveyed for the good of others.●
25
- Criminal Intimidation, Insult, and Annoyance: This section addresses criminal intimidation, intentional insult to provoke breach of peace, statements conducing to public mischief, and statements promoting enmity between classes.
26
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
 
 
 
 
 
 
 
 
 
28
 
 
 
 
 
29
 
30
  ipc_tool = Tool(
31
  name="IPC Information Retrieval",
32
  func=retrieve_info,
33
- description="Provides IPC Gist.No parameter required."
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
  )