Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import streamlit as st
|
2 |
from langchain_community.document_loaders.pdf import PyPDFDirectoryLoader
|
3 |
from langchain.text_splitter import CharacterTextSplitter
|
@@ -10,7 +11,7 @@ from langchain_community.llms import HuggingFaceHub
|
|
10 |
def make_vectorstore(embeddings):
|
11 |
loader = PyPDFDirectoryLoader("data")
|
12 |
documents = loader.load()
|
13 |
-
text_splitter = CharacterTextSplitter(chunk_size=
|
14 |
texts = text_splitter.split_documents(documents)
|
15 |
docsearch = FAISS.from_documents(texts, embeddings)
|
16 |
|
@@ -36,6 +37,9 @@ def get_response(conversation_chain, query):
|
|
36 |
|
37 |
def main():
|
38 |
st.title("Chat LLM")
|
|
|
|
|
|
|
39 |
|
40 |
print("Downloading Embeddings Model")
|
41 |
with st.spinner('Downloading Embeddings Model...'):
|
@@ -43,7 +47,7 @@ def main():
|
|
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")
|
@@ -52,10 +56,16 @@ def main():
|
|
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
|
|
|
|
|
59 |
|
60 |
if "messages" not in st.session_state:
|
61 |
st.session_state.messages = []
|
@@ -66,11 +76,6 @@ def main():
|
|
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)
|
|
|
1 |
+
import os
|
2 |
import streamlit as st
|
3 |
from langchain_community.document_loaders.pdf import PyPDFDirectoryLoader
|
4 |
from langchain.text_splitter import CharacterTextSplitter
|
|
|
11 |
def make_vectorstore(embeddings):
|
12 |
loader = PyPDFDirectoryLoader("data")
|
13 |
documents = loader.load()
|
14 |
+
text_splitter = CharacterTextSplitter(chunk_size=400, chunk_overlap=0)
|
15 |
texts = text_splitter.split_documents(documents)
|
16 |
docsearch = FAISS.from_documents(texts, embeddings)
|
17 |
|
|
|
37 |
|
38 |
def main():
|
39 |
st.title("Chat LLM")
|
40 |
+
# create a folder named data
|
41 |
+
if not os.path.exists("data"):
|
42 |
+
os.makedirs("data")
|
43 |
|
44 |
print("Downloading Embeddings Model")
|
45 |
with st.spinner('Downloading Embeddings Model...'):
|
|
|
47 |
|
48 |
print("Loading LLM from HuggingFace")
|
49 |
with st.spinner('Loading LLM from HuggingFace...'):
|
50 |
+
llm = HuggingFaceHub(repo_id="HuggingFaceH4/zephyr-7b-beta", model_kwargs={"temperature":0.7, "max_new_tokens":512, "top_p":0.95, "top_k":50})
|
51 |
|
52 |
# multiple pdfs uploader in the side bar
|
53 |
st.sidebar.title("Upload PDFs")
|
|
|
56 |
for file in uploaded_files:
|
57 |
with open(f"data/{file.name}", "wb") as f:
|
58 |
f.write(file.getbuffer())
|
59 |
+
with st.spinner('making a vectorstore database...'):
|
60 |
+
vectorstore = make_vectorstore(embeddings)
|
61 |
+
with st.spinner('making a conversation chain...'):
|
62 |
+
conversation_chain = get_conversation(vectorstore, llm)
|
63 |
st.sidebar.success("PDFs uploaded successfully")
|
64 |
else:
|
65 |
st.sidebar.warning("Please upload PDFs")
|
66 |
+
# add a clear chat button which will clear the session state
|
67 |
+
if st.button("Clear Chat"):
|
68 |
+
st.session_state.messages = []
|
69 |
|
70 |
if "messages" not in st.session_state:
|
71 |
st.session_state.messages = []
|
|
|
76 |
else:
|
77 |
st.chat_message("bot").markdown(message["content"])
|
78 |
|
|
|
|
|
|
|
|
|
|
|
79 |
user_prompt = st.chat_input("ask a question", key="user")
|
80 |
if user_prompt:
|
81 |
st.chat_message("user").markdown(user_prompt)
|