Skip to content

Commit

Permalink
Fixing Backward compatiblity for zero-shot (huggingface#13855)
Browse files Browse the repository at this point in the history
  • Loading branch information
Narsil authored and Alberto B茅gu茅 committed Jan 27, 2022
1 parent aac021c commit 1a41820
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/transformers/pipelines/zero_shot_classification.py
Expand Up @@ -191,10 +191,7 @@ def __call__(
else:
raise ValueError(f"Unable to understand extra arguments {args}")

result = super().__call__(sequences, **kwargs)
if len(result) == 1:
return result[0]
return result
return super().__call__(sequences, **kwargs)

def preprocess(self, inputs, candidate_labels=None, hypothesis_template="This example is {}."):
sequence_pairs, sequences = self._args_parser(inputs, candidate_labels, hypothesis_template)
Expand Down Expand Up @@ -264,4 +261,6 @@ def postprocess(self, model_outputs, multi_label=False):
"scores": scores[iseq, top_inds].tolist(),
}
)
if len(result) == 1:
return result[0]
return result
18 changes: 18 additions & 0 deletions tests/test_pipelines_zero_shot.py
Expand Up @@ -61,6 +61,24 @@ def run_pipeline_test(self, model, tokenizer, feature_extractor):
)
self.assertEqual(outputs, {"sequence": ANY(str), "labels": [ANY(str)], "scores": [ANY(float)]})

# https://github.com/huggingface/transformers/issues/13846
outputs = classifier(["I am happy"], ["positive", "negative"])
self.assertEqual(
outputs,
[
{"sequence": ANY(str), "labels": [ANY(str), ANY(str)], "scores": [ANY(float), ANY(float)]}
for i in range(1)
],
)
outputs = classifier(["I am happy", "I am sad"], ["positive", "negative"])
self.assertEqual(
outputs,
[
{"sequence": ANY(str), "labels": [ANY(str), ANY(str)], "scores": [ANY(float), ANY(float)]}
for i in range(2)
],
)

with self.assertRaises(ValueError):
classifier("", candidate_labels="politics")

Expand Down

0 comments on commit 1a41820

Please sign in to comment.