Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,18 +14,19 @@ def main():
|
|
| 14 |
st.set_page_config(page_title="Chat PDF")
|
| 15 |
st.header("Chat PDF 💬")
|
| 16 |
|
| 17 |
-
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
|
| 27 |
if not text:
|
| 28 |
-
st.write("No text could be extracted from the
|
| 29 |
return
|
| 30 |
|
| 31 |
char_text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000,
|
|
@@ -33,47 +34,20 @@ def main():
|
|
| 33 |
text_chunks = char_text_splitter.split_text(text)
|
| 34 |
|
| 35 |
embeddings = OpenAIEmbeddings()
|
| 36 |
-
docsearch = FAISS.from_texts(text_chunks, embeddings)
|
| 37 |
-
llm = OpenAI()
|
| 38 |
|
| 39 |
-
|
| 40 |
-
chain = load_qa_chain(llm, chain_type="stuff")
|
| 41 |
|
| 42 |
-
|
| 43 |
-
# Ask a question
|
| 44 |
-
query = st.text_input("Type your question:")
|
| 45 |
-
|
| 46 |
-
#except Exception as e:
|
| 47 |
-
#st.error(f"An error occurred: {e}")
|
| 48 |
|
| 49 |
-
# Check if the user wants to exit
|
| 50 |
-
if query.lower() == 'exit':
|
| 51 |
-
break
|
| 52 |
-
|
| 53 |
-
# Search documents for the query
|
| 54 |
-
#docs = docsearch.similarity_search(query)
|
| 55 |
if query:
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
# Get the response
|
| 61 |
-
response = chain.run(input_documents=docs, question=query)
|
| 62 |
-
|
| 63 |
-
# Print the question and response
|
| 64 |
-
print(" ")
|
| 65 |
-
print("Question:", query)
|
| 66 |
-
print("Answer:", response)
|
| 67 |
-
print("-" * 50)
|
| 68 |
|
| 69 |
-
|
| 70 |
-
#query = st.text_input("Type your question:")
|
| 71 |
-
#if query:
|
| 72 |
-
# docs = docsearch.similarity_search(query)
|
| 73 |
-
# response = chain.run(input_documents=docs, question=query)
|
| 74 |
-
# st.write(response)
|
| 75 |
except Exception as e:
|
| 76 |
st.error(f"An error occurred: {e}")
|
| 77 |
|
| 78 |
-
if __name__ ==
|
| 79 |
main()
|
|
|
|
| 14 |
st.set_page_config(page_title="Chat PDF")
|
| 15 |
st.header("Chat PDF 💬")
|
| 16 |
|
| 17 |
+
pdfs = st.file_uploader("Upload your PDF files", type="pdf", accept_multiple_files=True)
|
| 18 |
|
| 19 |
+
text = ""
|
| 20 |
+
if pdfs:
|
| 21 |
try:
|
| 22 |
+
for pdf in pdfs:
|
| 23 |
+
pdf_reader = PdfReader(pdf)
|
| 24 |
+
for page in pdf_reader.pages:
|
| 25 |
+
page_text = page.extract_text() or ""
|
| 26 |
+
text += page_text
|
| 27 |
|
| 28 |
if not text:
|
| 29 |
+
st.write("No text could be extracted from the PDFs.")
|
| 30 |
return
|
| 31 |
|
| 32 |
char_text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000,
|
|
|
|
| 34 |
text_chunks = char_text_splitter.split_text(text)
|
| 35 |
|
| 36 |
embeddings = OpenAIEmbeddings()
|
| 37 |
+
docsearch = FAISS.from_texts(text_chunks, embeddings)
|
| 38 |
+
llm = OpenAI()
|
| 39 |
|
| 40 |
+
chain = load_qa_chain(llm, chain_type="your_chain_type_here") # Replace "your_chain_type_here" with the actual chain type
|
|
|
|
| 41 |
|
| 42 |
+
query = st.text_input("Type your question:")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
if query:
|
| 45 |
+
docs = docsearch.similarity_search(query)
|
| 46 |
+
response = chain.run(input_documents=docs, question=query)
|
| 47 |
+
st.write(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
except Exception as e:
|
| 50 |
st.error(f"An error occurred: {e}")
|
| 51 |
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
main()
|