Update app.py
Browse files
app.py
CHANGED
|
@@ -101,10 +101,8 @@ def doj_info(q):
|
|
| 101 |
MINISTRY OF LAW AND JUSTICE
|
| 102 |
DEPARTMENT OF JUSTICE
|
| 103 |
----
|
| 104 |
-
Ministry of Law and Justice is the oldest limb of the Government of
|
| 105 |
-
|
| 106 |
-
Department of Legal Affairs, Legislative Department and the
|
| 107 |
-
Department of Justice.
|
| 108 |
VISION OF THE DEPARTMENT OF JUSTICE
|
| 109 |
Facilitating administration of Justice that ensures easy access and
|
| 110 |
timely delivery of Justice to all
|
|
@@ -317,7 +315,7 @@ Peoples’ Prosecution Service of the People’s Republic of China
|
|
| 317 |
relating to promotion of cooperation in Legal/Judicial matters was
|
| 318 |
signed on 23rd June, 2003.
|
| 319 |
--------
|
| 320 |
-
""".replace('/n','')
|
| 321 |
|
| 322 |
ipc_tool = Tool(
|
| 323 |
name="IPC Information Retrieval",
|
|
@@ -351,18 +349,18 @@ llm = ChatOpenAI(
|
|
| 351 |
max_retries=5
|
| 352 |
)
|
| 353 |
template="""
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
|
| 367 |
agent_tools = [ipc_tool, crpc_tool, doj_tool, faq_tool]
|
| 368 |
|
|
@@ -378,8 +376,16 @@ agent = initialize_agent(
|
|
| 378 |
|
| 379 |
def encode_image_to_base64(image_path):
|
| 380 |
return pytesseract.image_to_string(Image.open(image_path))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 381 |
def chatbot_response(history,query):
|
| 382 |
-
|
|
|
|
| 383 |
if query.get('files'):
|
| 384 |
# Encode image to base64
|
| 385 |
image_data=""
|
|
@@ -389,15 +395,14 @@ def chatbot_response(history,query):
|
|
| 389 |
# Create a multimodal message with both text and image data
|
| 390 |
message = HumanMessage(
|
| 391 |
content=[
|
| 392 |
-
{"type": "text", "text": template.format(query['text'] +" 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)}, # Add text input
|
| 393 |
|
| 394 |
]
|
| 395 |
)
|
| 396 |
#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
|
| 397 |
else:
|
| 398 |
# If no image, only pass the text
|
| 399 |
-
message = HumanMessage(content=[{"type": "text", "text": template.format(query)}])
|
| 400 |
-
|
| 401 |
# Invoke the model with the multimodal message
|
| 402 |
result = agent.invoke([message],handle_parsing_errors=True)
|
| 403 |
response = result['output']
|
|
|
|
| 101 |
MINISTRY OF LAW AND JUSTICE
|
| 102 |
DEPARTMENT OF JUSTICE
|
| 103 |
----
|
| 104 |
+
Ministry of Law and Justice is the oldest limb of the Government of India. The Ministry functions through three integral departments -
|
| 105 |
+
Department of Legal Affairs, Legislative Department and the Department of Justice.
|
|
|
|
|
|
|
| 106 |
VISION OF THE DEPARTMENT OF JUSTICE
|
| 107 |
Facilitating administration of Justice that ensures easy access and
|
| 108 |
timely delivery of Justice to all
|
|
|
|
| 315 |
relating to promotion of cooperation in Legal/Judicial matters was
|
| 316 |
signed on 23rd June, 2003.
|
| 317 |
--------
|
| 318 |
+
""".replace('/n',' ')
|
| 319 |
|
| 320 |
ipc_tool = Tool(
|
| 321 |
name="IPC Information Retrieval",
|
|
|
|
| 349 |
max_retries=5
|
| 350 |
)
|
| 351 |
template="""
|
| 352 |
+
You are a highly specialized legal assistant with deep knowledge of the Indian Penal Code (IPC).
|
| 353 |
+
Your primary task is to retrieve and summarize legal information accurately from the IPC.pdf document provided to you.
|
| 354 |
+
Your responses should be highly specific, fact-based, and free from any speculation or hallucinations.
|
| 355 |
+
Always cite the exact section from the IPC when providing an answer.
|
| 356 |
+
If the information is not available in the document, clearly state that and do not make any assumptions.
|
| 357 |
+
|
| 358 |
+
History: {}
|
| 359 |
+
|
| 360 |
+
User: {}
|
| 361 |
+
|
| 362 |
+
Response:
|
| 363 |
+
"""
|
| 364 |
|
| 365 |
agent_tools = [ipc_tool, crpc_tool, doj_tool, faq_tool]
|
| 366 |
|
|
|
|
| 376 |
|
| 377 |
def encode_image_to_base64(image_path):
|
| 378 |
return pytesseract.image_to_string(Image.open(image_path))
|
| 379 |
+
def parse(history):
|
| 380 |
+
p='\n'
|
| 381 |
+
if history==[]:
|
| 382 |
+
return "No Chat till now"
|
| 383 |
+
for i in history:
|
| 384 |
+
p+=i['role']+': '+i['content']+'\n'
|
| 385 |
+
return p
|
| 386 |
def chatbot_response(history,query):
|
| 387 |
+
l=parse(history)
|
| 388 |
+
|
| 389 |
if query.get('files'):
|
| 390 |
# Encode image to base64
|
| 391 |
image_data=""
|
|
|
|
| 395 |
# Create a multimodal message with both text and image data
|
| 396 |
message = HumanMessage(
|
| 397 |
content=[
|
| 398 |
+
{"type": "text", "text": template.format(l,query['text'] +" 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)}, # Add text input
|
| 399 |
|
| 400 |
]
|
| 401 |
)
|
| 402 |
#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
|
| 403 |
else:
|
| 404 |
# If no image, only pass the text
|
| 405 |
+
message = HumanMessage(content=[{"type": "text", "text": template.format(l,query)}])
|
|
|
|
| 406 |
# Invoke the model with the multimodal message
|
| 407 |
result = agent.invoke([message],handle_parsing_errors=True)
|
| 408 |
response = result['output']
|