diff --git a/inference/core/interfaces/http/http_api.py b/inference/core/interfaces/http/http_api.py index d66c1bde9..04d0fd089 100644 --- a/inference/core/interfaces/http/http_api.py +++ b/inference/core/interfaces/http/http_api.py @@ -7,7 +7,7 @@ import asgi_correlation_id import uvicorn -from fastapi import BackgroundTasks, FastAPI, Path, Query, Request +from fastapi import BackgroundTasks, Depends, FastAPI, Path, Query, Request from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse, RedirectResponse, Response from fastapi.staticfiles import StaticFiles @@ -158,6 +158,7 @@ handle_describe_workflows_blocks_request, handle_describe_workflows_interface, ) +from inference.core.interfaces.http.middlewares.gzip import gzip_response_if_requested from inference.core.interfaces.http.orjson_utils import ( orjson_response, serialise_workflow_result, @@ -1243,8 +1244,11 @@ async def get_execution_engine_versions() -> ExecutionEngineVersions: deprecated=True, ) @with_route_exceptions - async def describe_workflows_blocks() -> WorkflowsBlocksDescription: - return handle_describe_workflows_blocks_request() + async def describe_workflows_blocks( + request: Request, + ) -> Union[WorkflowsBlocksDescription, Response]: + result = handle_describe_workflows_blocks_request() + return gzip_response_if_requested(request=request, response=result) @app.post( "/workflows/blocks/describe", @@ -1258,20 +1262,24 @@ async def describe_workflows_blocks() -> WorkflowsBlocksDescription: ) @with_route_exceptions async def describe_workflows_blocks( - request: Optional[DescribeBlocksRequest] = None, - ) -> WorkflowsBlocksDescription: + request: Request, + request_payload: Optional[DescribeBlocksRequest] = None, + ) -> Union[WorkflowsBlocksDescription, Response]: # TODO: get rid of async: https://github.com/roboflow/inference/issues/569 dynamic_blocks_definitions = None requested_execution_engine_version = None - if request is not None: - dynamic_blocks_definitions = request.dynamic_blocks_definitions + if request_payload is not None: + dynamic_blocks_definitions = ( + request_payload.dynamic_blocks_definitions + ) requested_execution_engine_version = ( - request.execution_engine_version + request_payload.execution_engine_version ) - return handle_describe_workflows_blocks_request( + result = handle_describe_workflows_blocks_request( dynamic_blocks_definitions=dynamic_blocks_definitions, requested_execution_engine_version=requested_execution_engine_version, ) + return gzip_response_if_requested(request=request, response=result) @app.get( "/workflows/definition/schema", diff --git a/inference/core/interfaces/http/middlewares/__init__.py b/inference/core/interfaces/http/middlewares/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/inference/core/interfaces/http/middlewares/gzip.py b/inference/core/interfaces/http/middlewares/gzip.py new file mode 100644 index 000000000..d6461bc54 --- /dev/null +++ b/inference/core/interfaces/http/middlewares/gzip.py @@ -0,0 +1,22 @@ +import gzip +from typing import TypeVar, Union + +from fastapi import Request, Response +from pydantic import BaseModel + +T = TypeVar("T", bound=BaseModel) + + +def gzip_response_if_requested( + request: Request, + response: T, +) -> Union[Response, T]: + if "gzip" not in request.headers.get("Accept-Encoding", ""): + return response + response = Response( + content=response.json(), + ) + response.body = gzip.compress(response.body) + response.headers["Content-Encoding"] = "gzip" + response.headers["Content-Length"] = str(len(response.body)) + return response