Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rembg: add #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ The main outputs of the `flake.nix` at the moment are as follows:

![invokeai](/../images/invokeai.webp)

#### Rembg ( Automatic Background Remover )

- `nix run .#rembg-cpu -- i input.png output.png`
- `nix run .#rembg-cpu -- s --port 9226`

![rembg UI](/../images/rembg.webp)

## Install NixOS-WSL in Windows

If you're interested in running nixified.ai in the Windows Subsystem for Linux, you'll need to enable the WSL and then install NixOS-WSL via it. We provide a script that will do everything for you.
Expand Down
20 changes: 19 additions & 1 deletion flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
url = "github:koboldai/koboldai-client/1.19.2";
flake = false;
};
rembg-src = {
url = "github:danielgatis/rembg/v2.0.50";
flake = false;
};
flake-parts = {
url = "github:hercules-ci/flake-parts";
inputs.nixpkgs-lib.follows = "nixpkgs";
Expand All @@ -38,6 +42,7 @@
./modules/aipython3
./projects/invokeai
./projects/koboldai
./projects/rembg
./website
];
};
Expand Down
5 changes: 5 additions & 0 deletions modules/aipython3/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
overlays.torchRocm
];

aipython3-cpu = mkPythonPackages [
overlays.fixPackages
overlays.extraDeps
];

aipython3-nvidia = mkPythonPackages [
overlays.fixPackages
overlays.extraDeps
Expand Down
2 changes: 2 additions & 0 deletions modules/aipython3/overlays.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ pkgs: {
opencv-python-headless = final.opencv-python;
opencv-python = final.opencv4;

asyncer = callPackage ../../packages/asyncer { };
pymatting = callPackage ../../packages/pymatting { };
safetensors = callPackage ../../packages/safetensors { };
compel = callPackage ../../packages/compel { };
apispec-webframeworks = callPackage ../../packages/apispec-webframeworks { };
Expand Down
25 changes: 25 additions & 0 deletions packages/asyncer/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# WARNING: This file was automatically generated. You should avoid editing it.
# If you run pynixify again, the file will be either overwritten or
# deleted, and you will lose the changes you made to it.

{ anyio, buildPythonPackage, fetchPypi, lib }:

buildPythonPackage rec {
pname = "asyncer";
version = "0.0.2";

src = fetchPypi {
inherit pname version;
sha256 = "0czlyysclcs1lyqh5j3a3ak05jb1jys5sfdldgqbmsr66rgwhinm";
};

propagatedBuildInputs = [ anyio ];

# TODO FIXME
doCheck = false;

meta = with lib; {
description = "Asyncer, async and await, focused on developer experience.";
homepage = "https://github.com/tiangolo/asyncer";
};
}
26 changes: 26 additions & 0 deletions packages/pymatting/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# WARNING: This file was automatically generated. You should avoid editing it.
# If you run pynixify again, the file will be either overwritten or
# deleted, and you will lose the changes you made to it.

{ buildPythonPackage, fetchPypi, lib, numba, numpy, pillow, scipy }:

buildPythonPackage rec {
pname = "pymatting";
version = "1.1.8";

src = fetchPypi {
inherit version;
pname = "PyMatting";
sha256 = "1k1aacqrpx03vh7d37vq9m40j6hmcfyf716jvwqni6bl13phhdd7";
};

propagatedBuildInputs = [ numpy pillow numba scipy ];

# TODO FIXME
doCheck = false;

meta = with lib; {
description = "Python package for alpha matting.";
homepage = "https://pymatting.github.io";
};
}
37 changes: 37 additions & 0 deletions projects/rembg/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ config, inputs, lib, withSystem, ... }:

{
perSystem = { config, pkgs, ... }:
let
inherit (config.dependencySets) aipython3-cpu;

src = inputs.rembg-src;

mkInvokeAIVariant = args: pkgs.callPackage ./package.nix ({ inherit src; } // args);
in
{
packages = {
rembg-cpu = mkInvokeAIVariant {
aipython3 = aipython3-cpu;
};
};
};

flake.nixosModules =
let
packageModule = pkgAttrName: { pkgs, ... }: {
services.rembg.package = withSystem pkgs.system (
{ config, ... }: lib.mkOptionDefault config.packages.${pkgAttrName}
);
};
in
{
rembg = ./nixos;
rembg-cpu = {
imports = [
config.flake.nixosModules.rembg
(packageModule "rembg-cpu")
];
};
};
}
56 changes: 56 additions & 0 deletions projects/rembg/package.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{ aipython3
, callPackage
, src
}:
let
abseil-onnx = callPackage ./packages/abseil-onnx { };
microsoft-gsl = callPackage ./packages/microsoft-gsl { };
onnx-custom = callPackage ./packages/onnx { inherit aipython3; };
onnxruntime = callPackage ./packages/onnxruntime { inherit aipython3 microsoft-gsl onnx-custom; abseil-cpp = abseil-onnx; };
onnxruntime-pyt = callPackage ./packages/onnxruntime-python
{ inherit aipython3 onnxruntime; };
in

aipython3.buildPythonPackage {
pname = "rembg";
format = "pyproject";
version = "2.0.50";
inherit src;
propagatedBuildInputs = with aipython3; [
numpy
onnxruntime-pyt
opencv4
pillow
pooch
pymatting
scikit-image
scipy
setuptools
tqdm

# {{{ cli deps
aiohttp
asyncer
click
fastapi
filetype
gradio
python-multipart
uvicorn
watchdog
# }}}
];
nativeBuildInputs = [ aipython3.pythonRelaxDepsHook ];
pythonRemoveDeps = [ "clip" "pyreadline3" "flaskwebgui" "opencv-python" ];
pythonRelaxDeps = [ "dnspython" "protobuf" "flask" "flask-socketio" "pytorch-lightning" ];
postFixup = ''
chmod +x $out/bin/*
wrapPythonPrograms
'';
doCheck = false;
meta = {
description = "tool to remove image backgrounds";
homepage = "https://github.com/danielgatis/rembg/";
mainProgram = "rembg";
};
}
35 changes: 35 additions & 0 deletions projects/rembg/packages/abseil-onnx/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{ lib
, stdenv
, fetchFromGitHub
, cmake
, static ? stdenv.hostPlatform.isStatic
, cxxStandard ? null
}:

stdenv.mkDerivation (finalAttrs: {
pname = "abseil-cpp";
version = "20230125.3";

src = fetchFromGitHub {
owner = "abseil";
repo = "abseil-cpp";
rev = "refs/tags/${finalAttrs.version}";
hash = "sha256-PLoI7ix+reUqkZ947kWzls8lujYqWXk9A9a55UcfahI=";
};

cmakeFlags = [
"-DBUILD_SHARED_LIBS=${if static then "OFF" else "ON"}"
] ++ lib.optionals (cxxStandard != null) [
"-DCMAKE_CXX_STANDARD=${cxxStandard}"
];

nativeBuildInputs = [ cmake ];

meta = with lib; {
description = "An open-source collection of C++ code designed to augment the C++ standard library";
homepage = "https://abseil.io/";
license = licenses.asl20;
platforms = platforms.all;
maintainers = [ maintainers.andersk ];
};
})
37 changes: 37 additions & 0 deletions projects/rembg/packages/microsoft-gsl/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{ lib
, stdenv
, fetchFromGitHub
, cmake
, gtest
, pkg-config
}:

stdenv.mkDerivation rec {
pname = "microsoft-gsl";
version = "4.0.0";

src = fetchFromGitHub {
owner = "Microsoft";
repo = "GSL";
rev = "v${version}";
hash = "sha256-cXDFqt2KgMFGfdh6NGE+JmP4R0Wm9LNHM0eIblYe6zU=";
};

nativeBuildInputs = [ cmake pkg-config ];
buildInputs = [ gtest ];

doCheck = true;

meta = with lib; {
description = "C++ Core Guideline support library";
longDescription = ''
The Guideline Support Library (GSL) contains functions and types that are suggested for
use by the C++ Core Guidelines maintained by the Standard C++ Foundation.
This package contains Microsoft's implementation of GSL.
'';
homepage = "https://github.com/Microsoft/GSL";
license = licenses.mit;
platforms = platforms.all;
maintainers = with maintainers; [ thoughtpolice yuriaisaka ];
};
}
Loading