gemini-code-assist[bot] commented on code in PR #38110:
URL: https://github.com/apache/beam/pull/38110#discussion_r3189186018


##########
sdks/python/apache_beam/ml/inference/huggingface_inference.py:
##########
@@ -32,7 +32,11 @@
 import torch
 from transformers import AutoModel
 from transformers import Pipeline
-from transformers import TFAutoModel
+
+try:
+  from transformers import TFAutoModel
+except ImportError:
+  TFAutoModel = Any

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   The `Any` type is used here but it is not imported from the `typing` module. 
This will cause a `NameError` at runtime if the `transformers` package is not 
installed and the code enters the `except` block.
   
   ```suggestion
   try:
     from transformers import TFAutoModel
   except ImportError:
     from typing import Any
     TFAutoModel = Any
   ```



##########
sdks/python/apache_beam/yaml/tests/runinference_huggingface.yaml:
##########
@@ -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]
+                      }

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   The custom `inference_fn` is incorrectly implemented for batch processing 
and contradicts the test assertions. 
   
   1. **Batching Issue**: It returns a single dictionary instead of an iterable 
of results. `RunInference` expects the output length to match the input batch 
size. This will cause a failure for any batch size greater than 1.
   2. **Format Mismatch**: It wraps labels in lists (e.g., `['POSITIVE']`), but 
the `AssertEqual` transform expects a simple string (`'POSITIVE'`).
   3. **Redundancy**: The default behavior of `HuggingFacePipelineModelHandler` 
already correctly handles the pipeline call and returns the results in the 
expected format. 
   
   If you wish to keep the custom function as an example, it should return the 
list of predictions directly.
   
   ```yaml
                   inference_fn:
                     callable: |
                       def real_inference(batch, pipeline, inference_args):
                         predictions = pipeline(batch, **inference_args)
                         return [predictions] if isinstance(predictions, dict) 
else predictions
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to