Skip to content

Commit

Permalink
Merge pull request #1005 from roboflow/feat/add-counting-block
Browse files Browse the repository at this point in the history
Add SequenceElementsCount UQL operation
  • Loading branch information
PawelPeczek-Roboflow authored Feb 7, 2025
2 parents 9b48382 + 80b977d commit 7488613
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
13 changes: 5 additions & 8 deletions inference/core/interfaces/http/http_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import base64
import os
import traceback
from contextlib import asynccontextmanager
from functools import partial, wraps
from time import sleep
from typing import Any, Dict, List, Optional, Union
Expand Down Expand Up @@ -535,14 +534,7 @@ def __init__(

description = "Roboflow inference server"

@asynccontextmanager
async def lifespan(app: FastAPI):
yield
logger.info("Shutting down %s", description)
await usage_collector.async_push_usage_payloads()

app = FastAPI(
lifespan=lifespan,
title="Roboflow Inference Server",
description=description,
version=__version__,
Expand All @@ -559,6 +551,11 @@ async def lifespan(app: FastAPI):
root_path=root_path,
)

@app.on_event("shutdown")
async def on_shutdown():
logger.info("Shutting down %s", description)
await usage_collector.async_push_usage_payloads()

if ENABLE_PROMETHEUS:
InferenceInstrumentator(
app, model_manager=model_manager, endpoint="/metrics"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,20 @@ class SequenceLength(OperationDefinition):
type: Literal["SequenceLength"]


class SequenceElementsCount(OperationDefinition):
model_config = ConfigDict(
json_schema_extra={
"description": "Operation counts unique elements of input sequence",
"compound": False,
"input_kind": [
LIST_OF_VALUES_KIND,
],
"output_kind": [DICTIONARY_KIND],
},
)
type: Literal["SequenceElementsCount"]


class SequenceApply(OperationDefinition):
model_config = ConfigDict(
json_schema_extra={
Expand Down Expand Up @@ -582,6 +596,7 @@ class PickDetectionsByParentClass(OperationDefinition):
StringMatches,
ExtractImageProperty,
SequenceLength,
SequenceElementsCount,
Multiply,
Divide,
DetectionsSelection,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
from inference.core.workflows.core_steps.common.query_language.operations.sequences.base import (
aggregate_numeric_sequence,
aggregate_sequence,
get_sequence_elements_count,
get_sequence_length,
sequence_apply,
sequence_map,
Expand Down Expand Up @@ -189,6 +190,7 @@ def build_detections_filter_operation(
"RandomNumber": generate_random_number,
"StringMatches": string_matches,
"SequenceLength": get_sequence_length,
"SequenceElementsCount": get_sequence_elements_count,
"ExtractImageProperty": extract_image_property,
"Multiply": multiply,
"Divide": divide,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections import Counter
from typing import Any, List
from typing import Any, Dict, List

from inference.core.workflows.core_steps.common.query_language.entities.enums import (
SequenceAggregationFunction,
Expand All @@ -8,6 +8,9 @@
from inference.core.workflows.core_steps.common.query_language.errors import (
InvalidInputTypeError,
)
from inference.core.workflows.core_steps.common.query_language.operations.utils import (
safe_stringify,
)


def sequence_map(
Expand Down Expand Up @@ -115,3 +118,21 @@ def get_sequence_length(value: Any, execution_context: str, **kwargs) -> int:
context=f"step_execution | roboflow_query_language_evaluation | {execution_context}",
inner_error=e,
)


def get_sequence_elements_count(
value: Any, execution_context: str, **kwargs
) -> Dict[Any, int]:
try:
res = {}
for v in value:
value_as_str = safe_stringify(value=v)
res[value_as_str] = res.setdefault(value_as_str, 0) + 1
return res
except (TypeError, ValueError) as e:
raise InvalidInputTypeError(
public_message=f"While executing get_sequence_elements_count(...) in context {execution_context}, encountered "
f"value of type {type(value)} which is not suited to execute operation. Details: {e}",
context=f"step_execution | roboflow_query_language_evaluation | {execution_context}",
inner_error=e,
)
2 changes: 1 addition & 1 deletion requirements/_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ APScheduler>=3.10.1,<4.0.0
asyncua~=1.1.5
cython~=3.0.0
python-dotenv~=1.0.0
fastapi>=0.100,<0.111
fastapi>=0.100,<0.111 # be careful with upper pin - fastapi might remove support for on_event
numpy<=1.26.4
opencv-python>=4.8.1.78,<=4.10.0.84
pillow<11.0
Expand Down

0 comments on commit 7488613

Please sign in to comment.