Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 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 |
-
|
| 46 |
-
|
| 47 |
-
with get_openai_callback() as cb:
|
| 48 |
-
response = chain.run(input_documents=docs, question=user_question)
|
| 49 |
-
print(cb)
|
| 50 |
|
| 51 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
if __name__ ==
|
| 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()
|