pracheeeeez commited on
Commit
a0aee40
·
verified ·
1 Parent(s): c403a0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -49
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
- def get_text_chunks(raw_text):
11
- text_splitter = CharacterTextSplitter(
12
- separator="\n",
13
- chunk_size=1000, chunk_overlap=200,
14
- length_function= len
15
- )
16
- chunks = text_splitter.split(text)
17
- return chunks
18
-
19
- def get_pdf_text(pdf_docs):
20
- text =""
21
- for pdf in pdf_docs:
22
- pdf_reader = PdfReader(pdf)
23
- for page in pdf_reader.pages:
24
- text += page.extract_text()
25
- return text
26
-
27
- def get_vectorestore(text_chunks):
28
- embeddings =
29
- return
30
-
31
-
32
- def main():
33
- load_dotenv()
34
- st.set_page_config(page_title="Chat with your PDFs", page_icon= ":books:")
35
- st.header("Chat with your PDFs :books:")
36
- st.text_input("Ask a question about your documents:")
37
- with st.sidebar:
38
- st.subheader("Your Documemts")
39
- pdf_docs = st.file_uploader("Upload your PDFs here", accept_multiple_files= True)
40
- if st.button("Enter"):
41
- with st.spinner("Processing"):
42
- # get pdf text
43
- raw_text = get_pdf_text(pdf_docs)
44
- # get text chunks
45
- text_chunks = get_text_chunks(raw_text)
46
- # create vector store
47
- vectorstore = get_vectorstore(text_chunks)
48
-
49
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()