Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSError from bad request between sentence-transformers and huggingface_hub #3206

Open
Gwenn-LR opened this issue Jan 31, 2025 · 1 comment
Open

Comments

@Gwenn-LR
Copy link

Gwenn-LR commented Jan 31, 2025

Hi there !

First of all, thanks for your amazing work that I can find in many packages, proof of its significance.

I've stumbled upon an error during an automatic test of a componant from langchain_huggingface :

OSError: sentence-transformers/all-mpnet-base-v2 is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models'

After some researches, I've found that trying to load a basic model, huggingface_hub tries to pull adapter_config.json a file which does not exists on your hub.

Here is the steps I've followed to reproduce this issue:

  • From a new folder, init a poetry env
  • Once activated, add sentence-transformers

Here is the resulting environment I'm working with:

python = 3.10.16
sentence-transformers = 3.4.1
transformers = 4.48.2
huggingface-hub = 0.28.1

Here is the minimal code to reproduce:

from sentence_transformers import SentenceTransformer

SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
Here is the error:
Traceback (most recent call last):
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/huggingface_hub/utils/_http.py", line 406, in hf_raise_for_status
    response.raise_for_status()
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/requests/models.py", line 1024, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url: https://huggingface.co/sentence-transformers/all-mpnet-base-v2/resolve/main/adapter_config.json

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

Traceback (most recent call last):
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/transformers/utils/hub.py", line 403, in cached_file
    resolved_file = hf_hub_download(
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn
    return fn(*args, **kwargs)
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 860, in hf_hub_download
    return _hf_hub_download_to_cache_dir(
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 967, in _hf_hub_download_to_cache_dir
    _raise_on_head_call_error(head_call_error, force_download, local_files_only)
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1482, in _raise_on_head_call_error
    raise head_call_error
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1374, in _get_metadata_or_catch_error
    metadata = get_hf_file_metadata(
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/huggingface_hub/utils/_validators.py", line 114, in _inner_fn
    return fn(*args, **kwargs)
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 1294, in get_hf_file_metadata
    r = _request_wrapper(
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 278, in _request_wrapper
    response = _request_wrapper(
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/huggingface_hub/file_download.py", line 302, in _request_wrapper
    hf_raise_for_status(response)
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/huggingface_hub/utils/_http.py", line 454, in hf_raise_for_status
    raise _format(RepositoryNotFoundError, message, response) from e
huggingface_hub.errors.RepositoryNotFoundError: 401 Client Error. (Request ID: Root=1-679ca6f8-279698883fa41e502b1fcd68;bb773570-3c3b-484e-8f46-9467785c76eb)

Repository Not Found for url: https://huggingface.co/sentence-transformers/all-mpnet-base-v2/resolve/main/adapter_config.json.
Please make sure you specified the correct `repo_id` and `repo_type`.
If you are trying to access a private or gated repo, make sure you are authenticated.
Invalid credentials in Authorization header

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

Traceback (most recent call last):
  File "/home/***/.pyenv/versions/3.10.16/lib/python3.10/runpy.py", line 196, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/***/.pyenv/versions/3.10.16/lib/python3.10/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/***/.vscode/extensions/ms-python.debugpy-2024.14.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/__main__.py", line 71, in <module>
    cli.main()
  File "/home/***/.vscode/extensions/ms-python.debugpy-2024.14.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 501, in main
    run()
  File "/home/***/.vscode/extensions/ms-python.debugpy-2024.14.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher/../../debugpy/../debugpy/server/cli.py", line 351, in run_file
    runpy.run_path(target, run_name="__main__")
  File "/home/***/.vscode/extensions/ms-python.debugpy-2024.14.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 310, in run_path
    return _run_module_code(code, init_globals, run_name, pkg_name=pkg_name, script_name=fname)
  File "/home/***/.vscode/extensions/ms-python.debugpy-2024.14.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 127, in _run_module_code
    _run_code(code, mod_globals, init_globals, mod_name, mod_spec, pkg_name, script_name)
  File "/home/***/.vscode/extensions/ms-python.debugpy-2024.14.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_runpy.py", line 118, in _run_code
    exec(code, run_globals)
  File "/home/***/Documents/Scripts/test_sentence_transformers/test.py", line 3, in <module>
    SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/sentence_transformers/SentenceTransformer.py", line 308, in __init__
    modules, self.module_kwargs = self._load_sbert_model(
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/sentence_transformers/SentenceTransformer.py", line 1739, in _load_sbert_model
    module = module_class(model_name_or_path, cache_dir=cache_folder, backend=self.backend, **kwargs)
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/sentence_transformers/models/Transformer.py", line 80, in __init__
    config, is_peft_model = self._load_config(model_name_or_path, cache_dir, backend, config_args)
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/sentence_transformers/models/Transformer.py", line 121, in _load_config
    find_adapter_config_file(
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/transformers/utils/peft_utils.py", line 88, in find_adapter_config_file
    adapter_cached_filename = cached_file(
  File "/home/***/.cache/pypoetry/virtualenvs/test-sentence-transformers-OAhn6OUQ-py3.10/lib/python3.10/site-packages/transformers/utils/hub.py", line 426, in cached_file
    raise EnvironmentError(
OSError: sentence-transformers/all-mpnet-base-v2 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 either by logging in with `huggingface-cli login` or by passing `token=<your_token>`

So from what I've understood, initializing a SequenceTransformer with sentence-transformers/all-mpnet-base-v2 leads to an instanciation of Transformer with this same parameter. During initialization, it tries to Transformer._load_config from this model_name_or_path but evaluating the condition if find_adapter_config_file(***) is not None it can't find any file at the address https://huggingface.co/sentence-transformers/all-mpnet-base-v2/resolve/main/adapter_config.json so the first initialization finally raises an error.

I can't find any relative issue and since its a main functionnality of your package, I think the issue comes from my side, but I really can't find its origin.

Thanks for the attention you will bring to my issue ! Have a nice day :)

@tomaarsen
Copy link
Collaborator

Hello!

Your understanding is right, the model loads a Transformer module, which first determines what kind of configuration the model folder/repository contains. However, the find_adapter_config_file actually should not fail with an OSError if the model does not contain this adapter_config.json - it should return None if that is the case.

The real issue displayed here is that it could not find the sentence-transformers/all-mpnet-base-v2 repository itself. This is indicative of a network issue between your device and Hugging Face (because the https://huggingface.co/sentence-transformers/all-mpnet-base-v2 repository does very much exist), or an issue in your local cache.

My advice is to 1) try it again, see it perhaps it works now as some connection issue has been resolved. If that doesn't work, 2) you can try and find the cache of the model in ~/.cache/huggingface/hub/models--sentence-transformers--all-mpnet-base-v2 and delete that folder.

You can use this snippet to see if you can load the model again:

from sentence_transformers import SentenceTransformer

model = SentenceTransformer("sentence-transformers/all-mpnet-base-v2")

sentences = [
    "That is a happy person",
    "That is a happy dog",
    "That is a very happy person",
    "Today is a sunny day"
]
embeddings = model.encode(sentences)

similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
# [4, 4]

I hope this helps! Let me know if you still can't get it working, and we'll try more steps.

  • Tom Aarsen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants