RoAr777 commited on
Commit
dde0bfd
·
verified ·
1 Parent(s): 110ef21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -17
app.py CHANGED
@@ -11,7 +11,10 @@ import os
11
  import pytesseract
12
  from PIL import Image
13
  import pickle
14
- from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
 
 
 
15
  # Load the CSV data as a DataFrame
16
  df = pd.read_csv("hf://datasets/kshitij230/Indian-Law/Indian-Law.csv")
17
  model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
@@ -335,41 +338,51 @@ faq_tool=Tool(
335
  func=retrieve_faq,
336
  description="Provides Answers to commonly asked questions related to query keyword(s)"
337
  )
338
- llm = HuggingFaceEndpoint(
339
- model="meta-llama/Meta-Llama-3-8B",
340
- max_tokens=None,
341
- timeout=None,
342
- max_retries=2,
343
- prompt_template="""
344
  You are a highly specialized legal assistant with deep knowledge of the Indian Penal Code (IPC).
345
  Your primary task is to retrieve and summarize legal information accurately from the IPC.pdf document provided to you.
346
  Your responses should be highly specific, fact-based, and free from any speculation or hallucinations.
347
  Always cite the exact section from the IPC when providing an answer.
348
  If the information is not available in the document, clearly state that and do not make any assumptions.
349
- Respond to user queries and engage in conversation te resolve their query.
350
-
351
 
352
  Example task: "What is the punishment for theft according to the IPC?"
353
  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."
354
-
355
- History:{{history}}
356
 
357
- User: {{query}}
 
 
358
 
359
  Response:
360
- """
 
361
  )
362
 
363
- agent_tools = [ipc_tool,crpc_tool,doj_tool,faq_tool]
 
 
 
 
 
 
 
 
364
 
365
  agent = initialize_agent(
366
  tools=agent_tools,
367
- llm=llm,
368
  agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
369
  verbose=True,
370
  return_intermediate_steps=True,
371
- handle_parsing_errors=True,
372
  )
 
 
373
  def encode_image_to_base64(image_path):
374
  return pytesseract.image_to_string(Image.open(image_path))
375
  def chatbot_response(history,query):
@@ -393,7 +406,7 @@ def chatbot_response(history,query):
393
  message = HumanMessage(content=[{"type": "text", "text": query}])
394
 
395
  # Invoke the model with the multimodal message
396
- result = agent.invoke([history,message],handle_parsing_errors=True)
397
  response = result['output']
398
  intermediate_steps = result.get('intermediate_steps', [])
399
 
 
11
  import pytesseract
12
  from PIL import Image
13
  import pickle
14
+ from langchain.agents import AgentType, initialize_agent
15
+ from langchain.memory import ConversationBufferMemory
16
+ from langchain.llms import HuggingFaceHub
17
+ from langchain.tools import Tool
18
  # Load the CSV data as a DataFrame
19
  df = pd.read_csv("hf://datasets/kshitij230/Indian-Law/Indian-Law.csv")
20
  model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')
 
338
  func=retrieve_faq,
339
  description="Provides Answers to commonly asked questions related to query keyword(s)"
340
  )
341
+ from langchain.prompts import PromptTemplate
342
+ from langchain.chains import LLMChain
343
+ from langchain.memory import ConversationBufferMemory
344
+
345
+ prompt = PromptTemplate(
346
+ template="""
347
  You are a highly specialized legal assistant with deep knowledge of the Indian Penal Code (IPC).
348
  Your primary task is to retrieve and summarize legal information accurately from the IPC.pdf document provided to you.
349
  Your responses should be highly specific, fact-based, and free from any speculation or hallucinations.
350
  Always cite the exact section from the IPC when providing an answer.
351
  If the information is not available in the document, clearly state that and do not make any assumptions.
352
+ Respond to user queries and engage in conversation to resolve their query.
 
353
 
354
  Example task: "What is the punishment for theft according to the IPC?"
355
  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."
 
 
356
 
357
+ History: {history}
358
+
359
+ User: {query}
360
 
361
  Response:
362
+ """,
363
+ input_variables=["history", "query"]
364
  )
365
 
366
+ llm = HuggingFaceEndpoint(
367
+ repo_id="meta-llama/Meta-Llama-3-8B",
368
+ task="text-generation",
369
+ timeout=None
370
+ )
371
+
372
+ llm_chain = LLMChain(prompt=prompt, llm=llm, memory=ConversationBufferMemory())
373
+
374
+ agent_tools = [ipc_tool, crpc_tool, doj_tool, faq_tool]
375
 
376
  agent = initialize_agent(
377
  tools=agent_tools,
378
+ llm=llm_chain,
379
  agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
380
  verbose=True,
381
  return_intermediate_steps=True,
382
+ handle_parsing_errors=True
383
  )
384
+
385
+
386
  def encode_image_to_base64(image_path):
387
  return pytesseract.image_to_string(Image.open(image_path))
388
  def chatbot_response(history,query):
 
406
  message = HumanMessage(content=[{"type": "text", "text": query}])
407
 
408
  # Invoke the model with the multimodal message
409
+ result = agent.invoke({'query':message.content},handle_parsing_errors=True)
410
  response = result['output']
411
  intermediate_steps = result.get('intermediate_steps', [])
412