Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,90 +1,90 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
from dotenv import load_dotenv
|
| 3 |
-
from PyPDF2 import PdfReader
|
| 4 |
-
from
|
| 5 |
-
from langchain_community.embeddings import HuggingFaceInstructEmbeddings
|
| 6 |
-
from langchain_community.vectorstores import FAISS
|
| 7 |
-
from langchain.memory import ConversationBufferMemory
|
| 8 |
-
from langchain.chains import ConversationalRetrievalChain
|
| 9 |
-
from langchain.llms import HuggingFaceHub
|
| 10 |
-
from htmltemplates import css, bot_template, user_template
|
| 11 |
-
|
| 12 |
-
def get_text_chunks(text):
|
| 13 |
-
text_splitter = CharacterTextSplitter(
|
| 14 |
-
separator="\n",
|
| 15 |
-
chunk_size=1000, chunk_overlap=200,
|
| 16 |
-
length_function=len
|
| 17 |
-
)
|
| 18 |
-
chunks = text_splitter.split_text(text)
|
| 19 |
-
return chunks
|
| 20 |
-
|
| 21 |
-
def get_pdf_text(pdf_docs):
|
| 22 |
-
text = ""
|
| 23 |
-
for pdf in pdf_docs:
|
| 24 |
-
pdf_reader = PdfReader(pdf)
|
| 25 |
-
for page in pdf_reader.pages:
|
| 26 |
-
text += page.extract_text()
|
| 27 |
-
return text
|
| 28 |
-
|
| 29 |
-
def get_vectorstore(text_chunks):
|
| 30 |
-
embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl")
|
| 31 |
-
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
| 32 |
-
return vectorstore
|
| 33 |
-
|
| 34 |
-
def get_conversation_chain(vectorstore):
|
| 35 |
-
llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature": 0.5, "max_length": 512})
|
| 36 |
-
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 37 |
-
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 38 |
-
llm=llm,
|
| 39 |
-
retriever=vectorstore.as_retriever(),
|
| 40 |
-
memory=memory
|
| 41 |
-
)
|
| 42 |
-
return conversation_chain
|
| 43 |
-
|
| 44 |
-
def handle_userinput(user_question):
|
| 45 |
-
response = st.session_state.conversation({"question": user_question})
|
| 46 |
-
st.session_state.chat_history = response["chat_history"]
|
| 47 |
-
|
| 48 |
-
for i, message in enumerate(st.session_state.chat_history):
|
| 49 |
-
if i % 2 == 0:
|
| 50 |
-
st.write(user_template.replace("{{MSG}}", message["content"]), unsafe_allow_html=True)
|
| 51 |
-
else:
|
| 52 |
-
st.write(bot_template.replace("{{MSG}}", message["content"]), unsafe_allow_html=True)
|
| 53 |
-
|
| 54 |
-
def main():
|
| 55 |
-
load_dotenv()
|
| 56 |
-
st.set_page_config(page_title="Chat with your PDFs", page_icon=":books:")
|
| 57 |
-
|
| 58 |
-
st.write(css, unsafe_allow_html=True)
|
| 59 |
-
|
| 60 |
-
if "conversation" not in st.session_state:
|
| 61 |
-
st.session_state.conversation = None
|
| 62 |
-
if "chat_history" not in st.session_state:
|
| 63 |
-
st.session_state.chat_history = []
|
| 64 |
-
|
| 65 |
-
st.header("Chat with your PDFs :books:")
|
| 66 |
-
user_question = st.text_input("Ask a question about your documents:")
|
| 67 |
-
if user_question:
|
| 68 |
-
handle_userinput(user_question)
|
| 69 |
-
|
| 70 |
-
st.write(user_template.replace("{{MSG}}", "Hello Robot"), unsafe_allow_html=True)
|
| 71 |
-
st.write(bot_template.replace("{{MSG}}", "Hello"), unsafe_allow_html=True)
|
| 72 |
-
|
| 73 |
-
with st.sidebar:
|
| 74 |
-
st.subheader("Your Documents")
|
| 75 |
-
pdf_docs = st.file_uploader("Upload your PDFs here", accept_multiple_files=True)
|
| 76 |
-
if st.button("Enter"):
|
| 77 |
-
with st.spinner("Processing"):
|
| 78 |
-
# Get PDF text
|
| 79 |
-
raw_text = get_pdf_text(pdf_docs)
|
| 80 |
-
# Get text chunks
|
| 81 |
-
text_chunks = get_text_chunks(raw_text)
|
| 82 |
-
# Create vector store
|
| 83 |
-
vectorstore = get_vectorstore(text_chunks)
|
| 84 |
-
|
| 85 |
-
# Initialize conversation chain
|
| 86 |
-
st.session_state.conversation = get_conversation_chain(vectorstore)
|
| 87 |
-
st.success("Conversation chain initialized successfully.")
|
| 88 |
-
|
| 89 |
-
if __name__ == "__main__":
|
| 90 |
-
main()
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from PyPDF2 import PdfReader
|
| 4 |
+
from langchain_text_splitter import CharacterTextSplitter
|
| 5 |
+
from langchain_community.embeddings import HuggingFaceInstructEmbeddings
|
| 6 |
+
from langchain_community.vectorstores import FAISS
|
| 7 |
+
from langchain.memory import ConversationBufferMemory
|
| 8 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 9 |
+
from langchain.llms import HuggingFaceHub
|
| 10 |
+
from htmltemplates import css, bot_template, user_template
|
| 11 |
+
|
| 12 |
+
def get_text_chunks(text):
|
| 13 |
+
text_splitter = CharacterTextSplitter(
|
| 14 |
+
separator="\n",
|
| 15 |
+
chunk_size=1000, chunk_overlap=200,
|
| 16 |
+
length_function=len
|
| 17 |
+
)
|
| 18 |
+
chunks = text_splitter.split_text(text)
|
| 19 |
+
return chunks
|
| 20 |
+
|
| 21 |
+
def get_pdf_text(pdf_docs):
|
| 22 |
+
text = ""
|
| 23 |
+
for pdf in pdf_docs:
|
| 24 |
+
pdf_reader = PdfReader(pdf)
|
| 25 |
+
for page in pdf_reader.pages:
|
| 26 |
+
text += page.extract_text()
|
| 27 |
+
return text
|
| 28 |
+
|
| 29 |
+
def get_vectorstore(text_chunks):
|
| 30 |
+
embeddings = HuggingFaceInstructEmbeddings(model_name="hkunlp/instructor-xl")
|
| 31 |
+
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
| 32 |
+
return vectorstore
|
| 33 |
+
|
| 34 |
+
def get_conversation_chain(vectorstore):
|
| 35 |
+
llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature": 0.5, "max_length": 512})
|
| 36 |
+
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
| 37 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 38 |
+
llm=llm,
|
| 39 |
+
retriever=vectorstore.as_retriever(),
|
| 40 |
+
memory=memory
|
| 41 |
+
)
|
| 42 |
+
return conversation_chain
|
| 43 |
+
|
| 44 |
+
def handle_userinput(user_question):
|
| 45 |
+
response = st.session_state.conversation({"question": user_question})
|
| 46 |
+
st.session_state.chat_history = response["chat_history"]
|
| 47 |
+
|
| 48 |
+
for i, message in enumerate(st.session_state.chat_history):
|
| 49 |
+
if i % 2 == 0:
|
| 50 |
+
st.write(user_template.replace("{{MSG}}", message["content"]), unsafe_allow_html=True)
|
| 51 |
+
else:
|
| 52 |
+
st.write(bot_template.replace("{{MSG}}", message["content"]), unsafe_allow_html=True)
|
| 53 |
+
|
| 54 |
+
def main():
|
| 55 |
+
load_dotenv()
|
| 56 |
+
st.set_page_config(page_title="Chat with your PDFs", page_icon=":books:")
|
| 57 |
+
|
| 58 |
+
st.write(css, unsafe_allow_html=True)
|
| 59 |
+
|
| 60 |
+
if "conversation" not in st.session_state:
|
| 61 |
+
st.session_state.conversation = None
|
| 62 |
+
if "chat_history" not in st.session_state:
|
| 63 |
+
st.session_state.chat_history = []
|
| 64 |
+
|
| 65 |
+
st.header("Chat with your PDFs :books:")
|
| 66 |
+
user_question = st.text_input("Ask a question about your documents:")
|
| 67 |
+
if user_question:
|
| 68 |
+
handle_userinput(user_question)
|
| 69 |
+
|
| 70 |
+
st.write(user_template.replace("{{MSG}}", "Hello Robot"), unsafe_allow_html=True)
|
| 71 |
+
st.write(bot_template.replace("{{MSG}}", "Hello"), unsafe_allow_html=True)
|
| 72 |
+
|
| 73 |
+
with st.sidebar:
|
| 74 |
+
st.subheader("Your Documents")
|
| 75 |
+
pdf_docs = st.file_uploader("Upload your PDFs here", accept_multiple_files=True)
|
| 76 |
+
if st.button("Enter"):
|
| 77 |
+
with st.spinner("Processing"):
|
| 78 |
+
# Get PDF text
|
| 79 |
+
raw_text = get_pdf_text(pdf_docs)
|
| 80 |
+
# Get text chunks
|
| 81 |
+
text_chunks = get_text_chunks(raw_text)
|
| 82 |
+
# Create vector store
|
| 83 |
+
vectorstore = get_vectorstore(text_chunks)
|
| 84 |
+
|
| 85 |
+
# Initialize conversation chain
|
| 86 |
+
st.session_state.conversation = get_conversation_chain(vectorstore)
|
| 87 |
+
st.success("Conversation chain initialized successfully.")
|
| 88 |
+
|
| 89 |
+
if __name__ == "__main__":
|
| 90 |
+
main()
|