Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,83 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from
|
| 3 |
-
from langchain.
|
| 4 |
-
from
|
| 5 |
-
from
|
| 6 |
-
from langchain.
|
| 7 |
-
from
|
| 8 |
-
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
add_generation_prompt=True,
|
| 17 |
-
return_tensors='pt'
|
| 18 |
-
)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
# "content": "You are a friendly chatbot who can code",
|
| 34 |
-
# },
|
| 35 |
-
# {"role": "user", "content": prompt},
|
| 36 |
-
# ]
|
| 37 |
-
# prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 38 |
-
# outputs = pipe(prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
|
| 39 |
-
# print(outputs[0]["generated_text"].split("<|assistant|>")[1])
|
| 40 |
-
# return outputs[0]["generated_text"].split("<|assistant|>")[1]
|
| 41 |
|
| 42 |
def main():
|
| 43 |
-
st.title("
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
st.session_state.messages = []
|
| 54 |
-
|
| 55 |
-
prompt = st.text_input("Enter your question here:")
|
| 56 |
-
|
| 57 |
for message in st.session_state.messages:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from langchain_community.document_loaders.pdf import PyPDFDirectoryLoader
|
| 3 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
+
from langchain_community.embeddings import HuggingFaceInstructEmbeddings
|
| 5 |
+
from langchain_community.vectorstores import FAISS
|
| 6 |
+
from langchain.chains import RetrievalQA
|
| 7 |
+
from langchain.memory import ConversationBufferMemory
|
| 8 |
+
from langchain_community.llms import HuggingFaceHub
|
| 9 |
|
| 10 |
+
def make_vectorstore(embeddings):
|
| 11 |
+
loader = PyPDFDirectoryLoader("data")
|
| 12 |
+
documents = loader.load()
|
| 13 |
+
text_splitter = CharacterTextSplitter(chunk_size=200, chunk_overlap=0)
|
| 14 |
+
texts = text_splitter.split_documents(documents)
|
| 15 |
+
docsearch = FAISS.from_documents(texts, embeddings)
|
| 16 |
|
| 17 |
+
return docsearch
|
| 18 |
|
| 19 |
+
def get_conversation(vectorstore, model):
|
| 20 |
+
|
| 21 |
+
memory = ConversationBufferMemory(memory_key="messages", return_messages=True)
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
conversation_chain = RetrievalQA.from_llm(
|
| 24 |
+
llm=model,
|
| 25 |
+
retriever=vectorstore.as_retriever(),
|
| 26 |
+
memory=memory)
|
| 27 |
+
|
| 28 |
+
return conversation_chain
|
| 29 |
|
| 30 |
+
def get_response(conversation_chain, query):
|
| 31 |
+
# get the response
|
| 32 |
+
response = conversation_chain.invoke(query)
|
| 33 |
+
response = response["result"]
|
| 34 |
+
answer = response.split('\nHelpful Answer: ')[1]
|
| 35 |
+
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
def main():
|
| 38 |
+
st.title("Chat LLM")
|
| 39 |
+
|
| 40 |
+
print("Downloading Embeddings Model")
|
| 41 |
+
with st.spinner('Downloading Embeddings Model...'):
|
| 42 |
+
embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-base", model_kwargs = {'device': 'cpu'})
|
| 43 |
+
|
| 44 |
+
print("Loading LLM from HuggingFace")
|
| 45 |
+
with st.spinner('Loading LLM from HuggingFace...'):
|
| 46 |
+
llm = HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta", model_kwargs={"temperature":0.7, "max_new_tokens":512, "top_p":0.95, "top_k":50},)
|
| 47 |
+
|
| 48 |
+
# multiple pdfs uploader in the side bar
|
| 49 |
+
st.sidebar.title("Upload PDFs")
|
| 50 |
+
uploaded_files = st.sidebar.file_uploader("Upload PDFs", accept_multiple_files=True)
|
| 51 |
+
if uploaded_files:
|
| 52 |
+
for file in uploaded_files:
|
| 53 |
+
with open(f"data/{file.name}", "wb") as f:
|
| 54 |
+
f.write(file.getbuffer())
|
| 55 |
+
st.sidebar.success("PDFs uploaded successfully")
|
| 56 |
+
else:
|
| 57 |
+
st.sidebar.warning("Please upload PDFs")
|
| 58 |
+
# add a clear chat button which will clear the session state and the conversation history
|
| 59 |
+
|
| 60 |
+
if "messages" not in st.session_state:
|
| 61 |
st.session_state.messages = []
|
| 62 |
+
|
|
|
|
|
|
|
| 63 |
for message in st.session_state.messages:
|
| 64 |
+
if message["role"] == "user":
|
| 65 |
+
st.chat_message("user").markdown(message["content"])
|
| 66 |
+
else:
|
| 67 |
+
st.chat_message("bot").markdown(message["content"])
|
| 68 |
+
|
| 69 |
+
with st.spinner('making a vectorstore database...'):
|
| 70 |
+
vectorstore = make_vectorstore(embeddings)
|
| 71 |
+
with st.spinner('making a conversation chain...'):
|
| 72 |
+
conversation_chain = get_conversation(vectorstore, llm)
|
| 73 |
+
|
| 74 |
+
user_prompt = st.chat_input("ask a question", key="user")
|
| 75 |
+
if user_prompt:
|
| 76 |
+
st.chat_message("user").markdown(user_prompt)
|
| 77 |
+
st.session_state.messages.append({"role": "user", "content": user_prompt})
|
| 78 |
+
response = get_response(conversation_chain, user_prompt)
|
| 79 |
+
st.chat_message("bot").markdown(response)
|
| 80 |
+
st.session_state.messages.append({"role": "bot", "content": response})
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
main()
|