pythonhuggingface-transformers

How can I load a pretrained transformers model that was manually downloaded?


I am unable to download huggingface models through the Python functions due to SSL certificate errors. Perhaps it's due to my company firewall.

I am able to download the contents of a huggingface model repo through a browser to a folder. I'm trying to load this model from disk using TFPreTrainedModel.from_pretrained() and AutoTokenizer.from_pretrained(). From the docs, it seems this is a valid option.

I'm receiving an error message that isn't useful AttributeError: 'NoneType' object has no attribute 'from_pretrained'. Appreciate any help!

Example repo:

https://huggingface.co/cardiffnlp/twitter-roberta-base-sentiment-latest/tree/main

Code

from transformers import pipeline, TFPreTrainedModel, AutoTokenizer
import os

dir = "./models/twitter-roberta-base-sentiment-latest/"
print(os.listdir(dir)) # confirm the folder contents

model = TFPreTrainedModel.from_pretrained(dir)
tokenizer = AutoTokenizer.from_pretrained(dir)

analyze = pipeline(task="sentiment-analyis", model=model, tokenizer=tokenizer)
print(analyze("this is good"))
print(analyze("this is bad"))

Output

2025-02-21 16:40:05.896448: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
2025-02-21 16:40:06.653841: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
WARNING:tensorflow:From C:\Users\xxxxx\.pyenv\pyenv-win\versions\3.12.8\Lib\site-packages\tf_keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.

['config.json', 'gitattributes', 'merges.txt', 'pytorch_model.bin', 'README.md', 'special_tokens_map.json', 'tf_model.h5', 'vocab.json']
Traceback (most recent call last):
  File "C:\Users\xxxxx\OneDrive - DuPont\Python Projects\huggingface\sentiment.py", line 8, in <module>
    model = TFPreTrainedModel.from_pretrained(dir)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\xxxxx\.pyenv\pyenv-win\versions\3.12.8\Lib\site-packages\transformers\modeling_tf_utils.py", line 2726, in from_pretrained
    config, model_kwargs = cls.config_class.from_pretrained(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'from_pretrained'

Docs

https://huggingface.co/docs/transformers/v4.49.0/en/main_classes/model#transformers.TFPreTrainedModel

pretrained_model_name_or_path (str, optional) — Can be either:
A string, the model id of a pretrained model hosted inside a model repo on huggingface.co.
*A path to a directory containing model weights saved using save_pretrained(), e.g., ./my_model_directory/.*
A path or url to a PyTorch state_dict save file (e.g, ./pt_model/pytorch_model.bin). In this case, from_pt should be set to True and a configuration object should be provided as config argument. This loading path is slower than converting the PyTorch model in a TensorFlow model using the provided conversion scripts and loading the TensorFlow model afterwards.
None if you are both providing the configuration and state dictionary (resp. with keyword arguments config and state_dict).

Solution

  • It looks like the provided Tensorflow state-dict is missing some weights. When you load the PyTorch variant, the results look pretty decent:

    from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
    
    local_path = "/content/twitter-roberta-base-sentiment-latest"
    
    tokenizer = AutoTokenizer.from_pretrained(local_path)
    model = AutoModelForSequenceClassification.from_pretrained(local_path)
    
    analyze = pipeline(task="sentiment-analysis", model=model, tokenizer=tokenizer)
    
    print(analyze("this is good"))
    print(analyze("this is bad"))
    

    Output:

    [{'label': 'positive', 'score': 0.9303026795387268}]
    [{'label': 'negative', 'score': 0.8278112411499023}]
    

    But when you execute the same code with the TensorFlow equivalent (i.e. TFAutoModelForSequenceClassification instead of AutoModelForSequenceClassification), you receive a warning that the classifier (important component) is randomly initialized and the result you receive are not useful at all:

    Some layers of TFRobertaForSequenceClassification were not initialized from the model checkpoint at /content/twitter-roberta-base-sentiment-latest and are newly initialized: ['classifier']
    You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
    
    [{'label': 'negative', 'score': 0.3959597945213318}]
    [{'label': 'neutral', 'score': 0.35170331597328186}]
    

    There was an issue reported addressing that in 2023 (link), but no action so far from the model creators. You can fix the Tensorflow state_dict if you have to use this framework. Please open a separate question if you need assistance.

    To answer your original question on how to load locally stored models from huggingface, you usually don't need to figure out which classes you need because the pipeline will already take care of that for you. You can simplify your code in the following way:

    from transformers import pipeline
    
    local_path = "/content/twitter-roberta-base-sentiment-latest"
    
    analyze = pipeline(task="sentiment-analysis", model=local_path)
    

    You can also control with the framework parameter (values tf or pt), which framework you want to use. In general, something you experienced with cardiffnlp/twitter-roberta-base-sentiment-latest can happen again, therefore always verify the results and read the warning messages.