Repository: huggingface/transformers (Python)
Date: 2022-10-16 15:12:03+00:00
Task type: Bug Fix

Issue:
Call to pipeline.predict() fails
### System Info

- `transformers` version: 4.21.1
- Platform: macOS-12.5.1-arm64-arm-64bit
- Python version: 3.9.12
- Huggingface_hub version: 0.2.1
- PyTorch version (GPU?): 1.12.1 (False)
- Tensorflow version (GPU?): 2.9.2 (False)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: No
- Using distributed or parallel set-up in script?: No

### Who can help?

@narsil

### Information

- [ ] The official example scripts
- [X] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [ ] My own task or dataset (give details below)

### Reproduction

Execute the following piece of code resulted in an exception that is pasted below.

```python
from transformers import pipeline

pipe = pipeline("text-classification")
print(pipe.predict(["This restaurant is awesome"]))

```

Exception:

```
Traceback (most recent call last):
  File "pipeline_test.py", line 5, in <module>
    print(pipe.predict(["This restaurant is awesome"]))
  File "miniconda3/envs/mlflow-py3.9/lib/python3.9/site-packages/transformers/pipelines/base.py", line 840, in predict
    return self(X=X)
  File "miniconda3/envs/mlflow-py3.9/lib/python3.9/site-packages/transformers/pipelines/text_classification.py", line 138, in __call__
    result = super().__call__(*args, **kwargs)
TypeError: __call__() missing 1 required positional argument: 'inputs'
```

### Expected behavior

Successful predictions as shown below

```
[{'label': 'POSITIVE', 'score': 0.9998743534088135}]
```

### Proposed fix

I dig a bit deeper into the implementation based on the exception and found out that this [change](https://github.com/huggingface/transformers/compare/main...s-udhaya:transformers:fix_pipeline_predict#diff-441f558737166b045444da9c4be81f566b3d69054e8f20e288aed746a691fa61R845) fixes the issue. If this indeed a fix, I am happy to create a PR.


Issue discussion:

Solution patch:
diff --git a/src/transformers/pipelines/base.py b/src/transformers/pipelines/base.py
--- a/src/transformers/pipelines/base.py
+++ b/src/transformers/pipelines/base.py
@@ -836,13 +836,13 @@ def transform(self, X):
         """
         Scikit / Keras interface to transformers' pipelines. This method will forward to __call__().
         """
-        return self(X=X)
+        return self(X)
 
     def predict(self, X):
         """
         Scikit / Keras interface to transformers' pipelines. This method will forward to __call__().
         """
-        return self(X=X)
+        return self(X)
 
     @contextmanager
     def device_placement(self):


Test patch:
diff --git a/tests/pipelines/test_pipelines_common.py b/tests/pipelines/test_pipelines_common.py
--- a/tests/pipelines/test_pipelines_common.py
+++ b/tests/pipelines/test_pipelines_common.py
@@ -423,6 +423,56 @@ def test_unbatch_attentions_hidden_states(self):
         self.assertEqual(len(outputs), 20)
 
 
+class PipelineScikitCompatTest(unittest.TestCase):
+    @require_torch
+    def test_pipeline_predict_pt(self):
+        data = ["This is a test"]
+
+        text_classifier = pipeline(
+            task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="pt"
+        )
+
+        expected_output = [{"label": ANY(str), "score": ANY(float)}]
+        actual_output = text_classifier.predict(data)
+        self.assertEqual(expected_output, actual_output)
+
+    @require_tf
+    def test_pipeline_predict_tf(self):
+        data = ["This is a test"]
+
+        text_classifier = pipeline(
+            task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="tf"
+        )
+
+        expected_output = [{"label": ANY(str), "score": ANY(float)}]
+        actual_output = text_classifier.predict(data)
+        self.assertEqual(expected_output, actual_output)
+
+    @require_torch
+    def test_pipeline_transform_pt(self):
+        data = ["This is a test"]
+
+        text_classifier = pipeline(
+            task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="pt"
+        )
+
+        expected_output = [{"label": ANY(str), "score": ANY(float)}]
+        actual_output = text_classifier.transform(data)
+        self.assertEqual(expected_output, actual_output)
+
+    @require_tf
+    def test_pipeline_transform_tf(self):
+        data = ["This is a test"]
+
+        text_classifier = pipeline(
+            task="text-classification", model="hf-internal-testing/tiny-random-distilbert", framework="tf"
+        )
+
+        expected_output = [{"label": ANY(str), "score": ANY(float)}]
+        actual_output = text_classifier.transform(data)
+        self.assertEqual(expected_output, actual_output)
+
+
 class PipelinePadTest(unittest.TestCase):
     @require_torch
     def test_pipeline_padding(self):