Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,9 @@ logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(
|
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
def stitch_images_vertically(images: List[Image.Image]) -> Image.Image:
|
|
|
|
|
|
|
|
|
|
| 16 |
if not images:
|
| 17 |
return None
|
| 18 |
|
|
@@ -28,7 +31,11 @@ def stitch_images_vertically(images: List[Image.Image]) -> Image.Image:
|
|
| 28 |
|
| 29 |
return stitched_image
|
| 30 |
|
| 31 |
-
def process_pdf(pdf_file, pdf_url, progress=gr.Progress()):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
pdf_input_source = None
|
| 33 |
is_bytes = False
|
| 34 |
source_name = "document"
|
|
@@ -54,11 +61,12 @@ def process_pdf(pdf_file, pdf_url, progress=gr.Progress()):
|
|
| 54 |
raise gr.Error("Please upload a PDF file or provide a valid URL.")
|
| 55 |
|
| 56 |
progress(0.3, desc="Converting PDF pages to images...")
|
|
|
|
| 57 |
try:
|
| 58 |
if is_bytes:
|
| 59 |
-
images = convert_from_bytes(pdf_input_source, dpi=
|
| 60 |
else:
|
| 61 |
-
images = convert_from_path(pdf_input_source, dpi=
|
| 62 |
except (PDFInfoNotInstalledError, FileNotFoundError):
|
| 63 |
raise gr.Error("Server configuration error: Poppler dependency is missing.")
|
| 64 |
except (PDFPageCountError, Exception) as e:
|
|
@@ -107,6 +115,15 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 107 |
label="PDF URL",
|
| 108 |
placeholder="e.g., https://arxiv.org/pdf/1706.03762.pdf"
|
| 109 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
submit_btn = gr.Button("Stitch PDF Pages", variant="primary")
|
| 112 |
|
|
@@ -125,7 +142,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 125 |
|
| 126 |
submit_btn.click(
|
| 127 |
fn=process_pdf,
|
| 128 |
-
inputs=[pdf_file_input, pdf_url_input],
|
| 129 |
outputs=[output_image_preview, output_image_download]
|
| 130 |
)
|
| 131 |
|
|
|
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
def stitch_images_vertically(images: List[Image.Image]) -> Image.Image:
|
| 16 |
+
"""
|
| 17 |
+
Stitches a list of images together vertically.
|
| 18 |
+
"""
|
| 19 |
if not images:
|
| 20 |
return None
|
| 21 |
|
|
|
|
| 31 |
|
| 32 |
return stitched_image
|
| 33 |
|
| 34 |
+
def process_pdf(pdf_file, pdf_url, dpi, progress=gr.Progress()):
|
| 35 |
+
"""
|
| 36 |
+
Processes a PDF from a file upload or a URL, converts it to images with a specified DPI,
|
| 37 |
+
and stitches them together vertically.
|
| 38 |
+
"""
|
| 39 |
pdf_input_source = None
|
| 40 |
is_bytes = False
|
| 41 |
source_name = "document"
|
|
|
|
| 61 |
raise gr.Error("Please upload a PDF file or provide a valid URL.")
|
| 62 |
|
| 63 |
progress(0.3, desc="Converting PDF pages to images...")
|
| 64 |
+
logger.info(f"Using DPI: {dpi}")
|
| 65 |
try:
|
| 66 |
if is_bytes:
|
| 67 |
+
images = convert_from_bytes(pdf_input_source, dpi=dpi)
|
| 68 |
else:
|
| 69 |
+
images = convert_from_path(pdf_input_source, dpi=dpi)
|
| 70 |
except (PDFInfoNotInstalledError, FileNotFoundError):
|
| 71 |
raise gr.Error("Server configuration error: Poppler dependency is missing.")
|
| 72 |
except (PDFPageCountError, Exception) as e:
|
|
|
|
| 115 |
label="PDF URL",
|
| 116 |
placeholder="e.g., https://arxiv.org/pdf/1706.03762.pdf"
|
| 117 |
)
|
| 118 |
+
|
| 119 |
+
dpi_slider = gr.Slider(
|
| 120 |
+
minimum=100,
|
| 121 |
+
maximum=600,
|
| 122 |
+
step=50,
|
| 123 |
+
value=200,
|
| 124 |
+
label="Image Resolution (DPI)",
|
| 125 |
+
info="Higher DPI results in a clearer image but increases processing time and file size."
|
| 126 |
+
)
|
| 127 |
|
| 128 |
submit_btn = gr.Button("Stitch PDF Pages", variant="primary")
|
| 129 |
|
|
|
|
| 142 |
|
| 143 |
submit_btn.click(
|
| 144 |
fn=process_pdf,
|
| 145 |
+
inputs=[pdf_file_input, pdf_url_input, dpi_slider],
|
| 146 |
outputs=[output_image_preview, output_image_download]
|
| 147 |
)
|
| 148 |
|