# Using Token to Access Llama2

**URL:** <https://discuss.huggingface.co/t/using-token-to-access-llama2/62642>\
**Category:** Beginners\
**Created:** [November 18, 2023, 1:15am UTC](https://discuss.huggingface.co/t/using-token-to-access-llama2/62642 "2023-11-18T01:15:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wwgandy](https://avatars.discourse-cdn.com/v4/letter/w/bb73d2/32.png) [@wwgandy](https://discuss.huggingface.co/u/wwgandy)\
**Post date:** [November 18, 2023, 1:15am UTC](https://discuss.huggingface.co/t/using-token-to-access-llama2/62642/1 "2023-11-18T01:15:05Z")

</div>

I have been granted access to the Llama 70b model (it says that when I go to that page). I have a token. I tried this code (but I replaced “my token” with the actual token in quotes. However, I get this error: “OSError: You are trying to access a gated repo.  
Make sure to request access at [meta-llama/Llama-2-70b-chat-hf · Hugging Face](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) and pass a token having permission to this repo either by logging in with `huggingface-cli login` or by passing `token=<your_token>`.”

**MY CODE:**

from transformers import AutoModelForCausalLM, AutoTokenizer  
import os

hf\_access\_token = “my token”  
os.environ[“HF\_ACCESS\_TOKEN”] = hf\_access\_token

tokenizer = AutoTokenizer.from\_pretrained(“meta-llama/Llama-2-70b-chat-hf”)  
model = AutoModelForCausalLM.from\_pretrained(“meta-llama/Llama-2-70b-chat-hf”)

* * *

**I also tried this code with my actual token instead of your\_token\_here**

from transformers import LlamaForCausalLM, LlamaTokenizer  
import sentencepiece

token = “\<your\_token\_here\>” # Replace \<your\_token\_here\> with the token you obtained

tokenizer = LlamaTokenizer.from\_pretrained(“meta-llama/Llama-2-70b-chat-hf”, use\_auth\_token=token)  
model = LlamaForCausalLM.from\_pretrained(“meta-llama/Llama-2-70b-chat-hf”, use\_auth\_token=token)

Again, I got a message about it being a gated model. Can anyone tell me what I am doing wrong? Thank you .

---

<div class="post-metadata">

**Author:** ![DrewG](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@DrewG](https://discuss.huggingface.co/u/DrewG)\
**Post date:** [February 13, 2024, 9:50pm UTC](https://discuss.huggingface.co/t/using-token-to-access-llama2/62642/2 "2024-02-13T21:50:39Z")

</div>

I’ve been having the same silly problem. Try changing the parameter name from `use_auth_token` to simply `token` (ignoring the HF documentation:

```auto
token = “<your_token_here>” # Replace <your_token_here> with the token you obtained

tokenizer = LlamaTokenizer.from_pretrained(“meta-llama/Llama-2-70b-chat-hf”, token=token)
model = LlamaForCausalLM.from_pretrained(“meta-llama/Llama-2-70b-chat-hf”, token=token)

```

---

<div class="post-metadata">

**Author:** ![El-chapoo](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/el-chapoo/32/23658_2.png) [@El-chapoo](https://discuss.huggingface.co/u/El-chapoo)\
**Post date:** [February 21, 2024, 4:41pm UTC](https://discuss.huggingface.co/t/using-token-to-access-llama2/62642/3 "2024-02-21T16:41:17Z")

</div>

huggingface\_hub import notebook\_login

run this and then

notebook\_login()

paste your token

---

<div class="post-metadata">

**Author:** ![DrewG](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@DrewG](https://discuss.huggingface.co/u/DrewG)\
**Post date:** [February 21, 2024, 5:01pm UTC](https://discuss.huggingface.co/t/using-token-to-access-llama2/62642/4 "2024-02-21T17:01:35Z")

</div>

In my case (and I suspect many others), this will not work. I wasn’t able to interact with the script as this command requires. For context, I send a request to a remote machine which executes the script to download a HF model specified by the user, then run inference and send back the response. In situations where the script is noninteractive, use the DrewG’s solution above. In interactive sessions, El-chapoo’s should work well.
