QuickLearnerAI commited on
Commit
552e756
·
verified ·
1 Parent(s): 04204cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -10
app.py CHANGED
@@ -13,22 +13,18 @@ def extract_text_from_pdf(pdf_file):
13
  pdf_file.seek(0)
14
  else:
15
  pdf_content = pdf_file
16
-
17
  pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_content))
18
  for page_num in range(len(pdf_reader.pages)):
19
  page_text = pdf_reader.pages[page_num].extract_text()
20
  if page_text:
21
  text += page_text + "\n\n"
22
-
 
 
23
  if not text.strip():
24
- # Fall back to OCR
25
- text = extract_with_ocr(pdf_content)
26
- if not text.strip():
27
- return "No text could be extracted, even with OCR."
28
- return f"[Extracted with OCR]\n\n{text}"
29
-
30
  return text
31
-
32
  except Exception as e:
33
  return f"Error extracting text from PDF: {str(e)}"
34
 
@@ -189,4 +185,4 @@ with gr.Blocks(title="ChatPDF with Together AI", theme=gr.themes.Ocean()) as app
189
  )
190
 
191
  if __name__ == "__main__":
192
- app.launch(share=True)
 
13
  pdf_file.seek(0)
14
  else:
15
  pdf_content = pdf_file
16
+
17
  pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_content))
18
  for page_num in range(len(pdf_reader.pages)):
19
  page_text = pdf_reader.pages[page_num].extract_text()
20
  if page_text:
21
  text += page_text + "\n\n"
22
+ else:
23
+ text += f"[Page {page_num+1} - No extractable text found]\n\n"
24
+
25
  if not text.strip():
26
+ return "No text could be extracted from the PDF. The document may be scanned or image-based."
 
 
 
 
 
27
  return text
 
28
  except Exception as e:
29
  return f"Error extracting text from PDF: {str(e)}"
30
 
 
185
  )
186
 
187
  if __name__ == "__main__":
188
+ app.launch(share=True)