InvokeAI/invokeai/app/api/dependencies.py

80 lines
2.5 KiB
Python
Raw Normal View History

# Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654)
import os
2023-03-03 06:02:00 +00:00
from argparse import Namespace
from ...backend import Globals
from ..services.model_manager_initializer import get_model_manager
2023-03-11 22:00:00 +00:00
from ..services.restoration_services import RestorationServices
2023-03-03 06:02:00 +00:00
from ..services.graph import GraphExecutionState
from ..services.image_storage import DiskImageStorage
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
class ApiDependencies:
"""Contains and initializes all dependencies for the API"""
2023-03-03 06:02:00 +00:00
invoker: Invoker = None
@staticmethod
2023-03-11 16:32:57 +00:00
def initialize(config, event_handler_id: int):
Globals.try_patchmatch = config.patchmatch
Globals.always_use_cpu = config.always_use_cpu
Globals.internet_available = config.internet_available and check_internet()
Globals.disable_xformers = not config.xformers
Globals.ckpt_convert = config.ckpt_convert
# TODO: Use a logger
2023-03-03 06:02:00 +00:00
print(f">> Internet connectivity is {Globals.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")
)
images = DiskImageStorage(output_folder)
# TODO: build a file/path manager?
2023-03-03 06:02:00 +00:00
db_location = os.path.join(output_folder, "invokeai.db")
services = InvocationServices(
2023-03-11 16:32:57 +00:00
model_manager=get_model_manager(config),
2023-03-03 06:02:00 +00:00
events=events,
images=images,
queue=MemoryInvocationQueue(),
graph_execution_manager=SqliteItemStorage[GraphExecutionState](
filename=db_location, table_name="graph_executions"
),
processor=DefaultInvocationProcessor(),
2023-03-11 22:00:00 +00:00
restoration=RestorationServices(config),
)
ApiDependencies.invoker = Invoker(services)
2023-03-03 06:02:00 +00:00
@staticmethod
def shutdown():
if ApiDependencies.invoker:
ApiDependencies.invoker.stop()