Writo commited on
Commit
291c121
·
1 Parent(s): cea2ce6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -40
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
- if pdfs:
45
- try:
46
- start_time = time.time()
47
- for pdf in pdfs:
48
- pdf_reader = PdfReader(pdf)
49
- for page in pdf_reader.pages:
50
- page_text = page.extract_text() or ""
51
- text += page_text
52
-
53
- if not text:
54
- st.write("No text could be extracted from the PDFs.")
55
- return
56
-
57
- processing_time = time.time() - start_time
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
- text_chunks = char_text_splitter.split_text(text)
63
 
64
- embeddings = OpenAIEmbeddings()
65
- docsearch = FAISS.from_texts(text_chunks, embeddings)
66
- llm = OpenAI()
67
 
68
- chain = load_qa_chain(llm, chain_type="stuff")
69
 
70
- query = st.text_input("Type your question:")
71
 
72
- if query:
73
- docs = docsearch.similarity_search(query)
74
- response = chain.run(input_documents=docs, question=query)
75
 
76
- # Update chat history
77
- st.session_state.chat_history.append({"question": query, "answer": response})
78
 
79
- #clear the input
80
- st.session_state.query=""
81
 
82
- # Display chat history
83
- for chat in st.session_state.chat_history:
84
- st.text(f"Q: {chat['question']}\nA: {chat['answer']}")
85
- st.write("---")
86
-
87
- except Exception as e:
88
- logging.error(f"An error occurred: {e}")
89
- st.error(f"An error occurred: {e}")
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()