2022-12-01 05:33:20 +00:00
|
|
|
# Copyright (c) 2022 Kyle Schouviller (https://github.com/kyle0654)
|
|
|
|
|
|
|
|
import os
|
2023-03-03 06:02:00 +00:00
|
|
|
from argparse import Namespace
|
2022-12-01 05:33:20 +00:00
|
|
|
|
2023-03-04 01:19:37 +00:00
|
|
|
from ...backend import Globals
|
remove factory pattern
Factory pattern is now removed. Typical usage of the InvokeAIGenerator is now:
```
from invokeai.backend.generator import (
InvokeAIGeneratorBasicParams,
Txt2Img,
Img2Img,
Inpaint,
)
params = InvokeAIGeneratorBasicParams(
model_name = 'stable-diffusion-1.5',
steps = 30,
scheduler = 'k_lms',
cfg_scale = 8.0,
height = 640,
width = 640
)
print ('=== TXT2IMG TEST ===')
txt2img = Txt2Img(manager, params)
outputs = txt2img.generate(prompt='banana sushi', iterations=2)
for i in outputs:
print(f'image={output.image}, seed={output.seed}, model={output.params.model_name}, hash={output.model_hash}, steps={output.params.steps}')
```
The `params` argument is optional, so if you wish to accept default
parameters and selectively override them, just do this:
```
outputs = Txt2Img(manager).generate(prompt='banana sushi',
steps=50,
scheduler='k_heun',
model_name='stable-diffusion-2.1'
)
```
2023-03-11 00:33:04 +00:00
|
|
|
from ..services.generate_initializer import get_model_manager
|
2023-03-03 06:02:00 +00:00
|
|
|
from ..services.graph import GraphExecutionState
|
2022-12-01 05:33:20 +00:00
|
|
|
from ..services.image_storage import DiskImageStorage
|
|
|
|
from ..services.invocation_queue import MemoryInvocationQueue
|
|
|
|
from ..services.invocation_services import InvocationServices
|
2023-02-25 04:11:28 +00:00
|
|
|
from ..services.invoker import Invoker
|
2023-03-03 06:02:00 +00:00
|
|
|
from ..services.processor import DefaultInvocationProcessor
|
|
|
|
from ..services.sqlite import SqliteItemStorage
|
2022-12-01 05:33:20 +00:00
|
|
|
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:
|
|
|
|
"""
|
2022-12-01 05:33:20 +00:00
|
|
|
Return true if the internet is reachable.
|
|
|
|
It does this by pinging huggingface.co.
|
2023-03-03 06:02:00 +00:00
|
|
|
"""
|
2022-12-01 05:33:20 +00:00
|
|
|
import urllib.request
|
2023-03-03 06:02:00 +00:00
|
|
|
|
|
|
|
host = "http://huggingface.co"
|
2022-12-01 05:33:20 +00:00
|
|
|
try:
|
2023-03-03 06:02:00 +00:00
|
|
|
urllib.request.urlopen(host, timeout=1)
|
2022-12-01 05:33:20 +00:00
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
class ApiDependencies:
|
|
|
|
"""Contains and initializes all dependencies for the API"""
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
invoker: Invoker = None
|
|
|
|
|
|
|
|
@staticmethod
|
2023-03-03 06:02:00 +00:00
|
|
|
def initialize(args, config, event_handler_id: int):
|
2022-12-01 05:33:20 +00:00
|
|
|
Globals.try_patchmatch = args.patchmatch
|
|
|
|
Globals.always_use_cpu = args.always_use_cpu
|
|
|
|
Globals.internet_available = args.internet_available and check_internet()
|
|
|
|
Globals.disable_xformers = not args.xformers
|
|
|
|
Globals.ckpt_convert = args.ckpt_convert
|
|
|
|
|
|
|
|
# TODO: Use a logger
|
2023-03-03 06:02:00 +00:00
|
|
|
print(f">> Internet connectivity is {Globals.internet_available}")
|
2022-12-01 05:33:20 +00:00
|
|
|
|
remove factory pattern
Factory pattern is now removed. Typical usage of the InvokeAIGenerator is now:
```
from invokeai.backend.generator import (
InvokeAIGeneratorBasicParams,
Txt2Img,
Img2Img,
Inpaint,
)
params = InvokeAIGeneratorBasicParams(
model_name = 'stable-diffusion-1.5',
steps = 30,
scheduler = 'k_lms',
cfg_scale = 8.0,
height = 640,
width = 640
)
print ('=== TXT2IMG TEST ===')
txt2img = Txt2Img(manager, params)
outputs = txt2img.generate(prompt='banana sushi', iterations=2)
for i in outputs:
print(f'image={output.image}, seed={output.seed}, model={output.params.model_name}, hash={output.model_hash}, steps={output.params.steps}')
```
The `params` argument is optional, so if you wish to accept default
parameters and selectively override them, just do this:
```
outputs = Txt2Img(manager).generate(prompt='banana sushi',
steps=50,
scheduler='k_heun',
model_name='stable-diffusion-2.1'
)
```
2023-03-11 00:33:04 +00:00
|
|
|
model_manager = get_model_manager(args, config)
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
)
|
2022-12-01 05:33:20 +00:00
|
|
|
|
|
|
|
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")
|
2022-12-01 05:33:20 +00:00
|
|
|
|
2023-02-25 04:11:28 +00:00
|
|
|
services = InvocationServices(
|
2023-03-09 05:18:29 +00:00
|
|
|
generator_factory=generator_factory,
|
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(),
|
2022-12-01 05:33:20 +00:00
|
|
|
)
|
|
|
|
|
2023-02-25 04:11:28 +00:00
|
|
|
ApiDependencies.invoker = Invoker(services)
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2022-12-01 05:33:20 +00:00
|
|
|
@staticmethod
|
|
|
|
def shutdown():
|
|
|
|
if ApiDependencies.invoker:
|
|
|
|
ApiDependencies.invoker.stop()
|