Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,6 @@ from langchain.llms import OpenAI
|
|
| 10 |
import time
|
| 11 |
import logging
|
| 12 |
|
| 13 |
-
|
| 14 |
# Setup logging
|
| 15 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 16 |
|
|
@@ -37,56 +36,53 @@ def main():
|
|
| 37 |
|
| 38 |
pdfs = st.file_uploader("Upload your PDF files", type="pdf", accept_multiple_files=True)
|
| 39 |
|
| 40 |
-
|
| 41 |
if pdfs:
|
| 42 |
try:
|
|
|
|
| 43 |
text = ""
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
logging.info(f"Total PDF processing time: {processing_time} seconds")
|
| 59 |
-
|
| 60 |
-
char_text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000,
|
| 61 |
chunk_overlap=200, length_function=len)
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
main()
|
|
|
|
| 10 |
import time
|
| 11 |
import logging
|
| 12 |
|
|
|
|
| 13 |
# Setup logging
|
| 14 |
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 15 |
|
|
|
|
| 36 |
|
| 37 |
pdfs = st.file_uploader("Upload your PDF files", type="pdf", accept_multiple_files=True)
|
| 38 |
|
|
|
|
| 39 |
if pdfs:
|
| 40 |
try:
|
| 41 |
+
start_time = time.time()
|
| 42 |
text = ""
|
| 43 |
+
for pdf in pdfs:
|
| 44 |
+
pdf_reader = PdfReader(pdf)
|
| 45 |
+
for page in pdf_reader.pages:
|
| 46 |
+
page_text = page.extract_text() or ""
|
| 47 |
+
text += page_text
|
| 48 |
+
|
| 49 |
+
if not text:
|
| 50 |
+
st.write("No text could be extracted from the PDFs.")
|
| 51 |
+
return
|
| 52 |
+
|
| 53 |
+
processing_time = time.time() - start_time
|
| 54 |
+
logging.info(f"Total PDF processing time: {processing_time} seconds")
|
| 55 |
+
|
| 56 |
+
char_text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000,
|
|
|
|
|
|
|
|
|
|
| 57 |
chunk_overlap=200, length_function=len)
|
| 58 |
+
text_chunks = char_text_splitter.split_text(text)
|
| 59 |
|
| 60 |
+
embeddings = OpenAIEmbeddings()
|
| 61 |
+
docsearch = FAISS.from_texts(text_chunks, embeddings)
|
| 62 |
+
llm = OpenAI()
|
| 63 |
|
| 64 |
+
chain = load_qa_chain(llm, chain_type="stuff")
|
| 65 |
|
| 66 |
+
query = st.text_input("Type your question:")
|
| 67 |
|
| 68 |
+
if query:
|
| 69 |
+
docs = docsearch.similarity_search(query)
|
| 70 |
+
response = chain.run(input_documents=docs, question=query)
|
| 71 |
|
| 72 |
+
# Update chat history
|
| 73 |
+
st.session_state.chat_history.append({"question": query, "answer": response})
|
| 74 |
|
| 75 |
+
# Clear the input
|
| 76 |
+
st.session_state.query = ""
|
| 77 |
|
| 78 |
+
# Display chat history
|
| 79 |
+
for chat in st.session_state.chat_history:
|
| 80 |
+
st.text(f"Q: {chat['question']}\nA: {chat['answer']}")
|
| 81 |
+
st.write("---")
|
| 82 |
+
|
| 83 |
+
except Exception as e:
|
| 84 |
+
logging.error(f"An error occurred: {e}")
|
| 85 |
+
st.error(f"An error occurred: {e}")
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
main()
|