Resolved Technical Issues & Deployment Guide
This document tracks critical problems encountered during development and deployment (especially for Docker and Hugging Face Spaces) and their corresponding solutions.
1. Font Rendering & Layout Consistency
Problem: "Boxes" instead of Arabic Text
- Symptom: Arabic names appeared as empty boxes (tofu) or incorrectly rendered characters.
- Cause: The system was attempting to load Windows-specific font paths (e.g.,
C:/Windows/Fonts/...) which do not exist in Linux/Docker environments. - Solution:
- Implemented platform-agnostic font discovery in
core/layout_engine.py. - Added automatic detection of bundled fonts in the
assets/directory. - Prioritized
arialbd.ttffor Arabic support over legacy fonts.
- Implemented platform-agnostic font discovery in
Problem: Settings Changes Not Reflecting
- Symptom: Changing
id_font_sizeinsettings.jsonhad no effect until the app was restarted. - Cause: Settings were loaded once at module import time and cached as global constants.
- Solution: Modified
generate_layoutto callload_settings()at the start of every execution, ensuring real-time updates from the JSON file.
2. Hugging Face Spaces (Docker) Deployment
Problem: Obsolete libgl1-mesa-glx
- Symptom: Docker build failed with
E: Package 'libgl1-mesa-glx' has no installation candidate. - Cause: The base Debian image (Trixie/Sid) used by HF has obsoleted this package.
- Solution: Updated
Dockerfileto uselibgl1which provides the necessary OpenGL libraries for OpenCV.
Problem: Transformers 4.50+ Compatibility (BiRefNet/RMBG-2.0)
- Symptom:
AttributeError: 'BiRefNet' object has no attribute 'all_tied_weights_keys'or'NoneType' object has no attribute 'keys'. - Cause: The custom model code for BiRefNet/RMBG-2.0 is incompatible with internal changes in recent
transformersversions regarding tied weight tracking. - Solution:
- Applied a robust monkeypatch in
core/process_images.pyto thePreTrainedModelclass. - Forced
all_tied_weights_keysto always return an empty dictionary{}instead ofNone. - Pinned
transformers==4.48.2inrequirements.txtfor a stable environment.
- Applied a robust monkeypatch in
Problem: Meta-Tensor Initialization Bug
- Symptom: Model failed to load on CPU due to "meta tensors" not being correctly materialized.
- Cause: A bug in how
torch.linspaceinteracts with transformers'low_cpu_mem_usageflag for custom models. - Solution: Monkeypatched
torch.linspaceincore/process_images.pyto force materialization on CPU when a meta-tensor is detected.
3. Git & LFS Management
Problem: Binary File Rejection
- Symptom:
remote: Your push was rejected because it contains binary files. - Cause: Pushing large
.jpg,.png, or.cubefiles directly to Hugging Face without Git LFS, or having them in the Git history from previous commits. - Solution:
- Configured
.gitattributesto track*.png,*.TTF,*.npz, and*.cubewith LFS. - Used
git filter-branchto purge large binaries (raw/,white/, and root.jpgfiles) from the entire Git history to reduce repo size and satisfy HF hooks.
- Configured
5. Image Processing Pipeline & Dependencies
Problem: KeyError: 'setting text direction... not supported without libraqm'
- Symptom: Application crashes on Windows when attempting to render Arabic text in the layout.
- Cause: Pillow's
directionandfeaturesparameters require thelibraqmlibrary, which is difficult to install on Windows. - Solution:
- Added a safety check using
PIL.features.check("raqm"). - Implemented a fallback that relies on
arabic-reshaperandpython-bidifor manual shaping/reordering whenraqmis missing.
- Added a safety check using
Problem: Background Removal failing when Retouching is enabled
- Symptom: Background removal appeared "ignored" or reverted to original background after processing.
- Cause: The
retouch_image_pilfunction incore/retouch.pywas converting the image to RGB for OpenCV processing, stripping the Alpha channel (transparency) created by the BG removal step. - Solution:
- Updated
retouch_image_pilto detect and save the Alpha channel before processing. - Modified the logic to restore the Alpha channel to the final retouched PIL image before returning it to the pipeline.
- Updated
Problem: BiRefNet Model Inference Error
- Symptom:
TypeErroror indexing errors during background removal inference. - Cause: Inconsistent model output formats (list of tensors vs. a single tensor) depending on the environment or
transformersversion. - Solution: Updated
remove_backgroundincore/process_images.pyto check if output is a list/tuple and handle both cases robustly.
Problem: AttributeError: module 'mediapipe' has no attribute 'solutions'
- Symptom: Skin retouching fails in the Docker container with this error.
- Cause: Inconsistent behavior of the
mediapipepackage initialization in some Linux environments. - Solution:
- Explicitly imported submodules like
mediapipe.solutions.face_meshat the top of the file. - Switched from
python:3.10-slimto the fullpython:3.10image to ensure a complete build environment. - Added
libprotobuf-devandprotobuf-compilerto theDockerfile.
- Explicitly imported submodules like
Problem: Arabic/English Text appearing as "Boxes" (Tofu)
- Symptom: All text on the print layout appears as empty squares in the Hugging Face Space.
- Cause: The container lacked fonts with proper character support, and binary
.ttffiles were often corrupted as 130-byte Git LFS pointers. - Solution:
- Automated Downloads: Updated
Dockerfileto usewgetto pull real binary fonts directly from GitHub during the build. - Deep Search: Implemented a recursive font discovery system in
core/layout_engine.pythat scans/usr/share/fonts. - System Fallbacks: Installed
fonts-noto-extraandfonts-dejavu-coreas guaranteed system-level backups.
- Automated Downloads: Updated
Problem: Arabic Text appearing "Connected but Reversed"
- Symptom: Arabic letters connect correctly but flow from Left-to-Right (e.g., "م ح م د" instead of "محمد").
- Cause: Inconsistent behavior between local Windows (missing
libraqm) and Docker Linux (haslibraqm). Usingget_displayin an environment that already supports complex scripts causes a "double-reversal". - Solution:
- Intelligent Detection: Updated
_reshape_arabicincore/layout_engine.pyto check for Raqm support viaPIL.features.check("raqm"). - Conditional Reordering: The system now only applies
python-bidireordering if Raqm is absent. This ensures perfect rendering in both environments without manual code changes.
- Intelligent Detection: Updated
Problem: Manual Cropping ignored or shifted
- Symptom: After manually adjusting the crop in the web interface, the result still looked like the AI auto-crop or was completely wrong.
- Cause: The backend was using
cv2.imdecodewhich ignores EXIF orientation tags. Since the frontend cropper works on correctly oriented thumbnails, the coordinates sent to the backend didn't match the raw image orientation on disk. - Solution: Updated
core/crop.pyto use a_load_image_exif_safehelper that uses PIL to transpose the image before converting it to OpenCV format. This ensures coordinates from the web UI always match the backend image state.