Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,56 @@
|
|
| 1 |
import fitz # PyMuPDF
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
-
import pytesseract
|
| 4 |
from fpdf import FPDF
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
def
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
pdf
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
fn=process_pdf,
|
| 53 |
-
inputs=gr.File(label="Upload PDF"),
|
| 54 |
-
outputs=gr.File(label="Download Editable PDF"),
|
| 55 |
-
title="OCR PDF to Editable Text",
|
| 56 |
-
description="Upload a PDF to extract and replace text while preserving shapes and layout.",
|
| 57 |
)
|
| 58 |
|
| 59 |
-
|
|
|
|
|
|
|
|
|
| 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 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 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 |
+
# Save the processed text back to a new PDF with shapes preserved
|
| 23 |
+
processed_pages.append({"image": img, "text": text})
|
| 24 |
+
|
| 25 |
+
# Create a new PDF with editable text
|
| 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 |
+
pdf_output.multi_cell(0, 10, page_data["text"])
|
| 33 |
+
|
| 34 |
+
# Save the output PDF
|
| 35 |
+
output_file = "processed_output.pdf"
|
| 36 |
+
pdf_output.output(output_file)
|
| 37 |
+
|
| 38 |
+
return output_file
|
| 39 |
+
|
| 40 |
+
# Gradio interface
|
| 41 |
+
def process_pdf_interface(pdf_file):
|
| 42 |
+
output_file = process_pdf(pdf_file)
|
| 43 |
+
return output_file
|
| 44 |
+
|
| 45 |
+
# Create Gradio App
|
| 46 |
+
interface = gr.Interface(
|
| 47 |
+
fn=process_pdf_interface,
|
| 48 |
+
inputs=gr.inputs.File(label="Upload your PDF"),
|
| 49 |
+
outputs=gr.outputs.File(label="Download Processed PDF"),
|
| 50 |
+
title="PDF Text Processing App",
|
| 51 |
+
description="Upload a PDF to process its text, replace curved text with editable text, and download the updated PDF."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
)
|
| 53 |
|
| 54 |
+
# Launch Gradio app
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
interface.launch()
|