Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from pdf2image import convert_from_path
|
|
|
|
|
|
|
|
|
|
| 4 |
from fpdf import FPDF
|
| 5 |
-
import shutil
|
| 6 |
-
import subprocess
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
POPPLER_PATH = "/usr/bin/" # Adjust based on installation
|
| 11 |
-
|
| 12 |
-
def check_poppler_installed():
|
| 13 |
-
"""Check if Poppler utilities (pdfinfo) are installed."""
|
| 14 |
-
pdfinfo_path = shutil.which("pdfinfo")
|
| 15 |
-
if not pdfinfo_path:
|
| 16 |
-
raise EnvironmentError("Poppler-utils (pdfinfo) is not installed or not in PATH. "
|
| 17 |
-
"Install it using 'apt-get install -y poppler-utils'.")
|
| 18 |
|
| 19 |
def flatten_pdf_to_images(pdf_path, output_dir, dpi=300):
|
| 20 |
"""Convert PDF pages to high-resolution images."""
|
|
@@ -22,7 +15,7 @@ def flatten_pdf_to_images(pdf_path, output_dir, dpi=300):
|
|
| 22 |
images = convert_from_path(pdf_path, dpi=dpi, poppler_path=POPPLER_PATH)
|
| 23 |
except Exception as e:
|
| 24 |
raise RuntimeError(f"Error during PDF flattening: {e}")
|
| 25 |
-
|
| 26 |
image_paths = []
|
| 27 |
for i, image in enumerate(images):
|
| 28 |
output_path = os.path.join(output_dir, f"page_{i + 1}.png")
|
|
@@ -30,29 +23,8 @@ def flatten_pdf_to_images(pdf_path, output_dir, dpi=300):
|
|
| 30 |
image_paths.append(output_path)
|
| 31 |
return image_paths
|
| 32 |
|
| 33 |
-
def create_comparison_pdf(original_images, edited_images, output_pdf_path):
|
| 34 |
-
"""Generate a placeholder comparison PDF."""
|
| 35 |
-
pdf = FPDF()
|
| 36 |
-
pdf.set_auto_page_break(auto=True, margin=15)
|
| 37 |
-
|
| 38 |
-
for orig_img, edited_img in zip(original_images, edited_images):
|
| 39 |
-
pdf.add_page()
|
| 40 |
-
pdf.set_font("Arial", size=12)
|
| 41 |
-
|
| 42 |
-
# Add original image
|
| 43 |
-
pdf.cell(200, 10, txt="Original Image", ln=True)
|
| 44 |
-
pdf.image(orig_img, x=10, y=20, w=90)
|
| 45 |
-
|
| 46 |
-
# Add edited image
|
| 47 |
-
pdf.cell(200, 10, txt="Edited Image", ln=True)
|
| 48 |
-
pdf.image(edited_img, x=110, y=20, w=90)
|
| 49 |
-
|
| 50 |
-
pdf.output(output_pdf_path)
|
| 51 |
-
|
| 52 |
def process_pdfs(original_pdf, edited_pdf):
|
| 53 |
"""Main function to process PDFs and generate output."""
|
| 54 |
-
check_poppler_installed()
|
| 55 |
-
|
| 56 |
output_dir = "./output"
|
| 57 |
os.makedirs(output_dir, exist_ok=True)
|
| 58 |
|
|
@@ -60,20 +32,22 @@ def process_pdfs(original_pdf, edited_pdf):
|
|
| 60 |
original_images = flatten_pdf_to_images(original_pdf, output_dir)
|
| 61 |
edited_images = flatten_pdf_to_images(edited_pdf, output_dir)
|
| 62 |
|
| 63 |
-
#
|
| 64 |
comparison_pdf = os.path.join(output_dir, "comparison_result.pdf")
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
return comparison_pdf
|
| 67 |
|
| 68 |
def gradio_interface(original_pdf, edited_pdf):
|
| 69 |
"""Gradio interface function."""
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
return output_pdf
|
| 73 |
-
except Exception as e:
|
| 74 |
-
return str(e)
|
| 75 |
|
| 76 |
-
# Gradio Interface
|
| 77 |
interface = gr.Interface(
|
| 78 |
fn=gradio_interface,
|
| 79 |
inputs=[
|
|
@@ -86,13 +60,4 @@ interface = gr.Interface(
|
|
| 86 |
)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
-
# Ensure poppler-utils is available before launching the app
|
| 90 |
-
try:
|
| 91 |
-
check_poppler_installed()
|
| 92 |
-
except EnvironmentError as e:
|
| 93 |
-
print(str(e))
|
| 94 |
-
print("Ensure Poppler is installed in the environment for this app to function.")
|
| 95 |
-
exit(1)
|
| 96 |
-
|
| 97 |
interface.launch()
|
| 98 |
-
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from pdf2image import convert_from_path
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
from fpdf import FPDF
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Define path to Poppler
|
| 10 |
+
POPPLER_PATH = "/usr/bin/"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def flatten_pdf_to_images(pdf_path, output_dir, dpi=300):
|
| 13 |
"""Convert PDF pages to high-resolution images."""
|
|
|
|
| 15 |
images = convert_from_path(pdf_path, dpi=dpi, poppler_path=POPPLER_PATH)
|
| 16 |
except Exception as e:
|
| 17 |
raise RuntimeError(f"Error during PDF flattening: {e}")
|
| 18 |
+
|
| 19 |
image_paths = []
|
| 20 |
for i, image in enumerate(images):
|
| 21 |
output_path = os.path.join(output_dir, f"page_{i + 1}.png")
|
|
|
|
| 23 |
image_paths.append(output_path)
|
| 24 |
return image_paths
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
def process_pdfs(original_pdf, edited_pdf):
|
| 27 |
"""Main function to process PDFs and generate output."""
|
|
|
|
|
|
|
| 28 |
output_dir = "./output"
|
| 29 |
os.makedirs(output_dir, exist_ok=True)
|
| 30 |
|
|
|
|
| 32 |
original_images = flatten_pdf_to_images(original_pdf, output_dir)
|
| 33 |
edited_images = flatten_pdf_to_images(edited_pdf, output_dir)
|
| 34 |
|
| 35 |
+
# Create comparison PDF
|
| 36 |
comparison_pdf = os.path.join(output_dir, "comparison_result.pdf")
|
| 37 |
+
pdf = FPDF()
|
| 38 |
+
pdf.add_page()
|
| 39 |
+
pdf.set_font("Arial", size=12)
|
| 40 |
+
pdf.cell(200, 10, txt="Comparison PDF Placeholder", ln=True)
|
| 41 |
+
|
| 42 |
+
pdf.output(comparison_pdf)
|
| 43 |
return comparison_pdf
|
| 44 |
|
| 45 |
def gradio_interface(original_pdf, edited_pdf):
|
| 46 |
"""Gradio interface function."""
|
| 47 |
+
output_pdf = process_pdfs(original_pdf.name, edited_pdf.name)
|
| 48 |
+
return output_pdf
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# Updated Gradio Interface
|
| 51 |
interface = gr.Interface(
|
| 52 |
fn=gradio_interface,
|
| 53 |
inputs=[
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
interface.launch()
|
|
|