Newb unable to use a model on Mac

Need to use the Helsinki-NLP/opus-mt-tc-big-en-it · Hugging Face to translate some medical terms from English to Italian.
Setup an environment with conda as follows:

conda create --name huggingface python=3.9 black pylint
conda activate huggingface
conda install -c conda-forge tensorflow  
conda install -c huggingface transformers  
conda install -c conda-forge sentencepiece

then try to run the small sample program listed in the model’s page:

from transformers import MarianMTModel, MarianTokenizer

src_text = [
    "He was always very respectful.",
    "This cat is black. Is the dog, too?"
]

model_name = "pytorch-models/opus-mt-tc-big-en-it"
tokenizer = MarianTokenizer.from_pretrained(model_name)
model = MarianMTModel.from_pretrained(model_name)
translated = model.generate(**tokenizer(src_text, return_tensors="pt", padding=True))

for t in translated:
    print( tokenizer.decode(t, skip_special_tokens=True) )

get the following errors:

(huggingface) bob@Roberts-Mac-mini engdisorders2italian % python test.py                            
Traceback (most recent call last):
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/huggingface_hub-0.10.1-py3.8.egg/huggingface_hub/utils/_errors.py", line 213, in hf_raise_for_status
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://huggingface.co/pytorch-models/opus-mt-tc-big-en-it/resolve/main/source.spm

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/transformers/utils/hub.py", line 409, in cached_file
    resolved_file = hf_hub_download(
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/huggingface_hub-0.10.1-py3.8.egg/huggingface_hub/file_download.py", line 1053, in hf_hub_download
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/huggingface_hub-0.10.1-py3.8.egg/huggingface_hub/file_download.py", line 1359, in get_hf_file_metadata
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/huggingface_hub-0.10.1-py3.8.egg/huggingface_hub/utils/_errors.py", line 242, in hf_raise_for_status
huggingface_hub.utils._errors.RepositoryNotFoundError: 401 Client Error. (Request ID: hZlXOfQHdWwkux5NvawFh)

Repository Not Found for url: https://huggingface.co/pytorch-models/opus-mt-tc-big-en-it/resolve/main/source.spm.
Please make sure you specified the correct `repo_id` and `repo_type`.
If the repo is private, make sure you are authenticated.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/bob/Documents/work/engdisorders2italian/test.py", line 9, in <module>
    tokenizer = MarianTokenizer.from_pretrained(model_name)
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/transformers/tokenization_utils_base.py", line 1734, in from_pretrained
    resolved_vocab_files[file_id] = cached_file(
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/transformers/utils/hub.py", line 424, in cached_file
    raise EnvironmentError(
OSError: pytorch-models/opus-mt-tc-big-en-it is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models'
If this is a private repository, make sure to pass a token having permission to this repo with `use_auth_token` or log in with `huggingface-cli login` and pass `use_auth_token=True`.

no clue as how to fix this.
Environment is Mac mini with Apple CPU (M1 Arm) with MacOS Monterey
Python managed via conda

Thank you for any help

Hi @rjalex ! :slight_smile:

This issue is due to the wrong organization name, pytorch-models, when it should be Helsinki-NLP. There is a pull request to fix the code sample on the model card. The code should work if you use:

model_name = "Helsinki-NLP/opus-mt-tc-big-en-it"
1 Like

Thank you so much. Your correction is fixing THAT error indeed!
But I now get two other problems. With the fixed code I get:

ImportError: 
MarianMTModel requires the PyTorch library but it was not found in your environment.
However, we were able to find a TensorFlow installation. TensorFlow classes begin
with "TF", but are otherwise identically named to our PyTorch classes. This
means that the TF equivalent of the class you tried to import would be "TFMarianMTModel".
If you want to use TensorFlow, please use TF classes instead!

so I modified the import to
from transformers import TFMarianMTModel, MarianTokenizer
and the corresponding call to:
model = TFMarianMTModel.from_pretrained(MODEL_NAME)
but when I run the code I get the following:

All model checkpoint layers were used when initializing TFMarianMTModel.

All the layers of TFMarianMTModel were initialized from the model checkpoint at Helsinki-NLP/opus-mt-tc-big-en-it.
If your task is similar to the task the model of the checkpoint was trained on, you can already use TFMarianMTModel for predictions without further training.
Traceback (most recent call last):
  File "/Users/bob/Documents/work/engdisorders2italian/test.py", line 10, in <module>
    translated = model.generate(**tokenizer(src_text, return_tensors="pt", padding=True))
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/transformers/tokenization_utils_base.py", line 2484, in __call__
    encodings = self._call_one(text=text, text_pair=text_pair, **all_kwargs)
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/transformers/tokenization_utils_base.py", line 2570, in _call_one
    return self.batch_encode_plus(
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/transformers/tokenization_utils_base.py", line 2761, in batch_encode_plus
    return self._batch_encode_plus(
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/transformers/tokenization_utils.py", line 737, in _batch_encode_plus
    batch_outputs = self._batch_prepare_for_model(
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/transformers/tokenization_utils.py", line 817, in _batch_prepare_for_model
    batch_outputs = BatchEncoding(batch_outputs, tensor_type=return_tensors)
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/transformers/tokenization_utils_base.py", line 206, in __init__
    self.convert_to_tensors(tensor_type=tensor_type, prepend_batch_axis=prepend_batch_axis)
  File "/Users/bob/opt/miniconda3/envs/huggingface/lib/python3.9/site-packages/transformers/tokenization_utils_base.py", line 688, in convert_to_tensors
    raise ImportError("Unable to convert output to PyTorch tensors format, PyTorch is not installed.")
ImportError: Unable to convert output to PyTorch tensors format, PyTorch is not installed.

Sounds like you need to install PyTorch in your environment, check out the PyTorch installation docs!

OK, I thought Tensorflow (which I have installed) was an alternative to Pythorch.

Thanks and take care