async def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) install_serve_encoders_to_fastapi() self._serve_app = frozen_app # Use uvicorn's lifespan handling code to properly deal with # startup and shutdown event. self._serve_asgi_lifespan = LifespanOn( Config(self._serve_app, lifespan="on")) # Replace uvicorn logger with our own. self._serve_asgi_lifespan.logger = logger # LifespanOn's logger logs in INFO level thus becomes spammy # Within this block we temporarily uplevel for cleaner logging with LoggingContext(self._serve_asgi_lifespan.logger, level=logging.WARNING): await self._serve_asgi_lifespan.startup()
def __init__(self, http_adapter: Optional[Union[str, HTTPAdapterFn, Type[BaseModel]]] = None): """Create a FastAPI endpoint annotated with http_adapter dependency. Args: http_adapter(str, HTTPAdapterFn, None, Type[pydantic.BaseModel]): The FastAPI input conversion function or a pydantic model class. By default, Serve will directly pass in the request object starlette.requests.Request. You can pass in any FastAPI dependency resolver. When you pass in a string, Serve will import it. Please refer to Serve HTTP adatper documentation to learn more. """ install_serve_encoders_to_fastapi() http_adapter = load_http_adapter(http_adapter) self.app = FastAPI() @self.app.get("/") @self.app.post("/") async def handle_request(inp=Depends(http_adapter)): resp = await self.predict(inp) return resp