@@ -282,8 +282,8 @@ def inference_output_type(self):
282282 ('model_id' , Optional [str ])])
283283
284284
285- @ModelHandlerProvider .register_handler_type ('HuggingFacePipeline ' )
286- class HuggingFacePipelineProvider (ModelHandlerProvider ):
285+ @ModelHandlerProvider .register_handler_type ('HuggingFacePipelineModelHandler ' )
286+ class HuggingFacePipelineModelHandlerProvider (ModelHandlerProvider ):
287287 def __init__ (
288288 self ,
289289 task : Optional [str ] = None ,
@@ -294,6 +294,49 @@ def __init__(
294294 inference_fn : Optional [dict [str , str ]] = None ,
295295 load_pipeline_args : Optional [dict [str , Any ]] = None ,
296296 ** kwargs ):
297+ """
298+ ModelHandler for Hugging Face Pipelines.
299+
300+ This Model Handler can be used with RunInference to load a model using
301+ Hugging Face pipelines. Hugging Face pipelines provide a simple way to
302+ perform inference on various tasks (e.g. text classification, token
303+ classification, text generation).
304+
305+ This Model Handler requires either a `task` or `model` to be specified.
306+ Preprocessing and Postprocessing are described in more detail in the
307+ RunInference docs:
308+ https://beam.apache.org/releases/yamldoc/current/#runinference
309+
310+ For example: ::
311+
312+ - type: RunInference
313+ config:
314+ model_handler:
315+ type: HuggingFacePipelineModelHandler
316+ config:
317+ task: text-classification
318+ model: distilbert-base-uncased-finetuned-sst-2-english
319+ preprocess:
320+ callable: 'lambda x: x.text'
321+
322+ Args:
323+ task: The task for the pipeline. See Hugging Face documentation for
324+ a list of supported tasks.
325+ model: The model name on Hugging Face hub or a path to a local directory.
326+ If the model already defines the task, no need to specify the task.
327+ preprocess: A python callable, defined either inline, or using a file,
328+ that is invoked on the input row before sending to the model to be
329+ loaded by this ModelHandler.
330+ postprocess: A python callable, defined either inline, or using a file,
331+ that is invoked on the PredictionResult output by the ModelHandler
332+ before parsing into the output Beam Row.
333+ device: The device to run the pipeline on (e.g., 'cpu', 'cuda', 'cuda:0').
334+ Defaults to CPU.
335+ inference_fn: The custom inference function to use.
336+ load_pipeline_args: Extra arguments to pass to the Hugging Face pipeline
337+ loader (e.g. `transformers.pipeline`).
338+ **kwargs: Extra arguments to pass to the model handler.
339+ """
297340 try :
298341 from apache_beam .ml .inference .huggingface_inference import HuggingFacePipelineModelHandler
299342 except ImportError :
@@ -324,7 +367,7 @@ def __init__(
324367 def validate (config ):
325368 if not config or (not config .get ('task' ) and not config .get ('model' )):
326369 raise ValueError (
327- "HuggingFacePipeline requires either 'task' or "
370+ "HuggingFacePipelineModelHandler requires either 'task' or "
328371 "'model' to be specified." )
329372
330373 def inference_output_type (self ):
@@ -488,11 +531,11 @@ def fn(x: PredictionResult):
488531
489532 Args:
490533 model_handler: Specifies the parameters for the respective
491- enrichment_handler in a YAML/JSON format. To see the full set of
534+ model_handler in a YAML/JSON format. To see the full set of
492535 handler_config parameters, see their corresponding doc pages:
493536
494537 - [VertexAIModelHandlerJSON](https://beam.apache.org/releases/pydoc/current/apache_beam.yaml.yaml_ml.VertexAIModelHandlerJSONProvider) # pylint: disable=line-too-long
495- - [HuggingFacePipelineProvider ](https://beam.apache.org/releases/pydoc/current/apache_beam.yaml.yaml_ml.HuggingFacePipelineProvider ) # pylint: disable=line-too-long
538+ - [HuggingFacePipelineModelHandler ](https://beam.apache.org/releases/pydoc/current/apache_beam.yaml.yaml_ml.HuggingFacePipelineModelHandlerProvider ) # pylint: disable=line-too-long
496539 inference_tag: The tag to use for the returned inference. Default is
497540 'inference'.
498541 inference_args: Extra arguments for models whose inference call requires
0 commit comments