hardik90 commited on
Commit
064713e
·
verified ·
1 Parent(s): 3328efb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -24
app.py CHANGED
@@ -6,14 +6,18 @@ import fitz # PyMuPDF
6
  qa_model = pipeline("question-answering", "timpal0l/mdeberta-v3-base-squad2")
7
 
8
  # Function to extract text from a PDF file
9
- def extract_text_from_pdf(pdf_file):
10
- doc = fitz.open(pdf_file)
11
- text = ""
12
- for page_num in range(doc.page_count):
13
- page = doc[page_num]
14
- text += page.get_text()
15
- doc.close()
16
- return text
 
 
 
 
17
 
18
  # Streamlit app
19
  def main():
@@ -26,22 +30,23 @@ def main():
26
  # Read the PDF file and extract text
27
  pdf_text = extract_text_from_pdf(uploaded_file)
28
 
29
- # Display the extracted text
30
- st.subheader("Extracted Text from PDF")
31
- st.text(pdf_text)
32
-
33
- # Input for user question
34
- question = st.text_input("Ask a question about the PDF:")
35
-
36
- # Button to trigger question answering
37
- if st.button("Get Answer"):
38
- if question:
39
- # Use the QA model to get the answer
40
- answer = qa_model(question=question, context=pdf_text)
41
- st.subheader("Answer:")
42
- st.write(answer["answer"])
43
- else:
44
- st.warning("Please enter a question.")
 
45
 
46
  if __name__ == "__main__":
47
  main()
 
6
  qa_model = pipeline("question-answering", "timpal0l/mdeberta-v3-base-squad2")
7
 
8
  # Function to extract text from a PDF file
9
+ def extract_text_from_pdf(uploaded_file):
10
+ try:
11
+ doc = fitz.open(stream=uploaded_file, filetype="pdf")
12
+ text = ""
13
+ for page_num in range(doc.page_count):
14
+ page = doc[page_num]
15
+ text += page.get_text()
16
+ doc.close()
17
+ return text
18
+ except Exception as e:
19
+ st.error(f"Error extracting text from PDF: {str(e)}")
20
+ return None
21
 
22
  # Streamlit app
23
  def main():
 
30
  # Read the PDF file and extract text
31
  pdf_text = extract_text_from_pdf(uploaded_file)
32
 
33
+ if pdf_text is not None:
34
+ # Display the extracted text
35
+ st.subheader("Extracted Text from PDF")
36
+ st.text(pdf_text)
37
+
38
+ # Input for user question
39
+ question = st.text_input("Ask a question about the PDF:")
40
+
41
+ # Button to trigger question answering
42
+ if st.button("Get Answer"):
43
+ if question:
44
+ # Use the QA model to get the answer
45
+ answer = qa_model(question=question, context=pdf_text)
46
+ st.subheader("Answer:")
47
+ st.write(answer["answer"])
48
+ else:
49
+ st.warning("Please enter a question.")
50
 
51
  if __name__ == "__main__":
52
  main()