Spaces:
Running
Running
Upload app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from PyPDF2 import PdfReader, PdfWriter, PageObject
|
|
|
|
|
3 |
import tempfile
|
4 |
import os
|
5 |
import atexit
|
@@ -33,50 +35,125 @@ def merge_pdfs(pdf_files, order, start_on_odd=False):
|
|
33 |
|
34 |
return temp_file_path
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
# Create Gradio interface
|
37 |
with gr.Blocks() as demo:
|
38 |
-
gr.Markdown("# PDF Merger")
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
# Launch the Gradio app
|
82 |
demo.launch()
|
@@ -85,7 +162,7 @@ demo.launch()
|
|
85 |
def cleanup_temp_files():
|
86 |
temp_dir = tempfile.gettempdir()
|
87 |
for filename in os.listdir(temp_dir):
|
88 |
-
if filename.endswith('.pdf'):
|
89 |
os.remove(os.path.join(temp_dir, filename))
|
90 |
|
91 |
-
atexit.register(cleanup_temp_files)
|
|
|
1 |
import gradio as gr
|
2 |
from PyPDF2 import PdfReader, PdfWriter, PageObject
|
3 |
+
from pdf2image import convert_from_path
|
4 |
+
import img2pdf
|
5 |
import tempfile
|
6 |
import os
|
7 |
import atexit
|
|
|
35 |
|
36 |
return temp_file_path
|
37 |
|
38 |
+
def pdf_to_images(pdf_file):
|
39 |
+
# Convert PDF to images
|
40 |
+
temp_dir = tempfile.mkdtemp()
|
41 |
+
images = convert_from_path(pdf_file.name, output_folder=temp_dir, fmt='jpg')
|
42 |
+
image_paths = []
|
43 |
+
|
44 |
+
for i, image in enumerate(images):
|
45 |
+
image_path = os.path.join(temp_dir, f"page_{i + 1}.jpg")
|
46 |
+
image.save(image_path, "JPEG")
|
47 |
+
image_paths.append(image_path)
|
48 |
+
|
49 |
+
return image_paths
|
50 |
+
|
51 |
+
def images_to_pdf(image_files):
|
52 |
+
# Convert images to a single PDF
|
53 |
+
temp_file_path = os.path.join(tempfile.gettempdir(), "images_to_pdf.pdf")
|
54 |
+
with open(temp_file_path, "wb") as pdf_file:
|
55 |
+
pdf_file.write(img2pdf.convert([image.name for image in image_files]))
|
56 |
+
return temp_file_path
|
57 |
+
|
58 |
# Create Gradio interface
|
59 |
with gr.Blocks() as demo:
|
60 |
+
gr.Markdown("# PDF Merger and Converter")
|
61 |
+
|
62 |
+
with gr.Tabs():
|
63 |
+
with gr.TabItem("PDF Merger"):
|
64 |
+
pdf_input = gr.File(label="Upload PDF Files to Merge", file_types=[".pdf"], file_count="multiple")
|
65 |
+
order_input = gr.Textbox(label="Enter the order of PDFs as comma-separated numbers, skip the number if you want to skip the file", placeholder="1,2,3,... or 3,1,2")
|
66 |
+
|
67 |
+
with gr.Row():
|
68 |
+
merge_button = gr.Button("Merge PDFs (Normal)")
|
69 |
+
merge_odd_button = gr.Button("Merge PDFs (Each PDF starts on odd page)")
|
70 |
+
|
71 |
+
merged_result = gr.File(label="Download Merged PDF")
|
72 |
+
|
73 |
+
def merge_and_preview(pdf_files, order, start_on_odd=False):
|
74 |
+
n = len(pdf_files)
|
75 |
+
|
76 |
+
if not order:
|
77 |
+
# Default to natural order if order is empty
|
78 |
+
order = list(range(1, n + 1))
|
79 |
+
else:
|
80 |
+
try:
|
81 |
+
# Convert the input string to a list of integers
|
82 |
+
order = [int(x.strip()) for x in order.split(',')]
|
83 |
+
except ValueError:
|
84 |
+
return gr.Error("Invalid order format. Ensure it is comma-separated numbers.")
|
85 |
+
|
86 |
+
# Ensure the order does not reference non-existing files
|
87 |
+
if any(i < 0 or i > n for i in order):
|
88 |
+
return gr.Error(f"Order values must be between 0 and {n} (0 means to skip the file).")
|
89 |
+
|
90 |
+
# Merge PDFs with the specified start_on_odd option
|
91 |
+
merged_pdf_path = merge_pdfs(pdf_files, order, start_on_odd)
|
92 |
+
return merged_pdf_path
|
93 |
+
|
94 |
+
merge_button.click(
|
95 |
+
lambda *args: merge_and_preview(*args, False),
|
96 |
+
inputs=[pdf_input, order_input],
|
97 |
+
outputs=[merged_result]
|
98 |
+
)
|
99 |
+
|
100 |
+
merge_odd_button.click(
|
101 |
+
lambda *args: merge_and_preview(*args, True),
|
102 |
+
inputs=[pdf_input, order_input],
|
103 |
+
outputs=[merged_result]
|
104 |
+
)
|
105 |
+
|
106 |
+
with gr.TabItem("PDF to Image Converter"):
|
107 |
+
single_pdf_input = gr.File(label="Upload PDF File to Convert", file_types=[".pdf"], file_count="single")
|
108 |
+
image_output = gr.Gallery(label="Converted Images (JPG)", show_label=True)
|
109 |
+
|
110 |
+
def convert_pdf_to_images(pdf_file):
|
111 |
+
return pdf_to_images(pdf_file)
|
112 |
+
|
113 |
+
single_pdf_input.change(
|
114 |
+
convert_pdf_to_images,
|
115 |
+
inputs=[single_pdf_input],
|
116 |
+
outputs=[image_output]
|
117 |
+
)
|
118 |
+
|
119 |
+
with gr.TabItem("Image to PDF Converter"):
|
120 |
+
image_input = gr.File(label="Upload Images to Convert to PDF", file_types=[".jpg", ".png"], file_count="multiple")
|
121 |
+
order_option = gr.Radio(label="Select Order Type", choices=["Ordered", "Reverse", "Custom"], value="Ordered")
|
122 |
+
custom_order_input = gr.Textbox(label="Enter custom order (comma-separated indices)", visible=False)
|
123 |
+
image_gallery = gr.Gallery(label="Images Preview (Arrange Order)", show_label=True)
|
124 |
+
pdf_result = gr.File(label="Download PDF")
|
125 |
+
|
126 |
+
def update_custom_order_visibility(order_type):
|
127 |
+
return gr.update(visible=(order_type == "Custom"))
|
128 |
+
|
129 |
+
def sort_images(order_type, custom_order, images):
|
130 |
+
if order_type == "Reverse":
|
131 |
+
return images[::-1]
|
132 |
+
elif order_type == "Custom":
|
133 |
+
try:
|
134 |
+
indices = [int(i.strip()) - 1 for i in custom_order.split(',')]
|
135 |
+
return [images[i] for i in indices]
|
136 |
+
except (ValueError, IndexError):
|
137 |
+
return gr.Error("Invalid custom order. Ensure all indices are valid and within range.")
|
138 |
+
return images
|
139 |
+
|
140 |
+
order_option.change(
|
141 |
+
update_custom_order_visibility,
|
142 |
+
inputs=[order_option],
|
143 |
+
outputs=[custom_order_input]
|
144 |
+
)
|
145 |
+
|
146 |
+
gr.Button("Preview Sorted Images").click(
|
147 |
+
lambda order_type, custom_order, images: sort_images(order_type, custom_order, images),
|
148 |
+
inputs=[order_option, custom_order_input, image_input],
|
149 |
+
outputs=[image_gallery]
|
150 |
+
)
|
151 |
+
|
152 |
+
gr.Button("Generate PDF").click(
|
153 |
+
lambda order_type, custom_order, images: images_to_pdf(sort_images(order_type, custom_order, images)),
|
154 |
+
inputs=[order_option, custom_order_input, image_input],
|
155 |
+
outputs=[pdf_result]
|
156 |
+
)
|
157 |
|
158 |
# Launch the Gradio app
|
159 |
demo.launch()
|
|
|
162 |
def cleanup_temp_files():
|
163 |
temp_dir = tempfile.gettempdir()
|
164 |
for filename in os.listdir(temp_dir):
|
165 |
+
if filename.endswith('.pdf') or filename.endswith('.jpg'):
|
166 |
os.remove(os.path.join(temp_dir, filename))
|
167 |
|
168 |
+
atexit.register(cleanup_temp_files)
|