Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -243,6 +243,7 @@ cleanup_thread.start()
|
|
| 243 |
def process_pdf(file_obj, progress=gr.Progress()) -> Tuple[Optional[str], str, str]:
|
| 244 |
"""
|
| 245 |
Main processing function for Gradio interface
|
|
|
|
| 246 |
"""
|
| 247 |
if file_obj is None:
|
| 248 |
return None, "", "β οΈ Please upload a PDF file"
|
|
@@ -292,7 +293,8 @@ def process_pdf(file_obj, progress=gr.Progress()) -> Tuple[Optional[str], str, s
|
|
| 292 |
|
| 293 |
# Create ZIP file
|
| 294 |
progress(0.9, "Creating ZIP archive...")
|
| 295 |
-
|
|
|
|
| 296 |
|
| 297 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 298 |
for file_path in output_files:
|
|
@@ -315,28 +317,9 @@ def process_pdf(file_obj, progress=gr.Progress()) -> Tuple[Optional[str], str, s
|
|
| 315 |
<td style="padding: 10px; font-weight: bold; color: #334155;">β Segments Discarded (>5MB):</td>
|
| 316 |
<td style="padding: 10px; text-align: right; color: #dc2626; font-weight: 600;">{stats['segments_discarded']}</td>
|
| 317 |
</tr>
|
| 318 |
-
<tr style="border-bottom: 1px solid #e2e8f0; background: #f8fafc;">
|
| 319 |
-
<td style="padding: 10px; font-weight: bold; color: #334155;">π¦ Original Size:</td>
|
| 320 |
-
<td style="padding: 10px; text-align: right; color: #475569; font-weight: 600;">{stats['original_size_mb']:.2f} MB</td>
|
| 321 |
-
</tr>
|
| 322 |
-
<tr style="border-bottom: 1px solid #e2e8f0;">
|
| 323 |
-
<td style="padding: 10px; font-weight: bold; color: #334155;">π Total Output Size:</td>
|
| 324 |
-
<td style="padding: 10px; text-align: right; color: #475569; font-weight: 600;">{stats['total_output_size_mb']:.2f} MB</td>
|
| 325 |
-
</tr>
|
| 326 |
-
<tr style="border-bottom: 1px solid #e2e8f0; background: #f8fafc;">
|
| 327 |
-
<td style="padding: 10px; font-weight: bold; color: #334155;">π Largest Segment:</td>
|
| 328 |
-
<td style="padding: 10px; text-align: right; color: #475569; font-weight: 600;">{stats['largest_segment_mb']:.2f} MB</td>
|
| 329 |
-
</tr>
|
| 330 |
-
<tr style="background: white;">
|
| 331 |
-
<td style="padding: 10px; font-weight: bold; color: #334155;">π Smallest Segment:</td>
|
| 332 |
-
<td style="padding: 10px; text-align: right; color: #475569; font-weight: 600;">{stats['smallest_segment_mb']:.2f} MB</td>
|
| 333 |
-
</tr>
|
| 334 |
</table>
|
| 335 |
<p style="margin-top: 15px; color: #059669; font-weight: bold;">
|
| 336 |
-
β¨ Your file has been split successfully!
|
| 337 |
-
</p>
|
| 338 |
-
<p style="margin-top: 10px; color: #6b7280; font-size: 0.9em;">
|
| 339 |
-
β±οΈ Files will be automatically deleted after {CLEANUP_AFTER_MINUTES} minutes
|
| 340 |
</p>
|
| 341 |
</div>
|
| 342 |
"""
|
|
@@ -346,7 +329,8 @@ def process_pdf(file_obj, progress=gr.Progress()) -> Tuple[Optional[str], str, s
|
|
| 346 |
# Clean up input file to save space
|
| 347 |
input_path.unlink()
|
| 348 |
|
| 349 |
-
|
|
|
|
| 350 |
|
| 351 |
except Exception as e:
|
| 352 |
logger.error(f"Processing error: {str(e)}")
|
|
@@ -357,6 +341,59 @@ def process_pdf(file_obj, progress=gr.Progress()) -> Tuple[Optional[str], str, s
|
|
| 357 |
pass
|
| 358 |
return None, "", f"β Error: {str(e)}"
|
| 359 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
# Create Gradio interface with fixed theme
|
| 361 |
with gr.Blocks(
|
| 362 |
title="PDF Splitter - Fast & Simple",
|
|
|
|
| 243 |
def process_pdf(file_obj, progress=gr.Progress()) -> Tuple[Optional[str], str, str]:
|
| 244 |
"""
|
| 245 |
Main processing function for Gradio interface
|
| 246 |
+
Returns: (zip_file_path, statistics_html, status_message)
|
| 247 |
"""
|
| 248 |
if file_obj is None:
|
| 249 |
return None, "", "β οΈ Please upload a PDF file"
|
|
|
|
| 293 |
|
| 294 |
# Create ZIP file
|
| 295 |
progress(0.9, "Creating ZIP archive...")
|
| 296 |
+
zip_filename = f"pdf_segments_{session_id}.zip"
|
| 297 |
+
zip_path = session_dir / zip_filename
|
| 298 |
|
| 299 |
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 300 |
for file_path in output_files:
|
|
|
|
| 317 |
<td style="padding: 10px; font-weight: bold; color: #334155;">β Segments Discarded (>5MB):</td>
|
| 318 |
<td style="padding: 10px; text-align: right; color: #dc2626; font-weight: 600;">{stats['segments_discarded']}</td>
|
| 319 |
</tr>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
</table>
|
| 321 |
<p style="margin-top: 15px; color: #059669; font-weight: bold;">
|
| 322 |
+
β¨ Your file has been split successfully! Click the download button below.
|
|
|
|
|
|
|
|
|
|
| 323 |
</p>
|
| 324 |
</div>
|
| 325 |
"""
|
|
|
|
| 329 |
# Clean up input file to save space
|
| 330 |
input_path.unlink()
|
| 331 |
|
| 332 |
+
# IMPORTANT: Return the actual file path as a string for gr.File to handle
|
| 333 |
+
return str(zip_path), stats_html, "β
Processing complete! Your ZIP file is ready for download."
|
| 334 |
|
| 335 |
except Exception as e:
|
| 336 |
logger.error(f"Processing error: {str(e)}")
|
|
|
|
| 341 |
pass
|
| 342 |
return None, "", f"β Error: {str(e)}"
|
| 343 |
|
| 344 |
+
# Create Gradio interface with fixed download component
|
| 345 |
+
with gr.Blocks(
|
| 346 |
+
title="PDF Splitter - Fast & Simple",
|
| 347 |
+
theme=gr.themes.Base()
|
| 348 |
+
) as app:
|
| 349 |
+
|
| 350 |
+
gr.Markdown("""
|
| 351 |
+
# π PDF Splitter Tool
|
| 352 |
+
|
| 353 |
+
**Split large PDFs into 4.5MB segments - Files over 5MB are automatically discarded!**
|
| 354 |
+
""")
|
| 355 |
+
|
| 356 |
+
with gr.Row():
|
| 357 |
+
with gr.Column():
|
| 358 |
+
file_input = gr.File(
|
| 359 |
+
label="Upload PDF",
|
| 360 |
+
file_types=[".pdf"],
|
| 361 |
+
type="filepath"
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
split_btn = gr.Button(
|
| 365 |
+
"π Split PDF into 4.5MB Segments",
|
| 366 |
+
variant="primary",
|
| 367 |
+
size="lg"
|
| 368 |
+
)
|
| 369 |
+
|
| 370 |
+
with gr.Row():
|
| 371 |
+
status_text = gr.Markdown("Ready to process your PDF...")
|
| 372 |
+
|
| 373 |
+
with gr.Row():
|
| 374 |
+
stats_output = gr.HTML()
|
| 375 |
+
|
| 376 |
+
with gr.Row():
|
| 377 |
+
# FIXED: Set interactive=True and use value parameter correctly
|
| 378 |
+
download_file = gr.File(
|
| 379 |
+
label="π¦ Download ZIP (Contains only segments β€5MB)",
|
| 380 |
+
visible=True,
|
| 381 |
+
interactive=False, # Keep false initially
|
| 382 |
+
type="filepath" # Ensure it's set to filepath
|
| 383 |
+
)
|
| 384 |
+
|
| 385 |
+
# Handle processing with proper output mapping
|
| 386 |
+
def process_and_update(file_obj, progress=gr.Progress()):
|
| 387 |
+
zip_path, stats_html, status_msg = process_pdf(file_obj, progress)
|
| 388 |
+
# Return the file path directly for gr.File component
|
| 389 |
+
return zip_path, stats_html, status_msg
|
| 390 |
+
|
| 391 |
+
split_btn.click(
|
| 392 |
+
fn=process_and_update,
|
| 393 |
+
inputs=[file_input],
|
| 394 |
+
outputs=[download_file, stats_output, status_text]
|
| 395 |
+
)
|
| 396 |
+
|
| 397 |
# Create Gradio interface with fixed theme
|
| 398 |
with gr.Blocks(
|
| 399 |
title="PDF Splitter - Fast & Simple",
|