2023-12-10 00:46:47 +00:00
|
|
|
from logging import getLogger
|
|
|
|
from typing import Optional
|
|
|
|
|
2023-12-10 00:04:34 +00:00
|
|
|
from ..utils import (
|
|
|
|
ConversionContext,
|
|
|
|
build_cache_paths,
|
|
|
|
download_progress,
|
|
|
|
get_first_exists,
|
|
|
|
remove_prefix,
|
|
|
|
)
|
|
|
|
from .base import BaseClient
|
|
|
|
|
|
|
|
logger = getLogger(__name__)
|
|
|
|
|
|
|
|
CIVITAI_ROOT = "https://civitai.com/api/download/models/%s"
|
|
|
|
|
|
|
|
|
|
|
|
class CivitaiClient(BaseClient):
|
|
|
|
protocol = "civitai://"
|
|
|
|
root: str
|
|
|
|
token: Optional[str]
|
|
|
|
|
|
|
|
def __init__(self, token: Optional[str] = None, root=CIVITAI_ROOT):
|
|
|
|
self.root = root
|
|
|
|
self.token = token
|
|
|
|
|
|
|
|
def download(
|
|
|
|
self,
|
|
|
|
conversion: ConversionContext,
|
|
|
|
name: str,
|
|
|
|
source: str,
|
2023-12-10 00:46:47 +00:00
|
|
|
format: Optional[str] = None,
|
|
|
|
dest: Optional[str] = None,
|
2023-12-10 00:04:34 +00:00
|
|
|
) -> str:
|
|
|
|
"""
|
|
|
|
TODO: download with auth token
|
|
|
|
"""
|
|
|
|
cache_paths = build_cache_paths(
|
2023-12-10 00:46:47 +00:00
|
|
|
conversion,
|
|
|
|
name,
|
|
|
|
client=CivitaiClient.name,
|
|
|
|
format=format,
|
|
|
|
dest=dest,
|
2023-12-10 00:04:34 +00:00
|
|
|
)
|
|
|
|
cached = get_first_exists(cache_paths)
|
|
|
|
if cached:
|
|
|
|
return cached
|
|
|
|
|
|
|
|
source = self.root % (remove_prefix(source, CivitaiClient.protocol))
|
|
|
|
logger.info("downloading model from Civitai: %s -> %s", source, cache_paths[0])
|
|
|
|
return download_progress(source, cache_paths[0])
|