2023-05-21 12:15:44 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2023-09-24 05:12:51 +00:00
|
|
|
from typing import Callable, Optional
|
2023-07-12 15:14:22 +00:00
|
|
|
|
2023-05-17 09:13:53 +00:00
|
|
|
from PIL.Image import Image as PILImageType
|
2023-05-21 12:15:44 +00:00
|
|
|
|
2023-07-12 15:14:22 +00:00
|
|
|
from invokeai.app.invocations.metadata import ImageMetadata
|
2023-07-20 05:44:22 +00:00
|
|
|
from invokeai.app.models.image import (
|
|
|
|
ImageCategory,
|
|
|
|
InvalidImageCategoryException,
|
|
|
|
InvalidOriginException,
|
|
|
|
ResourceOrigin,
|
|
|
|
)
|
2023-05-23 08:59:43 +00:00
|
|
|
from invokeai.app.services.image_file_storage import (
|
2023-07-20 05:44:22 +00:00
|
|
|
ImageFileDeleteException,
|
|
|
|
ImageFileNotFoundException,
|
|
|
|
ImageFileSaveException,
|
|
|
|
)
|
2023-07-12 15:14:22 +00:00
|
|
|
from invokeai.app.services.image_record_storage import (
|
2023-07-20 05:44:22 +00:00
|
|
|
ImageRecordDeleteException,
|
|
|
|
ImageRecordNotFoundException,
|
|
|
|
ImageRecordSaveException,
|
|
|
|
)
|
2023-09-24 05:12:51 +00:00
|
|
|
from invokeai.app.services.invoker import Invoker
|
2023-08-18 14:57:18 +00:00
|
|
|
from invokeai.app.services.models.image_record import ImageDTO, ImageRecord, ImageRecordChanges, image_record_to_dto
|
2023-09-24 05:19:57 +00:00
|
|
|
from invokeai.app.services.shared.pagination import OffsetPaginatedResults
|
2023-07-12 15:14:22 +00:00
|
|
|
from invokeai.app.util.metadata import get_metadata_graph_from_raw_session
|
2023-05-17 09:13:53 +00:00
|
|
|
|
2023-05-22 05:48:12 +00:00
|
|
|
|
2023-05-21 12:15:44 +00:00
|
|
|
class ImageServiceABC(ABC):
|
2023-05-22 09:44:35 +00:00
|
|
|
"""High-level service for image management."""
|
2023-05-21 12:15:44 +00:00
|
|
|
|
feat(backend): selective invalidation for invocation cache
This change enhances the invocation cache logic to delete cache entries when the resources to which they refer are deleted.
For example, a cached output may refer to "some_image.png". If that image is deleted, and this particular cache entry is later retrieved by a node, that node's successors will receive references to the now non-existent "some_image.png". When they attempt to use that image, they will fail.
To resolve this, we need to invalidate the cache when the resources to which it refers are deleted. Two options:
- Invalidate the whole cache on every image/latents/etc delete
- Selectively invalidate cache entries when their resources are deleted
Node outputs can be any shape, with any number of resource references in arbitrarily nested pydantic models. Traversing that structure to identify resources is not trivial.
But invalidating the whole cache is a bit heavy-handed. It would be nice to be more selective.
Simple solution:
- Invocation outputs' resource references are always string identifiers - like the image's or latents' name
- Invocation outputs can be stringified, which includes said identifiers
- When the invocation is cached, we store the stringified output alongside the "live" output classes
- When a resource is deleted, pass its identifier to the cache service, which can then invalidate any cache entries that refer to it
The images and latents storage services have been outfitted with `on_deleted()` callbacks, and the cache service registers itself to handle those events. This logic was copied from `ItemStorageABC`.
`on_changed()` callback are also added to the images and latents services, though these are not currently used. Just following the existing pattern.
2023-09-20 08:26:47 +00:00
|
|
|
_on_changed_callbacks: list[Callable[[ImageDTO], None]]
|
|
|
|
_on_deleted_callbacks: list[Callable[[str], None]]
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
self._on_changed_callbacks = list()
|
|
|
|
self._on_deleted_callbacks = list()
|
|
|
|
|
|
|
|
def on_changed(self, on_changed: Callable[[ImageDTO], None]) -> None:
|
|
|
|
"""Register a callback for when an image is changed"""
|
|
|
|
self._on_changed_callbacks.append(on_changed)
|
|
|
|
|
|
|
|
def on_deleted(self, on_deleted: Callable[[str], None]) -> None:
|
|
|
|
"""Register a callback for when an image is deleted"""
|
|
|
|
self._on_deleted_callbacks.append(on_deleted)
|
|
|
|
|
|
|
|
def _on_changed(self, item: ImageDTO) -> None:
|
|
|
|
for callback in self._on_changed_callbacks:
|
|
|
|
callback(item)
|
|
|
|
|
|
|
|
def _on_deleted(self, item_id: str) -> None:
|
|
|
|
for callback in self._on_deleted_callbacks:
|
|
|
|
callback(item_id)
|
|
|
|
|
2023-05-21 12:15:44 +00:00
|
|
|
@abstractmethod
|
|
|
|
def create(
|
|
|
|
self,
|
|
|
|
image: PILImageType,
|
2023-05-27 11:39:20 +00:00
|
|
|
image_origin: ResourceOrigin,
|
2023-05-21 12:15:44 +00:00
|
|
|
image_category: ImageCategory,
|
|
|
|
node_id: Optional[str] = None,
|
|
|
|
session_id: Optional[str] = None,
|
2023-07-21 07:44:55 +00:00
|
|
|
board_id: Optional[str] = None,
|
2023-06-14 14:07:20 +00:00
|
|
|
is_intermediate: bool = False,
|
2023-07-12 15:14:22 +00:00
|
|
|
metadata: Optional[dict] = None,
|
2023-08-24 11:42:32 +00:00
|
|
|
workflow: Optional[str] = None,
|
2023-05-21 12:15:44 +00:00
|
|
|
) -> ImageDTO:
|
|
|
|
"""Creates an image, storing the file and its metadata."""
|
|
|
|
pass
|
|
|
|
|
2023-05-25 13:47:18 +00:00
|
|
|
@abstractmethod
|
|
|
|
def update(
|
|
|
|
self,
|
|
|
|
image_name: str,
|
|
|
|
changes: ImageRecordChanges,
|
|
|
|
) -> ImageDTO:
|
|
|
|
"""Updates an image."""
|
|
|
|
pass
|
|
|
|
|
2023-05-21 12:15:44 +00:00
|
|
|
@abstractmethod
|
2023-06-14 11:40:09 +00:00
|
|
|
def get_pil_image(self, image_name: str) -> PILImageType:
|
2023-05-21 12:15:44 +00:00
|
|
|
"""Gets an image as a PIL image."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2023-06-14 11:40:09 +00:00
|
|
|
def get_record(self, image_name: str) -> ImageRecord:
|
2023-05-21 12:15:44 +00:00
|
|
|
"""Gets an image record."""
|
|
|
|
pass
|
|
|
|
|
2023-05-23 08:59:43 +00:00
|
|
|
@abstractmethod
|
2023-06-14 11:40:09 +00:00
|
|
|
def get_dto(self, image_name: str) -> ImageDTO:
|
2023-05-23 08:59:43 +00:00
|
|
|
"""Gets an image DTO."""
|
|
|
|
pass
|
|
|
|
|
2023-07-12 15:14:22 +00:00
|
|
|
@abstractmethod
|
|
|
|
def get_metadata(self, image_name: str) -> ImageMetadata:
|
|
|
|
"""Gets an image's metadata."""
|
|
|
|
pass
|
|
|
|
|
2023-05-21 12:15:44 +00:00
|
|
|
@abstractmethod
|
2023-06-14 14:07:20 +00:00
|
|
|
def get_path(self, image_name: str, thumbnail: bool = False) -> str:
|
2023-05-23 12:57:29 +00:00
|
|
|
"""Gets an image's path."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def validate_path(self, path: str) -> bool:
|
|
|
|
"""Validates an image's path."""
|
2023-05-21 12:15:44 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2023-06-14 11:40:09 +00:00
|
|
|
def get_url(self, image_name: str, thumbnail: bool = False) -> str:
|
2023-05-23 12:57:29 +00:00
|
|
|
"""Gets an image's or thumbnail's URL."""
|
2023-05-21 12:15:44 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
|
|
|
def get_many(
|
|
|
|
self,
|
2023-05-28 08:59:14 +00:00
|
|
|
offset: int = 0,
|
|
|
|
limit: int = 10,
|
2023-05-27 11:39:20 +00:00
|
|
|
image_origin: Optional[ResourceOrigin] = None,
|
2023-05-28 08:59:14 +00:00
|
|
|
categories: Optional[list[ImageCategory]] = None,
|
2023-05-27 08:32:16 +00:00
|
|
|
is_intermediate: Optional[bool] = None,
|
2023-06-21 09:56:19 +00:00
|
|
|
board_id: Optional[str] = None,
|
2023-05-28 08:59:14 +00:00
|
|
|
) -> OffsetPaginatedResults[ImageDTO]:
|
2023-05-21 12:15:44 +00:00
|
|
|
"""Gets a paginated list of image DTOs."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
@abstractmethod
|
2023-06-14 11:40:09 +00:00
|
|
|
def delete(self, image_name: str):
|
2023-05-21 12:15:44 +00:00
|
|
|
"""Deletes an image."""
|
|
|
|
pass
|
|
|
|
|
2023-07-19 14:55:29 +00:00
|
|
|
@abstractmethod
|
2023-07-20 05:44:22 +00:00
|
|
|
def delete_intermediates(self) -> int:
|
|
|
|
"""Deletes all intermediate images."""
|
2023-07-19 14:55:29 +00:00
|
|
|
pass
|
|
|
|
|
2023-06-26 19:53:21 +00:00
|
|
|
@abstractmethod
|
|
|
|
def delete_images_on_board(self, board_id: str):
|
|
|
|
"""Deletes all images on a board."""
|
|
|
|
pass
|
|
|
|
|
2023-05-21 12:15:44 +00:00
|
|
|
|
|
|
|
class ImageService(ImageServiceABC):
|
2023-09-24 05:12:51 +00:00
|
|
|
__invoker: Invoker
|
2023-05-17 09:13:53 +00:00
|
|
|
|
2023-09-24 05:12:51 +00:00
|
|
|
def start(self, invoker: Invoker) -> None:
|
|
|
|
self.__invoker = invoker
|
2023-05-17 09:13:53 +00:00
|
|
|
|
|
|
|
def create(
|
|
|
|
self,
|
|
|
|
image: PILImageType,
|
2023-05-27 11:39:20 +00:00
|
|
|
image_origin: ResourceOrigin,
|
2023-05-17 09:13:53 +00:00
|
|
|
image_category: ImageCategory,
|
2023-05-21 10:05:33 +00:00
|
|
|
node_id: Optional[str] = None,
|
|
|
|
session_id: Optional[str] = None,
|
2023-07-21 07:44:55 +00:00
|
|
|
board_id: Optional[str] = None,
|
2023-05-25 13:47:18 +00:00
|
|
|
is_intermediate: bool = False,
|
2023-07-12 15:14:22 +00:00
|
|
|
metadata: Optional[dict] = None,
|
2023-08-24 11:42:32 +00:00
|
|
|
workflow: Optional[str] = None,
|
2023-05-21 10:05:33 +00:00
|
|
|
) -> ImageDTO:
|
2023-05-27 11:39:20 +00:00
|
|
|
if image_origin not in ResourceOrigin:
|
|
|
|
raise InvalidOriginException
|
2023-05-23 08:59:43 +00:00
|
|
|
|
|
|
|
if image_category not in ImageCategory:
|
|
|
|
raise InvalidImageCategoryException
|
|
|
|
|
2023-09-24 05:12:51 +00:00
|
|
|
image_name = self.__invoker.services.names.create_image_name()
|
2023-05-21 12:15:44 +00:00
|
|
|
|
2023-05-23 08:59:43 +00:00
|
|
|
(width, height) = image.size
|
|
|
|
|
2023-05-17 09:13:53 +00:00
|
|
|
try:
|
|
|
|
# TODO: Consider using a transaction here to ensure consistency between storage and database
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.image_records.save(
|
2023-05-23 08:59:43 +00:00
|
|
|
# Non-nullable fields
|
2023-05-17 09:13:53 +00:00
|
|
|
image_name=image_name,
|
2023-05-27 11:39:20 +00:00
|
|
|
image_origin=image_origin,
|
2023-05-17 09:13:53 +00:00
|
|
|
image_category=image_category,
|
2023-05-23 08:59:43 +00:00
|
|
|
width=width,
|
|
|
|
height=height,
|
2023-05-25 13:47:18 +00:00
|
|
|
# Meta fields
|
|
|
|
is_intermediate=is_intermediate,
|
2023-05-23 08:59:43 +00:00
|
|
|
# Nullable fields
|
2023-05-17 09:13:53 +00:00
|
|
|
node_id=node_id,
|
|
|
|
metadata=metadata,
|
2023-07-12 15:14:22 +00:00
|
|
|
session_id=session_id,
|
2023-05-23 08:59:43 +00:00
|
|
|
)
|
2023-07-21 07:44:55 +00:00
|
|
|
if board_id is not None:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.board_image_records.add_image_to_board(board_id=board_id, image_name=image_name)
|
|
|
|
self.__invoker.services.image_files.save(
|
|
|
|
image_name=image_name, image=image, metadata=metadata, workflow=workflow
|
|
|
|
)
|
2023-06-16 05:52:32 +00:00
|
|
|
image_dto = self.get_dto(image_name)
|
2023-05-17 09:13:53 +00:00
|
|
|
|
feat(backend): selective invalidation for invocation cache
This change enhances the invocation cache logic to delete cache entries when the resources to which they refer are deleted.
For example, a cached output may refer to "some_image.png". If that image is deleted, and this particular cache entry is later retrieved by a node, that node's successors will receive references to the now non-existent "some_image.png". When they attempt to use that image, they will fail.
To resolve this, we need to invalidate the cache when the resources to which it refers are deleted. Two options:
- Invalidate the whole cache on every image/latents/etc delete
- Selectively invalidate cache entries when their resources are deleted
Node outputs can be any shape, with any number of resource references in arbitrarily nested pydantic models. Traversing that structure to identify resources is not trivial.
But invalidating the whole cache is a bit heavy-handed. It would be nice to be more selective.
Simple solution:
- Invocation outputs' resource references are always string identifiers - like the image's or latents' name
- Invocation outputs can be stringified, which includes said identifiers
- When the invocation is cached, we store the stringified output alongside the "live" output classes
- When a resource is deleted, pass its identifier to the cache service, which can then invalidate any cache entries that refer to it
The images and latents storage services have been outfitted with `on_deleted()` callbacks, and the cache service registers itself to handle those events. This logic was copied from `ItemStorageABC`.
`on_changed()` callback are also added to the images and latents services, though these are not currently used. Just following the existing pattern.
2023-09-20 08:26:47 +00:00
|
|
|
self._on_changed(image_dto)
|
2023-06-16 05:52:32 +00:00
|
|
|
return image_dto
|
2023-05-23 08:59:43 +00:00
|
|
|
except ImageRecordSaveException:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Failed to save image record")
|
2023-05-17 09:13:53 +00:00
|
|
|
raise
|
2023-05-23 08:59:43 +00:00
|
|
|
except ImageFileSaveException:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Failed to save image file")
|
2023-05-17 09:13:53 +00:00
|
|
|
raise
|
2023-05-21 12:15:44 +00:00
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error(f"Problem saving image record and file: {str(e)}")
|
2023-05-21 12:15:44 +00:00
|
|
|
raise e
|
2023-05-17 09:13:53 +00:00
|
|
|
|
2023-05-25 13:47:18 +00:00
|
|
|
def update(
|
|
|
|
self,
|
|
|
|
image_name: str,
|
|
|
|
changes: ImageRecordChanges,
|
|
|
|
) -> ImageDTO:
|
|
|
|
try:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.image_records.update(image_name, changes)
|
feat(backend): selective invalidation for invocation cache
This change enhances the invocation cache logic to delete cache entries when the resources to which they refer are deleted.
For example, a cached output may refer to "some_image.png". If that image is deleted, and this particular cache entry is later retrieved by a node, that node's successors will receive references to the now non-existent "some_image.png". When they attempt to use that image, they will fail.
To resolve this, we need to invalidate the cache when the resources to which it refers are deleted. Two options:
- Invalidate the whole cache on every image/latents/etc delete
- Selectively invalidate cache entries when their resources are deleted
Node outputs can be any shape, with any number of resource references in arbitrarily nested pydantic models. Traversing that structure to identify resources is not trivial.
But invalidating the whole cache is a bit heavy-handed. It would be nice to be more selective.
Simple solution:
- Invocation outputs' resource references are always string identifiers - like the image's or latents' name
- Invocation outputs can be stringified, which includes said identifiers
- When the invocation is cached, we store the stringified output alongside the "live" output classes
- When a resource is deleted, pass its identifier to the cache service, which can then invalidate any cache entries that refer to it
The images and latents storage services have been outfitted with `on_deleted()` callbacks, and the cache service registers itself to handle those events. This logic was copied from `ItemStorageABC`.
`on_changed()` callback are also added to the images and latents services, though these are not currently used. Just following the existing pattern.
2023-09-20 08:26:47 +00:00
|
|
|
image_dto = self.get_dto(image_name)
|
|
|
|
self._on_changed(image_dto)
|
|
|
|
return image_dto
|
2023-05-25 13:47:18 +00:00
|
|
|
except ImageRecordSaveException:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Failed to update image record")
|
2023-05-25 13:47:18 +00:00
|
|
|
raise
|
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Problem updating image record")
|
2023-05-25 13:47:18 +00:00
|
|
|
raise e
|
|
|
|
|
2023-06-14 11:40:09 +00:00
|
|
|
def get_pil_image(self, image_name: str) -> PILImageType:
|
2023-05-17 09:13:53 +00:00
|
|
|
try:
|
2023-09-24 05:12:51 +00:00
|
|
|
return self.__invoker.services.image_files.get(image_name)
|
2023-05-23 08:59:43 +00:00
|
|
|
except ImageFileNotFoundException:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Failed to get image file")
|
2023-05-17 09:13:53 +00:00
|
|
|
raise
|
2023-05-21 12:15:44 +00:00
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Problem getting image file")
|
2023-05-21 12:15:44 +00:00
|
|
|
raise e
|
2023-05-17 09:13:53 +00:00
|
|
|
|
2023-06-14 11:40:09 +00:00
|
|
|
def get_record(self, image_name: str) -> ImageRecord:
|
2023-05-17 09:13:53 +00:00
|
|
|
try:
|
2023-09-24 05:12:51 +00:00
|
|
|
return self.__invoker.services.image_records.get(image_name)
|
2023-05-23 08:59:43 +00:00
|
|
|
except ImageRecordNotFoundException:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Image record not found")
|
2023-05-21 10:05:33 +00:00
|
|
|
raise
|
2023-05-21 12:15:44 +00:00
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Problem getting image record")
|
2023-05-21 12:15:44 +00:00
|
|
|
raise e
|
|
|
|
|
2023-06-14 11:40:09 +00:00
|
|
|
def get_dto(self, image_name: str) -> ImageDTO:
|
2023-05-21 10:05:33 +00:00
|
|
|
try:
|
2023-09-24 05:12:51 +00:00
|
|
|
image_record = self.__invoker.services.image_records.get(image_name)
|
2023-05-21 10:05:33 +00:00
|
|
|
|
|
|
|
image_dto = image_record_to_dto(
|
|
|
|
image_record,
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.urls.get_image_url(image_name),
|
|
|
|
self.__invoker.services.urls.get_image_url(image_name, True),
|
|
|
|
self.__invoker.services.board_image_records.get_board_for_image(image_name),
|
2023-05-17 09:13:53 +00:00
|
|
|
)
|
2023-05-21 10:05:33 +00:00
|
|
|
|
|
|
|
return image_dto
|
2023-05-23 08:59:43 +00:00
|
|
|
except ImageRecordNotFoundException:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Image record not found")
|
2023-05-17 09:13:53 +00:00
|
|
|
raise
|
2023-05-21 12:15:44 +00:00
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Problem getting image DTO")
|
2023-05-21 12:15:44 +00:00
|
|
|
raise e
|
2023-05-17 09:13:53 +00:00
|
|
|
|
2023-07-12 15:14:22 +00:00
|
|
|
def get_metadata(self, image_name: str) -> Optional[ImageMetadata]:
|
|
|
|
try:
|
2023-09-24 05:12:51 +00:00
|
|
|
image_record = self.__invoker.services.image_records.get(image_name)
|
|
|
|
metadata = self.__invoker.services.image_records.get_metadata(image_name)
|
2023-07-12 15:14:22 +00:00
|
|
|
|
|
|
|
if not image_record.session_id:
|
2023-08-01 08:08:17 +00:00
|
|
|
return ImageMetadata(metadata=metadata)
|
2023-07-12 15:14:22 +00:00
|
|
|
|
2023-09-24 05:12:51 +00:00
|
|
|
session_raw = self.__invoker.services.graph_execution_manager.get_raw(image_record.session_id)
|
2023-07-12 15:14:22 +00:00
|
|
|
graph = None
|
|
|
|
|
|
|
|
if session_raw:
|
|
|
|
try:
|
|
|
|
graph = get_metadata_graph_from_raw_session(session_raw)
|
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.warn(f"Failed to parse session graph: {e}")
|
2023-07-12 15:14:22 +00:00
|
|
|
graph = None
|
|
|
|
|
|
|
|
return ImageMetadata(graph=graph, metadata=metadata)
|
|
|
|
except ImageRecordNotFoundException:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Image record not found")
|
2023-07-12 15:14:22 +00:00
|
|
|
raise
|
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Problem getting image DTO")
|
2023-07-12 15:14:22 +00:00
|
|
|
raise e
|
|
|
|
|
2023-06-14 11:40:09 +00:00
|
|
|
def get_path(self, image_name: str, thumbnail: bool = False) -> str:
|
2023-05-22 05:48:12 +00:00
|
|
|
try:
|
2023-09-24 05:12:51 +00:00
|
|
|
return self.__invoker.services.image_files.get_path(image_name, thumbnail)
|
2023-05-22 05:48:12 +00:00
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Problem getting image path")
|
2023-05-22 05:48:12 +00:00
|
|
|
raise e
|
|
|
|
|
2023-05-23 12:57:29 +00:00
|
|
|
def validate_path(self, path: str) -> bool:
|
|
|
|
try:
|
2023-09-24 05:12:51 +00:00
|
|
|
return self.__invoker.services.image_files.validate_path(path)
|
2023-05-23 12:57:29 +00:00
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Problem validating image path")
|
2023-05-23 12:57:29 +00:00
|
|
|
raise e
|
|
|
|
|
2023-06-14 11:40:09 +00:00
|
|
|
def get_url(self, image_name: str, thumbnail: bool = False) -> str:
|
2023-05-22 05:48:12 +00:00
|
|
|
try:
|
2023-09-24 05:12:51 +00:00
|
|
|
return self.__invoker.services.urls.get_image_url(image_name, thumbnail)
|
2023-05-22 05:48:12 +00:00
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Problem getting image path")
|
2023-05-22 05:48:12 +00:00
|
|
|
raise e
|
|
|
|
|
2023-05-17 09:13:53 +00:00
|
|
|
def get_many(
|
|
|
|
self,
|
2023-05-28 08:59:14 +00:00
|
|
|
offset: int = 0,
|
|
|
|
limit: int = 10,
|
2023-05-27 11:39:20 +00:00
|
|
|
image_origin: Optional[ResourceOrigin] = None,
|
2023-05-28 08:59:14 +00:00
|
|
|
categories: Optional[list[ImageCategory]] = None,
|
2023-05-27 08:32:16 +00:00
|
|
|
is_intermediate: Optional[bool] = None,
|
2023-06-21 09:56:19 +00:00
|
|
|
board_id: Optional[str] = None,
|
2023-05-28 08:59:14 +00:00
|
|
|
) -> OffsetPaginatedResults[ImageDTO]:
|
2023-05-17 09:13:53 +00:00
|
|
|
try:
|
2023-09-24 05:12:51 +00:00
|
|
|
results = self.__invoker.services.image_records.get_many(
|
2023-05-28 08:59:14 +00:00
|
|
|
offset,
|
|
|
|
limit,
|
2023-05-27 11:39:20 +00:00
|
|
|
image_origin,
|
2023-05-28 08:59:14 +00:00
|
|
|
categories,
|
2023-05-26 23:17:06 +00:00
|
|
|
is_intermediate,
|
2023-06-21 09:56:19 +00:00
|
|
|
board_id,
|
2023-05-17 09:13:53 +00:00
|
|
|
)
|
|
|
|
|
2023-05-21 10:05:33 +00:00
|
|
|
image_dtos = list(
|
|
|
|
map(
|
|
|
|
lambda r: image_record_to_dto(
|
|
|
|
r,
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.urls.get_image_url(r.image_name),
|
|
|
|
self.__invoker.services.urls.get_image_url(r.image_name, True),
|
|
|
|
self.__invoker.services.board_image_records.get_board_for_image(r.image_name),
|
2023-05-21 10:05:33 +00:00
|
|
|
),
|
|
|
|
results.items,
|
2023-05-17 09:13:53 +00:00
|
|
|
)
|
2023-05-21 10:05:33 +00:00
|
|
|
)
|
2023-05-17 09:13:53 +00:00
|
|
|
|
2023-05-28 08:59:14 +00:00
|
|
|
return OffsetPaginatedResults[ImageDTO](
|
2023-05-21 10:05:33 +00:00
|
|
|
items=image_dtos,
|
2023-05-28 08:59:14 +00:00
|
|
|
offset=results.offset,
|
|
|
|
limit=results.limit,
|
2023-05-21 10:05:33 +00:00
|
|
|
total=results.total,
|
|
|
|
)
|
2023-05-17 09:13:53 +00:00
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Problem getting paginated image DTOs")
|
2023-05-21 12:15:44 +00:00
|
|
|
raise e
|
|
|
|
|
2023-06-14 11:40:09 +00:00
|
|
|
def delete(self, image_name: str):
|
2023-05-21 12:15:44 +00:00
|
|
|
try:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.image_files.delete(image_name)
|
|
|
|
self.__invoker.services.image_records.delete(image_name)
|
feat(backend): selective invalidation for invocation cache
This change enhances the invocation cache logic to delete cache entries when the resources to which they refer are deleted.
For example, a cached output may refer to "some_image.png". If that image is deleted, and this particular cache entry is later retrieved by a node, that node's successors will receive references to the now non-existent "some_image.png". When they attempt to use that image, they will fail.
To resolve this, we need to invalidate the cache when the resources to which it refers are deleted. Two options:
- Invalidate the whole cache on every image/latents/etc delete
- Selectively invalidate cache entries when their resources are deleted
Node outputs can be any shape, with any number of resource references in arbitrarily nested pydantic models. Traversing that structure to identify resources is not trivial.
But invalidating the whole cache is a bit heavy-handed. It would be nice to be more selective.
Simple solution:
- Invocation outputs' resource references are always string identifiers - like the image's or latents' name
- Invocation outputs can be stringified, which includes said identifiers
- When the invocation is cached, we store the stringified output alongside the "live" output classes
- When a resource is deleted, pass its identifier to the cache service, which can then invalidate any cache entries that refer to it
The images and latents storage services have been outfitted with `on_deleted()` callbacks, and the cache service registers itself to handle those events. This logic was copied from `ItemStorageABC`.
`on_changed()` callback are also added to the images and latents services, though these are not currently used. Just following the existing pattern.
2023-09-20 08:26:47 +00:00
|
|
|
self._on_deleted(image_name)
|
2023-05-23 08:59:43 +00:00
|
|
|
except ImageRecordDeleteException:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Failed to delete image record")
|
2023-05-21 12:15:44 +00:00
|
|
|
raise
|
2023-05-23 08:59:43 +00:00
|
|
|
except ImageFileDeleteException:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Failed to delete image file")
|
2023-05-21 12:15:44 +00:00
|
|
|
raise
|
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Problem deleting image record and file")
|
2023-05-17 09:13:53 +00:00
|
|
|
raise e
|
|
|
|
|
2023-06-26 19:53:21 +00:00
|
|
|
def delete_images_on_board(self, board_id: str):
|
|
|
|
try:
|
2023-09-24 05:12:51 +00:00
|
|
|
image_names = self.__invoker.services.board_image_records.get_all_board_image_names_for_board(board_id)
|
feat(ui): another go at gallery (#3791)
* feat(ui): migrate listImages to RTK query using createEntityAdapter
- see comments in `endpoints/images.ts` for explanation of the caching
- so far, only manually updating `all` images when new image is generated. no other manual cache updates are implemented, but will be needed.
- fixed some weirdness with loading state components (like the spinners in gallery)
- added `useThumbnailFallback` for `IAIDndImage`, this displays the tiny webp thumbnail while the full-size images load
- comment out some old thunk related stuff in gallerySlice, which is no longer needed
* feat(ui): add manual cache updates for board changes (wip)
- update RTK Query caches when adding/removing single image to/from board
- work more on migrating all image-related operations to RTK Query
* update AddImagesToBoardContext so that it works when user uses context menu + modal
* handle case where no image is selected
* get assets working for main list and boards - dnd only
* feat(ui): migrate image uploads to RTK Query
- minor refactor of `ImageUploader` and `useImageUploadButton` hooks, simplify some logic
- style filesystem upload overlay to match existing UI
- replace all old `imageUploaded` thunks with `uploadImage` RTK Query calls, update associated logic including canvas related uploads
- simplify `PostUploadAction`s that only need to display user input
* feat(ui): remove `receivedPageOfImages` thunks
* feat(ui): remove `receivedImageUrls` thunk
* feat(ui): finish removing all images thunks
stuff now broken:
- image usage
- delete board images
- on first load, no image selected
* feat(ui): simplify `updateImage` cache manipulation
- we don't actually ever change categories, so we can remove a lot of logic
* feat(ui): simplify canvas autosave
- instead of using a network request to set the canvas generation as not intermediate, we can just do that in the graph
* feat(ui): simplify & handle edge cases in cache updates
* feat(db, api): support `board_id='none'` for `get_many` images queries
This allows us to get all images that are not on a board.
* chore(ui): regen types
* feat(ui): add `All Assets`, `No Board` boards
Restructure boards:
- `all images` is all images
- `all assets` is all assets
- `no board` is all images/assets without a board set
- user boards may have images and assets
Update caching logic
- much simpler without every board having sub-views of images and assets
- update drag and drop operations for all possible interactions
* chore(ui): regen types
* feat(ui): move download to top of context menu
* feat(ui): improve drop overlay styles
* fix(ui): fix image not selected on first load
- listen for first load of all images board, then select the first image
* feat(ui): refactor board deletion
api changes:
- add route to list all image names for a board. this is required to handle board + image deletion. we need to know every image in the board to determine the image usage across the app. this is fetched only when the delete board and images modal is opened so it's as efficient as it can be.
- update the delete board route to respond with a list of deleted `board_images` and `images`, as image names. this is needed to perform accurate clientside state & cache updates after deleting.
db changes:
- remove unused `board_images` service method to get paginated images dtos for a board. this is now done thru the list images endpoint & images service. needs a small logic change on `images.delete_images_on_board`
ui changes:
- simplify the delete board modal - no context, just minor prop drilling. this is feasible for boards only because the components that need to trigger and manipulate the modal are very close together in the tree
- add cache updates for `deleteBoard` & `deleteBoardAndImages` mutations
- the only thing we cannot do directly is on `deleteBoardAndImages`, update the `No Board` board. we'd need to insert image dtos that we may not have loaded. instead, i am just invalidating the tags for that `listImages` cache. so when you `deleteBoardAndImages`, the `No Board` will re-fetch the initial image limit. i think this is more efficient than e.g. fetching all image dtos to insert then inserting them.
- handle image usage for `deleteBoardAndImages`
- update all (i think/hope) the little bits and pieces in the UI to accomodate these changes
* fix(ui): fix board selection logic
* feat(ui): add delete board modal loading state
* fix(ui): use thumbnails for board cover images
* fix(ui): fix race condition with board selection
when selecting a board that doesn't have any images loaded, we need to wait until the images haveloaded before selecting the first image.
this logic is debounced to ~1000ms.
* feat(ui): name 'No Board' correctly, change icon
* fix(ui): do not cache listAllImageNames query
if we cache it, we can end up with stale image usage during deletion.
we could of course manually update the cache as we are doing elsewhere. but because this is a relatively infrequent network request, i'd like to trade increased cache mgmt complexity here for increased resource usage.
* feat(ui): reduce drag preview opacity, remove border
* fix(ui): fix incorrect queryArg used in `deleteImage` and `updateImage` cache updates
* fix(ui): fix doubled open in new tab
* fix(ui): fix new generations not getting added to 'No Board'
* fix(ui): fix board id not changing on new image when autosave enabled
* fix(ui): context menu when selection is 0
need to revise how context menu is triggered later, when we approach multi select
* fix(ui): fix deleting does not update counts for all images and all assets
* fix(ui): fix all assets board name in boards list collapse button
* fix(ui): ensure we never go under 0 for total board count
* fix(ui): fix text overflow on board names
---------
Co-authored-by: Mary Hipp <maryhipp@Marys-MacBook-Air.local>
2023-07-19 16:06:38 +00:00
|
|
|
for image_name in image_names:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.image_files.delete(image_name)
|
|
|
|
self.__invoker.services.image_records.delete_many(image_names)
|
feat(backend): selective invalidation for invocation cache
This change enhances the invocation cache logic to delete cache entries when the resources to which they refer are deleted.
For example, a cached output may refer to "some_image.png". If that image is deleted, and this particular cache entry is later retrieved by a node, that node's successors will receive references to the now non-existent "some_image.png". When they attempt to use that image, they will fail.
To resolve this, we need to invalidate the cache when the resources to which it refers are deleted. Two options:
- Invalidate the whole cache on every image/latents/etc delete
- Selectively invalidate cache entries when their resources are deleted
Node outputs can be any shape, with any number of resource references in arbitrarily nested pydantic models. Traversing that structure to identify resources is not trivial.
But invalidating the whole cache is a bit heavy-handed. It would be nice to be more selective.
Simple solution:
- Invocation outputs' resource references are always string identifiers - like the image's or latents' name
- Invocation outputs can be stringified, which includes said identifiers
- When the invocation is cached, we store the stringified output alongside the "live" output classes
- When a resource is deleted, pass its identifier to the cache service, which can then invalidate any cache entries that refer to it
The images and latents storage services have been outfitted with `on_deleted()` callbacks, and the cache service registers itself to handle those events. This logic was copied from `ItemStorageABC`.
`on_changed()` callback are also added to the images and latents services, though these are not currently used. Just following the existing pattern.
2023-09-20 08:26:47 +00:00
|
|
|
for image_name in image_names:
|
|
|
|
self._on_deleted(image_name)
|
2023-06-26 19:53:21 +00:00
|
|
|
except ImageRecordDeleteException:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Failed to delete image records")
|
2023-06-26 19:53:21 +00:00
|
|
|
raise
|
|
|
|
except ImageFileDeleteException:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Failed to delete image files")
|
2023-06-26 19:53:21 +00:00
|
|
|
raise
|
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Problem deleting image records and files")
|
2023-06-26 19:53:21 +00:00
|
|
|
raise e
|
2023-07-20 05:44:22 +00:00
|
|
|
|
|
|
|
def delete_intermediates(self) -> int:
|
2023-07-19 14:55:29 +00:00
|
|
|
try:
|
2023-09-24 05:12:51 +00:00
|
|
|
image_names = self.__invoker.services.image_records.delete_intermediates()
|
2023-07-20 05:44:22 +00:00
|
|
|
count = len(image_names)
|
|
|
|
for image_name in image_names:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.image_files.delete(image_name)
|
feat(backend): selective invalidation for invocation cache
This change enhances the invocation cache logic to delete cache entries when the resources to which they refer are deleted.
For example, a cached output may refer to "some_image.png". If that image is deleted, and this particular cache entry is later retrieved by a node, that node's successors will receive references to the now non-existent "some_image.png". When they attempt to use that image, they will fail.
To resolve this, we need to invalidate the cache when the resources to which it refers are deleted. Two options:
- Invalidate the whole cache on every image/latents/etc delete
- Selectively invalidate cache entries when their resources are deleted
Node outputs can be any shape, with any number of resource references in arbitrarily nested pydantic models. Traversing that structure to identify resources is not trivial.
But invalidating the whole cache is a bit heavy-handed. It would be nice to be more selective.
Simple solution:
- Invocation outputs' resource references are always string identifiers - like the image's or latents' name
- Invocation outputs can be stringified, which includes said identifiers
- When the invocation is cached, we store the stringified output alongside the "live" output classes
- When a resource is deleted, pass its identifier to the cache service, which can then invalidate any cache entries that refer to it
The images and latents storage services have been outfitted with `on_deleted()` callbacks, and the cache service registers itself to handle those events. This logic was copied from `ItemStorageABC`.
`on_changed()` callback are also added to the images and latents services, though these are not currently used. Just following the existing pattern.
2023-09-20 08:26:47 +00:00
|
|
|
self._on_deleted(image_name)
|
2023-07-19 14:55:29 +00:00
|
|
|
return count
|
|
|
|
except ImageRecordDeleteException:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Failed to delete image records")
|
2023-07-19 14:55:29 +00:00
|
|
|
raise
|
|
|
|
except ImageFileDeleteException:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Failed to delete image files")
|
2023-07-19 14:55:29 +00:00
|
|
|
raise
|
|
|
|
except Exception as e:
|
2023-09-24 05:12:51 +00:00
|
|
|
self.__invoker.services.logger.error("Problem deleting image records and files")
|
2023-07-19 14:55:29 +00:00
|
|
|
raise e
|