Spaces:
Running
Running
harshitv804
commited on
Commit
•
f9219f9
1
Parent(s):
631beb9
Upload app1.py
Browse files
app1.py
ADDED
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain_community.vectorstores import FAISS
|
2 |
+
from langchain_community.embeddings import HuggingFaceEmbeddings
|
3 |
+
from langchain.prompts import PromptTemplate
|
4 |
+
from langchain_fireworks import Fireworks
|
5 |
+
import os
|
6 |
+
from langchain.memory import ConversationBufferWindowMemory
|
7 |
+
from langchain.chains import ConversationalRetrievalChain
|
8 |
+
import streamlit as st
|
9 |
+
import time
|
10 |
+
st.set_page_config(page_title="LawGPT")
|
11 |
+
col1, col2, col3 = st.columns([1,4,1])
|
12 |
+
with col2:
|
13 |
+
st.image("https://github.com/harshitv804/LawGPT/assets/100853494/ecff5d3c-f105-4ba2-a93a-500282f0bf00")
|
14 |
+
|
15 |
+
st.markdown(
|
16 |
+
"""
|
17 |
+
<style>
|
18 |
+
div.stButton > button:first-child {
|
19 |
+
background-color: #ffd0d0;
|
20 |
+
}
|
21 |
+
div.stButton > button:active {
|
22 |
+
background-color: #ff6262;
|
23 |
+
}
|
24 |
+
div[data-testid="stStatusWidget"] div button {
|
25 |
+
display: none;
|
26 |
+
}
|
27 |
+
|
28 |
+
.reportview-container {
|
29 |
+
margin-top: -2em;
|
30 |
+
}
|
31 |
+
#MainMenu {visibility: hidden;}
|
32 |
+
.stDeployButton {display:none;}
|
33 |
+
footer {visibility: hidden;}
|
34 |
+
#stDecoration {display:none;}
|
35 |
+
button[title="View fullscreen"]{
|
36 |
+
visibility: hidden;}
|
37 |
+
</style>
|
38 |
+
""",
|
39 |
+
unsafe_allow_html=True,
|
40 |
+
)
|
41 |
+
|
42 |
+
def reset_conversation():
|
43 |
+
st.session_state.messages = []
|
44 |
+
st.session_state.memory.clear()
|
45 |
+
|
46 |
+
if "messages" not in st.session_state:
|
47 |
+
st.session_state.messages = []
|
48 |
+
|
49 |
+
if "memory" not in st.session_state:
|
50 |
+
st.session_state.memory = ConversationBufferWindowMemory(k=2, memory_key="chat_history",return_messages=True)
|
51 |
+
|
52 |
+
embeddings = HuggingFaceEmbeddings(model_name="nomic-ai/nomic-embed-text-v1",model_kwargs={"trust_remote_code":True,"revision":"289f532e14dbbbd5a04753fa58739e9ba766f3c7"})
|
53 |
+
db = FAISS.load_local("ipc_vector_db", embeddings)
|
54 |
+
db_retriever = db.as_retriever(search_type="similarity",search_kwargs={"k": 4})
|
55 |
+
|
56 |
+
prompt_template = """Follow these instructions clearly. This is a chat template and you are a Indian lawyer chat bot who provides correct information on Indian Penal Code. Give only the required answer to the question do not give unwanted information. Do not repeat anything, Generate only the answer, don't ask any questions. You are given the following pieces of information to answer the user's question correctly. You will be given context, chat history and the question. Choose only the required context based on the user's question. If the question is not related to the chat history, then don't use the history. Use chat history when required for similar related questions. While searching for the relevant information always give priority to the context given. Always take the context related only to the question. Use your own knowledge base and answer the question when the context is not related to the user's question. Utilize the provided knowledge base and search for relevant information from the context. Follow the user's question and the format closely. The answer should be short and concise. If you don't know the answer, just say that you don't know, don't try to make up your own questions and answers. Add bullet points and bold text using markdown in the required area if needed, to make it more pleasing to eyes. Do not repeat anything from the prompt template.
|
57 |
+
CONTEXT: {context}
|
58 |
+
CHAT HISTORY: {chat_history}
|
59 |
+
QUESTION: {question}
|
60 |
+
ANSWER:
|
61 |
+
"""
|
62 |
+
|
63 |
+
prompt = PromptTemplate(template=prompt_template,
|
64 |
+
input_variables=['context', 'question', 'chat_history'])
|
65 |
+
|
66 |
+
TOGETHER_AI_API= os.environ['TOGETHER_AI']
|
67 |
+
llm = Together(
|
68 |
+
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
|
69 |
+
temperature=0.7,
|
70 |
+
max_tokens=1024,
|
71 |
+
together_api_key=f"{TOGETHER_AI_API}"
|
72 |
+
)
|
73 |
+
|
74 |
+
qa = ConversationalRetrievalChain.from_llm(
|
75 |
+
llm=llm,
|
76 |
+
memory=st.session_state.memory,
|
77 |
+
retriever=db_retriever,
|
78 |
+
combine_docs_chain_kwargs={'prompt': prompt}
|
79 |
+
)
|
80 |
+
|
81 |
+
for message in st.session_state.messages:
|
82 |
+
with st.chat_message(message.get("role")):
|
83 |
+
st.write(message.get("content"))
|
84 |
+
|
85 |
+
input_prompt = st.chat_input("Say something")
|
86 |
+
|
87 |
+
if input_prompt:
|
88 |
+
with st.chat_message("user"):
|
89 |
+
st.write(input_prompt)
|
90 |
+
|
91 |
+
st.session_state.messages.append({"role":"user","content":input_prompt})
|
92 |
+
|
93 |
+
with st.chat_message("assistant"):
|
94 |
+
with st.status("Thinking 💡...",expanded=True):
|
95 |
+
result = qa.invoke(input=input_prompt)
|
96 |
+
|
97 |
+
message_placeholder = st.empty()
|
98 |
+
|
99 |
+
full_response = "⚠️ **_Note: Information provided may be inaccurate._** \n\n\n"
|
100 |
+
for chunk in result["answer"]:
|
101 |
+
full_response+=chunk
|
102 |
+
time.sleep(0.02)
|
103 |
+
|
104 |
+
message_placeholder.markdown(full_response+" ▌")
|
105 |
+
st.button('Reset All Chat 🗑️', on_click=reset_conversation)
|
106 |
+
|
107 |
+
st.session_state.messages.append({"role":"assistant","content":result["answer"]})
|