RoAr777 commited on
Commit
a9a4798
·
verified ·
1 Parent(s): 8b832ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +242 -12
app.py CHANGED
@@ -78,7 +78,229 @@ def retrieve_info2(query):
78
  results = retrieve_info_with_citation2(query)
79
  formatted_results = "\n\n".join([f"{i+1}. {match}\n{citation}" for i, (match, citation) in enumerate(results)])
80
  return formatted_results
81
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
  ipc_tool = Tool(
84
  name="IPC Information Retrieval",
@@ -91,32 +313,39 @@ crpc_tool=Tool(
91
  func=retrieve_info2,
92
  description="Retrieve information from the Code of Criminal Procedure(CrPC) Related to query keyword(s)."
93
  )
 
 
 
 
 
 
94
  llm = ChatGoogleGenerativeAI(
95
  model="gemini-1.5-pro",
96
  temperature=0.25,
97
  max_tokens=None,
98
  timeout=None,
99
  max_retries=2,
100
- )
101
- prompt="""
102
  You are a highly specialized legal assistant with deep knowledge of the Indian Penal Code (IPC).
103
  Your primary task is to retrieve and summarize legal information accurately from the IPC.pdf document provided to you.
104
  Your responses should be highly specific, fact-based, and free from any speculation or hallucinations.
105
  Always cite the exact section from the IPC when providing an answer.
106
  If the information is not available in the document, clearly state that and do not make any assumptions.
107
-
108
 
109
 
110
  Example task: "What is the punishment for theft according to the IPC?"
111
  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."
112
 
113
- History:{{}}
114
 
115
- Task: {{}}
116
 
117
  Response:
118
  """
119
- agent_tools = [ipc_tool,crpc_tool]
 
 
120
 
121
  agent = initialize_agent(
122
  tools=agent_tools,
@@ -129,7 +358,7 @@ agent = initialize_agent(
129
  def encode_image_to_base64(image_path):
130
  return pytesseract.image_to_string(Image.open(image_path))
131
  def chatbot_response(m,query):
132
- k=query['text']
133
  if query.get('files'):
134
  # Encode image to base64
135
  image_data=""
@@ -143,22 +372,23 @@ def chatbot_response(m,query):
143
 
144
  ]
145
  )
146
- k+=" System :Image(s) was added to this prompt by this user. Text Extracted from this image (Some words may be misspelled ,Use your understanding ):"+image_data
147
  else:
148
  # If no image, only pass the text
149
  message = HumanMessage(content=[{"type": "text", "text": query}])
150
 
151
  # Invoke the model with the multimodal message
152
- result = agent.invoke(prompt.format(m,k))
153
  response = result['output']
154
  intermediate_steps = result.get('intermediate_steps', [])
155
 
156
- thought_process = ""
 
157
  for action, observation in intermediate_steps:
158
  thought_process += f"**Thought:** {action.log}\n"
159
  thought_process += f"**Action:** {action.tool}\n"
160
  thought_process += f"**Observation:** {observation}\n\n"
161
-
162
  return response, thought_process.strip()
163
  # Step 5: Gradio Interface
164
  from gradio import ChatMessage
 
78
  results = retrieve_info_with_citation2(query)
79
  formatted_results = "\n\n".join([f"{i+1}. {match}\n{citation}" for i, (match, citation) in enumerate(results)])
80
  return formatted_results
81
+ def doj_info(q):
82
+ return """
83
+ MINISTRY OF LAW AND JUSTICE
84
+ DEPARTMENT OF JUSTICE
85
+ ----
86
+ Ministry of Law and Justice is the oldest limb of the Government of
87
+ India. The Ministry functions through three integral departments -
88
+ Department of Legal Affairs, Legislative Department and the
89
+ Department of Justice.
90
+ VISION OF THE DEPARTMENT OF JUSTICE
91
+ Facilitating administration of Justice that ensures easy access and
92
+ timely delivery of Justice to all
93
+ FUNCTIONS OF THE DEPARTMENT OF JUSTICE
94
+ Department of Justice performs the Administrative functions in
95
+ respect of setting up of higher courts, appointment of Judges in
96
+ higher Judiciary, maintenance and revision of the conditions and
97
+ rules of service of the Judges and issues relating to legal reforms.
98
+ The Department of Justice is also responsible jointly with the
99
+ judiciary for reduction of pendency of cases in courts. It provides
100
+ funding assistance to State Governments for modernization of
101
+ infrastructure and for projects such as computerization of
102
+ subordinate courts. Detailed functions of Department of Justice are
103
+ at Annexe.
104
+ SCHEMES UNDER THE DEPARTMENT OF JUSTICE:
105
+ 2
106
+ Apart from above functions, the Department of Justice administers
107
+ various schemes to improve justice delivery.
108
+ 1. Centrally sponsored scheme for development of
109
+ infrastructure for the judiciary
110
+ A Centrally Sponsored Scheme for the development of infrastructure
111
+ facilities for the judiciary is being implemented by the Department of
112
+ Justice. The scheme provides funding for construction of courtbuildings and residential accommodation for judicial officers/judges
113
+ covering both the High Courts and districts/subordinate Courts. One
114
+ of the main conditions of the scheme is that the State Government
115
+ must provide 25%of the amount against 75% released by the
116
+ Centre.
117
+ 2. Gram Nayayalayas (People’s Court)
118
+ The Gram Nyayalayas Act 2008 has been enacted to provide for the
119
+ establishment of Gram Nyayalayas at the grass-root level for the
120
+ purpose of providing access to justice to the citizens at their door
121
+ steps and to ensure that opportunities for securing justice are not
122
+ denied to any citizen by reason of social, economic or other
123
+ disabilities.
124
+ The Central Government has committed to fund the initial cost in
125
+ terms of the non- recurring expenses for setting up these courts.
126
+ 3. e-Courts
127
+ The Government is implementing an e-Courts Mission Mode Project
128
+ for computerization of District & Subordinate Courts in the country
129
+ and for up gradation of ICT infrastructure of the Supreme Court and
130
+ the High Courts. By virtue of this, case filing, allocation, registration,
131
+ case workflow, orders and judgements will all be ICT enabled in the
132
+ 3
133
+ long run. The project was built a national judicial data grid which
134
+ enables lawyers and litigants to access case information and the
135
+ judiciary to improve case and court management.
136
+ 4. National Mission for Justice Delivery and Legal Reform
137
+ The National Mission for Justice Delivery and Legal Reforms was set
138
+ up in August, 2011 to achieve the twin goals of (i) increasing access
139
+ by reducing delays and arrears; and (ii) enhancing accountability
140
+ through structural changes and by setting performance standards
141
+ and capacities.
142
+ 5. Legal Aid to Poor
143
+ Assistance is provided to poor people throughout the country for
144
+ enabling them to access free legal services. The activities and free
145
+ legal services are provided through National Legal Services
146
+ Authority (NALSA) established vide National Legal Services
147
+ Authority Act, 1987.
148
+ 6. Access to Justice for the marginalized
149
+ The Department of Justice is implementing two projects on ‘Access
150
+ to Justice for Marginalised People’ one of them with UNDP support.
151
+ The focus of the projects has been on empowering the poor and
152
+ marginalized, make them aware of their rights to demand legal
153
+ services, while at the same time supporting national and local justice
154
+ delivery institutions to bring justice to the poor.
155
+ 7. Fast Track Courts
156
+ The Eleventh Finance Commission had recommended a scheme for
157
+ creation of Fast Track Courts (FTCs) in the country for disposal of
158
+ long pending Sessions and other cases. Fast Track Courts are set
159
+ 4
160
+ up by the State Governments in consultation with the respective
161
+ High Court. Central Government provided financial assistance to
162
+ states for Fast Track Courts for eleven years from 2000-2001 to
163
+ 2010-2011. In its judgment in Brij Mohan Lal vs Union of India &
164
+ Others on 19.04.2012, the Supreme Court has directed the States
165
+ that they shall not take a decision to continue the Fast Track Courts
166
+ scheme on an adhoc and temporary basis. They (States) will need to
167
+ decide either to bring the Fast Track Courts scheme to an end or to
168
+ continue the same as a permanent feature in the State. A number of
169
+ States are now continuing Fast Track Courts from their own
170
+ resources.
171
+ In the Conference of Chief Ministers and Chief Justices held in New
172
+ Delhi on 7th April, 2013, it has been resolved that the State
173
+ Governments shall, in consultation with the Chief Justices of the
174
+ respective High Courts, take necessary steps to establish suitable
175
+ number of FTCs relating to offences against women, children,
176
+ differently abled persons, senior citizens and marginalized sections
177
+ of the society, and provide adequate funds for the purpose of
178
+ creating and continuing them. Government has requested the State
179
+ Governments and the Chief Justices of the High Courts to implement
180
+ this decision.
181
+ The 14th Finance Commission has endorsed the proposal to
182
+ strengthen the judicial system in States which includes, inter-alia,
183
+ establishing 1800 FTCs for a period of five years for cases of
184
+ heinous crimes; cases involving senior citizens, women, children,
185
+ disabled and litigants affected with HIV AIDS and other terminal
186
+ ailments; and civil disputes involving land acquisition and
187
+ 5
188
+ property/rent disputes pending for more than five years. The 14th
189
+ Finance Commission has urged State Governments to use
190
+ additional fiscal space provided by the Commission in the tax
191
+ devolution to meet such requirements.
192
+ JUDICIAL STRUCTURE
193
+ India has a three tier judicial structure. At the lowest level are the
194
+ District and Subordinate Courts, in over 600 administrative districts.
195
+ At the next level are the High Courts in the States. By and large,
196
+ each State has a High Court. But some states have a common High
197
+ Court. (There are a total of 24 High Courts in the country). At the
198
+ apex level is the Supreme Court of India situated at New Delhi.
199
+ 1. Supreme Court of India
200
+ The Supreme Court of India comprises the Chief Justice and 30
201
+ other Judges appointed by the President of India. The Judges of the
202
+ Supreme Court are appointed by the President under Article 124 (2)
203
+ of the Constitution while the Judges of the High Courts are
204
+ appointed under Article 217 (1) of the Constitution. The President is
205
+ required to hold consultation with such of the Judges of the Supreme
206
+ Court and of the High Courts in the State as he / she may deem
207
+ necessary for the purpose. However, consultation with the Chief
208
+ Justice of India is mandatory and constitutionally a must, for
209
+ appointment of Judges other than the Chief Justice in the Supreme
210
+ Court. Supreme Court Judges retire upon attaining the age of 65
211
+ years. In order to be appointed as a Judge of the Supreme Court, a
212
+ person must be a citizen of India and must have been, for at least
213
+ 6
214
+ five years, a Judge of a High Court or of two or more such Courts in
215
+ succession, or an Advocate of a High Court or of two or more such
216
+ Courts in succession for at least 10 years or he must be, in the
217
+ opinion of the President, a distinguished jurist. Provisions exist for
218
+ the appointment of a Judge of a High Court as an Ad-hoc Judge of
219
+ the Supreme Court and for retired Judges of the Supreme Court or
220
+ High Courts to sit and act as Judges of that Court.
221
+ 2. High Courts
222
+ The High Court is the apex court of the State’s judicial
223
+ administration. The Judges of the High Courts are appointed by the
224
+ President under Article 217 (1) of the Constitution. There are 24
225
+ High Courts in the country, three having jurisdiction over more than
226
+ one State. Among the Union Territories Delhi alone has a High Court
227
+ of its own. Other six Union Territories come under the jurisdiction of
228
+ different State High Courts. Each High Court comprises a Chief
229
+ Justice and such other Judges as the President may, from time to
230
+ time, appoint. The Chief Justice of a High Court is appointed by the
231
+ President in consultation with the Chief Justice of India and the
232
+ Governor of the State. The procedure for appointing puisne Judges
233
+ is the same except that the Chief Justice of the High Court
234
+ concerned is also consulted. They hold office until the age of 62
235
+ years and are removable in the same manner as a Judge of the
236
+ Supreme Court. To be eligible for appointment as a Judge one must
237
+ be a citizen of India and have held a judicial office in India for ten
238
+ years or must have practised as an Advocate of a High Court or two
239
+ or more such Courts in succession for a similar period.
240
+ The transfer of Judges from one High Court to another High Court is
241
+ made by the President after consultation with the Chief Justice of
242
+ India under Article 222 (1) of the Constitution.
243
+ 7
244
+ 3. Subordinate Courts
245
+ Different State laws provide for different kinds of jurisdiction of
246
+ courts. Each State is divided into judicial districts presided over by a
247
+ District and Sessions Judge, which is the principal civil court of
248
+ original jurisdiction and can try all offences including those
249
+ punishable with death. The Sessions Judge is the highest judicial
250
+ authority in a district. Below him, there are Courts of civil jurisdiction,
251
+ known in different States as Munsifs, Sub-Judges, Civil Judges and
252
+ the like. Similarly, the criminal judiciary comprises the Chief Judicial
253
+ Magistrates and Judicial Magistrates of First and Second Class.
254
+ In exercise of powers conferred under proviso to Article 309 read
255
+ with Articles 233 and 234 of the Constitution, the State Government
256
+ frames rules and regulations in consultation with the High Court for
257
+ appointments, posting and promotion of District Judges. As per
258
+ Article 235, the control over subordinate courts in a State vests in
259
+ the High Court. The members of the State Judicial Service are
260
+ governed by these rules and regulations. Therefore, the service
261
+ conditions, including appointment, promotion, and reservations etc.
262
+ of judicial officers of the District/Subordinate Courts are governed by
263
+ the respective State Governments.
264
+ NATIONAL JUDICIAL APPOINTMENTS COMMISSION
265
+ The Government of India has decided to set up a National judicial
266
+ Appointments Commission (NJAC) for appointment of Judges of
267
+ Supreme Court and High Courts. The NJAC would replace the
268
+ present Collegium system of the Supreme Court for recommending
269
+ appointment of Judges in higher judiciary.
270
+ 8
271
+ The Constitution Amendment Act, 2014 published on 31st December,
272
+ 2014 provides for the composition and the functions of the National
273
+ Judicial Appointments Commission (NJAC). The NJAC would be
274
+ chaired by the Chief Justice of India. Its membership would include
275
+ two senior most Judges of the Supreme Court, the Union Minister of
276
+ Law & Justice, two eminent persons to be nominated by a committee
277
+ of the Prime Minister of India, the Chief Justice of India, and the
278
+ Leader of the Opposition in the House of the People, or if there is no
279
+ Leader of the Opposition, then the Leader of the single largest
280
+ Opposition Party in the House of the People. Secretary (Justice) will
281
+ be the Convenor of the Commission.
282
+ GRIEVANCES AGAINST JUDICIARY
283
+ Department of Justice receives online/off line grievances from public
284
+ against judgments of the Courts, delay in their cases and against
285
+ Judges/Judicial Officers. These grievances are forwarded to the
286
+ Secretary General of Supreme Court/Registrar Generals of the
287
+ concerned High Courts for disposal at their end.
288
+ PREVIOUS VISIT OF CHINESE DELEGATION
289
+ A Chinese Delegation lead by Shri Zhang Sujun, Hon’ble Vice
290
+ Minister for Justice of People’s Republic of China, visited
291
+ Department of Justice on 26th Nov, 2012 and held discussions with
292
+ Secretary (Justice) and other senior officers of Department of
293
+ Justice. A copy of the Record of Discussion of this meeting is at
294
+ Annexe-I.
295
+ MOU BETWEEN INDIA & CHINA
296
+ A Memorandum of Understanding between the Ministry of law &
297
+ Justice of the Government of Republic of India and the Supreme
298
+ 9
299
+ Peoples’ Prosecution Service of the People’s Republic of China
300
+ relating to promotion of cooperation in Legal/Judicial matters was
301
+ signed on 23rd June, 2003.
302
+ --------
303
+ """
304
 
305
  ipc_tool = Tool(
306
  name="IPC Information Retrieval",
 
313
  func=retrieve_info2,
314
  description="Retrieve information from the Code of Criminal Procedure(CrPC) Related to query keyword(s)."
315
  )
316
+
317
+ doj_tool=Tool(
318
+ name="Department of Justice Info",
319
+ func=doj_info,
320
+ description="Provides Summarized Information about Department of Justice."
321
+ )
322
  llm = ChatGoogleGenerativeAI(
323
  model="gemini-1.5-pro",
324
  temperature=0.25,
325
  max_tokens=None,
326
  timeout=None,
327
  max_retries=2,
328
+ prompt_template="""
 
329
  You are a highly specialized legal assistant with deep knowledge of the Indian Penal Code (IPC).
330
  Your primary task is to retrieve and summarize legal information accurately from the IPC.pdf document provided to you.
331
  Your responses should be highly specific, fact-based, and free from any speculation or hallucinations.
332
  Always cite the exact section from the IPC when providing an answer.
333
  If the information is not available in the document, clearly state that and do not make any assumptions.
334
+ Respond to user queries and engage in conversation te resolve their query.
335
 
336
 
337
  Example task: "What is the punishment for theft according to the IPC?"
338
  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."
339
 
340
+ History:{{history}}
341
 
342
+ User: {{query}}
343
 
344
  Response:
345
  """
346
+ )
347
+
348
+ agent_tools = [ipc_tool,crpc_tool,doj_tool]
349
 
350
  agent = initialize_agent(
351
  tools=agent_tools,
 
358
  def encode_image_to_base64(image_path):
359
  return pytesseract.image_to_string(Image.open(image_path))
360
  def chatbot_response(m,query):
361
+
362
  if query.get('files'):
363
  # Encode image to base64
364
  image_data=""
 
372
 
373
  ]
374
  )
375
+ #k+=" System :Image(s) was added to this prompt by this user. Text Extracted from this image (Some words may be misspelled ,Use your understanding ):"+image_data
376
  else:
377
  # If no image, only pass the text
378
  message = HumanMessage(content=[{"type": "text", "text": query}])
379
 
380
  # Invoke the model with the multimodal message
381
+ result = agent.invoke([history,message],handle_parsing_errors=True)
382
  response = result['output']
383
  intermediate_steps = result.get('intermediate_steps', [])
384
 
385
+ thought_process = """
386
+ <div style="font-size: 12px; font-weight: bold;">"""
387
  for action, observation in intermediate_steps:
388
  thought_process += f"**Thought:** {action.log}\n"
389
  thought_process += f"**Action:** {action.tool}\n"
390
  thought_process += f"**Observation:** {observation}\n\n"
391
+ thought_process+="</div>"
392
  return response, thought_process.strip()
393
  # Step 5: Gradio Interface
394
  from gradio import ChatMessage