diff --git a/src/transformers/pipelines.py b/src/transformers/pipelines.py index 45331fe4..a5907905 100755 --- a/src/transformers/pipelines.py +++ b/src/transformers/pipelines.py @@ -85,7 +85,7 @@ if TYPE_CHECKING: logger = logging.get_logger(__name__) -def get_framework(model=None): +def get_framework(model): """ Select framework (TensorFlow or PyTorch) to use. @@ -94,19 +94,24 @@ def get_framework(model=None): If both frameworks are installed, picks the one corresponding to the model passed (either a model class or the model name). If no specific model is provided, defaults to using PyTorch. """ - if is_tf_available() and is_torch_available() and model is not None and not isinstance(model, str): - # Both framework are available but the user supplied a model class instance. - # Try to guess which framework to use from the model classname - framework = "tf" if model.__class__.__name__.startswith("TF") else "pt" - elif not is_tf_available() and not is_torch_available(): + if not is_tf_available() and not is_torch_available(): raise RuntimeError( "At least one of TensorFlow 2.0 or PyTorch should be installed. " "To install TensorFlow 2.0, read the instructions at https://www.tensorflow.org/install/ " "To install PyTorch, read the instructions at https://pytorch.org/." ) - else: - # framework = 'tf' if is_tf_available() else 'pt' - framework = "pt" if is_torch_available() else "tf" + if isinstance(model, str): + if is_torch_available() and not is_tf_available(): + model = AutoModel.from_pretrained(model) + elif is_tf_available() and not is_torch_available(): + model = TFAutoModel.from_pretrained(model) + else: + try: + model = AutoModel.from_pretrained(model) + except OSError: + model = TFAutoModel.from_pretrained(model) + + framework = "tf" if model.__class__.__name__.startswith("TF") else "pt" return framework diff --git a/tests/test_pipelines.py b/tests/test_pipelines.py index 18157875..28dd6a60 100644 --- a/tests/test_pipelines.py +++ b/tests/test_pipelines.py @@ -10,6 +10,7 @@ DEFAULT_DEVICE_NUM = -1 if torch_device == "cpu" else 0 VALID_INPUTS = ["A simple string", ["list of strings"]] NER_FINETUNED_MODELS = ["sshleifer/tiny-dbmdz-bert-large-cased-finetuned-conll03-english"] +TF_NER_FINETUNED_MODELS = ["Narsil/small"] # xlnet-base-cased disabled for now, since it crashes TF2 FEATURE_EXTRACT_FINETUNED_MODELS = ["sshleifer/tiny-distilbert-base-cased"] @@ -804,6 +805,14 @@ class NerPipelineTests(unittest.TestCase): nlp = pipeline(task="ner", model=model_name, tokenizer=model_name, framework="tf", grouped_entities=True) self._test_ner_pipeline(nlp, mandatory_keys) + @require_tf + def test_tf_only_ner(self): + mandatory_keys = {"entity", "word", "score"} + for model_name in TF_NER_FINETUNED_MODELS: + # We don't specificy framework='tf' but it gets detected automatically + nlp = pipeline(task="ner", model=model_name, tokenizer=model_name) + self._test_ner_pipeline(nlp, mandatory_keys) + class PipelineCommonTests(unittest.TestCase): pipelines = SUPPORTED_TASKS.keys()