Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app.py +133 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from pdf2image import convert_from_path
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import uuid
|
| 6 |
+
|
| 7 |
+
def pdf_to_jpeg(pdf_file, dpi, quality):
|
| 8 |
+
"""
|
| 9 |
+
Converts a PDF file into a list of high-resolution JPEG images.
|
| 10 |
+
|
| 11 |
+
Args:
|
| 12 |
+
pdf_file (str): Path to the uploaded PDF file.
|
| 13 |
+
dpi (int): Dots per inch for resolution.
|
| 14 |
+
quality (int): JPEG quality (1-100).
|
| 15 |
+
|
| 16 |
+
Returns:
|
| 17 |
+
list: List of paths to the generated JPEG files.
|
| 18 |
+
"""
|
| 19 |
+
if pdf_file is None:
|
| 20 |
+
raise gr.Error("Please upload a PDF file first.")
|
| 21 |
+
|
| 22 |
+
try:
|
| 23 |
+
# Create a unique directory for this session's output to avoid collisions
|
| 24 |
+
session_id = str(uuid.uuid4())
|
| 25 |
+
output_dir = Path(f"outputs/{session_id}")
|
| 26 |
+
output_dir.mkdir(parents=True, exist_ok=True)
|
| 27 |
+
|
| 28 |
+
# Convert PDF to images
|
| 29 |
+
# dpi=300 is generally considered high resolution for printing/viewing
|
| 30 |
+
images = convert_from_path(pdf_file, dpi=dpi)
|
| 31 |
+
|
| 32 |
+
jpeg_paths = []
|
| 33 |
+
for i, image in enumerate(images):
|
| 34 |
+
# Generate filename: page_1.jpg, page_2.jpg...
|
| 35 |
+
file_path = output_dir / f"page_{i+1}.jpg"
|
| 36 |
+
# Save as JPEG with specified quality
|
| 37 |
+
image.save(file_path, "JPEG", quality=quality)
|
| 38 |
+
jpeg_paths.append(str(file_path))
|
| 39 |
+
|
| 40 |
+
return jpeg_paths
|
| 41 |
+
|
| 42 |
+
except Exception as e:
|
| 43 |
+
raise gr.Error(f"An error occurred during conversion: {str(e)}")
|
| 44 |
+
|
| 45 |
+
# Custom professional theme for Gradio 6
|
| 46 |
+
custom_theme = gr.themes.Soft(
|
| 47 |
+
primary_hue="indigo",
|
| 48 |
+
secondary_hue="slate",
|
| 49 |
+
neutral_hue="gray",
|
| 50 |
+
font=gr.themes.GoogleFont("Inter"),
|
| 51 |
+
spacing_size="lg",
|
| 52 |
+
radius_size="md"
|
| 53 |
+
).set(
|
| 54 |
+
button_primary_background_fill="*primary_600",
|
| 55 |
+
button_primary_background_fill_hover="*primary_700",
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
with gr.Blocks() as demo:
|
| 59 |
+
# Header section
|
| 60 |
+
gr.Markdown(
|
| 61 |
+
"""
|
| 62 |
+
# 📄 PDF to High-Res JPEG Converter
|
| 63 |
+
Convert your PDF documents into professional, high-resolution JPEG images.
|
| 64 |
+
Each page of your PDF will be processed as a separate high-quality image slice.
|
| 65 |
+
|
| 66 |
+
***
|
| 67 |
+
[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
|
| 68 |
+
"""
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
with gr.Column(scale=1):
|
| 73 |
+
gr.Markdown("### ⚙️ Conversion Settings")
|
| 74 |
+
pdf_input = gr.File(
|
| 75 |
+
label="Upload PDF",
|
| 76 |
+
file_types=[".pdf"],
|
| 77 |
+
file_count="single"
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
with gr.Group():
|
| 81 |
+
dpi_slider = gr.Slider(
|
| 82 |
+
minimum=72,
|
| 83 |
+
maximum=600,
|
| 84 |
+
value=300,
|
| 85 |
+
step=1,
|
| 86 |
+
label="Resolution (DPI)",
|
| 87 |
+
info="300 DPI is standard for high quality. 600 DPI is ultra-high."
|
| 88 |
+
)
|
| 89 |
+
quality_slider = gr.Slider(
|
| 90 |
+
minimum=1,
|
| 91 |
+
maximum=100,
|
| 92 |
+
value=95,
|
| 93 |
+
step=1,
|
| 94 |
+
label="JPEG Quality",
|
| 95 |
+
info="Higher value means better quality but larger file size."
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
convert_btn = gr.Button("Convert to JPEG", variant="primary", size="lg")
|
| 99 |
+
clear_btn = gr.Button("Clear All", variant="secondary")
|
| 100 |
+
|
| 101 |
+
with gr.Column(scale=2):
|
| 102 |
+
gr.Markdown("### 🖼️ Generated Images")
|
| 103 |
+
# Gallery is perfect for showing multiple slices of the PDF
|
| 104 |
+
output_gallery = gr.Gallery(
|
| 105 |
+
label="Processed Pages",
|
| 106 |
+
show_label=False,
|
| 107 |
+
columns=2,
|
| 108 |
+
object_fit="contain",
|
| 109 |
+
height="auto"
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# Event Listeners
|
| 113 |
+
convert_btn.click(
|
| 114 |
+
fn=pdf_to_jpeg,
|
| 115 |
+
inputs=[pdf_input, dpi_slider, quality_slider],
|
| 116 |
+
outputs=[output_gallery],
|
| 117 |
+
api_visibility="public"
|
| 118 |
+
)
|
| 119 |
+
|
| 120 |
+
clear_btn.click(
|
| 121 |
+
fn=lambda: (None, None),
|
| 122 |
+
inputs=None,
|
| 123 |
+
outputs=[pdf_input, output_gallery],
|
| 124 |
+
api_visibility="public"
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# Gradio 6 Launch Configuration
|
| 128 |
+
demo.launch(
|
| 129 |
+
theme=custom_theme,
|
| 130 |
+
footer_links=[
|
| 131 |
+
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
|
| 132 |
+
]
|
| 133 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=6.0
|
| 2 |
+
pdf2image
|
| 3 |
+
Pillow
|
| 4 |
+
requests
|