-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split run_app.py and api_app.py so that api_app.py is more narrowly r…
…esponsible for just initializing the FastAPI app. This also gives clearer control over the order of the initialization steps, which will be important as we add planned torch configurations that must be applied before torch is imported.
- Loading branch information
Showing
2 changed files
with
62 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,68 @@ | ||
"""This is a wrapper around the main app entrypoint, to allow for CLI args to be parsed before running the app.""" | ||
import uvicorn | ||
|
||
from invokeai.app.services.config.config_default import get_config | ||
from invokeai.app.util.startup_utils import ( | ||
apply_monkeypatches, | ||
check_cudnn, | ||
enable_dev_reload, | ||
find_open_port, | ||
register_mime_types, | ||
) | ||
from invokeai.backend.util.logging import InvokeAILogger | ||
from invokeai.frontend.cli.arg_parser import InvokeAIArgs | ||
|
||
def run_app() -> None: | ||
# Before doing _anything_, parse CLI args! | ||
from invokeai.frontend.cli.arg_parser import InvokeAIArgs | ||
|
||
def get_app(): | ||
"""Import the app and event loop. We wrap this in a function to more explicitly control when it happens, because | ||
importing from api_app does a bunch of stuff - it's more like calling a function than importing a module. | ||
""" | ||
from invokeai.app.api_app import app, loop | ||
|
||
return app, loop | ||
|
||
|
||
def run_app() -> None: | ||
"""The main entrypoint for the app.""" | ||
# Parse the CLI arguments. | ||
InvokeAIArgs.parse_args() | ||
|
||
from invokeai.app.api_app import invoke_api | ||
# Load config. | ||
app_config = get_config() | ||
|
||
logger = InvokeAILogger.get_logger(config=app_config) | ||
|
||
# Find an open port, and modify the config accordingly. | ||
orig_config_port = app_config.port | ||
app_config.port = find_open_port(app_config.port) | ||
if orig_config_port != app_config.port: | ||
logger.warning(f"Port {orig_config_port} is already in use. Using port {app_config.port}.") | ||
|
||
# Miscellaneous startup tasks. | ||
apply_monkeypatches() | ||
register_mime_types() | ||
if app_config.dev_reload: | ||
enable_dev_reload() | ||
check_cudnn(logger) | ||
|
||
# Initialize the app and event loop. | ||
app, loop = get_app() | ||
|
||
# Start the server. | ||
config = uvicorn.Config( | ||
app=app, | ||
host=app_config.host, | ||
port=app_config.port, | ||
loop="asyncio", | ||
log_level=app_config.log_level_network, | ||
ssl_certfile=app_config.ssl_certfile, | ||
ssl_keyfile=app_config.ssl_keyfile, | ||
) | ||
server = uvicorn.Server(config) | ||
|
||
# replace uvicorn's loggers with InvokeAI's for consistent appearance | ||
uvicorn_logger = InvokeAILogger.get_logger("uvicorn") | ||
uvicorn_logger.handlers.clear() | ||
for hdlr in logger.handlers: | ||
uvicorn_logger.addHandler(hdlr) | ||
|
||
invoke_api() | ||
loop.run_until_complete(server.serve()) |