RoAr777 commited on
Commit
2c39844
Β·
verified Β·
1 Parent(s): 9c75eb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -68
app.py CHANGED
@@ -11,70 +11,6 @@ import pytesseract
11
  from PIL import Image
12
  pytesseract.pytesseract.tesseract_cmd = r"tesseract.exe"
13
 
14
- model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
15
- index = faiss.read_index('IPC_index.faiss')
16
- #index2 = faiss.read_index('CrpC_index.faiss')
17
-
18
-
19
- # Step 3: Retrieval with Citations using PDF filename
20
- def retrieve_info_with_citation(query, top_k=5):
21
- query_embedding = model.encode([query])
22
- D, I = index.search(query_embedding, k=top_k)
23
-
24
- results = []
25
- for i in range(min(top_k, len(I[0]))):
26
- if D[0][i] < 1.0: # Relevance threshold
27
- chunk_index = I[0][i]
28
- citation = f"Source: IPC"
29
- results.append((match, citation))
30
- else:
31
- break
32
-
33
- if results:
34
- return results
35
- else:
36
- return [("I'm sorry, I couldn't find relevant information.", "Source: N/A")]
37
-
38
-
39
- def retrieve_info_with_citation2(query, top_k=5):
40
- query_embedding = model.encode([query])
41
- D, I = index.search(query_embedding, k=top_k)
42
-
43
- results = []
44
- for i in range(min(top_k, len(I[0]))):
45
- if D[0][i] < 1.0: # Relevance threshold
46
- chunk_index = I[0][i]
47
- citation = f"Source: CrPC"
48
- results.append((match, citation))
49
- else:
50
- break
51
-
52
- if results:
53
- return results
54
- else:
55
- return [("I'm sorry, I couldn't find relevant information.", "Source: N/A")]
56
-
57
- def retrieve_info(query):
58
- results = retrieve_info_with_citation(query)
59
- formatted_results = "\n\n".join([f"{i+1}. {match}\n{citation}" for i, (match, citation) in enumerate(results)])
60
- return formatted_results
61
-
62
- def retrieve_info2(query):
63
- results = retrieve_info_with_citation2(query)
64
- formatted_results = "\n\n".join([f"{i+1}. {match}\n{citation}" for i, (match, citation) in enumerate(results)])
65
- return formatted_results
66
-
67
- ipc_tool = Tool(
68
- name="IPC Information Retrieval",
69
- func=retrieve_info,
70
- description="Retrieve information from the Indian Penal Code Related to query keyword(s)."
71
- )
72
-
73
- crpc_tool=Tool(
74
- name="CrPC Information Retrieval",
75
- func=retrieve_info2,
76
- description="Retrieve information from the Code of Criminal Procedure(CrPC) Related to query keyword(s)."
77
- )
78
  llm = ChatGoogleGenerativeAI(
79
  model="gemini-1.5-pro",
80
  temperature=0.25,
@@ -90,6 +26,19 @@ llm = ChatGoogleGenerativeAI(
90
 
91
  Example task: "What is the punishment for theft according to the IPC?"
92
  Example response: "According to Section 379 of the IPC, the punishment for theft is imprisonment of either description for a term which may extend to three years, or with fine, or with both."
 
 
 
 
 
 
 
 
 
 
 
 
 
93
 
94
  Task: {{query}}
95
 
@@ -97,11 +46,8 @@ llm = ChatGoogleGenerativeAI(
97
  """,
98
  )
99
 
100
- agent_tools = [ipc_tool,crpc_tool]
101
-
102
  agent = initialize_agent(
103
- tools=agent_tools,
104
- llm=llm,
105
  agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
106
  verbose=True,
107
  return_intermediate_steps=True,
 
11
  from PIL import Image
12
  pytesseract.pytesseract.tesseract_cmd = r"tesseract.exe"
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  llm = ChatGoogleGenerativeAI(
15
  model="gemini-1.5-pro",
16
  temperature=0.25,
 
26
 
27
  Example task: "What is the punishment for theft according to the IPC?"
28
  Example response: "According to Section 379 of the IPC, the punishment for theft is imprisonment of either description for a term which may extend to three years, or with fine, or with both."
29
+ IPC in gist:
30
+ 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.●
31
+ 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."●
32
+ Punishments: The Indian Penal Code outlines six categories of punishment: death, imprisonment for life, imprisonment (rigorous or simple), forfeiture of property, and fine.●
33
+ Offenses Against the State: These include waging war against the Government of India, sedition, and offenses relating to the Army, Navy, and Air Force.●
34
+ Offenses Against Public Tranquility: This category encompasses unlawful assembly, rioting, and affray (fighting in a public place).●
35
+ 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.●
36
+ Offenses Relating to Documents and Property Marks: This part deals with forgery, using forged documents, counterfeiting, and offenses related to property marks.●
37
+ Criminal Breach of Contracts of Service: The code addresses breaches of contract related to attending to helpless individuals.●
38
+ 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.●
39
+ 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.●
40
+ 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.
41
+
42
 
43
  Task: {{query}}
44
 
 
46
  """,
47
  )
48
 
 
 
49
  agent = initialize_agent(
50
+ llm=llm,
 
51
  agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
52
  verbose=True,
53
  return_intermediate_steps=True,