lokesh341 commited on
Commit
e949cd7
·
verified ·
1 Parent(s): 36dbd5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -49
app.py CHANGED
@@ -1,58 +1,62 @@
1
  import fitz # PyMuPDF
2
- from pytesseract import image_to_string
3
  from PIL import Image
 
4
  from fpdf import FPDF
5
  import gradio as gr
6
 
7
- # Function to process PDF
8
- def process_pdf(pdf_file):
9
- # Open the uploaded PDF
10
- pdf_document = fitz.open(stream=pdf_file.read(), filetype="pdf")
11
- processed_pages = []
12
-
13
- for page_number in range(len(pdf_document)):
14
- # Extract each page as an image
15
- page = pdf_document.load_page(page_number)
16
- pix = page.get_pixmap()
17
- img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
18
-
19
- # Perform OCR on the image
20
- text = image_to_string(img)
21
-
22
- # Store extracted text and original image for later processing
23
- processed_pages.append({"image": img, "text": text})
24
-
25
- # Create a new PDF with editable text overlaying preserved shapes
26
- pdf_output = FPDF()
27
- pdf_output.set_auto_page_break(auto=True, margin=15)
28
-
29
- for page_data in processed_pages:
30
- pdf_output.add_page()
31
- pdf_output.set_font("Arial", size=12)
32
-
33
- # Overlay extracted text as editable content
34
- pdf_output.multi_cell(0, 10, page_data["text"])
35
-
36
- # Save the output PDF
37
- output_file = "processed_output.pdf"
38
- pdf_output.output(output_file)
39
-
40
- return output_file
41
-
42
- # Gradio interface function
43
- def gradio_interface(pdf_file):
44
- output_pdf_path = process_pdf(pdf_file)
 
 
 
 
 
 
45
  return output_pdf_path
46
 
47
- # Create Gradio app
48
- interface = gr.Interface(
49
- fn=gradio_interface,
50
- inputs=gr.inputs.File(label="Upload a PDF"),
51
- outputs=gr.outputs.File(label="Download Processed PDF"),
52
- title="PDF Text Processing",
53
- description="Upload a PDF, extract text, and download a processed version with editable text and preserved diagrams."
54
  )
55
 
56
- # Launch the app
57
- if __name__ == "__main__":
58
- interface.launch()
 
1
  import fitz # PyMuPDF
 
2
  from PIL import Image
3
+ import pytesseract
4
  from fpdf import FPDF
5
  import gradio as gr
6
 
7
+ # Path to Tesseract executable
8
+ pytesseract.pytesseract.tesseract_cmd = "/usr/bin/tesseract"
9
+
10
+ # Step 1: Convert PDF Pages to Images
11
+ def pdf_to_images(pdf_path):
12
+ pdf_document = fitz.open(pdf_path)
13
+ images = []
14
+ for page_num in range(len(pdf_document)):
15
+ page = pdf_document[page_num]
16
+ pix = page.get_pixmap() # Render the page as an image
17
+ image_path = f"page_{page_num + 1}.png"
18
+ pix.save(image_path)
19
+ images.append(image_path)
20
+ return images
21
+
22
+ # Step 2: Extract Text Using Tesseract OCR
23
+ def extract_text(images):
24
+ text_pages = []
25
+ for image_path in images:
26
+ text = pytesseract.image_to_string(Image.open(image_path)) # Perform OCR
27
+ text_pages.append(text)
28
+ return text_pages
29
+
30
+ # Step 3: Replace Curved Text with Editable Text in a New PDF
31
+ def create_editable_pdf(images, text_pages, output_pdf_path):
32
+ pdf = FPDF()
33
+ pdf.set_auto_page_break(auto=True, margin=15)
34
+ for text in text_pages:
35
+ pdf.add_page()
36
+ pdf.set_font("Arial", size=12)
37
+ pdf.multi_cell(0, 10, text)
38
+ pdf.output(output_pdf_path)
39
+
40
+ # Main Function
41
+ def process_pdf(file):
42
+ input_pdf_path = file.name
43
+ output_pdf_path = "Editable_Output.pdf"
44
+
45
+ # Convert PDF to images and perform OCR
46
+ images = pdf_to_images(input_pdf_path)
47
+ text_pages = extract_text(images)
48
+
49
+ # Create a new editable PDF
50
+ create_editable_pdf(images, text_pages, output_pdf_path)
51
  return output_pdf_path
52
 
53
+ # Gradio Interface
54
+ iface = gr.Interface(
55
+ fn=process_pdf,
56
+ inputs=gr.File(label="Upload PDF"),
57
+ outputs=gr.File(label="Download Editable PDF"),
58
+ title="OCR PDF to Editable Text",
59
+ description="Upload a PDF to extract and replace text while preserving shapes and layout.",
60
  )
61
 
62
+ iface.launch(share=True)