Commit
β’
95abc0b
1
Parent(s):
20f3b41
draft
Browse files
app.py
ADDED
@@ -0,0 +1,120 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import random
|
4 |
+
import tempfile
|
5 |
+
from pdf2image import convert_from_path
|
6 |
+
from PyPDF2 import PdfReader
|
7 |
+
from huggingface_hub import create_repo, upload_folder, HfApi
|
8 |
+
|
9 |
+
|
10 |
+
def pdf_to_images(pdf_files, sample_size, temp_dir):
|
11 |
+
if not os.path.exists(temp_dir):
|
12 |
+
os.makedirs(temp_dir)
|
13 |
+
|
14 |
+
all_images = []
|
15 |
+
for pdf_file in pdf_files:
|
16 |
+
pdf_path = pdf_file.name
|
17 |
+
pdf = PdfReader(pdf_path)
|
18 |
+
total_pages = len(pdf.pages)
|
19 |
+
|
20 |
+
# Determine the number of pages to convert
|
21 |
+
pages_to_convert = (
|
22 |
+
total_pages if sample_size == 0 else min(sample_size, total_pages)
|
23 |
+
)
|
24 |
+
|
25 |
+
# Select random pages if sampling
|
26 |
+
if sample_size > 0 and sample_size < total_pages:
|
27 |
+
selected_pages = sorted(
|
28 |
+
random.sample(range(1, total_pages + 1), pages_to_convert)
|
29 |
+
)
|
30 |
+
else:
|
31 |
+
selected_pages = range(1, total_pages + 1)
|
32 |
+
|
33 |
+
# Convert selected PDF pages to images
|
34 |
+
for page_num in selected_pages:
|
35 |
+
images = convert_from_path(
|
36 |
+
pdf_path, first_page=page_num, last_page=page_num
|
37 |
+
)
|
38 |
+
for image in images:
|
39 |
+
image_path = os.path.join(
|
40 |
+
temp_dir, f"{os.path.basename(pdf_path)}_page_{page_num}.jpg"
|
41 |
+
)
|
42 |
+
image.save(image_path, "JPEG")
|
43 |
+
all_images.append(image_path)
|
44 |
+
|
45 |
+
return all_images, f"Saved {len(all_images)} images to temporary directory"
|
46 |
+
|
47 |
+
|
48 |
+
def process_pdfs(pdf_files, sample_size, hf_repo, oauth_token: gr.OAuthToken | None):
|
49 |
+
if not pdf_files:
|
50 |
+
return None, "No PDF files uploaded."
|
51 |
+
|
52 |
+
if oauth_token is None:
|
53 |
+
return None, "Please log in to upload to Hugging Face."
|
54 |
+
|
55 |
+
try:
|
56 |
+
with tempfile.TemporaryDirectory() as temp_dir:
|
57 |
+
images_dir = os.path.join(temp_dir, "images")
|
58 |
+
os.makedirs(images_dir)
|
59 |
+
|
60 |
+
images, message = pdf_to_images(pdf_files, sample_size, images_dir)
|
61 |
+
|
62 |
+
if hf_repo:
|
63 |
+
try:
|
64 |
+
api = HfApi(token=oauth_token.token)
|
65 |
+
api.create_repo(
|
66 |
+
hf_repo,
|
67 |
+
repo_type="dataset",
|
68 |
+
)
|
69 |
+
api.upload_folder(
|
70 |
+
folder_path=images_dir,
|
71 |
+
repo_id=hf_repo,
|
72 |
+
repo_type="dataset",
|
73 |
+
path_in_repo="images",
|
74 |
+
)
|
75 |
+
message += (
|
76 |
+
f"\nUploaded images to Hugging Face repo: {hf_repo}/images"
|
77 |
+
)
|
78 |
+
except Exception as e:
|
79 |
+
message += f"\nFailed to upload to Hugging Face: {str(e)}"
|
80 |
+
|
81 |
+
return images, message
|
82 |
+
except Exception as e:
|
83 |
+
return None, f"An error occurred: {str(e)}"
|
84 |
+
|
85 |
+
|
86 |
+
# Define the Gradio interface
|
87 |
+
with gr.Blocks() as demo:
|
88 |
+
gr.Markdown("# PDF to Image Converter")
|
89 |
+
gr.Markdown(
|
90 |
+
"Upload PDF(s), convert pages to images, and optionally upload them to a Hugging Face repo. If a sample size is specified, random pages will be selected."
|
91 |
+
)
|
92 |
+
|
93 |
+
with gr.Row():
|
94 |
+
gr.LoginButton(size="sm")
|
95 |
+
|
96 |
+
with gr.Row():
|
97 |
+
pdf_files = gr.File(file_count="multiple", label="Upload PDF(s)")
|
98 |
+
sample_size = gr.Slider(
|
99 |
+
minimum=0,
|
100 |
+
maximum=50,
|
101 |
+
step=1,
|
102 |
+
value=0,
|
103 |
+
label="Sample Size (0 for all pages)",
|
104 |
+
)
|
105 |
+
hf_repo = gr.Textbox(
|
106 |
+
label="Hugging Face Repo", placeholder="username/repo-name"
|
107 |
+
)
|
108 |
+
|
109 |
+
output_gallery = gr.Gallery(label="Converted Images")
|
110 |
+
status_text = gr.Textbox(label="Status")
|
111 |
+
|
112 |
+
submit_button = gr.Button("Process PDFs")
|
113 |
+
submit_button.click(
|
114 |
+
process_pdfs,
|
115 |
+
inputs=[pdf_files, sample_size, hf_repo],
|
116 |
+
outputs=[output_gallery, status_text],
|
117 |
+
)
|
118 |
+
|
119 |
+
# Launch the app
|
120 |
+
demo.launch(debug=True)
|