Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,20 @@
|
|
| 1 |
-
from langchain_community.document_loaders import PyPDFLoader,DirectoryLoader
|
| 2 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
| 3 |
-
from langchain.embeddings.openai import OpenAIEmbeddings
|
| 4 |
-
|
| 5 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
-
from langchain_community.vectorstores import FAISS
|
| 7 |
import os
|
|
|
|
|
|
|
|
|
|
| 8 |
from langchain.prompts import PromptTemplate
|
| 9 |
-
from langchain_together import Together
|
| 10 |
from langchain.chat_models import ChatOpenAI
|
| 11 |
-
from langchain.embeddings import openai
|
| 12 |
from langchain.memory import ConversationBufferWindowMemory
|
| 13 |
from langchain.chains import ConversationalRetrievalChain
|
| 14 |
-
|
| 15 |
-
import
|
| 16 |
|
| 17 |
-
|
| 18 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 19 |
client = OpenAI(api_key=api_key)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
custom_template ="""<s>[INST]You will start the conversation by greeting the user and introducing yourself as qanoon-bot,
|
| 24 |
stating your availability for legal assistance. Your next step will depend on the user's response.
|
| 25 |
If the user expresses a need for legal assistance in Pakistan, you will ask them to describe their case or problem.
|
| 26 |
After receiving the case or problem details from the user, you will provide the solutions and procedures according to the knowledge base and also give related penal codes and procedures.
|
|
@@ -35,17 +29,15 @@ ANSWER:
|
|
| 35 |
</s>[INST]
|
| 36 |
"""
|
| 37 |
|
| 38 |
-
embeddings=OpenAIEmbeddings()
|
| 39 |
-
#embeddings = HuggingFaceEmbeddings(model_name="nomic-ai/nomic-embed-text-v1",model_kwargs={"trust_remote_code":True,"revision":"289f532e14dbbbd5a04753fa58739e9ba766f3c7"})
|
| 40 |
-
#vectordb = Chroma.from_documents(texts, embedding=embeddings, persist_directory="./data")
|
| 41 |
-
#db_retriever =vectordb.as_retriever(search_type="similarity",search_kwargs={'k':4})
|
| 42 |
|
|
|
|
| 43 |
db = FAISS.load_local("vectordb", embeddings, allow_dangerous_deserialization=True)
|
| 44 |
-
db_retriever = db.as_retriever(search_type="similarity",search_kwargs={"k": 4})
|
| 45 |
-
|
| 46 |
|
|
|
|
| 47 |
st.set_page_config(page_title="Qanoon-Bot")
|
| 48 |
-
col1, col2, col3 = st.columns([1,4,1])
|
| 49 |
with col2:
|
| 50 |
st.image("https://s3.ap-south-1.amazonaws.com/makerobosfastcdn/cms-assets/Legal_AI_Chatbot.png")
|
| 51 |
|
|
@@ -58,44 +50,43 @@ div.stButton > button:first-child {
|
|
| 58 |
div.stButton > button:active {
|
| 59 |
background-color: #ff6262;
|
| 60 |
}
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
""",
|
| 76 |
unsafe_allow_html=True,
|
| 77 |
)
|
| 78 |
|
|
|
|
| 79 |
def reset_conversation():
|
| 80 |
-
|
| 81 |
-
|
| 82 |
|
|
|
|
| 83 |
if "messages" not in st.session_state:
|
| 84 |
st.session_state.messages = []
|
| 85 |
|
| 86 |
if "memory" not in st.session_state:
|
| 87 |
-
st.session_state.memory = ConversationBufferWindowMemory(k=5, memory_key="chat_history",return_messages=True,output_key='answer')
|
| 88 |
-
|
| 89 |
-
#embeddings = HuggingFaceEmbeddings(model_name="nomic-ai/nomic-embed-text-v1",model_kwargs={"trust_remote_code":True,"revision":"289f532e14dbbbd5a04753fa58739e9ba766f3c7"})
|
| 90 |
-
#db=FAISS.load_local("/content/ipc_vector_db", embeddings, allow_dangerous_deserialization=True)
|
| 91 |
|
|
|
|
|
|
|
| 92 |
|
| 93 |
-
|
|
|
|
| 94 |
|
| 95 |
-
#
|
| 96 |
-
|
| 97 |
-
from config import together_api
|
| 98 |
-
llm=ChatOpenAI(temperature=0.2,model_name='gpt-3.5-turbo-0125')
|
| 99 |
qa = ConversationalRetrievalChain.from_llm(
|
| 100 |
llm=llm,
|
| 101 |
memory=st.session_state.memory,
|
|
@@ -103,30 +94,31 @@ qa = ConversationalRetrievalChain.from_llm(
|
|
| 103 |
combine_docs_chain_kwargs={'prompt': prompt}
|
| 104 |
)
|
| 105 |
|
|
|
|
| 106 |
for message in st.session_state.messages:
|
| 107 |
with st.chat_message(message.get("role")):
|
| 108 |
st.write(message.get("content"))
|
| 109 |
|
|
|
|
| 110 |
input_prompt = st.chat_input("Say something")
|
| 111 |
|
| 112 |
if input_prompt:
|
| 113 |
with st.chat_message("user"):
|
| 114 |
st.write(input_prompt)
|
| 115 |
|
| 116 |
-
st.session_state.messages.append({"role":"user","content":input_prompt})
|
| 117 |
|
| 118 |
with st.chat_message("assistant"):
|
| 119 |
-
with st.status("Thinking π‘...",expanded=True):
|
| 120 |
result = qa.invoke(input=input_prompt)
|
| 121 |
|
| 122 |
message_placeholder = st.empty()
|
| 123 |
|
| 124 |
-
full_response = "**_Note: Information provided by Qanoon-Bot may be inaccurate.
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
st.session_state.messages.append({"role":"assistant","content":result["answer"]})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import time
|
| 4 |
+
from openai import OpenAI
|
| 5 |
from langchain.prompts import PromptTemplate
|
|
|
|
| 6 |
from langchain.chat_models import ChatOpenAI
|
|
|
|
| 7 |
from langchain.memory import ConversationBufferWindowMemory
|
| 8 |
from langchain.chains import ConversationalRetrievalChain
|
| 9 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 10 |
+
from langchain_community.vectorstores import FAISS
|
| 11 |
|
| 12 |
+
# Set up OpenAI API key
|
| 13 |
api_key = os.getenv("OPENAI_API_KEY")
|
| 14 |
client = OpenAI(api_key=api_key)
|
| 15 |
|
| 16 |
+
# Custom template to guide the LLM model
|
| 17 |
+
custom_template = """<s>[INST]You will start the conversation by greeting the user and introducing yourself as qanoon-bot,
|
|
|
|
| 18 |
stating your availability for legal assistance. Your next step will depend on the user's response.
|
| 19 |
If the user expresses a need for legal assistance in Pakistan, you will ask them to describe their case or problem.
|
| 20 |
After receiving the case or problem details from the user, you will provide the solutions and procedures according to the knowledge base and also give related penal codes and procedures.
|
|
|
|
| 29 |
</s>[INST]
|
| 30 |
"""
|
| 31 |
|
| 32 |
+
embeddings = OpenAIEmbeddings()
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# Load vector database
|
| 35 |
db = FAISS.load_local("vectordb", embeddings, allow_dangerous_deserialization=True)
|
| 36 |
+
db_retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 4})
|
|
|
|
| 37 |
|
| 38 |
+
# Streamlit page configuration
|
| 39 |
st.set_page_config(page_title="Qanoon-Bot")
|
| 40 |
+
col1, col2, col3 = st.columns([1, 4, 1])
|
| 41 |
with col2:
|
| 42 |
st.image("https://s3.ap-south-1.amazonaws.com/makerobosfastcdn/cms-assets/Legal_AI_Chatbot.png")
|
| 43 |
|
|
|
|
| 50 |
div.stButton > button:active {
|
| 51 |
background-color: #ff6262;
|
| 52 |
}
|
| 53 |
+
div[data-testid="stStatusWidget"] div button {
|
| 54 |
+
display: none;
|
| 55 |
+
}
|
| 56 |
+
.reportview-container {
|
| 57 |
+
margin-top: -2em;
|
| 58 |
+
}
|
| 59 |
+
#MainMenu {visibility: hidden;}
|
| 60 |
+
.stDeployButton {display:none;}
|
| 61 |
+
footer {visibility: hidden;}
|
| 62 |
+
#stDecoration {display:none;}
|
| 63 |
+
button[title="View fullscreen"] {
|
| 64 |
+
visibility: hidden;
|
| 65 |
+
}
|
| 66 |
+
</style>
|
| 67 |
""",
|
| 68 |
unsafe_allow_html=True,
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# Function to reset conversation
|
| 72 |
def reset_conversation():
|
| 73 |
+
st.session_state.messages = []
|
| 74 |
+
st.session_state.memory.clear()
|
| 75 |
|
| 76 |
+
# Initialize session state
|
| 77 |
if "messages" not in st.session_state:
|
| 78 |
st.session_state.messages = []
|
| 79 |
|
| 80 |
if "memory" not in st.session_state:
|
| 81 |
+
st.session_state.memory = ConversationBufferWindowMemory(k=5, memory_key="chat_history", return_messages=True, output_key='answer')
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
# Initialize the prompt
|
| 84 |
+
prompt = PromptTemplate(template=custom_template, input_variables=['context', 'chat_history', 'question'])
|
| 85 |
|
| 86 |
+
# Initialize the LLM
|
| 87 |
+
llm = ChatOpenAI(temperature=0.2, model_name='gpt-3.5-turbo-0125')
|
| 88 |
|
| 89 |
+
# Create the ConversationalRetrievalChain
|
|
|
|
|
|
|
|
|
|
| 90 |
qa = ConversationalRetrievalChain.from_llm(
|
| 91 |
llm=llm,
|
| 92 |
memory=st.session_state.memory,
|
|
|
|
| 94 |
combine_docs_chain_kwargs={'prompt': prompt}
|
| 95 |
)
|
| 96 |
|
| 97 |
+
# Display chat history
|
| 98 |
for message in st.session_state.messages:
|
| 99 |
with st.chat_message(message.get("role")):
|
| 100 |
st.write(message.get("content"))
|
| 101 |
|
| 102 |
+
# Handle user input
|
| 103 |
input_prompt = st.chat_input("Say something")
|
| 104 |
|
| 105 |
if input_prompt:
|
| 106 |
with st.chat_message("user"):
|
| 107 |
st.write(input_prompt)
|
| 108 |
|
| 109 |
+
st.session_state.messages.append({"role": "user", "content": input_prompt})
|
| 110 |
|
| 111 |
with st.chat_message("assistant"):
|
| 112 |
+
with st.status("Thinking π‘...", expanded=True):
|
| 113 |
result = qa.invoke(input=input_prompt)
|
| 114 |
|
| 115 |
message_placeholder = st.empty()
|
| 116 |
|
| 117 |
+
full_response = "**_Note: Information provided by Qanoon-Bot may be inaccurate._** \n\n\n"
|
| 118 |
+
for chunk in result["answer"]:
|
| 119 |
+
full_response += chunk
|
| 120 |
+
time.sleep(0.02)
|
| 121 |
+
message_placeholder.markdown(full_response + " β")
|
| 122 |
+
|
| 123 |
+
st.session_state.messages.append({"role": "assistant", "content": result["answer"]})
|
| 124 |
+
st.button('Reset All Chat ποΈ', on_click=reset_conversation)
|
|
|