Skip to content

Commit

Permalink
Merge pull request #783 from roboflow/enable_gzip
Browse files Browse the repository at this point in the history
Add gzip support
  • Loading branch information
PawelPeczek-Roboflow authored Nov 7, 2024
2 parents b267e96 + 4c71598 commit 5068dd0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
26 changes: 17 additions & 9 deletions inference/core/interfaces/http/http_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Empty file.
22 changes: 22 additions & 0 deletions inference/core/interfaces/http/middlewares/gzip.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5068dd0

Please sign in to comment.