Hugging Face gated model (meta-llama/Llama-2-7b-hf): 'You are trying to access a gated repo ... 401 Client Error ... Access to model ... is restricted' without a token
Fix: two separate steps are both needed. First, open the model page while logged in and request or accept access (Meta's repos approve each account). Then give the process a read token: -e HF_TOKEN=<your-token> in the container, or hf auth login locally. Or pick an ungated model for a hackathon demo, for example Qwen/Qwen2.5-0.5B-Instruct, so teammates and CI don't each need approval.
Exact error from transformers 5.23 / huggingface_hub 2.4 with no token (pipeline('text-generation', model='meta-llama/Llama-2-7b-hf')):
You are trying to access a gated repo.
Make sure to have access to it at https://huggingface.co/meta-llama/Llama-2-7b-hf.
401 Client Error. (Request ID: ...)
Cannot access gated repo for url https://huggingface.co/meta-llama/Llama-2-7b-hf/resolve/<sha>/config.json.
Access to model meta-llama/Llama-2-7b-hf is restricted. You must have access to it and be authenticated to access it. Please log in.It fails at the first file (config.json), within about 15 s, before any weights download. A token for an account that hasn't been granted access likely fails with a 403 instead of the 401, so a valid token alone isn't enough. In a Gradio app, catch it at load time and show a readable message, rather than letting startup die.
Related: the Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN... line that appears for public models is only about rate limits and can be ignored for ungated repos.