From 49212a19be8d45b02e137fd654bdfd313b1ca1e8 Mon Sep 17 00:00:00 2001 From: Micuks Date: Sat, 28 Mar 2026 23:31:40 +0800 Subject: [PATCH 1/3] feat: add pyproject.toml with uv support and optional dependency groups Migrate from requirements.txt to pyproject.toml for modern Python packaging. Add optional dependency groups: paddleocr, formula, rmbg, torch, sam3. Configure uv sources for local sam3_src editable install. Also update .gitignore to exclude uv.lock and *.egg-info/. --- .gitignore | 2 ++ pyproject.toml | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 pyproject.toml diff --git a/.gitignore b/.gitignore index f05287c..9df5467 100644 --- a/.gitignore +++ b/.gitignore @@ -5,9 +5,11 @@ __pycache__/ *.pyo *.pyd .Python +*.egg-info/ env/ venv/ .venv/ +uv.lock # Environment Variables .env diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..64f2d9f --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,58 @@ +[project] +name = "edit-banana" +version = "0.1.0" +description = "Universal Content Re-Editor: Make the Uneditable, Editable" +readme = "README.md" +license = "Apache-2.0" +requires-python = ">=3.10" +dependencies = [ + "pyyaml", + "opencv-python-headless", + "numpy", + "Pillow", + "scikit-image", + "requests", + "fastapi", + "uvicorn[standard]", + "pytesseract", +] + +[project.optional-dependencies] +paddleocr = [ + "paddlepaddle==3.2.2", + "paddleocr", +] +formula = [ + "pix2text", + "onnxruntime", +] +rmbg = [ + "onnxruntime", +] +torch = [ + "torch>=2.0", + "torchvision", +] +sam3 = [ + "edit-banana[torch]", + "sam3", + "einops", + "pycocotools", + "psutil", +] + +[project.scripts] +edit-banana = "main:main" + +[tool.setuptools.packages.find] +include = ["modules*"] + +[tool.uv] +dev-dependencies = [] + +[tool.uv.sources] +sam3 = { path = "sam3_src", editable = true } +# On macOS Apple Silicon, use default PyPI torch (includes MPS support). +# For Linux+CUDA, override via: +# torch = { index = "pytorch-cu121" } +# torchvision = { index = "pytorch-cu121" } From d0d3745854f03892099c618885befe875629151c Mon Sep 17 00:00:00 2001 From: Micuks Date: Sat, 28 Mar 2026 23:31:53 +0800 Subject: [PATCH 2/3] feat: add Apple Silicon MPS device support for SAM3 Auto-detect MPS backend in SAM3Model when no device is specified (cuda > mps > cpu). Update config.yaml.example to document the "mps" option and the PYTORCH_ENABLE_MPS_FALLBACK=1 env var. Tested on M-series MacBook with PyTorch 2.11 + MPS backend. Upstream SAM3 MPS patches tracked in facebookresearch/sam3#173. --- config/config.yaml.example | 6 +++--- modules/sam3_info_extractor.py | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/config/config.yaml.example b/config/config.yaml.example index 3b62b60..a9469b2 100644 --- a/config/config.yaml.example +++ b/config/config.yaml.example @@ -3,9 +3,9 @@ sam3: checkpoint_path: "models/sam3_ms/sam3.pt" bpe_path: "models/bpe_simple_vocab_16e6.txt.gz" - # device: "cuda" | "cpu" | leave empty for auto (cuda if available) - # Set "cpu" if GPU does not match current PyTorch - # device: "cpu" + # device: "cuda" | "mps" | "cpu" | leave empty for auto (cuda > mps > cpu) + # Apple Silicon: use "mps" (requires PYTORCH_ENABLE_MPS_FALLBACK=1) + # device: "mps" score_threshold: 0.5 epsilon_factor: 0.02 min_area: 100 diff --git a/modules/sam3_info_extractor.py b/modules/sam3_info_extractor.py index 00b7b28..664524c 100644 --- a/modules/sam3_info_extractor.py +++ b/modules/sam3_info_extractor.py @@ -218,7 +218,14 @@ def __init__(self, checkpoint_path: str, bpe_path: str, device: str = None): super().__init__() self.checkpoint_path = checkpoint_path self.bpe_path = bpe_path - self.device = device or ("cuda" if torch.cuda.is_available() else "cpu") + if device: + self.device = device + elif torch.cuda.is_available(): + self.device = "cuda" + elif torch.backends.mps.is_available(): + self.device = "mps" + else: + self.device = "cpu" self._processor = None # 图像状态缓存 From 1aabf62f20f2a2e879ddefc8fbcf6d02719a1b06 Mon Sep 17 00:00:00 2001 From: Micuks Date: Sun, 29 Mar 2026 00:40:46 +0800 Subject: [PATCH 3/3] fix: auto-set PYTORCH_ENABLE_MPS_FALLBACK before torch import MPS backend lacks a few ops (e.g. aten::_assert_async). Setting PYTORCH_ENABLE_MPS_FALLBACK=1 in main.py before any torch import lets PyTorch silently fall back to CPU for unsupported ops, so users don't need to remember the env var. --- main.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/main.py b/main.py index 28d1e1b..89c69f9 100644 --- a/main.py +++ b/main.py @@ -24,6 +24,9 @@ # Skip PaddleX model host connectivity check to avoid startup delay os.environ.setdefault("PADDLE_PDX_DISABLE_MODEL_SOURCE_CHECK", "True") +# MPS (Apple Silicon) lacks a few ops; let PyTorch fall back to CPU for those. +# Must be set before `import torch`. +os.environ.setdefault("PYTORCH_ENABLE_MPS_FALLBACK", "1") # Suppress requests urllib3/chardet version warning warnings.filterwarnings("ignore", message=".*doesn't match a supported version.*")