mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
402cf9b0ee
Refactor services folder/module structure. **Motivation** While working on our services I've repeatedly encountered circular imports and a general lack of clarity regarding where to put things. The structure introduced goes a long way towards resolving those issues, setting us up for a clean structure going forward. **Services** Services are now in their own folder with a few files: - `services/{service_name}/__init__.py`: init as needed, mostly empty now - `services/{service_name}/{service_name}_base.py`: the base class for the service - `services/{service_name}/{service_name}_{impl_type}.py`: the default concrete implementation of the service - typically one of `sqlite`, `default`, or `memory` - `services/{service_name}/{service_name}_common.py`: any common items - models, exceptions, utilities, etc Though it's a bit verbose to have the service name both as the folder name and the prefix for files, I found it is _extremely_ confusing to have all of the base classes just be named `base.py`. So, at the cost of some verbosity when importing things, I've included the service name in the filename. There are some minor logic changes. For example, in `InvocationProcessor`, instead of assigning the model manager service to a variable to be used later in the file, the service is used directly via the `Invoker`. **Shared** Things that are used across disparate services are in `services/shared/`: - `default_graphs.py`: previously in `services/` - `graphs.py`: previously in `services/` - `paginatation`: generic pagination models used in a few services - `sqlite`: the `SqliteDatabase` class, other sqlite-specific things
125 lines
3.8 KiB
Python
125 lines
3.8 KiB
Python
from typing import Any, Callable, Union
|
|
|
|
from pydantic import Field
|
|
|
|
from invokeai.app.invocations.baseinvocation import (
|
|
BaseInvocation,
|
|
BaseInvocationOutput,
|
|
InvocationContext,
|
|
invocation,
|
|
invocation_output,
|
|
)
|
|
from invokeai.app.invocations.image import ImageField
|
|
|
|
|
|
# Define test invocations before importing anything that uses invocations
|
|
@invocation_output("test_list_output")
|
|
class ListPassThroughInvocationOutput(BaseInvocationOutput):
|
|
collection: list[ImageField] = Field(default_factory=list)
|
|
|
|
|
|
@invocation("test_list")
|
|
class ListPassThroughInvocation(BaseInvocation):
|
|
collection: list[ImageField] = Field(default_factory=list)
|
|
|
|
def invoke(self, context: InvocationContext) -> ListPassThroughInvocationOutput:
|
|
return ListPassThroughInvocationOutput(collection=self.collection)
|
|
|
|
|
|
@invocation_output("test_prompt_output")
|
|
class PromptTestInvocationOutput(BaseInvocationOutput):
|
|
prompt: str = Field(default="")
|
|
|
|
|
|
@invocation("test_prompt")
|
|
class PromptTestInvocation(BaseInvocation):
|
|
prompt: str = Field(default="")
|
|
|
|
def invoke(self, context: InvocationContext) -> PromptTestInvocationOutput:
|
|
return PromptTestInvocationOutput(prompt=self.prompt)
|
|
|
|
|
|
@invocation("test_error")
|
|
class ErrorInvocation(BaseInvocation):
|
|
def invoke(self, context: InvocationContext) -> PromptTestInvocationOutput:
|
|
raise Exception("This invocation is supposed to fail")
|
|
|
|
|
|
@invocation_output("test_image_output")
|
|
class ImageTestInvocationOutput(BaseInvocationOutput):
|
|
image: ImageField = Field()
|
|
|
|
|
|
@invocation("test_text_to_image")
|
|
class TextToImageTestInvocation(BaseInvocation):
|
|
prompt: str = Field(default="")
|
|
prompt2: str = Field(default="")
|
|
|
|
def invoke(self, context: InvocationContext) -> ImageTestInvocationOutput:
|
|
return ImageTestInvocationOutput(image=ImageField(image_name=self.id))
|
|
|
|
|
|
@invocation("test_image_to_image")
|
|
class ImageToImageTestInvocation(BaseInvocation):
|
|
prompt: str = Field(default="")
|
|
image: Union[ImageField, None] = Field(default=None)
|
|
|
|
def invoke(self, context: InvocationContext) -> ImageTestInvocationOutput:
|
|
return ImageTestInvocationOutput(image=ImageField(image_name=self.id))
|
|
|
|
|
|
@invocation_output("test_prompt_collection_output")
|
|
class PromptCollectionTestInvocationOutput(BaseInvocationOutput):
|
|
collection: list[str] = Field(default_factory=list)
|
|
|
|
|
|
@invocation("test_prompt_collection")
|
|
class PromptCollectionTestInvocation(BaseInvocation):
|
|
collection: list[str] = Field()
|
|
|
|
def invoke(self, context: InvocationContext) -> PromptCollectionTestInvocationOutput:
|
|
return PromptCollectionTestInvocationOutput(collection=self.collection.copy())
|
|
|
|
|
|
# Importing these must happen after test invocations are defined or they won't register
|
|
from invokeai.app.services.events.events_base import EventServiceBase # noqa: E402
|
|
from invokeai.app.services.shared.graph import Edge, EdgeConnection # noqa: E402
|
|
|
|
|
|
def create_edge(from_id: str, from_field: str, to_id: str, to_field: str) -> Edge:
|
|
return Edge(
|
|
source=EdgeConnection(node_id=from_id, field=from_field),
|
|
destination=EdgeConnection(node_id=to_id, field=to_field),
|
|
)
|
|
|
|
|
|
class TestEvent:
|
|
event_name: str
|
|
payload: Any
|
|
|
|
def __init__(self, event_name: str, payload: Any):
|
|
self.event_name = event_name
|
|
self.payload = payload
|
|
|
|
|
|
class TestEventService(EventServiceBase):
|
|
events: list
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.events = list()
|
|
|
|
def dispatch(self, event_name: str, payload: Any) -> None:
|
|
pass
|
|
|
|
|
|
def wait_until(condition: Callable[[], bool], timeout: int = 10, interval: float = 0.1) -> None:
|
|
import time
|
|
|
|
start_time = time.time()
|
|
while time.time() - start_time < timeout:
|
|
if condition():
|
|
return
|
|
time.sleep(interval)
|
|
raise TimeoutError("Condition not met")
|