Browse Source

Split depedencies (#471)

Daniel Gatis 2 years ago
parent
commit
bcb45a27ea

+ 2 - 3
.github/workflows/lint_python.yml

@@ -10,9 +10,8 @@ jobs:
             - uses: actions/setup-python@v4
               with:
                 python-version: 3.11
-            - run: pip install --upgrade pip wheel
-            - run: pip install --upgrade setuptools
-            - run: pip install bandit black flake8 flake8-bugbear flake8-comprehensions isort safety mypy
+            - name: Install dependencies
+              run: pip install .[cli,dev]
             - run: mypy --install-types --non-interactive --ignore-missing-imports ./rembg
             - run: bandit --recursive --skip B101,B104,B310,B311,B303,B110 --exclude ./rembg/_version.py ./rembg
             - run: black --force-exclude rembg/_version.py --check --diff ./rembg

+ 3 - 5
.github/workflows/publish_pypi.yml

@@ -13,11 +13,9 @@ jobs:
             - uses: actions/setup-python@v4
               with:
                 python-version: 3.11
-            - name: "Installs dependencies"
-              run: |
-                  python3 -m pip install --upgrade pip
-                  python3 -m pip install setuptools wheel twine
-            - name: "Builds and uploads to PyPI"
+            - name: Install dependencies
+              run: pip install .[cli,dev]
+            - name: Builds and uploads to PyPI
               run: |
                   python3 setup.py sdist bdist_wheel
                   python3 -m twine upload dist/*

+ 2 - 5
.github/workflows/test-install.yml

@@ -15,11 +15,8 @@ jobs:
               uses: actions/setup-python@v4
               with:
                   python-version: ${{ matrix.python-version }}
-            - name: Install package
-              run: |
-                  python -m pip install --upgrade pip
-                  pip install .
-                  pip install pytest
+            - name: Install dependencies
+              run: pip install .[cli,dev]
             - name: Test installation with pytest
               run: |
                   pytest

+ 0 - 25
.github/workflows/test-specific-environment.yml

@@ -1,25 +0,0 @@
-name: Test in specific environment
-
-on: [push]
-
-jobs:
-    build:
-        runs-on: ubuntu-latest
-        strategy:
-            matrix:
-                python-version: ["3.8", "3.9", "3.10", "3.11"]
-
-        steps:
-            - uses: actions/checkout@v3
-            - name: Set up Python ${{ matrix.python-version }}
-              uses: actions/setup-python@v4
-              with:
-                  python-version: ${{ matrix.python-version }}
-            - name: Set up environment
-              run: |
-                  python -m pip install --upgrade pip
-                  pip install pytest
-                  pip install -r requirements.txt
-            - name: Test with pytest
-              run: |
-                  PYTHONPATH=$PYTHONPATH:. pytest .

+ 4 - 2
README.md

@@ -80,7 +80,8 @@ python: >3.7, <3.12
 CPU support:
 
 ```bash
-pip install rembg
+pip install rembg # for library
+pip install rembg[cli] # for library + cli
 ```
 
 GPU support:
@@ -96,7 +97,8 @@ Go to https://onnxruntime.ai and check the installation matrix.
 If yes, just run:
 
 ```bash
-pip install rembg[gpu]
+pip install rembg[gpu] # for library
+pip install rembg[gpu,cli] # for library + cli
 ```
 
 ## Usage as a cli

+ 28 - 9
rembg/cli.py

@@ -1,14 +1,33 @@
-import click
+import pkg_resources
 
-from . import _version
-from .commands import command_functions
 
-
[email protected]()
[email protected]_option(version=_version.get_versions()["version"])
 def main() -> None:
-    pass
+    package_distribution = pkg_resources.get_distribution("rembg")
+
+    for extra in package_distribution.extras:
+        if extra == "cli":
+            requirements = package_distribution.requires(extras=(extra,))
+            for requirement in requirements:
+                try:
+                    pkg_resources.require(requirement.project_name)
+                except pkg_resources.DistributionNotFound:
+                    print(f"Missing dependency: '{requirement.project_name}'")
+                    print(
+                        "Please, install rembg with the cli feature: pip install rembg[cli]"
+                    )
+                    exit(1)
+
+    import click
+
+    from . import _version
+    from .commands import command_functions
+
+    @click.group()
+    @click.version_option(version=_version.get_versions()["version"])
+    def _main() -> None:
+        pass
 
+    for command in command_functions:
+        _main.add_command(command)
 
-for command in command_functions:
-    main.add_command(command)
+    _main()

+ 0 - 1
requirements-gpu.txt

@@ -1 +0,0 @@
-onnxruntime-gpu

+ 0 - 19
requirements.txt

@@ -1,19 +0,0 @@
-aiohttp
-asyncer
-click
-fastapi
-filetype
-gradio
-imagehash
-numpy
-onnxruntime
-opencv-python-headless
-pillow
-pooch
-pymatting
-python-multipart
-scikit-image
-scipy
-tqdm
-uvicorn
-watchdog

+ 50 - 30
setup.py

@@ -11,6 +11,52 @@ here = pathlib.Path(__file__).parent.resolve()
 
 long_description = (here / "README.md").read_text(encoding="utf-8")
 
+install_requires = [
+    "numpy",
+    "onnxruntime",
+    "opencv-python-headless",
+    "pillow",
+    "pooch",
+    "pymatting",
+    "scikit-image",
+    "scipy",
+]
+
+extras_require = {
+    "dev": [
+        "bandit",
+        "black",
+        "flake8",
+        "imagehash",
+        "isort",
+        "mypy",
+        "pytest",
+        "setuptools",
+        "twine",
+        "wheel",
+    ],
+    "gpu": ["onnxruntime-gpu"],
+    "cli": [
+        "aiohttp",
+        "asyncer",
+        "click",
+        "fastapi",
+        "filetype",
+        "gradio",
+        "python-multipart",
+        "tqdm",
+        "uvicorn",
+        "watchdog",
+    ],
+}
+
+entry_points = {
+    "console_scripts": [
+        "rembg=rembg.cli:main",
+    ],
+}
+
+
 setup(
     name="rembg",
     description="Remove image background",
@@ -35,37 +81,11 @@ setup(
         "Programming Language :: Python :: 3.11",
     ],
     keywords="remove, background, u2net",
-    packages=["rembg", "rembg.sessions", "rembg.commands"],
     python_requires=">=3.8, <3.12",
-    install_requires=[
-        "aiohttp",
-        "asyncer",
-        "click",
-        "fastapi",
-        "filetype",
-        "gradio",
-        "imagehash",
-        "numpy",
-        "onnxruntime",
-        "opencv-python-headless",
-        "pillow",
-        "pooch",
-        "pymatting",
-        "python-multipart",
-        "scikit-image",
-        "scipy",
-        "tqdm",
-        "uvicorn",
-        "watchdog",
-    ],
-    entry_points={
-        "console_scripts": [
-            "rembg=rembg.cli:main",
-        ],
-    },
-    extras_require={
-        "gpu": ["onnxruntime-gpu"],
-    },
+    packages=find_packages(),
+    install_requires=install_requires,
+    entry_points=entry_points,
+    extras_require=extras_require,
     version=versioneer.get_version(),
     cmdclass=versioneer.get_cmdclass(),
 )