Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,21 +13,24 @@ def compress_pdf(input_file, url, strength):
|
|
| 13 |
if input_file is not None and url and url.strip() != "":
|
| 14 |
return None, "Please provide either a file or a URL, not both."
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
response = requests.get(url)
|
| 19 |
response.raise_for_status()
|
| 20 |
pdf_content = io.BytesIO(response.content)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
reader = PyPDF2.PdfReader(pdf_content)
|
| 33 |
writer = PyPDF2.PdfWriter()
|
|
@@ -85,6 +88,8 @@ def compress_pdf(input_file, url, strength):
|
|
| 85 |
except Exception as e:
|
| 86 |
return None, f"Error compressing PDF: {str(e)}"
|
| 87 |
|
|
|
|
|
|
|
| 88 |
def process_and_compress(input_file, url, strength):
|
| 89 |
sys.setrecursionlimit(10000)
|
| 90 |
|
|
|
|
| 13 |
if input_file is not None and url and url.strip() != "":
|
| 14 |
return None, "Please provide either a file or a URL, not both."
|
| 15 |
|
| 16 |
+
try:
|
| 17 |
+
if url and url.strip() != "":
|
| 18 |
response = requests.get(url)
|
| 19 |
response.raise_for_status()
|
| 20 |
pdf_content = io.BytesIO(response.content)
|
| 21 |
+
initial_size = len(response.content)
|
| 22 |
+
else:
|
| 23 |
+
if hasattr(input_file, 'name'):
|
| 24 |
+
# If input_file is a file path
|
| 25 |
+
with open(input_file.name, 'rb') as file:
|
| 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
|
| 30 |
+
pdf_content = io.BytesIO(input_file.read())
|
| 31 |
+
pdf_content.seek(0, io.SEEK_END)
|
| 32 |
+
initial_size = pdf_content.tell()
|
| 33 |
+
pdf_content.seek(0)
|
| 34 |
|
| 35 |
reader = PyPDF2.PdfReader(pdf_content)
|
| 36 |
writer = PyPDF2.PdfWriter()
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
return None, f"Error compressing PDF: {str(e)}"
|
| 90 |
|
| 91 |
+
# The rest of the code remains the same
|
| 92 |
+
|
| 93 |
def process_and_compress(input_file, url, strength):
|
| 94 |
sys.setrecursionlimit(10000)
|
| 95 |
|