Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
import requests
|
4 |
import io
|
5 |
import tempfile
|
6 |
-
import sys
|
7 |
import os
|
8 |
|
9 |
def compress_pdf(input_file, url, strength):
|
@@ -22,8 +21,7 @@ def compress_pdf(input_file, url, strength):
|
|
22 |
else:
|
23 |
if hasattr(input_file, 'name'):
|
24 |
# If input_file is a file path
|
25 |
-
|
26 |
-
pdf_content = io.BytesIO(file.read())
|
27 |
initial_size = os.path.getsize(input_file.name)
|
28 |
else:
|
29 |
# If input_file is file-like object
|
@@ -32,29 +30,19 @@ def compress_pdf(input_file, url, strength):
|
|
32 |
initial_size = pdf_content.tell()
|
33 |
pdf_content.seek(0)
|
34 |
|
35 |
-
reader = PyPDF2.PdfReader(pdf_content)
|
36 |
-
writer = PyPDF2.PdfWriter()
|
37 |
-
|
38 |
if strength == "Low":
|
39 |
-
|
40 |
elif strength == "Medium":
|
41 |
-
|
42 |
else: # High
|
43 |
-
|
44 |
-
|
45 |
-
# Apply compression to each page
|
46 |
-
for page in reader.pages:
|
47 |
-
page.compress_content_streams() # Apply content stream compression
|
48 |
-
writer.add_page(page)
|
49 |
-
|
50 |
-
# Set compression parameters
|
51 |
-
writer.compress = True
|
52 |
|
53 |
-
# Write the compressed PDF to a temporary file
|
54 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
55 |
-
writer.write(temp_file)
|
56 |
temp_file_path = temp_file.name
|
57 |
|
|
|
|
|
|
|
58 |
# Check the compression ratio achieved
|
59 |
compressed_size = os.path.getsize(temp_file_path)
|
60 |
compression_ratio = compressed_size / initial_size
|
@@ -72,8 +60,6 @@ def compress_pdf(input_file, url, strength):
|
|
72 |
# The rest of the code remains the same
|
73 |
|
74 |
def process_and_compress(input_file, url, strength):
|
75 |
-
sys.setrecursionlimit(10000)
|
76 |
-
|
77 |
output_file, message = compress_pdf(input_file, url, strength)
|
78 |
if output_file:
|
79 |
return output_file, message
|
@@ -85,7 +71,7 @@ with gr.Blocks() as demo:
|
|
85 |
with gr.Row():
|
86 |
input_file = gr.File(label="Upload PDF")
|
87 |
url_input = gr.Textbox(label="Or enter PDF URL")
|
88 |
-
strength = gr.Radio(["Low", "Medium", "High"], label="Compression Strength", value="Medium", info="Low:
|
89 |
compress_btn = gr.Button("Compress")
|
90 |
output_file = gr.File(label="Download Compressed PDF")
|
91 |
message = gr.Textbox(label="Message")
|
|
|
1 |
import gradio as gr
|
2 |
+
import pikepdf
|
3 |
import requests
|
4 |
import io
|
5 |
import tempfile
|
|
|
6 |
import os
|
7 |
|
8 |
def compress_pdf(input_file, url, strength):
|
|
|
21 |
else:
|
22 |
if hasattr(input_file, 'name'):
|
23 |
# If input_file is a file path
|
24 |
+
pdf_content = input_file.name
|
|
|
25 |
initial_size = os.path.getsize(input_file.name)
|
26 |
else:
|
27 |
# If input_file is file-like object
|
|
|
30 |
initial_size = pdf_content.tell()
|
31 |
pdf_content.seek(0)
|
32 |
|
|
|
|
|
|
|
33 |
if strength == "Low":
|
34 |
+
compress_level = 1
|
35 |
elif strength == "Medium":
|
36 |
+
compress_level = 2
|
37 |
else: # High
|
38 |
+
compress_level = 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
|
|
40 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
|
|
41 |
temp_file_path = temp_file.name
|
42 |
|
43 |
+
pdf = pikepdf.Pdf.open(pdf_content)
|
44 |
+
pdf.save(temp_file_path, compress_streams=True, compress_images=True, optimize_images=True)
|
45 |
+
|
46 |
# Check the compression ratio achieved
|
47 |
compressed_size = os.path.getsize(temp_file_path)
|
48 |
compression_ratio = compressed_size / initial_size
|
|
|
60 |
# The rest of the code remains the same
|
61 |
|
62 |
def process_and_compress(input_file, url, strength):
|
|
|
|
|
63 |
output_file, message = compress_pdf(input_file, url, strength)
|
64 |
if output_file:
|
65 |
return output_file, message
|
|
|
71 |
with gr.Row():
|
72 |
input_file = gr.File(label="Upload PDF")
|
73 |
url_input = gr.Textbox(label="Or enter PDF URL")
|
74 |
+
strength = gr.Radio(["Low", "Medium", "High"], label="Compression Strength", value="Medium", info="Low: Minimal compression, Medium: Moderate compression, High: Maximum compression")
|
75 |
compress_btn = gr.Button("Compress")
|
76 |
output_file = gr.File(label="Download Compressed PDF")
|
77 |
message = gr.Textbox(label="Message")
|