StableDiffusion Modelのダウンロード

CivitAi

アカウント作って、設定からAPI Keyを作成

pip install requests

api_key:作成したAPI Key
model_version_id:下図の後半の部分(2741698)。並べれば一括ダウンロード可

path:ダウンロードするディレクトリ(output_path = f"{path}/{filename}"としているので末尾に/は入れない)

import requests
import time
import os

api_key = ""
model_version_id = [1234567, 7654321]

path = "/workspace/runpod-slim/ComfyUI/models/loras"

for vid in model_version_id:
    try:
        url = f"https://civitai.com/api/download/models/{vid}"
        headers = {"Authorization": f"Bearer {api_key}"}
        with requests.get(url, headers=headers, stream=True) as r:
            r.raise_for_status()
            cd = r.headers.get("content-disposition")
            if cd and "filename=" in cd:
                filename = cd.split("filename=")[-1].strip('"')
            else:
                filename = f"{vid}.safetensors"
            output_path = f"{path}/{filename}"
            os.makedirs(os.path.dirname(output_path), exist_ok=True)
            with open(output_path, "wb") as f:
                for chunk in r.iter_content(chunk_size=8192):
                    f.write(chunk)
        print(f"downloaded {filename}")
    except Exception as e:
        print(f"failed {vid}: {e}")

    time.sleep(1)

print("END")

HuggingFace

pip install huggingface_hub
hf download [REPOSITORY] --include "[PATH]" --local-dir [LOCAL_PATH]

[REPOSITORY]:リポジトリのルート。「作成者/リポジトリ名」の形
[PATH]:ダウンロードしたいファイルのパス
[LOCAL_PATH]:ダウンロード先のパス

--repo-type dataset:データセットのダウンロード

リポジトリ全体をダウンロード

hf download [REPOSITORY] --local-dir [LOCAL_PATH] --local-dir-use-symlinks False