Authenticated but still unable to access model

I think I’m going insane.

I have been trying to access the Llama-2-7b-chat model which requires Meta to grant you a licence, and then HuggingFace to accept you using that licence. I defintiely have the licence from Meta, receiving two emails confirming it. Likewise, I have gotten permission from HuggingFace that I can access the model, as not only did I get an email, but on the link I can see

Everything is telling me, I can access this model. However, I get the error:

Traceback (most recent call last):
  File "/home/archie/Project/39Env/39venv/lib/python3.9/site-packages/huggingface_hub/utils/", line 286, in hf_raise_for_status
  File "/home/archie/Project/39Env/39venv/lib/python3.9/site-packages/requests/", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url:

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

Traceback (most recent call last):
  File "/home/archie/Project/39Env/39venv/lib/python3.9/site-packages/transformers/utils/", line 430, in cached_file
    resolved_file = hf_hub_download(
  File "/home/archie/Project/39Env/39venv/lib/python3.9/site-packages/huggingface_hub/utils/", line 118, in _inner_fn
    return fn(*args, **kwargs)
  File "/home/archie/Project/39Env/39venv/lib/python3.9/site-packages/huggingface_hub/", line 1457, in hf_hub_download
  File "/home/archie/Project/39Env/39venv/lib/python3.9/site-packages/huggingface_hub/", line 451, in http_get
    r = _request_wrapper(
  File "/home/archie/Project/39Env/39venv/lib/python3.9/site-packages/huggingface_hub/", line 409, in _request_wrapper
  File "/home/archie/Project/39Env/39venv/lib/python3.9/site-packages/huggingface_hub/utils/", line 302, in hf_raise_for_status
    raise GatedRepoError(message, response) from e
huggingface_hub.utils._errors.GatedRepoError: 401 Client Error. (Request ID: Root=1-65860f70-301020471abb8dd86535f434;30d52fbe-f7d1-4f11-be9f-bab338fdd795)

Cannot access gated repo for url
Repo model meta-llama/Llama-2-7b-chat-hf is gated. You must be authenticated to access it.

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

Traceback (most recent call last):
  File "/home/archie/Project/39Env/Llama2Learn/llama/", line 8, in <module>
    model = LlamaForCausalLM.from_pretrained(model_dir, token="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")
  File "/home/archie/Project/39Env/39venv/lib/python3.9/site-packages/transformers/", line 2600, in from_pretrained
    resolved_config_file = cached_file(
  File "/home/archie/Project/39Env/39venv/lib/python3.9/site-packages/transformers/utils/", line 445, in cached_file
    raise EnvironmentError(
OSError: You are trying to access a gated repo.
Make sure to request access at and pass a token having permission to this repo either by logging in with `huggingface-cli login` or by passing `token=<your_token>`.

The code I am trying to run is as follows:

import torch
import transformers
from pathlib import Path

from transformers import LlamaForCausalLM, LlamaTokenizer

model_dir = "meta-llama/llama-2-7b-chat-hf"
model = LlamaForCausalLM.from_pretrained(model_dir)

tokenizer = LlamaTokenizer.from_pretrained(model_dir)

I am running it on WSL Ubuntu, running the program in a python 3.9.18 virtual environment.

Now before someone yells, Yes I do know that I need a user access code to login so that HuggingFace knows that I am actually me, and I have done that! I have tried doing it by calling huggingface-cli login in the cmd line and using my code. I have tried it by passing the token as a parameter in the from_pretrained function. I have tried calling the login from within the code in case it wasn’t jelling. None of these options did anything different. I have refreshed the tokens, and my computer, and tried it over a couple days, and still nothing.

I originally tried to get it to work by using the local file, which has been correctly downloaded to my computer, but that brought about other issues. Primarily it being unable to find config.json that it needed to find.

I am desparate for any help here; like I am nearing my breaking point. Please, huggingface forum, you are my only hope.

It looks like the environment variable with your token isn’t properly set/recognized.

Could you try running !huggingface-cli whoami in the terminal?

See this guide: Command Line Interface (CLI).