Writo commited on
Commit
8be95ac
·
1 Parent(s): 072b777

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -18
app.py CHANGED
@@ -6,20 +6,16 @@ from langchain.embeddings.openai import OpenAIEmbeddings
6
  from langchain.vectorstores import FAISS
7
  from langchain.chains.question_answering import load_qa_chain
8
  from langchain.llms import OpenAI
9
-
10
  import time
11
  import logging
 
12
 
13
  # Setup logging
14
- #logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
15
-
16
- # Configure logging to write to a file
17
  logging.basicConfig(level=logging.INFO,
18
  format='%(asctime)s - %(levelname)s - %(message)s',
19
  filename='app_log.log', # Log file name
20
  filemode='a') # Append mode
21
 
22
-
23
  def process_pdf(pdf):
24
  start_time = time.time()
25
  pdf_reader = PdfReader(pdf)
@@ -31,13 +27,10 @@ def process_pdf(pdf):
31
  return text
32
 
33
  def main():
34
- # Replace with the appropriate path or setup for Hugging Face Spaces
35
  load_dotenv()
36
-
37
  st.set_page_config(page_title="Chat PDF")
38
  st.header("Chat PDF 💬")
39
 
40
- # Initialize session state for chat history
41
  if 'chat_history' not in st.session_state:
42
  st.session_state.chat_history = []
43
 
@@ -48,10 +41,7 @@ def main():
48
  start_time = time.time()
49
  text = ""
50
  for pdf in pdfs:
51
- pdf_reader = PdfReader(pdf)
52
- for page in pdf_reader.pages:
53
- page_text = page.extract_text() or ""
54
- text += page_text
55
 
56
  if not text:
57
  st.write("No text could be extracted from the PDFs.")
@@ -67,24 +57,26 @@ def main():
67
  embeddings = OpenAIEmbeddings()
68
  docsearch = FAISS.from_texts(text_chunks, embeddings)
69
  llm = OpenAI()
70
-
71
- chain = load_qa_chain(llm, chain_type="stuff")
72
 
73
  query = st.text_input("Type your question:")
74
 
75
  if query:
 
76
  docs = docsearch.similarity_search(query)
77
  response = chain.run(input_documents=docs, question=query)
 
78
 
79
  # Update chat history
80
- st.session_state.chat_history.append({"question": query, "answer": response})
81
-
 
82
  # Clear the input
83
  st.session_state.query = ""
84
-
85
  # Display chat history
86
  for chat in st.session_state.chat_history:
87
- st.text(f"Q: {chat['question']}\nA: {chat['answer']}")
88
  st.write("---")
89
 
90
  except Exception as e:
 
6
  from langchain.vectorstores import FAISS
7
  from langchain.chains.question_answering import load_qa_chain
8
  from langchain.llms import OpenAI
 
9
  import time
10
  import logging
11
+ import os
12
 
13
  # Setup logging
 
 
 
14
  logging.basicConfig(level=logging.INFO,
15
  format='%(asctime)s - %(levelname)s - %(message)s',
16
  filename='app_log.log', # Log file name
17
  filemode='a') # Append mode
18
 
 
19
  def process_pdf(pdf):
20
  start_time = time.time()
21
  pdf_reader = PdfReader(pdf)
 
27
  return text
28
 
29
  def main():
 
30
  load_dotenv()
 
31
  st.set_page_config(page_title="Chat PDF")
32
  st.header("Chat PDF 💬")
33
 
 
34
  if 'chat_history' not in st.session_state:
35
  st.session_state.chat_history = []
36
 
 
41
  start_time = time.time()
42
  text = ""
43
  for pdf in pdfs:
44
+ text += process_pdf(pdf)
 
 
 
45
 
46
  if not text:
47
  st.write("No text could be extracted from the PDFs.")
 
57
  embeddings = OpenAIEmbeddings()
58
  docsearch = FAISS.from_texts(text_chunks, embeddings)
59
  llm = OpenAI()
60
+ chain = load_qa_chain(llm, chain_type="stuff")
 
61
 
62
  query = st.text_input("Type your question:")
63
 
64
  if query:
65
+ qa_start_time = time.time()
66
  docs = docsearch.similarity_search(query)
67
  response = chain.run(input_documents=docs, question=query)
68
+ qa_end_time = time.time()
69
 
70
  # Update chat history
71
+ processing_info = f"Processing Time: {qa_end_time - qa_start_time:.2f} seconds"
72
+ st.session_state.chat_history.append({"question": query, "answer": response, "time": processing_info})
73
+
74
  # Clear the input
75
  st.session_state.query = ""
76
+
77
  # Display chat history
78
  for chat in st.session_state.chat_history:
79
+ st.text(f"Q: {chat['question']}\nA: {chat['answer']}\n{chat['time']}")
80
  st.write("---")
81
 
82
  except Exception as e: