Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
@@ -2,11 +2,10 @@ import gradio as gr
|
|
2 |
import PyPDF2
|
3 |
import requests
|
4 |
import io
|
5 |
-
import os
|
6 |
import tempfile
|
7 |
import sys
|
8 |
|
9 |
-
def compress_pdf(input_file, url,
|
10 |
if input_file is None and (url is None or url.strip() == ""):
|
11 |
return None, "Please provide either a file or a URL."
|
12 |
|
@@ -29,15 +28,15 @@ def compress_pdf(input_file, url, quality):
|
|
29 |
|
30 |
for i, page in enumerate(reader.pages):
|
31 |
try:
|
32 |
-
if
|
33 |
-
page.compress_content_streams() #
|
34 |
-
elif
|
35 |
-
page.compress_content_streams(
|
36 |
-
else: # Low
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
except Exception as e:
|
42 |
return None, f"Error compressing page {i+1}: {str(e)}"
|
43 |
|
@@ -49,11 +48,10 @@ def compress_pdf(input_file, url, quality):
|
|
49 |
except Exception as e:
|
50 |
return None, f"Error compressing PDF: {str(e)}"
|
51 |
|
52 |
-
def process_and_compress(input_file, url,
|
53 |
-
# Increase recursion limit
|
54 |
sys.setrecursionlimit(10000)
|
55 |
|
56 |
-
output_file, message = compress_pdf(input_file, url,
|
57 |
if output_file:
|
58 |
return output_file, message
|
59 |
else:
|
@@ -64,14 +62,14 @@ with gr.Blocks() as demo:
|
|
64 |
with gr.Row():
|
65 |
input_file = gr.File(label="Upload PDF")
|
66 |
url_input = gr.Textbox(label="Or enter PDF URL")
|
67 |
-
|
68 |
compress_btn = gr.Button("Compress")
|
69 |
output_file = gr.File(label="Download Compressed PDF")
|
70 |
message = gr.Textbox(label="Message")
|
71 |
|
72 |
compress_btn.click(
|
73 |
process_and_compress,
|
74 |
-
inputs=[input_file, url_input,
|
75 |
outputs=[output_file, message]
|
76 |
)
|
77 |
|
|
|
2 |
import PyPDF2
|
3 |
import requests
|
4 |
import io
|
|
|
5 |
import tempfile
|
6 |
import sys
|
7 |
|
8 |
+
def compress_pdf(input_file, url, strength):
|
9 |
if input_file is None and (url is None or url.strip() == ""):
|
10 |
return None, "Please provide either a file or a URL."
|
11 |
|
|
|
28 |
|
29 |
for i, page in enumerate(reader.pages):
|
30 |
try:
|
31 |
+
if strength == "High":
|
32 |
+
page.compress_content_streams() # Maximum compression
|
33 |
+
elif strength == "Medium":
|
34 |
+
page.compress_content_streams() # Medium compression (same as high for now)
|
35 |
+
else: # Low strength
|
36 |
+
writer.add_page(page) # No compression
|
37 |
+
|
38 |
+
if strength != "Low":
|
39 |
+
writer.add_page(page)
|
40 |
except Exception as e:
|
41 |
return None, f"Error compressing page {i+1}: {str(e)}"
|
42 |
|
|
|
48 |
except Exception as e:
|
49 |
return None, f"Error compressing PDF: {str(e)}"
|
50 |
|
51 |
+
def process_and_compress(input_file, url, strength):
|
|
|
52 |
sys.setrecursionlimit(10000)
|
53 |
|
54 |
+
output_file, message = compress_pdf(input_file, url, strength)
|
55 |
if output_file:
|
56 |
return output_file, message
|
57 |
else:
|
|
|
62 |
with gr.Row():
|
63 |
input_file = gr.File(label="Upload PDF")
|
64 |
url_input = gr.Textbox(label="Or enter PDF URL")
|
65 |
+
strength = gr.Radio(["High", "Medium", "Low"], label="Compression Strength", value="Medium", info="High: Maximum compression, Low: Least compression")
|
66 |
compress_btn = gr.Button("Compress")
|
67 |
output_file = gr.File(label="Download Compressed PDF")
|
68 |
message = gr.Textbox(label="Message")
|
69 |
|
70 |
compress_btn.click(
|
71 |
process_and_compress,
|
72 |
+
inputs=[input_file, url_input, strength],
|
73 |
outputs=[output_file, message]
|
74 |
)
|
75 |
|