drewThomasson commited on
Commit
fda9a03
·
verified ·
1 Parent(s): 71f9e38

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pytesseract
3
+ from pdf2image import convert_from_path
4
+ import tempfile
5
+ import os
6
+
7
+ def ocr_pdf(file_path):
8
+ with tempfile.TemporaryDirectory() as temp_dir:
9
+ # Convert PDF to images
10
+ images = convert_from_path(file_path, output_folder=temp_dir)
11
+
12
+ # Extract text from each page image
13
+ extracted_text = ""
14
+ for i, image in enumerate(images):
15
+ text = pytesseract.image_to_string(image)
16
+ extracted_text += f"--- Page {i+1} ---\n{text}\n\n"
17
+
18
+ # Save the extracted text to a .txt file
19
+ output_txt_path = os.path.join(temp_dir, "extracted_text.txt")
20
+ with open(output_txt_path, "w") as f:
21
+ f.write(extracted_text)
22
+
23
+ return output_txt_path
24
+
25
+ # Gradio Interface
26
+ iface = gr.Interface(
27
+ fn=lambda file: ocr_pdf(file.name), # Pass file path instead of file object
28
+ inputs=gr.File(label="Upload PDF File"),
29
+ outputs=gr.File(label="Download Extracted Text (.txt)"), # Outputs a downloadable .txt file
30
+ title="PDF to Text OCR"
31
+ )
32
+
33
+ if __name__ == "__main__":
34
+ iface.launch()