Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.6k
Hugging face model handler #3#38696
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
2ca9ed8105fd221076e2716cb9894a6b393a252373b7a755471dc2f421d5116d5ab85bde050388cf9686f8b3ec5524f2857b6fc74File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| { | ||
| "comment": "Modify this file in a trivial way to cause this test suite to run", | ||
| "revision": 2 | ||
| "revision": 3 | ||
| } |
| 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. | ||
| pipelines: | ||
| - pipeline: | ||
| type: chain | ||
| transforms: | ||
| - type: Create | ||
| config: | ||
| elements: | ||
| - text: "I love Apache Beam!" | ||
| - text: "I hate this error." | ||
| - type: RunInference | ||
| config: | ||
| model_handler: | ||
| type: "HuggingFacePipeline" | ||
| config: | ||
| task: "text-classification" | ||
| inference_fn: | ||
| callable: | | ||
| def real_inference(batch, pipeline, inference_args): | ||
| predictions = pipeline(batch, **inference_args) | ||
| # If it's a single dictionary (batch size of 1), wrap it in a list | ||
| if isinstance(predictions, dict): | ||
| predictions = [predictions] | ||
| return { | ||
| 'label': [p['label'] for p in predictions], | ||
| 'score': [p['score'] for p in predictions] | ||
| } | ||
derrickaw marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| preprocess: | ||
| callable: 'lambda x: x.text' | ||
| - type: MapToFields | ||
| config: | ||
| language: python | ||
| fields: | ||
| text: text | ||
| sentiment: | ||
| callable: 'lambda x: x.inference.inference["label"]' | ||
| - type: AssertEqual | ||
| config: | ||
| elements: | ||
| - text: "I love Apache Beam!" | ||
| sentiment: "POSITIVE" | ||
| - text: "I hate this error." | ||
| sentiment: "NEGATIVE" | ||
| options: | ||
| yaml_experimental_features: ['ML'] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -282,6 +282,55 @@ def inference_output_type(self): | ||
| ('model_id', Optional[str])]) | ||
| @ModelHandlerProvider.register_handler_type('HuggingFacePipeline') | ||
| class HuggingFacePipelineProvider(ModelHandlerProvider): | ||
| def __init__( | ||
| self, | ||
| task: Optional[str] = None, | ||
| model: Optional[str] = None, | ||
| preprocess: Optional[dict[str, str]] = None, | ||
| postprocess: Optional[dict[str, str]] = None, | ||
| device: Optional[Any] = 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(config): | ||
| if not config or (not config.get('task') and not config.get('model')): | ||
| raise ValueError( | ||
| "HuggingFacePipeline requires either 'task' or " | ||
| "'model' to be specified.") | ||
derrickaw marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def inference_output_type(self): | ||
| return Any | ||
| @beam.ptransform.ptransform_fn | ||
| def run_inference( | ||
| pcoll, | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -654,7 +654,8 @@ def get_portability_package_data(): | ||
| 'transformers': [ | ||
| 'transformers>=4.28.0,<4.56.0', | ||
| 'tensorflow>=2.12.0', | ||
| 'torch>=1.9.0' | ||
| # Avoid torch 2.12.0+ which fails to run unit tests with segfault | ||
| 'torch>=1.9.0,<2.12.0' | ||
derrickaw marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| ], | ||
| 'ml_cpu': [ | ||
| 'tensorflow>=2.12.0', | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.