Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

fix: download ipadapter model #21

Merged
merged 8 commits into from
Nov 20, 2023
25 changes: 18 additions & 7 deletions modules/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import streamlit as st
from urllib.parse import urlparse, parse_qs
from huggingface_hub import hf_hub_download
from repositories.ComfyUI.folder_paths import folder_names_and_paths
from repositories.ComfyUI.folder_paths import folder_names_and_paths, models_dir

@st.cache_data(ttl=24*60*60)
def get_civitai_model_meta(model_version_url):
Expand Down Expand Up @@ -40,7 +40,7 @@ def get_local_model_file(model_url):
path_parts = parsed_url.path.split('/')
subfolder = path_parts[5:-1]
if len(subfolder) > 0:
subfolder = os.path.sep.join(subfolder)
subfolder = "/".join(subfolder)
local_model_file = os.path.join(path_parts[1], path_parts[2], subfolder, path_parts[-1])
else:
local_model_file = os.path.join(path_parts[1], path_parts[2], path_parts[-1])
Expand Down Expand Up @@ -69,18 +69,29 @@ def download_model(model_url, model_path):
try:
# download model from huggingface model hub
parsed_url = urlparse(model_url)
path_sep = '/'
path_parts = parsed_url.path.split('/')
repo_id = path_sep.join(path_parts[1:3]) #
repo_id = '/'.join(path_parts[1:3]) #
if len(path_parts[5:-1]) > 0:
subfolder = path_sep.join(path_parts[5:-1]) #
subfolder = "/".join(path_parts[5:-1]) #
else:
subfolder = None

filename = path_parts[-1] # 最后一个元素是filename
file_extension = filename.split('.')[-1]
if 'ipadapter' == model_path:
# load IPAdapterPlus
MODELS_DIR = os.path.join(models_dir, 'ipadapter')
if not os.path.exists(MODELS_DIR):
os.makedirs(MODELS_DIR)
folder_names_and_paths['ipadapter'] = ([MODELS_DIR], ['.bin', '.safetensors'])

model_dir, model_extension = folder_names_and_paths[model_path]
local_dir = path_sep.join([model_dir[0], repo_id])
logger.debug(f"repo_id: {repo_id}, subfolder: {subfolder}, filename: {filename} local_dir: {local_dir}")

local_dir = os.path.sep.join([model_dir[0], path_parts[1], path_parts[2]])
logger.debug(f"url: {model_url} model_path: {model_path} repo_id: {repo_id}, subfolder: {subfolder}, filename: {filename} local_dir: {local_dir}")

if not os.path.exists(local_dir):
os.makedirs(local_dir)
if f".{file_extension}" in model_extension:
# save to model_dir[0]
return hf_hub_download(repo_id=repo_id, filename=filename, subfolder=subfolder, local_dir=local_dir)
Expand Down
22 changes: 4 additions & 18 deletions modules/publish_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,11 @@

@st.cache_data(ttl=24*60*60)
def get_huggingface_model_meta(model_url):
# parse model info from download url,
# get model info from download url,
# eg: https://huggingface.co/segmind/SSD-1B/blob/main/unet/diffusion_pytorch_model.fp16.safetensors

# only support huggingface model hub
parsed_url = urlparse(model_url)
path_parts = parsed_url.path.split('/')
repo_id = '/'.join(path_parts[1:3])
if len(path_parts[5:-1]) > 0:
subfolder = os.path.sep.join(path_parts[5:-1])
else:
subfolder = None
filename = path_parts[-1] # 最后一个元素是filename
logger.debug(f"repo_id: {repo_id}, subfolder: {subfolder}, filename: {filename}")
if repo_id and filename:
hf_url = hf_hub_url(repo_id, filename, subfolder=subfolder)
if hf_url:
hf_meta = get_hf_file_metadata(url=hf_url)
logger.debug(f"hf_meta, {hf_meta}")
return hf_meta
hf_meta = get_hf_file_metadata(url=model_url)
logger.debug(f"hf_meta, {hf_meta}")
return hf_meta

@st.cache_data(ttl=60*60)
def get_civitai_model_meta(model_version_url):
Expand Down