Download modal/tools_org.py from arcacolab/data: direct link, hf CLI and curl.
- Browser
- Download file 14.6 kB
-
https://huggingface.co/arcacolab/data/resolve/main/modal/tools_org.py
- Command line
-
hf download hf://arcacolab/data/modal/tools_org.py
-
curl -L -o tools_org.py https://huggingface.co/arcacolab/data/resolve/main/modal/tools_org.py
14.6 kB
| """ | |
| ComfyUI Tools | |
| - Installs the small set of utilities used by this notebook. | |
| - Registers the IPython line magic: %download URL [filename] | |
| Usage: | |
| hf_token = "" | |
| civitai_token = "" | |
| %cd /content/ComfyUI/models/diffusion_models | |
| %download https://huggingface.co/owner/repo/resolve/main/model.safetensors | |
| """ | |
| from __future__ import annotations | |
| import os | |
| import platform | |
| import re | |
| import shutil | |
| import subprocess | |
| import sys | |
| import urllib.parse | |
| from pathlib import Path | |
| # --------------------------------------------------------------------- | |
| # Installation settings | |
| # --------------------------------------------------------------------- | |
| CONTENT_DIR = Path("/content") | |
| COMFYUI_DIR = CONTENT_DIR / "ComfyUI" | |
| COMFYUI_REPO = "https://github.com/Comfy-Org/ComfyUI.git" | |
| # ์ฌ๊ธฐ์ ์ปค์คํ ๋ ธ๋ Git ์ ์ฅ์ ์ฃผ์๋ฅผ ์ถ๊ฐํ๋ฉด ๋ผ. | |
| CUSTOM_NODES: list[str] = [ | |
| "https://github.com/Comfy-Org/ComfyUI-Manager.git", | |
| "https://github.com/pythongosssss/ComfyUI-Custom-Scripts.git", | |
| "https://github.com/bedovyy/chibi-client", | |
| "https://github.com/cosmicbuffalo/comfyui-mobile-frontend.git", | |
| "https://github.com/rgthree/rgthree-comfy.git", | |
| "https://github.com/ruwwww/comfyui-spectrum-sdxl", | |
| "https://github.com/kijai/ComfyUI-KJNodes.git", | |
| #"https://github.com/BobJohnson24/ComfyUI-INT8-Fast.git", | |
| "https://github.com/Anzhc/anzhc-qwen2d-comfyui.git", | |
| "https://github.com/arcacolab/honey_client.git", | |
| "https://github.com/kohya-ss/ComfyUI-Anima-LLLite.git", | |
| #"https://github.com/ClownsharkBatwing/RES4LYF.git", ์ด์๋ผ ์ ์ค๋ฅ๋์ง?? | |
| "https://github.com/ssitu/ComfyUI_UltimateSDUpscale", | |
| "https://github.com/namemechan/ComfyUI-DCW.git", | |
| "https://github.com/ElectronGirl/ComfyUI-Anima-Ref-Adapter.git", | |
| "https://github.com/arcacolab/snapref", | |
| "https://github.com/iljung1106/comfyui-anima-safe-pag.git", | |
| "https://github.com/crystian/ComfyUI-Crystools.git", | |
| "https://github.com/AlekPet/ComfyUI_Custom_Nodes_AlekPet.git", | |
| "https://github.com/pamparamm/ComfyUI-ppm.git", | |
| "https://github.com/ltdrdata/was-node-suite-comfyui.git", | |
| "https://github.com/sorryhyun/ComfyUI-Spectrum-KSampler.git", | |
| "https://github.com/1lch2/ComfyUI-AnimaPromptFormatter.git", | |
| # "https://github.com/owner/custom-node.git", | |
| ] | |
| # --------------------------------------------------------------------- | |
| # Output helpers | |
| # --------------------------------------------------------------------- | |
| def _status(message: str) -> None: | |
| print(f" {message}", flush=True) | |
| def _run_quiet(command: list[str], *, label: str) -> None: | |
| """Run an installer command quietly, but show its log if it fails.""" | |
| result = subprocess.run( | |
| command, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.STDOUT, | |
| text=True, | |
| ) | |
| if result.returncode != 0: | |
| _status(f"โ {label} ์คํจ (์ข ๋ฃ ์ฝ๋: {result.returncode})") | |
| if result.stdout: | |
| print(result.stdout.rstrip()) | |
| raise RuntimeError(f"{label} installation failed") | |
| # --------------------------------------------------------------------- | |
| # Environment setup | |
| # --------------------------------------------------------------------- | |
| def _install_system_packages() -> None: | |
| """Ensure aria2, curl, and git are available.""" | |
| required = { | |
| "aria2c": "aria2", | |
| "curl": "curl", | |
| "git": "git", | |
| } | |
| missing = [package for executable, package in required.items() if not shutil.which(executable)] | |
| if not missing: | |
| return | |
| if not shutil.which("apt-get"): | |
| raise RuntimeError( | |
| "ํ์ ์์คํ ๋๊ตฌ๊ฐ ์์ด: " + ", ".join(missing) | |
| + " (apt-get๋ ์ฌ์ฉํ ์ ์์ด.)" | |
| ) | |
| _status("aria2, curl, git ์ค์น ์ค...") | |
| _run_quiet(["apt-get", "update", "-qq"], label="apt ํจํค์ง ๋ชฉ๋ก ๊ฐฑ์ ") | |
| _run_quiet( | |
| ["apt-get", "install", "-y", "-qq", *missing], | |
| label="์์คํ ๋๊ตฌ ์ค์น", | |
| ) | |
| still_missing = [name for name in required if not shutil.which(name)] | |
| if still_missing: | |
| raise RuntimeError("์ค์น ํ์๋ ์คํ ํ์ผ์ ์ฐพ์ ์ ์์ด: " + ", ".join(still_missing)) | |
| def _install_cloudflared() -> None: | |
| if shutil.which("cloudflared"): | |
| return | |
| if not shutil.which("apt-get"): | |
| raise RuntimeError( | |
| "cloudflared๊ฐ ์๊ณ apt-get๋ ์ฌ์ฉํ ์ ์์ด. " | |
| "cloudflared๋ฅผ ์ด๋ฏธ์ง์ ๋ฏธ๋ฆฌ ์ค์นํด์ผ ํด." | |
| ) | |
| machine = platform.machine().lower() | |
| arch_map = { | |
| "x86_64": "amd64", | |
| "amd64": "amd64", | |
| "aarch64": "arm64", | |
| "arm64": "arm64", | |
| } | |
| arch = arch_map.get(machine) | |
| if not arch: | |
| raise RuntimeError(f"์ง์ํ์ง ์๋ CPU ์ํคํ ์ฒ์ผ: {machine}") | |
| package_url = ( | |
| "https://github.com/cloudflare/cloudflared/releases/latest/download/" | |
| f"cloudflared-linux-{arch}.deb" | |
| ) | |
| package_path = Path("/tmp/cloudflared.deb") | |
| _status("cloudflared ์ค์น ์ค...") | |
| _run_quiet( | |
| ["curl", "-fsSL", package_url, "-o", str(package_path)], | |
| label="cloudflared ๋ค์ด๋ก๋", | |
| ) | |
| _run_quiet( | |
| ["dpkg", "-i", str(package_path)], | |
| label="cloudflared ์ค์น", | |
| ) | |
| package_path.unlink(missing_ok=True) | |
| if not shutil.which("cloudflared"): | |
| raise RuntimeError("cloudflared ์ค์น ํ ์คํ ํ์ผ์ ์ฐพ์ ์ ์์ด.") | |
| def _install_python_packages() -> None: | |
| packages = ["requests", "huggingface_hub"] | |
| _status("Python ํจํค์ง ํ์ธ ์ค...") | |
| _run_quiet( | |
| [sys.executable, "-m", "pip", "install", "-q", *packages], | |
| label="Python ํจํค์ง ์ค์น", | |
| ) | |
| def _run_git_clone(repo_url: str, destination: Path, *, label: str) -> None: | |
| if destination.exists(): | |
| if (destination / ".git").is_dir(): | |
| _status(f"โช {label} ์ด๋ฏธ ์กด์ฌํจ. Clone ์๋ต.") | |
| return | |
| raise RuntimeError( | |
| f"{label} ๊ฒฝ๋ก๊ฐ ์ด๋ฏธ ์กด์ฌํ์ง๋ง Git ์ ์ฅ์๊ฐ ์๋์ผ: {destination}" | |
| ) | |
| _status(f"{label} Clone ์ค...") | |
| _run_quiet( | |
| ["git", "clone", "--depth", "1", repo_url, str(destination)], | |
| label=f"{label} Clone", | |
| ) | |
| def _install_requirements(requirements_file: Path, *, label: str) -> None: | |
| if not requirements_file.is_file(): | |
| _status(f"โช {label}: requirements.txt ์์. ์๋ต.") | |
| return | |
| _status(f"{label} ์์กด์ฑ ์ค์น ์ค...") | |
| _run_quiet( | |
| [sys.executable, "-m", "pip", "install", "-q", "-r", str(requirements_file)], | |
| label=f"{label} ์์กด์ฑ ์ค์น", | |
| ) | |
| def _install_comfyui() -> None: | |
| # /content ๋๋ ํฐ๋ฆฌ๋ง ์์ฑํ๊ณ , ComfyUI ๋๋ ํฐ๋ฆฌ๋ git clone์ด ์์ฑํ๊ฒ ๋๋ค. | |
| CONTENT_DIR.mkdir(parents=True, exist_ok=True) | |
| _run_git_clone(COMFYUI_REPO, COMFYUI_DIR, label="ComfyUI") | |
| if not (COMFYUI_DIR / "main.py").is_file(): | |
| raise RuntimeError(f"ComfyUI main.py๋ฅผ ์ฐพ์ ์ ์์ด: {COMFYUI_DIR}") | |
| _install_requirements(COMFYUI_DIR / "requirements.txt", label="ComfyUI") | |
| def _install_custom_nodes() -> None: | |
| if not CUSTOM_NODES: | |
| _status("์ปค์คํ ๋ ธ๋ ๋ชฉ๋ก์ด ๋น์ด ์์ด. Clone ์๋ต.") | |
| return | |
| custom_nodes_dir = COMFYUI_DIR / "custom_nodes" | |
| custom_nodes_dir.mkdir(parents=True, exist_ok=True) | |
| for repo_url in CUSTOM_NODES: | |
| repo_url = repo_url.strip() | |
| if not repo_url: | |
| continue | |
| repo_name = repo_url.rstrip("/").rsplit("/", 1)[-1] | |
| if repo_name.endswith(".git"): | |
| repo_name = repo_name[:-4] | |
| if not repo_name: | |
| raise RuntimeError(f"์ฌ๋ฐ๋ฅด์ง ์์ ์ปค์คํ ๋ ธ๋ ์ ์ฅ์ ์ฃผ์์ผ: {repo_url}") | |
| node_dir = custom_nodes_dir / repo_name | |
| _run_git_clone(repo_url, node_dir, label=f"์ปค์คํ ๋ ธ๋ {repo_name}") | |
| _install_requirements(node_dir / "requirements.txt", label=repo_name) | |
| def setup() -> None: | |
| _status("ํ์ ๋๊ตฌ ํ์ธ ์ค...") | |
| _install_system_packages() | |
| _install_cloudflared() | |
| _install_python_packages() | |
| _status("ComfyUI ์ค์น ์ค...") | |
| _install_comfyui() | |
| _install_custom_nodes() | |
| _status("ํ๊ฒฝ ๊ตฌ์ถ ์๋ฃ.") | |
| # --------------------------------------------------------------------- | |
| # Download helpers | |
| # --------------------------------------------------------------------- | |
| def _get_notebook_token(name: str) -> str: | |
| """Read a token from the active IPython user namespace.""" | |
| try: | |
| from IPython import get_ipython | |
| shell = get_ipython() | |
| if shell is not None: | |
| value = shell.user_ns.get(name, "") | |
| return str(value).strip() if value else "" | |
| except Exception: | |
| pass | |
| return "" | |
| def _build_download_command(url: str, filename: str | None) -> list[str]: | |
| parsed = urllib.parse.urlparse(url) | |
| host = (parsed.hostname or "").lower() | |
| headers: list[str] = [] | |
| if host == "huggingface.co" or host.endswith(".huggingface.co"): | |
| token = _get_notebook_token("hf_token") | |
| if token: | |
| headers.append(f"Authorization: Bearer {token}") | |
| elif host == "civitai.com" or host.endswith(".civitai.com") or host == "civitai.red" or host.endswith(".civitai.red"): | |
| token = _get_notebook_token("civitai_token") | |
| if token and "/api/download/models/" in parsed.path: | |
| # Civitai's direct model-download endpoint accepts token as a query parameter. | |
| query = urllib.parse.parse_qsl(parsed.query, keep_blank_values=True) | |
| if not any(key.lower() == "token" for key, _ in query): | |
| query.append(("token", token)) | |
| url = urllib.parse.urlunparse( | |
| parsed._replace(query=urllib.parse.urlencode(query)) | |
| ) | |
| command = [ | |
| "aria2c", | |
| "--continue=true", | |
| "--allow-overwrite=true", | |
| "--auto-file-renaming=false", | |
| "--max-tries=5", | |
| "--retry-wait=3", | |
| "--connect-timeout=30", | |
| "--timeout=60", | |
| "--console-log-level=notice", | |
| "--summary-interval=1", | |
| "--download-result=hide", | |
| "-x", "8", | |
| "-s", "8", | |
| "-k", "1M", | |
| ] | |
| for header in headers: | |
| command.extend(["--header", header]) | |
| if filename: | |
| command.extend(["--out", filename]) | |
| command.append(url) | |
| return command | |
| def _download(url: str, filename: str | None = None) -> None: | |
| url = url.strip() | |
| if not url: | |
| _status("โ ๏ธ ๋ค์ด๋ก๋ ์ฃผ์๊ฐ ๋น์ด ์์ด.") | |
| return | |
| parsed = urllib.parse.urlparse(url) | |
| if parsed.scheme not in ("http", "https") or not parsed.netloc: | |
| _status("โ ์ฌ๋ฐ๋ฅธ HTTP/HTTPS ์ฃผ์๊ฐ ์๋์ผ.") | |
| return | |
| if not shutil.which("aria2c"): | |
| _status("โ aria2c๊ฐ ์์ด. ๋จผ์ tools.py ์ค์น ์ ์ ์คํํด ์ค.") | |
| return | |
| if filename: | |
| filename = Path(filename).name | |
| else: | |
| filename = Path(urllib.parse.unquote(parsed.path)).name or "downloaded_file" | |
| command = _build_download_command(url, filename) | |
| _status(f"โฌ๏ธ {filename} ๋ค์ด๋ก๋ ์ค...") | |
| try: | |
| process = subprocess.Popen( | |
| command, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.STDOUT, | |
| text=True, | |
| bufsize=1, | |
| ) | |
| last_progress = "" | |
| assert process.stdout is not None | |
| for raw_line in process.stdout: | |
| line = raw_line.strip() | |
| if not line: | |
| continue | |
| # aria2 progress lines usually begin with [#...]. | |
| if line.startswith("[#"): | |
| progress = re.sub(r"\s+", " ", line) | |
| if progress != last_progress: | |
| print(f"\r โฌ๏ธ {filename} {progress[:180]}", end="", flush=True) | |
| last_progress = progress | |
| return_code = process.wait() | |
| if return_code == 0: | |
| print(f"\r โ {filename} ๋ค์ด๋ก๋ ์๋ฃ.{' ' * 30}") | |
| else: | |
| print() | |
| _status(f"โ {filename} ๋ค์ด๋ก๋ ์คํจ (aria2 ์ข ๋ฃ ์ฝ๋: {return_code})") | |
| except KeyboardInterrupt: | |
| process.terminate() | |
| process.wait() | |
| print() | |
| _status("๋ค์ด๋ก๋ ์ทจ์๋จ.") | |
| except Exception as exc: | |
| print() | |
| _status(f"โ ๋ค์ด๋ก๋ ์ค ์ค๋ฅ ๋ฐ์: {exc}") | |
| # --------------------------------------------------------------------- | |
| # IPython magic registration | |
| # --------------------------------------------------------------------- | |
| def register_download_magic() -> None: | |
| from IPython import get_ipython | |
| shell = get_ipython() | |
| if shell is None: | |
| raise RuntimeError("์ด ํ์ผ์ Jupyter/IPython ์ปค๋์์ ์คํํด์ผ ํด.") | |
| def download_magic(line: str) -> None: | |
| # shlex handles quoted URLs/filenames and keeps the command simple. | |
| import shlex | |
| try: | |
| args = shlex.split(line) | |
| except ValueError as exc: | |
| _status(f"โ ๋ช ๋ น์ด ํ์ ์ค๋ฅ: {exc}") | |
| return | |
| if not args: | |
| _status("์ฌ์ฉ๋ฒ: %download URL [filename]") | |
| return | |
| if len(args) > 2: | |
| _status("์ฌ์ฉ๋ฒ: %download URL [filename]") | |
| return | |
| _download(args[0], args[1] if len(args) == 2 else None) | |
| shell.register_magic_function(download_magic, "line", "download") | |
| # --------------------------------------------------------------------- | |
| # Run setup and register the magic when this file is executed with %run. | |
| # --------------------------------------------------------------------- | |
| try: | |
| setup() | |
| register_download_magic() | |
| except Exception as error: | |
| print(f"\nโ tools.py ์ด๊ธฐํ ์คํจ: {error}") | |
| raise | |
| # --------------------------------------------------------------------- | |
| # ๊ธฐ์ด์ ์ธ ์ํฌํ๋ก์ฐ ๋ค์ด๋ก๋ ์ํ๋ํธ | |
| # --------------------------------------------------------------------- | |
| import os, urllib.request as r | |
| os.makedirs("/content/ComfyUI/user/default/workflows", exist_ok=True) | |
| r.urlretrieve("https://huggingface.co/arcacolab/data/resolve/main/modal/workflows/anima_inpaint_lllite_v3_m.json", "/content/ComfyUI/user/default/workflows/anima_inpaint_lllite_v3_m.json") | |
| r.urlretrieve("https://huggingface.co/arcacolab/data/resolve/main/modal/workflows/anima_simple_m.json", "/content/ComfyUI/user/default/workflows/anima_simple_m.json") | |
| r.urlretrieve("https://huggingface.co/arcacolab/data/resolve/main/modal/workflows/sanpref_simple_m.json", "/content/ComfyUI/user/default/workflows/sanpref_simple_m.json") |