From dd9daf8efb617244524790795aba085ebda6721b Mon Sep 17 00:00:00 2001 From: psychedelicious <4822129+psychedelicious@users.noreply.github.com> Date: Fri, 1 Mar 2024 10:04:59 +1100 Subject: [PATCH] chore: ruff --- .../app/services/config/config_default.py | 1 + invokeai/app/services/download/__init__.py | 1 + .../app/services/model_records/__init__.py | 1 + .../model_records/model_records_sql.py | 1 - .../sqlite_migrator/sqlite_migrator_common.py | 3 +-- invokeai/backend/image_util/__init__.py | 1 + .../backend/image_util/invisible_watermark.py | 1 + invokeai/backend/image_util/patchmatch.py | 1 + invokeai/backend/image_util/pngwriter.py | 1 + invokeai/backend/image_util/safety_checker.py | 1 + invokeai/backend/install/check_root.py | 1 + invokeai/backend/install/install_helper.py | 1 + invokeai/backend/model_manager/__init__.py | 1 + invokeai/backend/model_manager/config.py | 1 + .../convert_ckpt_to_diffusers.py | 2 +- .../backend/model_manager/load/__init__.py | 1 + .../load/convert_cache/convert_cache_base.py | 1 + .../load/model_loader_registry.py | 1 + .../model_manager/load/model_loaders/lora.py | 1 - .../load/model_loaders/stable_diffusion.py | 1 - .../load/model_loaders/textual_inversion.py | 1 - .../model_manager/metadata/__init__.py | 1 + invokeai/backend/model_patcher.py | 1 + invokeai/backend/stable_diffusion/__init__.py | 1 + .../stable_diffusion/diffusion/__init__.py | 1 + invokeai/backend/training/__init__.py | 1 + .../training/textual_inversion_training.py | 6 +++--- invokeai/backend/util/__init__.py | 1 + invokeai/backend/util/attention.py | 1 + invokeai/backend/util/silence_warnings.py | 1 + invokeai/backend/util/util.py | 19 +++++++++++-------- invokeai/frontend/CLI/__init__.py | 1 + .../frontend/install/invokeai_configure.py | 1 + invokeai/frontend/install/invokeai_update.py | 1 + invokeai/frontend/install/widgets.py | 1 + invokeai/frontend/merge/__init__.py | 1 + invokeai/frontend/merge/merge_diffusers.py | 1 + invokeai/frontend/training/__init__.py | 1 + .../frontend/training/textual_inversion.py | 1 - invokeai/version/__init__.py | 1 + .../services/download/test_download_queue.py | 1 + .../model_metadata/test_model_metadata.py | 1 + tests/backend/util/test_logging.py | 1 + tests/test_path.py | 1 + 44 files changed, 51 insertions(+), 19 deletions(-) diff --git a/invokeai/app/services/config/config_default.py b/invokeai/app/services/config/config_default.py index 01fd5e2179..64d464002b 100644 --- a/invokeai/app/services/config/config_default.py +++ b/invokeai/app/services/config/config_default.py @@ -166,6 +166,7 @@ two configs are kept in separate sections of the config file: ... """ + from __future__ import annotations import os diff --git a/invokeai/app/services/download/__init__.py b/invokeai/app/services/download/__init__.py index 04c1dfdb1d..371c531387 100644 --- a/invokeai/app/services/download/__init__.py +++ b/invokeai/app/services/download/__init__.py @@ -1,4 +1,5 @@ """Init file for download queue.""" + from .download_base import DownloadJob, DownloadJobStatus, DownloadQueueServiceBase, UnknownJobIDException from .download_default import DownloadQueueService, TqdmProgress diff --git a/invokeai/app/services/model_records/__init__.py b/invokeai/app/services/model_records/__init__.py index 1622066715..7f888cf1f3 100644 --- a/invokeai/app/services/model_records/__init__.py +++ b/invokeai/app/services/model_records/__init__.py @@ -1,4 +1,5 @@ """Init file for model record services.""" + from .model_records_base import ( # noqa F401 DuplicateModelException, InvalidModelException, diff --git a/invokeai/app/services/model_records/model_records_sql.py b/invokeai/app/services/model_records/model_records_sql.py index dcd1114655..60f0ad86a8 100644 --- a/invokeai/app/services/model_records/model_records_sql.py +++ b/invokeai/app/services/model_records/model_records_sql.py @@ -39,7 +39,6 @@ Typical usage: configs = store.search_by_attr(base_model='sd-2', model_type='main') """ - import json import sqlite3 from math import ceil diff --git a/invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_common.py b/invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_common.py index 47ed5da505..9b2444dae4 100644 --- a/invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_common.py +++ b/invokeai/app/services/shared/sqlite_migrator/sqlite_migrator_common.py @@ -17,8 +17,7 @@ class MigrateCallback(Protocol): See :class:`Migration` for an example. """ - def __call__(self, cursor: sqlite3.Cursor) -> None: - ... + def __call__(self, cursor: sqlite3.Cursor) -> None: ... class MigrationError(RuntimeError): diff --git a/invokeai/backend/image_util/__init__.py b/invokeai/backend/image_util/__init__.py index 0be5a78a93..ea35a5f153 100644 --- a/invokeai/backend/image_util/__init__.py +++ b/invokeai/backend/image_util/__init__.py @@ -1,6 +1,7 @@ """ Initialization file for invokeai.backend.image_util methods. """ + from .patchmatch import PatchMatch # noqa: F401 from .pngwriter import PngWriter, PromptFormatter, retrieve_metadata, write_metadata # noqa: F401 from .seamless import configure_model_padding # noqa: F401 diff --git a/invokeai/backend/image_util/invisible_watermark.py b/invokeai/backend/image_util/invisible_watermark.py index 37b3ca918c..b21cc2e124 100644 --- a/invokeai/backend/image_util/invisible_watermark.py +++ b/invokeai/backend/image_util/invisible_watermark.py @@ -3,6 +3,7 @@ This module defines a singleton object, "invisible_watermark" that wraps the invisible watermark model. It respects the global "invisible_watermark" configuration variable, that allows the watermarking to be supressed. """ + import cv2 import numpy as np from imwatermark import WatermarkEncoder diff --git a/invokeai/backend/image_util/patchmatch.py b/invokeai/backend/image_util/patchmatch.py index 5514e3d33a..08f3f4ffff 100644 --- a/invokeai/backend/image_util/patchmatch.py +++ b/invokeai/backend/image_util/patchmatch.py @@ -4,6 +4,7 @@ wraps the actual patchmatch object. It respects the global "try_patchmatch" attribute, so that patchmatch loading can be suppressed or deferred """ + import numpy as np import invokeai.backend.util.logging as logger diff --git a/invokeai/backend/image_util/pngwriter.py b/invokeai/backend/image_util/pngwriter.py index c9c58264c2..f537b4681c 100644 --- a/invokeai/backend/image_util/pngwriter.py +++ b/invokeai/backend/image_util/pngwriter.py @@ -6,6 +6,7 @@ PngWriter -- Converts Images generated by T2I into PNGs, finds Exports function retrieve_metadata(path) """ + import json import os import re diff --git a/invokeai/backend/image_util/safety_checker.py b/invokeai/backend/image_util/safety_checker.py index 92ddef5ecc..b92a73c24f 100644 --- a/invokeai/backend/image_util/safety_checker.py +++ b/invokeai/backend/image_util/safety_checker.py @@ -3,6 +3,7 @@ This module defines a singleton object, "safety_checker" that wraps the safety_checker model. It respects the global "nsfw_checker" configuration variable, that allows the checker to be supressed. """ + import numpy as np from PIL import Image diff --git a/invokeai/backend/install/check_root.py b/invokeai/backend/install/check_root.py index cbf9976123..2b2116993b 100644 --- a/invokeai/backend/install/check_root.py +++ b/invokeai/backend/install/check_root.py @@ -1,6 +1,7 @@ """ Check that the invokeai_root is correctly configured and exit if not. """ + import sys from invokeai.app.services.config import InvokeAIAppConfig diff --git a/invokeai/backend/install/install_helper.py b/invokeai/backend/install/install_helper.py index 999dcdd100..975088409c 100644 --- a/invokeai/backend/install/install_helper.py +++ b/invokeai/backend/install/install_helper.py @@ -1,4 +1,5 @@ """Utility (backend) functions used by model_install.py""" + from logging import Logger from pathlib import Path from typing import Any, Dict, List, Optional diff --git a/invokeai/backend/model_manager/__init__.py b/invokeai/backend/model_manager/__init__.py index 88356d0468..966e6ae21d 100644 --- a/invokeai/backend/model_manager/__init__.py +++ b/invokeai/backend/model_manager/__init__.py @@ -1,4 +1,5 @@ """Re-export frequently-used symbols from the Model Manager backend.""" + from .config import ( AnyModel, AnyModelConfig, diff --git a/invokeai/backend/model_manager/config.py b/invokeai/backend/model_manager/config.py index 78635636b9..e22f74c767 100644 --- a/invokeai/backend/model_manager/config.py +++ b/invokeai/backend/model_manager/config.py @@ -19,6 +19,7 @@ Typical usage: Validation errors will raise an InvalidModelConfigException error. """ + import time from enum import Enum from typing import Literal, Optional, Type, Union diff --git a/invokeai/backend/model_manager/convert_ckpt_to_diffusers.py b/invokeai/backend/model_manager/convert_ckpt_to_diffusers.py index 6f5acd5832..6b3e953b38 100644 --- a/invokeai/backend/model_manager/convert_ckpt_to_diffusers.py +++ b/invokeai/backend/model_manager/convert_ckpt_to_diffusers.py @@ -15,7 +15,7 @@ # # Adapted for use in InvokeAI by Lincoln Stein, July 2023 # -""" Conversion script for the Stable Diffusion checkpoints.""" +"""Conversion script for the Stable Diffusion checkpoints.""" import re from contextlib import nullcontext diff --git a/invokeai/backend/model_manager/load/__init__.py b/invokeai/backend/model_manager/load/__init__.py index a0421017db..f47a2c4368 100644 --- a/invokeai/backend/model_manager/load/__init__.py +++ b/invokeai/backend/model_manager/load/__init__.py @@ -2,6 +2,7 @@ """ Init file for the model loader. """ + from importlib import import_module from pathlib import Path diff --git a/invokeai/backend/model_manager/load/convert_cache/convert_cache_base.py b/invokeai/backend/model_manager/load/convert_cache/convert_cache_base.py index 6268c099a5..ef363cc7f4 100644 --- a/invokeai/backend/model_manager/load/convert_cache/convert_cache_base.py +++ b/invokeai/backend/model_manager/load/convert_cache/convert_cache_base.py @@ -1,6 +1,7 @@ """ Disk-based converted model cache. """ + from abc import ABC, abstractmethod from pathlib import Path diff --git a/invokeai/backend/model_manager/load/model_loader_registry.py b/invokeai/backend/model_manager/load/model_loader_registry.py index ce1110e749..e7747650d4 100644 --- a/invokeai/backend/model_manager/load/model_loader_registry.py +++ b/invokeai/backend/model_manager/load/model_loader_registry.py @@ -14,6 +14,7 @@ Use like this: ).load_model(model_config, submodel_type) """ + import hashlib from abc import ABC, abstractmethod from pathlib import Path diff --git a/invokeai/backend/model_manager/load/model_loaders/lora.py b/invokeai/backend/model_manager/load/model_loaders/lora.py index fe804ef565..e308531a4f 100644 --- a/invokeai/backend/model_manager/load/model_loaders/lora.py +++ b/invokeai/backend/model_manager/load/model_loaders/lora.py @@ -1,7 +1,6 @@ # Copyright (c) 2024, Lincoln D. Stein and the InvokeAI Development Team """Class for LoRA model loading in InvokeAI.""" - from logging import Logger from pathlib import Path from typing import Optional, Tuple diff --git a/invokeai/backend/model_manager/load/model_loaders/stable_diffusion.py b/invokeai/backend/model_manager/load/model_loaders/stable_diffusion.py index 5884f84e8d..0f0483d8dc 100644 --- a/invokeai/backend/model_manager/load/model_loaders/stable_diffusion.py +++ b/invokeai/backend/model_manager/load/model_loaders/stable_diffusion.py @@ -1,7 +1,6 @@ # Copyright (c) 2024, Lincoln D. Stein and the InvokeAI Development Team """Class for StableDiffusion model loading in InvokeAI.""" - from pathlib import Path from typing import Optional diff --git a/invokeai/backend/model_manager/load/model_loaders/textual_inversion.py b/invokeai/backend/model_manager/load/model_loaders/textual_inversion.py index 094d4d7c5c..fb9204a701 100644 --- a/invokeai/backend/model_manager/load/model_loaders/textual_inversion.py +++ b/invokeai/backend/model_manager/load/model_loaders/textual_inversion.py @@ -1,7 +1,6 @@ # Copyright (c) 2024, Lincoln D. Stein and the InvokeAI Development Team """Class for TI model loading in InvokeAI.""" - from pathlib import Path from typing import Optional, Tuple diff --git a/invokeai/backend/model_manager/metadata/__init__.py b/invokeai/backend/model_manager/metadata/__init__.py index a35e55f3d2..56e741cd00 100644 --- a/invokeai/backend/model_manager/metadata/__init__.py +++ b/invokeai/backend/model_manager/metadata/__init__.py @@ -18,6 +18,7 @@ assert isinstance(data, CivitaiMetadata) if data.allow_commercial_use: print("Commercial use of this model is allowed") """ + from .fetch import CivitaiMetadataFetch, HuggingFaceMetadataFetch, ModelMetadataFetchBase from .metadata_base import ( AnyModelRepoMetadata, diff --git a/invokeai/backend/model_patcher.py b/invokeai/backend/model_patcher.py index 473a088308..76271fc025 100644 --- a/invokeai/backend/model_patcher.py +++ b/invokeai/backend/model_patcher.py @@ -1,5 +1,6 @@ # Copyright (c) 2024 Ryan Dick, Lincoln D. Stein, and the InvokeAI Development Team """These classes implement model patching with LoRAs and Textual Inversions.""" + from __future__ import annotations import pickle diff --git a/invokeai/backend/stable_diffusion/__init__.py b/invokeai/backend/stable_diffusion/__init__.py index 75e6aa0a5d..8b3f701064 100644 --- a/invokeai/backend/stable_diffusion/__init__.py +++ b/invokeai/backend/stable_diffusion/__init__.py @@ -1,6 +1,7 @@ """ Initialization file for the invokeai.backend.stable_diffusion package """ + from .diffusers_pipeline import PipelineIntermediateState, StableDiffusionGeneratorPipeline # noqa: F401 from .diffusion import InvokeAIDiffuserComponent # noqa: F401 from .diffusion.cross_attention_map_saving import AttentionMapSaver # noqa: F401 diff --git a/invokeai/backend/stable_diffusion/diffusion/__init__.py b/invokeai/backend/stable_diffusion/diffusion/__init__.py index 00e6f1b916..e68340168a 100644 --- a/invokeai/backend/stable_diffusion/diffusion/__init__.py +++ b/invokeai/backend/stable_diffusion/diffusion/__init__.py @@ -1,6 +1,7 @@ """ Initialization file for invokeai.models.diffusion """ + from .cross_attention_control import InvokeAICrossAttentionMixin # noqa: F401 from .cross_attention_map_saving import AttentionMapSaver # noqa: F401 from .shared_invokeai_diffusion import InvokeAIDiffuserComponent # noqa: F401 diff --git a/invokeai/backend/training/__init__.py b/invokeai/backend/training/__init__.py index ed3ceb90ec..6b5aa7327d 100644 --- a/invokeai/backend/training/__init__.py +++ b/invokeai/backend/training/__init__.py @@ -1,4 +1,5 @@ """ Initialization file for invokeai.backend.training """ + from .textual_inversion_training import do_textual_inversion_training, parse_args # noqa: F401 diff --git a/invokeai/backend/training/textual_inversion_training.py b/invokeai/backend/training/textual_inversion_training.py index 9a38c006a5..7ddcf14367 100644 --- a/invokeai/backend/training/textual_inversion_training.py +++ b/invokeai/backend/training/textual_inversion_training.py @@ -858,9 +858,9 @@ def do_textual_inversion_training( # Let's make sure we don't update any embedding weights besides the newly added token index_no_updates = torch.arange(len(tokenizer)) != placeholder_token_id with torch.no_grad(): - accelerator.unwrap_model(text_encoder).get_input_embeddings().weight[ - index_no_updates - ] = orig_embeds_params[index_no_updates] + accelerator.unwrap_model(text_encoder).get_input_embeddings().weight[index_no_updates] = ( + orig_embeds_params[index_no_updates] + ) # Checks if the accelerator has performed an optimization step behind the scenes if accelerator.sync_gradients: diff --git a/invokeai/backend/util/__init__.py b/invokeai/backend/util/__init__.py index 7b48f0364e..ee6793376f 100644 --- a/invokeai/backend/util/__init__.py +++ b/invokeai/backend/util/__init__.py @@ -1,6 +1,7 @@ """ Initialization file for invokeai.backend.util """ + from .attention import auto_detect_slice_size # noqa: F401 from .devices import ( # noqa: F401 CPU_DEVICE, diff --git a/invokeai/backend/util/attention.py b/invokeai/backend/util/attention.py index 910933044e..88dc6e5cec 100644 --- a/invokeai/backend/util/attention.py +++ b/invokeai/backend/util/attention.py @@ -3,6 +3,7 @@ Utility routine used for autodetection of optimal slice size for attention mechanism. """ + import psutil import torch diff --git a/invokeai/backend/util/silence_warnings.py b/invokeai/backend/util/silence_warnings.py index 068b605da9..4c566ba759 100644 --- a/invokeai/backend/util/silence_warnings.py +++ b/invokeai/backend/util/silence_warnings.py @@ -1,4 +1,5 @@ """Context class to silence transformers and diffusers warnings.""" + import warnings from typing import Any diff --git a/invokeai/backend/util/util.py b/invokeai/backend/util/util.py index ae376b41b2..ac7a64e807 100644 --- a/invokeai/backend/util/util.py +++ b/invokeai/backend/util/util.py @@ -340,14 +340,17 @@ def download_with_resume(url: str, dest: Path, access_token: str = None) -> Path logger.error(f"ERROR DOWNLOADING {url}: {resp.text}") return None - with open(dest, open_mode) as file, tqdm( - desc=str(dest), - initial=exist_size, - total=content_length, - unit="iB", - unit_scale=True, - unit_divisor=1000, - ) as bar: + with ( + open(dest, open_mode) as file, + tqdm( + desc=str(dest), + initial=exist_size, + total=content_length, + unit="iB", + unit_scale=True, + unit_divisor=1000, + ) as bar, + ): for data in resp.iter_content(chunk_size=1024): size = file.write(data) bar.update(size) diff --git a/invokeai/frontend/CLI/__init__.py b/invokeai/frontend/CLI/__init__.py index f8864bbe66..63d8d66246 100644 --- a/invokeai/frontend/CLI/__init__.py +++ b/invokeai/frontend/CLI/__init__.py @@ -1,4 +1,5 @@ """ Initialization file for invokeai.frontend.CLI """ + from .CLI import main as invokeai_command_line_interface # noqa: F401 diff --git a/invokeai/frontend/install/invokeai_configure.py b/invokeai/frontend/install/invokeai_configure.py index 2a62430b69..73132ee064 100644 --- a/invokeai/frontend/install/invokeai_configure.py +++ b/invokeai/frontend/install/invokeai_configure.py @@ -1,6 +1,7 @@ """ Wrapper for invokeai.backend.configure.invokeai_configure """ + from ...backend.install.invokeai_configure import main as invokeai_configure # noqa: F401 __all__ = ["invokeai_configure"] diff --git a/invokeai/frontend/install/invokeai_update.py b/invokeai/frontend/install/invokeai_update.py index 06c3520a89..32a6208e6b 100644 --- a/invokeai/frontend/install/invokeai_update.py +++ b/invokeai/frontend/install/invokeai_update.py @@ -2,6 +2,7 @@ Minimalist updater script. Prompts user for the tag or branch to update to and runs pip install . """ + import os import platform from distutils.version import LooseVersion diff --git a/invokeai/frontend/install/widgets.py b/invokeai/frontend/install/widgets.py index 4dbc6349a0..49ca1e3583 100644 --- a/invokeai/frontend/install/widgets.py +++ b/invokeai/frontend/install/widgets.py @@ -1,6 +1,7 @@ """ Widget class definitions used by model_select.py, merge_diffusers.py and textual_inversion.py """ + import curses import math import os diff --git a/invokeai/frontend/merge/__init__.py b/invokeai/frontend/merge/__init__.py index a18da9c0d4..4e56b146f4 100644 --- a/invokeai/frontend/merge/__init__.py +++ b/invokeai/frontend/merge/__init__.py @@ -1,4 +1,5 @@ """ Initialization file for invokeai.frontend.merge """ + from .merge_diffusers import main as invokeai_merge_diffusers # noqa: F401 diff --git a/invokeai/frontend/merge/merge_diffusers.py b/invokeai/frontend/merge/merge_diffusers.py index ff9acd569e..78b5df859b 100644 --- a/invokeai/frontend/merge/merge_diffusers.py +++ b/invokeai/frontend/merge/merge_diffusers.py @@ -4,6 +4,7 @@ used to merge 2-3 models together and create a new InvokeAI-registered diffusion Copyright (c) 2023 Lincoln Stein and the InvokeAI Development Team """ + import argparse import curses import re diff --git a/invokeai/frontend/training/__init__.py b/invokeai/frontend/training/__init__.py index d4eff2f7fd..7e002b4c03 100644 --- a/invokeai/frontend/training/__init__.py +++ b/invokeai/frontend/training/__init__.py @@ -1,4 +1,5 @@ """ Initialization file for invokeai.frontend.training """ + from .textual_inversion import main as invokeai_textual_inversion # noqa: F401 diff --git a/invokeai/frontend/training/textual_inversion.py b/invokeai/frontend/training/textual_inversion.py index 6250b313b0..a7dc36515f 100644 --- a/invokeai/frontend/training/textual_inversion.py +++ b/invokeai/frontend/training/textual_inversion.py @@ -6,7 +6,6 @@ This is the frontend to "textual_inversion_training.py". Copyright (c) 2023-24 Lincoln Stein and the InvokeAI Development Team """ - import os import re import shutil diff --git a/invokeai/version/__init__.py b/invokeai/version/__init__.py index 90cca7c27f..dbc30bc636 100644 --- a/invokeai/version/__init__.py +++ b/invokeai/version/__init__.py @@ -1,6 +1,7 @@ """ initialization file for invokeai """ + from .invokeai_version import __version__ # noqa: F401 __app_id__ = "invoke-ai/InvokeAI" diff --git a/tests/app/services/download/test_download_queue.py b/tests/app/services/download/test_download_queue.py index ff9b193b17..9c1826170e 100644 --- a/tests/app/services/download/test_download_queue.py +++ b/tests/app/services/download/test_download_queue.py @@ -1,4 +1,5 @@ """Test the queued download facility""" + import re import time from pathlib import Path diff --git a/tests/backend/model_manager/model_metadata/test_model_metadata.py b/tests/backend/model_manager/model_metadata/test_model_metadata.py index f7f2603e7c..0c6b1a6e93 100644 --- a/tests/backend/model_manager/model_metadata/test_model_metadata.py +++ b/tests/backend/model_manager/model_metadata/test_model_metadata.py @@ -1,6 +1,7 @@ """ Test model metadata fetching and storage. """ + import datetime from pathlib import Path diff --git a/tests/backend/util/test_logging.py b/tests/backend/util/test_logging.py index 0d229df7c1..944ffad47a 100644 --- a/tests/backend/util/test_logging.py +++ b/tests/backend/util/test_logging.py @@ -1,6 +1,7 @@ """ Test interaction of logging with configuration system. """ + import io import logging import re diff --git a/tests/test_path.py b/tests/test_path.py index 2f2e3779c7..c03536c13a 100644 --- a/tests/test_path.py +++ b/tests/test_path.py @@ -2,6 +2,7 @@ Not really a test, but a way to verify that the paths are existing and fail early if they are not. """ + import pathlib import unittest from os import path as osp