Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,72 @@
|
|
| 1 |
-
from cgitb import text
|
| 2 |
-
import streamlit as st
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
from PyPDF2 import PdfReader
|
| 5 |
-
from langchain_text_splitters import CharacterTextSplitter
|
| 6 |
-
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
| 7 |
-
from langchain.vectorstores import FAISS
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
main()
|
|
|
|
| 1 |
+
from cgitb import text
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from dotenv import load_dotenv
|
| 4 |
+
from PyPDF2 import PdfReader
|
| 5 |
+
from langchain_text_splitters import CharacterTextSplitter
|
| 6 |
+
from langchain.embeddings import HuggingFaceInstructEmbeddings
|
| 7 |
+
from langchain.vectorstores import FAISS
|
| 8 |
+
from langchain.memory import ConversationalBufferMemory
|
| 9 |
+
from langchain.chains import ConversationalRetrievalChain
|
| 10 |
+
from langchain.llms import HuggingFaceHub
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def get_text_chunks(raw_text):
|
| 14 |
+
text_splitter = CharacterTextSplitter(
|
| 15 |
+
separator="\n",
|
| 16 |
+
chunk_size=1000, chunk_overlap=200,
|
| 17 |
+
length_function= len
|
| 18 |
+
)
|
| 19 |
+
chunks = text_splitter.split(text)
|
| 20 |
+
return chunks
|
| 21 |
+
|
| 22 |
+
def get_pdf_text(pdf_docs):
|
| 23 |
+
text =""
|
| 24 |
+
for pdf in pdf_docs:
|
| 25 |
+
pdf_reader = PdfReader(pdf)
|
| 26 |
+
for page in pdf_reader.pages:
|
| 27 |
+
text += page.extract_text()
|
| 28 |
+
return text
|
| 29 |
+
|
| 30 |
+
def get_vectorestore(text_chunks):
|
| 31 |
+
embeddings = HuggingFaceInstructEmbeddings(model_name= "hkunlp/instructor-xl")
|
| 32 |
+
vectorestore = FAISS.from_texts(texts=text_chunks, embedding= embeddings)
|
| 33 |
+
return vectorestore
|
| 34 |
+
|
| 35 |
+
def get_conversation_chain(vectorstore):
|
| 36 |
+
llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", model_kwargs={"temperature":0.5, "max_length":512})
|
| 37 |
+
memory = ConversationalBufferMemory(memory_key="chat_history", return_message= True)
|
| 38 |
+
conversation_chain = ConversationalRetrievalChain.from_llm(
|
| 39 |
+
llm=llm,
|
| 40 |
+
retriever=vectorstore.as_retriever(),
|
| 41 |
+
memory= memory
|
| 42 |
+
)
|
| 43 |
+
return get_conversation_chain
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
return
|
| 47 |
+
def main():
|
| 48 |
+
load_dotenv()
|
| 49 |
+
st.set_page_config(page_title="Chat with your PDFs", page_icon= ":books:")
|
| 50 |
+
|
| 51 |
+
if "conversation" not in st.session_state:
|
| 52 |
+
st.session_state.conversation = None
|
| 53 |
+
|
| 54 |
+
st.header("Chat with your PDFs :books:")
|
| 55 |
+
st.text_input("Ask a question about your documents:")
|
| 56 |
+
with st.sidebar:
|
| 57 |
+
st.subheader("Your Documemts")
|
| 58 |
+
pdf_docs = st.file_uploader("Upload your PDFs here", accept_multiple_files= True)
|
| 59 |
+
if st.button("Enter"):
|
| 60 |
+
with st.spinner("Processing"):
|
| 61 |
+
# get pdf text
|
| 62 |
+
raw_text = get_pdf_text(pdf_docs)
|
| 63 |
+
# get text chunks
|
| 64 |
+
text_chunks = get_text_chunks(raw_text)
|
| 65 |
+
# create vector store
|
| 66 |
+
vectorstore = get_vectorstore(text_chunks)
|
| 67 |
+
|
| 68 |
+
# conversation_chain
|
| 69 |
+
st.session_state.conversation = get_conversation_chain(vectorstore)
|
| 70 |
+
st.session_state.conversation
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
main()
|