InvokeAI/invokeai/app/api/dependencies.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
3.7 KiB
Python
Raw Normal View History

# Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654)
2023-05-21 10:24:37 +00:00
from logging import Logger
import os
from invokeai.app.services.image_record_storage import SqliteImageRecordStorage
from invokeai.app.services.images import ImageService
2023-05-22 05:48:12 +00:00
from invokeai.app.services.metadata import CoreMetadataService
from invokeai.app.services.resource_name import SimpleNameService
from invokeai.app.services.urls import LocalUrlService
2023-05-21 10:24:37 +00:00
from invokeai.backend.util.logging import InvokeAILogger
from ..services.default_graphs import create_system_graphs
from ..services.latent_storage import DiskLatentsStorage, ForwardCacheLatentsStorage
from ..services.model_manager_initializer import get_model_manager
2023-03-11 22:00:00 +00:00
from ..services.restoration_services import RestorationServices
from ..services.graph import GraphExecutionState, LibraryGraph
from ..services.image_file_storage import DiskImageFileStorage
from ..services.invocation_queue import MemoryInvocationQueue
from ..services.invocation_services import InvocationServices
from ..services.invoker import Invoker
2023-03-03 06:02:00 +00:00
from ..services.processor import DefaultInvocationProcessor
from ..services.sqlite import SqliteItemStorage
from .events import FastAPIEventService
# TODO: is there a better way to achieve this?
2023-03-03 06:02:00 +00:00
def check_internet() -> bool:
"""
Return true if the internet is reachable.
It does this by pinging huggingface.co.
2023-03-03 06:02:00 +00:00
"""
import urllib.request
2023-03-03 06:02:00 +00:00
host = "http://huggingface.co"
try:
2023-03-03 06:02:00 +00:00
urllib.request.urlopen(host, timeout=1)
return True
except:
return False
2023-05-21 10:24:37 +00:00
logger = InvokeAILogger.getLogger()
class ApiDependencies:
"""Contains and initializes all dependencies for the API"""
2023-03-03 06:02:00 +00:00
invoker: Invoker = None
2023-05-24 15:00:07 +00:00
@staticmethod
def initialize(config, event_handler_id: int, logger: Logger = logger):
logger.info(f"Internet connectivity is {config.internet_available}")
events = FastAPIEventService(event_handler_id)
2023-03-03 06:02:00 +00:00
output_folder = os.path.abspath(
os.path.join(os.path.dirname(__file__), "../../../../outputs")
)
# TODO: build a file/path manager?
2023-03-03 06:02:00 +00:00
db_location = os.path.join(output_folder, "invokeai.db")
graph_execution_manager = SqliteItemStorage[GraphExecutionState](
filename=db_location, table_name="graph_executions"
)
urls = LocalUrlService()
metadata = CoreMetadataService()
image_record_storage = SqliteImageRecordStorage(db_location)
image_file_storage = DiskImageFileStorage(f"{output_folder}/images")
names = SimpleNameService()
latents = ForwardCacheLatentsStorage(
DiskLatentsStorage(f"{output_folder}/latents")
)
images = ImageService(
image_record_storage=image_record_storage,
image_file_storage=image_file_storage,
metadata=metadata,
url=urls,
logger=logger,
names=names,
2023-05-22 05:48:12 +00:00
graph_execution_manager=graph_execution_manager,
)
services = InvocationServices(
model_manager=get_model_manager(config, logger),
2023-03-03 06:02:00 +00:00
events=events,
latents=latents,
images=images,
2023-03-03 06:02:00 +00:00
queue=MemoryInvocationQueue(),
graph_library=SqliteItemStorage[LibraryGraph](
filename=db_location, table_name="graphs"
),
graph_execution_manager=graph_execution_manager,
2023-03-03 06:02:00 +00:00
processor=DefaultInvocationProcessor(),
2023-05-24 15:00:07 +00:00
restoration=RestorationServices(config, logger),
configuration=config,
logger=logger,
)
create_system_graphs(services.graph_library)
ApiDependencies.invoker = Invoker(services)
2023-03-03 06:02:00 +00:00
@staticmethod
def shutdown():
if ApiDependencies.invoker:
ApiDependencies.invoker.stop()