Skip to content
Draft
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
62 changes: 62 additions & 0 deletions sdks/python/apache_beam/yaml/tests/runinference_huggingface.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

fixtures:
- name: mock_pipeline
type: unittest.mock.patch
config:
target: transformers.pipeline

pipelines:
- pipeline:
type: chain
transforms:
- type: Create
config:
elements:
- text: "Hello world"
- text: "Bye world"
- type: RunInference
config:
model_handler:
type: "HuggingFacePipeline"
config:
task: "text-classification"
model: "unused"
inference_fn:
callable: |
def mock_inference(batch, pipeline, inference_args):
return [[dict(label='POSITIVE', score=0.9)] for _ in batch]
preprocess:
callable: 'lambda x: x.text'
- type: MapToFields
config:
language: python
fields:
text: text
inference:
callable: |
def get_json(x):
import json
return json.dumps(x.inference.inference, indent=0).strip()
- type: AssertEqual
config:
elements:
- text: "Hello world"
inference: "[\n{\n\"label\": \"POSITIVE\",\n\"score\": 0.9\n}\n]"
- text: "Bye world"
inference: "[\n{\n\"label\": \"POSITIVE\",\n\"score\": 0.9\n}\n]"
options:
yaml_experimental_features: ['ML']
46 changes: 46 additions & 0 deletions sdks/python/apache_beam/yaml/yaml_ml.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,52 @@ def inference_output_type(self):
('model_id', Optional[str])])


@ModelHandlerProvider.register_handler_type('HuggingFacePipeline')
class HuggingFacePipelineProvider(ModelHandlerProvider):
def __init__(
self,
task: str = "",
model: str = "",
preprocess: Optional[dict[str, str]] = None,
postprocess: Optional[dict[str, str]] = None,
device: Optional[str] = None,
inference_fn: Optional[dict[str, str]] = None,
load_pipeline_args: Optional[dict[str, Any]] = None,
**kwargs):
try:
from apache_beam.ml.inference.huggingface_inference import HuggingFacePipelineModelHandler
except ImportError:
raise ValueError(
'Unable to import HuggingFacePipelineModelHandler. Please '
'install transformers dependencies.')

kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}

inference_fn_obj = self.parse_processing_transform(
inference_fn, 'inference_fn') if inference_fn else None

handler_kwargs = {}
if inference_fn_obj:
handler_kwargs['inference_fn'] = inference_fn_obj

_handler = HuggingFacePipelineModelHandler(
task=task,
model=model,
device=device,
load_pipeline_args=load_pipeline_args,
**handler_kwargs,
**kwargs)

super().__init__(_handler, preprocess, postprocess)

@staticmethod
def validate(model_handler_spec):
pass

def inference_output_type(self):
return Any


@beam.ptransform.ptransform_fn
def run_inference(
pcoll,
Expand Down
Loading