Skip to content

Commit

Permalink
added standalone cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
telamonian committed Aug 23, 2024
1 parent cc7e6a3 commit 966bc6b
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 8 deletions.
104 changes: 104 additions & 0 deletions comfy_cli/cmdline.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from comfy_cli.config_manager import ConfigManager
from comfy_cli.constants import GPU_OPTION, CUDAVersion
from comfy_cli.env_checker import EnvChecker
from comfy_cli.standalone import StandalonePython
from comfy_cli.update import check_for_updates
from comfy_cli.workspace_manager import WorkspaceManager, check_comfy_repo

Expand Down Expand Up @@ -543,6 +544,109 @@ def feedback():
print("Thank you for your feedback!")


@app.command(help="Download a standalone Python interpreter and dependencies based on an existing comfyui workspace")
@tracking.track_command()
def standalone(
platform: Annotated[
Optional[constants.OS],
typer.Option(
show_default=False,
help="Create standalone Python for specified platform",
)] = None,
proc: Annotated[
Optional[constants.PROC],
typer.Option(
show_default=False,
help="Create standalone Python for specified processor",
)] = None,
nvidia: Annotated[
Optional[bool],
typer.Option(
show_default=False,
help="Create standalone Python for Nvidia gpu",
callback=g_gpu_exclusivity.validate,
),
] = None,
cuda_version: Annotated[CUDAVersion, typer.Option(show_default=True)] = CUDAVersion.v12_1,
amd: Annotated[
Optional[bool],
typer.Option(
show_default=False,
help="Create standalone Python for AMD gpu",
callback=g_gpu_exclusivity.validate,
),
] = None,
m_series: Annotated[
Optional[bool],
typer.Option(
show_default=False,
help="Create standalone Python for Mac M-Series gpu",
callback=g_gpu_exclusivity.validate,
),
] = None,
intel_arc: Annotated[
Optional[bool],
typer.Option(
hidden=True,
show_default=False,
help="(Beta support) Create standalone Python for Intel Arc gpu, based on https://github.com/comfyanonymous/ComfyUI/pull/3439",
callback=g_gpu_exclusivity.validate,
),
] = None,
cpu: Annotated[
Optional[bool],
typer.Option(
show_default=False,
help="Create standalone Python for CPU",
callback=g_gpu_exclusivity.validate,
),
] = None,
):
comfy_path, _ = workspace_manager.get_workspace_path()

platform = utils.get_os() if platform is None else platform
proc = utils.get_proc() if proc is None else proc

if cpu:
gpu = GPU_OPTION.CPU
elif nvidia:
gpu = GPU_OPTION.NVIDIA
elif amd:
gpu = GPU_OPTION.AMD
elif m_series:
gpu = GPU_OPTION.M_SERIES
elif intel_arc:
gpu = GPU_OPTION.INTEL_ARC
else:
if platform == constants.OS.MACOS:
gpu = ui.prompt_select_enum(
"What type of Mac do you have?",
[GPU_OPTION.M_SERIES, GPU_OPTION.MAC_INTEL],
)
else:
gpu = ui.prompt_select_enum(
"What GPU do you have?",
[GPU_OPTION.NVIDIA, GPU_OPTION.AMD, GPU_OPTION.INTEL_ARC, GPU_OPTION.CPU],
)

if gpu == GPU_OPTION.INTEL_ARC:
print("[bold yellow]Installing on Intel ARC is not yet completely supported[/bold yellow]")
env_check = env_checker.EnvChecker()
if env_check.conda_env is None:
print("[bold red]Intel ARC support requires conda environment to be activated.[/bold red]")
raise typer.Exit(code=1)
if intel_arc is None:
confirm_result = ui.prompt_confirm_action(
"Are you sure you want to try beta install feature on Intel ARC?", True
)
if not confirm_result:
raise typer.Exit(code=0)
print("[bold yellow]Installing on Intel ARC is in beta stage.[/bold yellow]")

sty = StandalonePython.FromDistro(platform=platform, proc=proc)
sty.precache_comfy_deps(comfyDir=comfy_path, gpu=gpu)
sty.to_tarball()

app.add_typer(models_command.app, name="model", help="Manage models.")
app.add_typer(custom_nodes.app, name="node", help="Manage custom nodes.")
app.add_typer(custom_nodes.manager_app, name="manager", help="Manage ComfyUI-Manager.")
Expand Down
7 changes: 4 additions & 3 deletions comfy_cli/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from enum import Enum


class OS(Enum):
class OS(str, Enum):
WINDOWS = "windows"
MACOS = "macos"
LINUX = "linux"

class PROC(Enum):
class PROC(str, Enum):
X86_64 = "x86_64"
ARM = "arm"

Expand Down Expand Up @@ -61,7 +61,8 @@ class CUDAVersion(str, Enum):
v11_8 = "11.8"


class GPU_OPTION(Enum):
class GPU_OPTION(str, Enum):
CPU = None
NVIDIA = "nvidia"
AMD = "amd"
INTEL_ARC = "intel_arc"
Expand Down
25 changes: 21 additions & 4 deletions comfy_cli/standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def __init__(self, rpath: PathLike):
self.bin = self.rpath / "bin"
self.executable = self.bin / "python"

# paths to store package artifacts
self.cache = self.rpath / "cache"
self.wheels = self.rpath / "wheels"

self.dep_comp = None

# upgrade pip if needed, install uv
Expand Down Expand Up @@ -123,18 +127,31 @@ def run_comfy_cli(self, *args: list[str]):
def install_comfy(self, *args: list[str], gpu_arg: str = "--nvidia"):
self.run_comfy_cli("--here", "--skip-prompt", "install", "--fast-deps", gpu_arg, *args)

def compile_comfy_deps(self, comfyDir: PathLike, gpu: str, outDir: PathLike = "."):
def compile_comfy_deps(self, comfyDir: PathLike, gpu: str, outDir: Optional[PathLike] = None):
outDir = self.rpath if outDir is None else outDir

self.dep_comp = DependencyCompiler(cwd=comfyDir, executable=self.executable, gpu=gpu, outDir=outDir)
self.dep_comp.compile_comfy_deps()

def install_comfy_deps(self, comfyDir: PathLike, gpu: str, outDir: PathLike = "."):
def install_comfy_deps(self, comfyDir: PathLike, gpu: str, outDir: Optional[PathLike] = None):
outDir = self.rpath if outDir is None else outDir

self.dep_comp = DependencyCompiler(cwd=comfyDir, executable=self.executable, gpu=gpu, outDir=outDir)
self.dep_comp.install_core_plus_ext()

def precache_comfy_deps(self, comfyDir: PathLike, gpu: str, outDir: PathLike = "."):
def precache_comfy_deps(self, comfyDir: PathLike, gpu: str, outDir: Optional[PathLike] = None):
outDir = self.rpath if outDir is None else outDir

self.dep_comp = DependencyCompiler(cwd=comfyDir, executable=self.executable, gpu=gpu, outDir=outDir)
self.dep_comp.precache_comfy_deps()

def wheel_comfy_deps(self, comfyDir: PathLike, gpu: str, outDir: PathLike = "."):
def wheel_comfy_deps(self, comfyDir: PathLike, gpu: str, outDir: Optional[PathLike] = None):
outDir = self.rpath if outDir is None else outDir

self.dep_comp = DependencyCompiler(cwd=comfyDir, executable=self.executable, gpu=gpu, outDir=outDir)
self.dep_comp.wheel_comfy_deps()

def to_tarball(self, outPath: Optional[PathLike] = None):
outPath = self.rpath.with_suffix(".tgz") if outPath is None else Path(outPath)
with tarfile.open(outPath, "w:gz") as tar:
tar.add(self.rpath, arcname=self.rpath.parent)
3 changes: 2 additions & 1 deletion comfy_cli/uv.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ def __init__(
):
self.cwd = Path(cwd).expanduser().resolve()
self.outDir = Path(outDir).expanduser().resolve()
self.executable = Path(executable).expanduser().resolve()
# use .absolute since .resolve breaks the softlink-is-interpreter assumption of venvs
self.executable = Path(executable).expanduser().absolute()
self.gpu = DependencyCompiler.Resolve_Gpu(gpu)
self.reqFiles = [Path(reqFile) for reqFile in reqFilesExt] if reqFilesExt is not None else None

Expand Down

0 comments on commit 966bc6b

Please sign in to comment.