mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
4536e4a8b6
* add basic functionality for model metadata fetching from hf and civitai * add storage * start unit tests * add unit tests and documentation * add missing dependency for pytests * remove redundant fetch; add modified/published dates; updated docs * add code to select diffusers files based on the variant type * implement Civitai installs * make huggingface parallel downloading work * add unit tests for model installation manager - Fixed race condition on selection of download destination path - Add fixtures common to several model_manager_2 unit tests - Added dummy model files for testing diffusers and safetensors downloading/probing - Refactored code for selecting proper variant from list of huggingface repo files - Regrouped ordering of methods in model_install_default.py * improve Civitai model downloading - Provide a better error message when Civitai requires an access token (doesn't give a 403 forbidden, but redirects to the HTML of an authorization page -- arrgh) - Handle case of Civitai providing a primary download link plus additional links for VAEs, config files, etc * add routes for retrieving metadata and tags * code tidying and documentation * fix ruff errors * add file needed to maintain test root diretory in repo for unit tests * fix self->cls in classmethod * add pydantic plugin for mypy * use TestSession instead of requests.Session to prevent any internet activity improve logging fix error message formatting fix logging again fix forward vs reverse slash issue in Windows install tests * Several fixes of problems detected during PR review: - Implement cancel_model_install_job and get_model_install_job routes to allow for better control of model download and install. - Fix thread deadlock that occurred after cancelling an install. - Remove unneeded pytest_plugins section from tests/conftest.py - Remove unused _in_terminal_state() from model_install_default. - Remove outdated documentation from several spots. - Add workaround for Civitai API results which don't return correct URL for the default model. * fix docs and tests to match get_job_by_source() rather than get_job() * Update invokeai/backend/model_manager/metadata/fetch/huggingface.py Co-authored-by: Ryan Dick <ryanjdick3@gmail.com> * Call CivitaiMetadata.model_validate_json() directly Co-authored-by: Ryan Dick <ryanjdick3@gmail.com> * Second round of revisions suggested by @ryanjdick: - Fix type mismatch in `list_all_metadata()` route. - Do not have a default value for the model install job id - Remove static class variable declarations from non Pydantic classes - Change `id` field to `model_id` for the sqlite3 `model_tags` table. - Changed AFTER DELETE triggers to ON DELETE CASCADE for the metadata and tags tables. - Made the `id` field of the `model_metadata` table into a primary key to achieve uniqueness. * Code cleanup suggested in PR review: - Narrowed the declaration of the `parts` attribute of the download progress event - Removed auto-conversion of str to Url in Url-containing sources - Fixed handling of `InvalidModelConfigException` - Made unknown sources raise `NotImplementedError` rather than `Exception` - Improved status reporting on cached HuggingFace access tokens * Multiple fixes: - `job.total_size` returns a valid size for locally installed models - new route `list_models` returns a paged summary of model, name, description, tags and other essential info - fix a few type errors * consolidated all invokeai root pytest fixtures into a single location * Update invokeai/backend/model_manager/metadata/metadata_store.py Co-authored-by: psychedelicious <4822129+psychedelicious@users.noreply.github.com> * Small tweaks in response to review comments: - Remove flake8 configuration from pyproject.toml - Use `id` rather than `modelId` for huggingface `ModelInfo` object - Use `last_modified` rather than `LastModified` for huggingface `ModelInfo` object - Add `sha256` field to file metadata downloaded from huggingface - Add `Invoker` argument to the model installer `start()` and `stop()` routines (but made it optional in order to facilitate use of the service outside the API) - Removed redundant `PRAGMA foreign_keys` from metadata store initialization code. * Additional tweaks and minor bug fixes - Fix calculation of aggregate diffusers model size to only count the size of files, not files + directories (which gives different unit test results on different filesystems). - Refactor _get_metadata() and _get_download_urls() to have distinct code paths for Civitai, HuggingFace and URL sources. - Forward the `inplace` flag from the source to the job and added unit test for this. - Attach cached model metadata to the job rather than to the model install service. * fix unit test that was breaking on windows due to CR/LF changing size of test json files * fix ruff formatting * a few last minor fixes before merging: - Turn job `error` and `error_type` into properties derived from the exception. - Add TODO comment about the reason for handling temporary directory destruction manually rather than using tempfile.tmpdir(). * add unit tests for reporting HTTP download errors --------- Co-authored-by: Lincoln Stein <lstein@gmail.com> Co-authored-by: Ryan Dick <ryanjdick3@gmail.com> Co-authored-by: psychedelicious <4822129+psychedelicious@users.noreply.github.com>
188 lines
7.7 KiB
Python
188 lines
7.7 KiB
Python
# Copyright (c) 2023 Lincoln D. Stein and the InvokeAI Development Team
|
|
|
|
"""
|
|
This module fetches model metadata objects from the Civitai model repository.
|
|
In addition to the `from_url()` and `from_id()` methods inherited from the
|
|
`ModelMetadataFetchBase` base class.
|
|
|
|
Civitai has two separate ID spaces: a model ID and a version ID. The
|
|
version ID corresponds to a specific model, and is the ID accepted by
|
|
`from_id()`. The model ID corresponds to a family of related models,
|
|
such as different training checkpoints or 16 vs 32-bit versions. The
|
|
`from_civitai_modelid()` method will accept a model ID and return the
|
|
metadata from the default version within this model set. The default
|
|
version is the same as what the user sees when they click on a model's
|
|
thumbnail.
|
|
|
|
Usage:
|
|
|
|
from invokeai.backend.model_manager.metadata.fetch import CivitaiMetadataFetch
|
|
|
|
fetcher = CivitaiMetadataFetch()
|
|
metadata = fetcher.from_url("https://civitai.com/models/206883/split")
|
|
print(metadata.trained_words)
|
|
"""
|
|
|
|
import re
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
from typing import Any, Dict, Optional
|
|
|
|
import requests
|
|
from pydantic.networks import AnyHttpUrl
|
|
from requests.sessions import Session
|
|
|
|
from ..metadata_base import (
|
|
AnyModelRepoMetadata,
|
|
CivitaiMetadata,
|
|
CommercialUsage,
|
|
LicenseRestrictions,
|
|
RemoteModelFile,
|
|
UnknownMetadataException,
|
|
)
|
|
from .fetch_base import ModelMetadataFetchBase
|
|
|
|
CIVITAI_MODEL_PAGE_RE = r"https?://civitai.com/models/(\d+)"
|
|
CIVITAI_VERSION_PAGE_RE = r"https?://civitai.com/models/(\d+)\?modelVersionId=(\d+)"
|
|
CIVITAI_DOWNLOAD_RE = r"https?://civitai.com/api/download/models/(\d+)"
|
|
|
|
CIVITAI_VERSION_ENDPOINT = "https://civitai.com/api/v1/model-versions/"
|
|
CIVITAI_MODEL_ENDPOINT = "https://civitai.com/api/v1/models/"
|
|
|
|
|
|
class CivitaiMetadataFetch(ModelMetadataFetchBase):
|
|
"""Fetch model metadata from Civitai."""
|
|
|
|
def __init__(self, session: Optional[Session] = None):
|
|
"""
|
|
Initialize the fetcher with an optional requests.sessions.Session object.
|
|
|
|
By providing a configurable Session object, we can support unit tests on
|
|
this module without an internet connection.
|
|
"""
|
|
self._requests = session or requests.Session()
|
|
|
|
def from_url(self, url: AnyHttpUrl) -> AnyModelRepoMetadata:
|
|
"""
|
|
Given a URL to a CivitAI model or version page, return a ModelMetadata object.
|
|
|
|
In the event that the URL points to a model page without the particular version
|
|
indicated, the default model version is returned. Otherwise, the requested version
|
|
is returned.
|
|
"""
|
|
if match := re.match(CIVITAI_VERSION_PAGE_RE, str(url), re.IGNORECASE):
|
|
model_id = match.group(1)
|
|
version_id = match.group(2)
|
|
return self.from_civitai_versionid(int(version_id), int(model_id))
|
|
elif match := re.match(CIVITAI_MODEL_PAGE_RE, str(url), re.IGNORECASE):
|
|
model_id = match.group(1)
|
|
return self.from_civitai_modelid(int(model_id))
|
|
elif match := re.match(CIVITAI_DOWNLOAD_RE, str(url), re.IGNORECASE):
|
|
version_id = match.group(1)
|
|
return self.from_civitai_versionid(int(version_id))
|
|
raise UnknownMetadataException("The url '{url}' does not match any known Civitai URL patterns")
|
|
|
|
def from_id(self, id: str) -> AnyModelRepoMetadata:
|
|
"""
|
|
Given a Civitai model version ID, return a ModelRepoMetadata object.
|
|
|
|
May raise an `UnknownMetadataException`.
|
|
"""
|
|
return self.from_civitai_versionid(int(id))
|
|
|
|
def from_civitai_modelid(self, model_id: int) -> CivitaiMetadata:
|
|
"""
|
|
Return metadata from the default version of the indicated model.
|
|
|
|
May raise an `UnknownMetadataException`.
|
|
"""
|
|
model_url = CIVITAI_MODEL_ENDPOINT + str(model_id)
|
|
model_json = self._requests.get(model_url).json()
|
|
return self._from_model_json(model_json)
|
|
|
|
def _from_model_json(self, model_json: Dict[str, Any], version_id: Optional[int] = None) -> CivitaiMetadata:
|
|
try:
|
|
version_id = version_id or model_json["modelVersions"][0]["id"]
|
|
except TypeError as excp:
|
|
raise UnknownMetadataException from excp
|
|
|
|
# loop till we find the section containing the version requested
|
|
version_sections = [x for x in model_json["modelVersions"] if x["id"] == version_id]
|
|
if not version_sections:
|
|
raise UnknownMetadataException(f"Version {version_id} not found in model metadata")
|
|
|
|
version_json = version_sections[0]
|
|
safe_thumbnails = [x["url"] for x in version_json["images"] if x["nsfw"] == "None"]
|
|
|
|
# Civitai has one "primary" file plus others such as VAEs. We only fetch the primary.
|
|
primary = [x for x in version_json["files"] if x.get("primary")]
|
|
assert len(primary) == 1
|
|
primary_file = primary[0]
|
|
|
|
url = primary_file["downloadUrl"]
|
|
if "?" not in url: # work around apparent bug in civitai api
|
|
metadata_string = ""
|
|
for key, value in primary_file["metadata"].items():
|
|
if not value:
|
|
continue
|
|
metadata_string += f"&{key}={value}"
|
|
url = url + f"?type={primary_file['type']}{metadata_string}"
|
|
model_files = [
|
|
RemoteModelFile(
|
|
url=url,
|
|
path=Path(primary_file["name"]),
|
|
size=int(primary_file["sizeKB"] * 1024),
|
|
sha256=primary_file["hashes"]["SHA256"],
|
|
)
|
|
]
|
|
return CivitaiMetadata(
|
|
id=model_json["id"],
|
|
name=version_json["name"],
|
|
version_id=version_json["id"],
|
|
version_name=version_json["name"],
|
|
created=datetime.fromisoformat(_fix_timezone(version_json["createdAt"])),
|
|
updated=datetime.fromisoformat(_fix_timezone(version_json["updatedAt"])),
|
|
published=datetime.fromisoformat(_fix_timezone(version_json["publishedAt"])),
|
|
base_model_trained_on=version_json["baseModel"], # note - need a dictionary to turn into a BaseModelType
|
|
files=model_files,
|
|
download_url=version_json["downloadUrl"],
|
|
thumbnail_url=safe_thumbnails[0] if safe_thumbnails else None,
|
|
author=model_json["creator"]["username"],
|
|
description=model_json["description"],
|
|
version_description=version_json["description"] or "",
|
|
tags=model_json["tags"],
|
|
trained_words=version_json["trainedWords"],
|
|
nsfw=model_json["nsfw"],
|
|
restrictions=LicenseRestrictions(
|
|
AllowNoCredit=model_json["allowNoCredit"],
|
|
AllowCommercialUse=CommercialUsage(model_json["allowCommercialUse"]),
|
|
AllowDerivatives=model_json["allowDerivatives"],
|
|
AllowDifferentLicense=model_json["allowDifferentLicense"],
|
|
),
|
|
)
|
|
|
|
def from_civitai_versionid(self, version_id: int, model_id: Optional[int] = None) -> CivitaiMetadata:
|
|
"""
|
|
Return a CivitaiMetadata object given a model version id.
|
|
|
|
May raise an `UnknownMetadataException`.
|
|
"""
|
|
if model_id is None:
|
|
version_url = CIVITAI_VERSION_ENDPOINT + str(version_id)
|
|
version = self._requests.get(version_url).json()
|
|
model_id = version["modelId"]
|
|
|
|
model_url = CIVITAI_MODEL_ENDPOINT + str(model_id)
|
|
model_json = self._requests.get(model_url).json()
|
|
return self._from_model_json(model_json, version_id)
|
|
|
|
@classmethod
|
|
def from_json(cls, json: str) -> CivitaiMetadata:
|
|
"""Given the JSON representation of the metadata, return the corresponding Pydantic object."""
|
|
metadata = CivitaiMetadata.model_validate_json(json)
|
|
return metadata
|
|
|
|
|
|
def _fix_timezone(date: str) -> str:
|
|
return re.sub(r"Z$", "+00:00", date)
|