-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decouple code for server startup, lifecycle hooks, FastAPI app, and r…
…outes
- Loading branch information
Showing
6 changed files
with
93 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import logging | ||
|
||
from fastapi import FastAPI | ||
|
||
from .lifecycle_hooks import lifespan | ||
from .routes import application_router, generation_router | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def add_routes(app: FastAPI): | ||
app.include_router(application_router) | ||
app.include_router(generation_router) | ||
|
||
|
||
def get_app() -> FastAPI: | ||
app = FastAPI(lifespan=lifespan) | ||
app = add_routes(app) | ||
return app |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Copyright 2024 Advanced Micro Devices, Inc. | ||
# | ||
# Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
# See https://llvm.org/LICENSE.txt for license information. | ||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
from contextlib import asynccontextmanager | ||
import logging | ||
from typing import Any | ||
from fastapi import FastAPI | ||
|
||
from .components.manager import SystemManager | ||
|
||
sysman: SystemManager | ||
services: dict[str, Any] = {} | ||
|
||
|
||
@asynccontextmanager | ||
async def lifespan(app: FastAPI): | ||
global sysman | ||
global services | ||
|
||
sysman.start() | ||
try: | ||
for service_name, service in services.items(): | ||
logging.info("Initializing service '%s': %r", service_name, service) | ||
service.start() | ||
except: | ||
sysman.shutdown() | ||
raise | ||
yield | ||
try: | ||
for service_name, service in services.items(): | ||
logging.info("Shutting down service '%s'", service_name) | ||
service.shutdown() | ||
finally: | ||
sysman.shutdown() |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from .application import application_router | ||
from .generate import generation_router | ||
|
||
__all__ = ["application_router", "generation_router"] |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from fastapi import APIRouter, Response | ||
|
||
application_router = APIRouter() | ||
|
||
|
||
@application_router.get("/health") | ||
async def health() -> Response: | ||
return Response(status_code=200) |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from fastapi import APIRouter, Request | ||
|
||
from shortfin.interop.fastapi import FastAPIResponder | ||
|
||
from ..components.generate import ClientGenerateBatchProcess | ||
from ..components.io_struct import GenerateReqInput | ||
from ..lifecycle_hooks import services | ||
|
||
generation_router = APIRouter() | ||
|
||
|
||
@generation_router.post("/generate") | ||
@generation_router.put("/generate") | ||
async def generate_request(gen_req: GenerateReqInput, request: Request): | ||
service = services["default"] | ||
gen_req.post_init() | ||
responder = FastAPIResponder(request) | ||
ClientGenerateBatchProcess(service, gen_req, responder).launch() | ||
return await responder.response |
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