-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
63 lines (45 loc) · 1.58 KB
/
app.py
1
2
3
4
5
6
7
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
51
52
53
54
55
56
57
58
59
60
61
62
63
import streamlit as st
from dotenv import load_dotenv
from PyPDF2 import PdfReader
from langchain.text_splitter import CharacterTextSplitter
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import FAISS
USE_OPENAI = True
def get_pdf_text(pdf_docs):
text = ""
for pdf in pdf_docs:
pdf_reader = PdfReader(pdf)
for page in pdf_reader.pages:
text += page.extract_text()
return text
def get_text_chunks(text):
text_splitter = CharacterTextSplitter(
separator="\n",
chunk_size= 1000,
chunk_overlap= 200,
length_function=len
)
chunks = text_splitter.split_text(text)
return chunks
def get_vectorstore(text_chunks):
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_texts(texts=text_chunks, embedding=embeddings)
return vectorstore
def main():
load_dotenv()
st.set_page_config(page_title="Chat with multiple PDFs", page_icon=":books:")
st.header("Chat with multiple PDFs :books:")
st.text_input("Ask a question about doc:")
with st.sidebar:
st.subheader("Your Documents")
pdf_docs = st.file_uploader("Upload your PDFs here", accept_multiple_files=True)
if st.button("Process"):
with st.spinner("Processing"):
#get pdf text
raw_text = get_pdf_text(pdf_docs)
#get the text chunks
text_chunks = get_text_chunks(raw_text)
#create vector store
vectorstore = get_vectorstore(text_chunks)
if __name__ == '__main__':
main()