{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "6038b6d3-baa6-4aa3-90f8-6f46bf673cdf", "metadata": {}, "outputs": [], "source": [ "import os\n", "from IPython.display import clear_output\n", "from subprocess import call, getoutput, Popen, run\n", "import time\n", "import ipywidgets as widgets\n", "import requests\n", "import sys\n", "import fileinput\n", "from torch.hub import download_url_to_file\n", "from urllib.parse import urlparse, parse_qs, unquote\n", "import re\n", "import six\n", "import gdown\n", "\n", "from urllib.request import urlopen, Request\n", "import tempfile\n", "from tqdm import tqdm\n", "from bs4 import BeautifulSoup\n", "import zipfile\n", "\n", "def modeldwn(model_LINK):\n", "\n", " if model_LINK == \"\":\n", " print(\"[1;33mNothing to do\")\n", " else:\n", " os.makedirs(\n", " \"/workspace/ComfyUI/models/checkpoints\",\n", " exist_ok=True,\n", " )\n", "\n", " src = getsrc(model_LINK)\n", "\n", " if src == \"civitai\":\n", " modelname = get_name(model_LINK, False)\n", " loramodel = f\"/workspace/ComfyUI/models/checkpoints/{modelname}\"\n", " if not os.path.exists(loramodel):\n", " dwn(model_LINK, loramodel, \"Downloading the checkpoint model\")\n", " clear_output()\n", " else:\n", " print(\"[1;33mModel already exists\")\n", " elif src == \"gdrive\":\n", " modelname = get_true_name(model_LINK)\n", " loramodel = f\"/workspace/ComfyUI/models/checkpoints/{modelname}\"\n", " if not os.path.exists(loramodel):\n", " gdown.download(\n", " url=model_LINK.replace(\"/file/d/\", \"/uc?id=\").replace(\"/view\", \"\"),\n", " output=loramodel,\n", " quiet=False,\n", " )\n", " clear_output()\n", " else:\n", " print(\"[1;33mModel already exists\")\n", " else:\n", " modelname = os.path.basename(model_LINK)\n", " loramodel = f\"/workspace/ComfyUI/models/checkpoints/{modelname}\"\n", " if not os.path.exists(loramodel):\n", " gdown.download(\n", " url=model_LINK, output=loramodel, quiet=False, fuzzy=True\n", " )\n", " clear_output()\n", " else:\n", " print(\"[1;33mModel already exists\")\n", "\n", " if os.path.exists(loramodel):\n", " print(\"[1;32mCheckpoints downloaded\")\n", " else:\n", " print(\"[1;31mWrong link, check that the link is valid\")\n", "\n", "def getsrc(url):\n", "\n", " parsed_url = urlparse(url)\n", "\n", " if parsed_url.netloc == \"civitai.com\":\n", " src = \"civitai\"\n", " elif parsed_url.netloc == \"drive.google.com\":\n", " src = \"gdrive\"\n", " elif parsed_url.netloc == \"huggingface.co\":\n", " src = \"huggingface\"\n", " else:\n", " src = \"others\"\n", " return src\n", "def get_true_name(url):\n", " response = requests.get(url)\n", " soup = BeautifulSoup(response.text, \"html.parser\")\n", " title_tag = soup.find(\"title\")\n", " if title_tag:\n", " title_text = title_tag.text\n", " # Извлечение имени файла из тега title (предполагая, что имя файла указано в теге title)\n", " file_name = title_text.split(\" - \")[0]\n", " return file_name\n", " else:\n", " raise RuntimeError(\"Could not find the title tag in the HTML\")\n", "\n", "\n", "def get_name(url, gdrive):\n", "\n", " from gdown.download import get_url_from_gdrive_confirmation\n", "\n", " if not gdrive:\n", " response = requests.get(url, allow_redirects=False)\n", " if \"Location\" in response.headers:\n", " redirected_url = response.headers[\"Location\"]\n", " quer = parse_qs(urlparse(redirected_url).query)\n", " if \"response-content-disposition\" in quer:\n", " disp_val = quer[\"response-content-disposition\"][0].split(\";\")\n", " for vals in disp_val:\n", " if vals.strip().startswith(\"filename=\"):\n", " filenm = unquote(vals.split(\"=\", 1)[1].strip())\n", " return filenm.replace('\"', \"\")\n", " else:\n", " headers = {\n", " \"User-Agent\": \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36\"\n", " }\n", " lnk = \"https://drive.google.com/uc?id={id}&export=download\".format(\n", " id=url[url.find(\"/d/\") + 3 : url.find(\"/view\")]\n", " )\n", " res = requests.session().get(lnk, headers=headers, stream=True, verify=True)\n", " res = requests.session().get(\n", " get_url_from_gdrive_confirmation(res.text),\n", " headers=headers,\n", " stream=True,\n", " verify=True,\n", " )\n", " content_disposition = six.moves.urllib_parse.unquote(\n", " res.headers[\"Content-Disposition\"]\n", " )\n", " filenm = (\n", " re.search(r\"filename\\*=UTF-8''(.*)\", content_disposition)\n", " .groups()[0]\n", " .replace(os.path.sep, \"_\")\n", " )\n", " return filenm\n", "\n", "def dwn(url, dst, msg):\n", " file_size = None\n", " req = Request(url, headers={\"User-Agent\": \"torch.hub\"})\n", " u = urlopen(req)\n", " meta = u.info()\n", " if hasattr(meta, \"getheaders\"):\n", " content_length = meta.getheaders(\"Content-Length\")\n", " else:\n", " content_length = meta.get_all(\"Content-Length\")\n", " if content_length is not None and len(content_length) > 0:\n", " file_size = int(content_length[0])\n", "\n", " with tqdm(\n", " total=file_size,\n", " disable=False,\n", " mininterval=0.5,\n", " bar_format=msg + \" |{bar:20}| {percentage:3.0f}%\",\n", " ) as pbar:\n", " with open(dst, \"wb\") as f:\n", " while True:\n", " buffer = u.read(8192)\n", " if len(buffer) == 0:\n", " break\n", " f.write(buffer)\n", " pbar.update(len(buffer))\n", " f.close()\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "61e91850-23ac-49e4-a747-0d5f1883e663", "metadata": {}, "outputs": [], "source": [ "modeldwn(\"https://civitai.com/api/download/models/56071\")\n", "modeldwn(\"https://civitai.com/api/download/models/128713\")" ] }, { "cell_type": "code", "execution_count": null, "id": "17ac89a0-45ff-4a3c-8ecc-4dd4642a2e44", "metadata": {}, "outputs": [], "source": [ "python -m venv venv\n", "source venv/bin/activate\n", "cd ComfyUI\n", "pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu121\n", "\n", "requirements.txt\n", "torch\n", "torchsde\n", "einops\n", "transformers>=4.25.1\n", "safetensors>=0.3.0\n", "aiohttp\n", "accelerate\n", "pyyaml\n", "Pillow\n", "scipy\n", "tqdm\n", "psutil\n", "opencv-python\n", "imageio-ffmpeg\n", "\n", "pip install -r requirements.txt\n", "\n", "cd /workspace/ComfyUI/custom_nodes\n", "git clone https://github.com/ltdrdata/ComfyUI-Manager.git\n", "git clone https://github.com/Kosinkadink/ComfyUI-AnimateDiff-Evolved\n", "git clone https://github.com/Kosinkadink/ComfyUI-Advanced-ControlNet\n", "git clone https://github.com/Kosinkadink/ComfyUI-VideoHelperSuite.git\n", "git clone https://github.com/Fannovel16/comfyui_controlnet_aux.git\n", "\n", "cd /workspace/ComfyUI/models/vae\n", "gdown https://drive.google.com/uc?id=1ukSErH_cjsgb1SzHmnMpEYGsrmwtaLGz\n", "gdown https://drive.google.com/uc?id=1hWfed2gQABLn30v8pxSbgLWe_h2udg_R\n", "\n", "cd /workspace/ComfyUI/models/checkpoints\n", "gdown https://drive.google.com/uc?id=1O0ez3jYp_u0DLcdsQ9EsIHSQZciOakWn\n", "gdown https://drive.google.com/uc?id=1qzrSYXM-fzfcQKN0Z2bsA-Qh1zcOasPu\n", "\n", "cd /workspace/ComfyUI/custom_nodes/ComfyUI-AnimateDiff-Evolved/models\n", "wget https://huggingface.co/guoyww/animatediff/resolve/main/mm_sd_v15_v2.ckpt\n", "wget https://huggingface.co/CiaraRowles/TemporalDiff/resolve/main/temporaldiff-v1-animatediff.safetensors\n", "\n", "cd /workspace/ComfyUI/models/controlnet/\n", "wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11f1p_sd15_depth.pth\n", "wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_lineart.pth\n", "wget https://huggingface.co/lllyasviel/ControlNet-v1-1/resolve/main/control_v11p_sd15_openpose.pth\n", "wget https://huggingface.co/monster-labs/control_v1p_sd15_qrcode_monster/resolve/main/control_v1p_sd15_qrcode_monster.safetensors\n", "\n", "cd /workspace/ComfyUI/models/clip_vision/\n", "wget https://huggingface.co/h94/IP-Adapter/resolve/main/models/image_encoder/model.safetensors\n", "\n", "cd /workspace/ComfyUI/models/upscale_models/\n", "wget https://huggingface.co/lokCX/4x-Ultrasharp/resolve/main/4x-UltraSharp.pth\n", "\n", "cd /workspace/ComfyUI\n", "python main.py --listen" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }