import streamlit as st from langchain_huggingface import HuggingFaceEmbeddings from langchain_community.vectorstores import Chroma from langchain_community.llms import HuggingFaceHub from langchain.prompts import PromptTemplate from langchain.chains import RetrievalQA import warnings, os from dotenv import load_dotenv warnings.filterwarnings("ignore") import sys warnings.filterwarnings("ignore") __import__('pysqlite3') import sys sys.modules['sqlite3'] = sys.modules.pop('pysqlite3') # Load environment variables from .env file load_dotenv() data_directory = os.path.join(os.path.dirname(__file__), "data") os.environ["HUGGINGFACEHUB_API_TOKEN"] = os.getenv("HUGGINGFACEHUB_API_TOKEN") # st.secrets["huggingface_api_token"] # Don't forget to add your hugging face token # Load the vector store from disk embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") vector_store = Chroma(embedding_function=embedding_model, persist_directory=data_directory) # Initialize the Hugging Face Hub LLM hf_hub_llm = HuggingFaceHub( repo_id="meta-llama/Meta-Llama-3-8B-Instruct", model_kwargs={"temperature": 1, "max_new_tokens":1024}, ) prompt_template = """ You are an AI chatbot specializing in the domain of law, focusing on the recent changes made by the Indian government on July 1 2024 from the old THE INDIAN PENAL CODE(IPC) law to the new Bharatiya Nyaya Sanhita(BNS) law, 2023. Your task is to provide information about this transition. Here are your specific instructions: 1. **Simple Definitions**: Provide a brief, easy-to-understand definition of the BNS law for the general public. 2. **Codes Comparison**: Share the sections and clauses for both the IPC and the BNS, highlighting the changes. 3. **Punishments and Revisions**: Detail the punishments, penalties, and any improvements or revisions made in the BNS law. 4. **Detailed Comparison**: Conduct a comprehensive comparison between the IPC and the BNS. 5. **Articles and Videos**: Include references to relevant articles and videos discussing the new BNS law from authoritative sources. Ensure the information is accurate, concise, and accessible to users with varying levels of legal knowledge. Now, when the user interacts with you by saying 'hi', 'hello', or 'how are you', respond in an interactive manner to engage them effectively in a single line strictly. Do not call yourself as chatbot, call yourself as Lexify. If user asks about the number of Laws in IPC and BNS answer the given text as "The BNS has 356 sections, whereas the IPC had 511; 175 sections were changed, 8 new ones added, and 22 repealed." User Query: {context} Question: {question} Answer: """ custom_prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"]) rag_chain = RetrievalQA.from_chain_type( llm=hf_hub_llm, chain_type="stuff", retriever=vector_store.as_retriever(top_k=3), # retriever is set to fetch top 3 results chain_type_kwargs={"prompt": custom_prompt}) def get_response(question): result = rag_chain({"query": question}) response_text = result["result"] answer_start = response_text.find("Answer:") + len("Answer:") answer = response_text[answer_start:].strip() return answer # Streamlit app # Remove whitespace from the top of the page and sidebar st.markdown( """ """.format( padding_top=1, padding_bottom=1 ), unsafe_allow_html=True, ) st.markdown("""