Spaces:
Runtime error
Runtime error
Download workers/pdf_worker.py from cloudaiRambo/cloudairambo: direct link, hf CLI and curl.
- Browser
- Download file 1.19 kB
-
https://huggingface.co/spaces/cloudaiRambo/cloudairambo/resolve/main/workers/pdf_worker.py
- Command line
-
hf download hf://spaces/cloudaiRambo/cloudairambo/workers/pdf_worker.py
-
curl -L -o pdf_worker.py https://huggingface.co/spaces/cloudaiRambo/cloudairambo/resolve/main/workers/pdf_worker.py
1.19 kB
| # backend/workers/pdf_worker.py | |
| import os | |
| import uuid | |
| import shutil | |
| from celery_app import celery_app | |
| from routers.pdf import ( | |
| ghostscript_compress, | |
| PRESET_MAP, | |
| TEMP_DIR, | |
| safe_remove, | |
| ) | |
| def pdf_compress_task(self, input_path: str, preset: str): | |
| output_path = None | |
| try: | |
| preset_value = PRESET_MAP.get(preset, "/screen") | |
| output_path = os.path.join( | |
| TEMP_DIR, | |
| f"compressed_{uuid.uuid4().hex}.pdf" | |
| ) | |
| # 🔥 HEAVY WORK (Ghostscript) | |
| ghostscript_compress( | |
| input_path=input_path, | |
| output_path=output_path, | |
| preset=preset_value | |
| ) | |
| if not os.path.exists(output_path): | |
| raise Exception("Compression failed: output file missing") | |
| return { | |
| "status": "done", | |
| "output_path": output_path, | |
| "size": os.path.getsize(output_path), | |
| } | |
| except Exception as e: | |
| return { | |
| "status": "failed", | |
| "error": str(e), | |
| } | |
| finally: | |
| # Cleanup input | |
| try: | |
| safe_remove(input_path) | |
| except: | |
| pass | |