Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gzip support #783

Merged
merged 6 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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