Raise an exception if the pipeline allocator can't determine the tokenizer from the model.

This commit is contained in:
Morgan Funtowicz 2019-12-13 14:12:54 +01:00
parent be5bf7b81b
commit 28e64ad5a4

View File

@ -370,11 +370,12 @@ def pipeline(task: str, model, config: Optional[PretrainedConfig] = None, tokeni
Utility factory method to build pipeline.
"""
# Try to infer tokenizer from model name (if provided as str)
if tokenizer is None and isinstance(model, str):
tokenizer = model
else:
if not isinstance(tokenizer, PreTrainedTokenizer):
if not isinstance(model, str):
# Impossible to guest what is the right tokenizer here
raise Exception('Tokenizer cannot be None if provided model is a PreTrainedModel instance')
else:
tokenizer = model
tokenizer = tokenizer if isinstance(tokenizer, PreTrainedTokenizer) else AutoTokenizer.from_pretrained(tokenizer)