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

Paligemma Workflows Block #399

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions inference/core/workflows/core_steps/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
LMMForClassificationBlock,
)
from inference.core.workflows.core_steps.models.foundation.ocr import OCRModelBlock
from inference.core.workflows.core_steps.models.foundation.paligemma import (
PaligemmaBlock,
hansent marked this conversation as resolved.
Show resolved Hide resolved
)
from inference.core.workflows.core_steps.models.foundation.yolo_world import (
YoloWorldModelBlock,
)
Expand Down Expand Up @@ -74,4 +77,5 @@ def load_blocks() -> list:
DetectionFilterBlock,
DetectionOffsetBlock,
RelativeStaticCropBlock,
PaligemmaBlock,
]
109 changes: 109 additions & 0 deletions inference/core/workflows/core_steps/models/foundation/paligemma.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
from typing import Any, Dict, List, Literal, Optional, Tuple, Type, Union

from pydantic import AliasChoices, ConfigDict, Field

from inference.core.entities.requests.paligemma import PaliGemmaInferenceRequest
from inference.core.managers.base import ModelManager
from inference.core.workflows.core_steps.common.utils import (
attach_parent_info,
attach_prediction_type_info,
load_core_model,
)
from inference.core.workflows.entities.base import OutputDefinition
from inference.core.workflows.entities.types import (
BATCH_OF_PARENT_ID_KIND,
BATCH_OF_STRING_KIND,
STRING_KIND,
FlowControl,
StepOutputImageSelector,
WorkflowImageSelector,
WorkflowParameterSelector,
)
from inference.core.workflows.prototypes.block import (
WorkflowBlock,
WorkflowBlockManifest,
)
from inference.models.paligemma.paligemma import PaliGemma
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly


LONG_DESCRIPTION = """
Paligemma block TODO
"""


class BlockManifest(WorkflowBlockManifest):
model_config = ConfigDict(
json_schema_extra={
"short_description": "Run Paligemma model.",
"long_description": LONG_DESCRIPTION,
"license": "Apache-2.0",
"block_type": "model",
}
)
type: Literal["Paligemma"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be PaliGemmaModel (all model blocks should end in Model IMO)

images: Union[WorkflowImageSelector, StepOutputImageSelector] = Field(
description="Reference at image to be used as input for step processing",
examples=["$inputs.image", "$steps.cropping.crops"],
validation_alias=AliasChoices("images", "image"),
)
prompt: Union[WorkflowParameterSelector(kind=[STRING_KIND]), str] = Field(
description="Holds unconstrained text prompt to LMM mode",
examples=["my prompt", "$inputs.prompt"],
)

@classmethod
def describe_outputs(cls) -> List[OutputDefinition]:
return [
OutputDefinition(name="parent_id", kind=[BATCH_OF_PARENT_ID_KIND]),
OutputDefinition(name="model_output", kind=[BATCH_OF_STRING_KIND]),
]


class PaligemmaBlock(WorkflowBlock):

def __init__(
self,
model_manager: ModelManager,
api_key: Optional[str],
):
self._model_manager = model_manager
self._api_key = api_key

@classmethod
def get_init_parameters(cls) -> List[str]:
return ["model_manager", "api_key"]

@classmethod
def get_manifest(cls) -> Type[WorkflowBlockManifest]:
return BlockManifest

async def run_locally(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want this one to be setup both for local & async execution since it can't run without an NVIDIA GPU. You may want to be doing realtime video for your workflow (eg on a Jetson) but occasionally call out to a beefy server somewhere for a LLM response.

self,
images: List[dict],
prompt: str,
) -> Union[List[Dict[str, Any]], Tuple[List[Dict[str, Any]], FlowControl]]:

responses = []

for img in images:

inference_request = PaliGemmaInferenceRequest(
image=img, prompt=prompt, api_key=self._api_key
)
paligemma_model_id = load_core_model(
model_manager=self._model_manager,
inference_request=inference_request,
core_model="paligemma",
)

response = await self._model_manager.infer_from_request(
paligemma_model_id, inference_request
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this require a server to be running?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I don't think requires server, this is how we load all the models in workflow blocks

)

responses.append(
{
"parent_id": img["parent_id"],
"model_output": response.response,
}
)

return responses
Loading