annas4421 commited on
Commit
7ee10b4
·
verified ·
1 Parent(s): 2e82eb6

Upload 4 files

Browse files
Files changed (5) hide show
  1. .gitattributes +1 -0
  2. app.py +121 -0
  3. requirements.txt +12 -0
  4. vector-db/index.faiss +3 -0
  5. vector-db/index.pkl +3 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ vector-db/index.faiss filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_community.document_loaders import PyPDFLoader,DirectoryLoader
2
+ from langchain.embeddings import HuggingFaceEmbeddings
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ from langchain_community.vectorstores import FAISS
5
+ import os
6
+ from langchain.prompts import PromptTemplate
7
+ from langchain_together import Together
8
+ from langchain.chat_models import ChatOpenAI
9
+ from langchain.embeddings import openai
10
+ from langchain.memory import ConversationBufferWindowMemory
11
+ from langchain.chains import ConversationalRetrievalChain
12
+ import streamlit as st
13
+ import time
14
+
15
+ os.environ["OPENAI_API_KEY"] = "sk-proj-Ht5xjDYvIipDt2wohlMCT3BlbkFJUCTqmrg8TRufd642B0cc"
16
+ openai.api_key = os.environ["OPENAI_API_KEY"]
17
+ from langchain.embeddings.openai import OpenAIEmbeddings
18
+ embeddings=OpenAIEmbeddings()
19
+
20
+ #embeddings = HuggingFaceEmbeddings(model_name="nomic-ai/nomic-embed-text-v1",model_kwargs={"trust_remote_code":True,"revision":"289f532e14dbbbd5a04753fa58739e9ba766f3c7"})
21
+ #vectordb = Chroma.from_documents(texts, embedding=embeddings, persist_directory="./data")
22
+ #db_retriever =vectordb.as_retriever(search_type="similarity",search_kwargs={'k':4})
23
+
24
+ db = FAISS.load_local("vector-db", embeddings, allow_dangerous_deserialization=True)
25
+ db_retriever = db.as_retriever(search_type="similarity",search_kwargs={"k": 4})
26
+
27
+
28
+ st.set_page_config(page_title="Qanoon-Bot")
29
+ col1, col2, col3 = st.columns([1,4,1])
30
+ with col2:
31
+ st.image("https://s3.ap-south-1.amazonaws.com/makerobosfastcdn/cms-assets/Legal_AI_Chatbot.png")
32
+
33
+ st.markdown(
34
+ """
35
+ <style>
36
+ div.stButton > button:first-child {
37
+ background-color: #ffd0d0;
38
+ }
39
+ div.stButton > button:active {
40
+ background-color: #ff6262;
41
+ }
42
+ div[data-testid="stStatusWidget"] div button {
43
+ display: none;
44
+ }
45
+
46
+ .reportview-container {
47
+ margin-top: -2em;
48
+ }
49
+ #MainMenu {visibility: hidden;}
50
+ .stDeployButton {display:none;}
51
+ footer {visibility: hidden;}
52
+ #stDecoration {display:none;}
53
+ button[title="View fullscreen"]{
54
+ visibility: hidden;}
55
+ </style>
56
+ """,
57
+ unsafe_allow_html=True,
58
+ )
59
+
60
+ def reset_conversation():
61
+ st.session_state.messages = []
62
+ st.session_state.memory.clear()
63
+
64
+ if "messages" not in st.session_state:
65
+ st.session_state.messages = []
66
+
67
+ if "memory" not in st.session_state:
68
+ st.session_state.memory = ConversationBufferWindowMemory(k=2, memory_key="chat_history",return_messages=True)
69
+
70
+ ##embeddings = HuggingFaceEmbeddings(model_name="nomic-ai/nomic-embed-text-v1",model_kwargs={"trust_remote_code":True,"revision":"289f532e14dbbbd5a04753fa58739e9ba766f3c7"})
71
+ #db=FAISS.load_local("/content/ipc_vector_db", embeddings, allow_dangerous_deserialization=True)
72
+
73
+ prompt_template = """<s>[INST]This is a chat template and As a legal chat bot specializing in pakistan Property laws and rights queries and , your primary objective is to provide accurate and concise information based on the user's questions. offer relevant context from the knowledge base while avoiding unnecessary details. Your responses will be brief, to the point, and in compliance with the established format. If a question falls outside the given context, you will refrain from utilizing the chat history and instead rely on your own knowledge base to generate an appropriate response. You will prioritize the user's query and refrain from posing additional questions. The aim is to deliver professional, precise, and contextually relevant information pertaining to the Indian Penal Code.
74
+ CONTEXT: {context}
75
+ CHAT HISTORY: {chat_history}
76
+ QUESTION: {question}
77
+ ANSWER:
78
+ </s>[INST]
79
+ """
80
+
81
+ prompt = PromptTemplate(template=prompt_template,
82
+ input_variables=['context', 'question', 'chat_history'])
83
+
84
+ # You can also use other LLMs options from https://python.langchain.com/docs/integrations/llms. Here I have used TogetherAI API
85
+
86
+
87
+ llm=ChatOpenAI(temperature=0.2,model_name='gpt-4-turbo')
88
+ qa = ConversationalRetrievalChain.from_llm(
89
+ llm=llm,
90
+ memory=st.session_state.memory,
91
+ retriever=db_retriever,
92
+ combine_docs_chain_kwargs={'prompt': prompt}
93
+ )
94
+
95
+ for message in st.session_state.messages:
96
+ with st.chat_message(message.get("role")):
97
+ st.write(message.get("content"))
98
+
99
+ input_prompt = st.chat_input("Say something")
100
+
101
+ if input_prompt:
102
+ with st.chat_message("user"):
103
+ st.write(input_prompt)
104
+
105
+ st.session_state.messages.append({"role":"user","content":input_prompt})
106
+
107
+ with st.chat_message("assistant"):
108
+ with st.status("Thinking 💡...",expanded=True):
109
+ result = qa.invoke(input=input_prompt)
110
+
111
+ message_placeholder = st.empty()
112
+
113
+ full_response = "**_Note: Information provided by Qanoon-Bot may be inaccurate. ** \n\n\n"
114
+ for chunk in result["answer"]:
115
+ full_response+=chunk
116
+ time.sleep(0.02)
117
+
118
+ message_placeholder.markdown(full_response+" ▌")
119
+ st.button('Reset All Chat 🗑️', on_click=reset_conversation)
120
+
121
+ st.session_state.messages.append({"role":"assistant","content":result["answer"]})
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ langchain
2
+ pypdf
3
+ transformers
4
+ sentence-transformers
5
+ accelerate
6
+ faiss-cpu
7
+ streamlit
8
+ tiktoken
9
+ openai
10
+ langchain-fireworks
11
+ einops
12
+ langchain-together
vector-db/index.faiss ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0261ac5964a31be34506606a99a0c181920835c14a42592fe41ce9e78e12f31b
3
+ size 11870253
vector-db/index.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:314117d8fcda7744e1a7185ed972f047f85a4579a55d64a02ffe1e5f16b0db43
3
+ size 1876337