Benjy commited on
Commit
4a019f2
·
verified ·
1 Parent(s): 258b7b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -29
app.py CHANGED
@@ -15,40 +15,39 @@ load_dotenv()
15
  def main():
16
  st.set_page_config(page_title="PDF Chat")
17
  st.header("Chat with your PDFs 💬")
18
-
19
  # Upload PDF files
20
  pdf_files = st.file_uploader("Upload your PDF files", type="pdf", accept_multiple_files=True)
21
-
22
  if pdf_files:
23
  for idx, pdf_file in enumerate(pdf_files):
24
- pdf_reader = PdfReader(pdf_file)
25
-
26
- text = ""
27
- for page in pdf_reader.pages:
28
- text += page.extract_text()
29
-
30
- text_splitter = CharacterTextSplitter(
31
- separator="\n",
32
- chunk_size=1000,
33
- chunk_overlap=200,
34
- length_function=len
35
- )
36
- chunks = text_splitter.split_text(text)
37
-
38
- embeddings = OpenAIEmbeddings()
39
- knowledge_base = FAISS.from_texts(chunks, embeddings)
40
-
41
- user_question = st.text_input(f"Ask a question about '{pdf_file.name}':", key=f"question_{idx}")
42
- if user_question:
43
- docs = knowledge_base.similarity_search(user_question)
44
 
45
- llm = OpenAI()
46
- chain = load_qa_chain(llm, chain_type="stuff")
47
- with get_openai_callback() as cb:
48
- response = chain.run(input_documents=docs, question=user_question)
49
- print(cb)
50
 
51
- st.write(response)
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- if __name__ == '__main__':
54
  main()
 
15
  def main():
16
  st.set_page_config(page_title="PDF Chat")
17
  st.header("Chat with your PDFs 💬")
18
+
19
  # Upload PDF files
20
  pdf_files = st.file_uploader("Upload your PDF files", type="pdf", accept_multiple_files=True)
 
21
  if pdf_files:
22
  for idx, pdf_file in enumerate(pdf_files):
23
+ try:
24
+ pdf_reader = PdfReader(pdf_file)
25
+ text = ""
26
+ for page in pdf_reader.pages:
27
+ text += page.extract_text()
28
+
29
+ text_splitter = CharacterTextSplitter(
30
+ separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len
31
+ )
32
+ chunks = text_splitter.split_text(text)
 
 
 
 
 
 
 
 
 
 
33
 
34
+ embeddings = OpenAIEmbeddings()
35
+ knowledge_base = FAISS.from_texts(chunks, embeddings)
 
 
 
36
 
37
+ user_question = st.text_input(f"Ask a question about '{pdf_file.name}':", key=f"question_{idx}")
38
+ if user_question:
39
+ docs = knowledge_base.similarity_search(user_question)
40
+
41
+ llm = OpenAI()
42
+ chain = load_qa_chain(llm, chain_type="stuff")
43
+
44
+ with get_openai_callback() as cb:
45
+ response = chain.run(input_documents=docs, question=user_question)
46
+ print(cb)
47
+
48
+ st.write(response)
49
+ except Exception as e:
50
+ st.error(f"An error occurred while processing '{pdf_file.name}': {str(e)}. This file may be protected by the author, or contain scanned text which this basic demo is not set up to process.")
51
 
52
+ if __name__ == "__main__":
53
  main()