04 Mar How to create Hugging Face API key (Access Token)
Let us see how to generate a Hugging Face Access Token. We discussed what is an access token in the previous lesson. If you do need an access token (e.g., for gated models or the Inference API), here are the steps to generate one.
Steps to generate a Hugging Face API Key (Access Token)
Step 1: Go to https://huggingface.co/join and sign up as we did below to create a new account:
Above, click Next
Step 2: Create your account. Add your username and full name. You can leave the optional boxes and click Create Account as shown below:
Step 3: Click on your profile picture in the top-right corner and navigate Access Tokens:
Step 4: Now, it is not allowing me to create an access token. The Create new token is disabled.
To fix this, go to your email account and verify your account. The notification is also visible as shown below “Please confirm your account email address before creating access tokens”:
Step 5: We will click on the link in the email to verify:
Step 6: Now, click Create new token to generate a token.
Step 7: Add a name to the token:
Step 8: We have added a name to the token:
Step 9: Now, go below and click Create token:
Step 10: The key was created successfully. Copy it and keep it secure. Do not share the key with anyone. Click Done:
Step 11: Now, our keys are visible. Here, all your keys will be visible. The usage date will also be visible here:
Example: Using a Hugging Face Access Token
Now, you can set the access token (key) in the code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
from langchain.llms import HuggingFaceHub import os # Set your Hugging Face API token os.environ["HUGGINGFACEHUB_API_TOKEN"] = "your_huggingface_api_token" # Load a newer Llama model (e.g., meta-llama/Llama-3) llm = HuggingFaceHub( repo_id="meta-llama/Llama-3-8b-chat-hf", # Replace with the correct repo_id for Llama 3 model_kwargs={ "temperature": 0.7, # Controls randomness (lower = more deterministic) "max_length": 100, # Maximum number of tokens to generate "top_p": 0.9, # Nucleus sampling parameter "do_sample": True # Enable sampling for diverse outputs } ) # Use the model response = llm("What is the capital of France?") print(response) |
Example: No Access Token Needed
Here, we are using a public model therefore access token isn’t required:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from transformers import pipeline from langchain.llms import HuggingFacePipeline # Load a public model (no token needed) summarizer = pipeline("summarization", model="facebook/bart-large-cnn") # Wrap it in LangChain llm = HuggingFacePipeline(pipeline=summarizer) # Use the model response = llm("LangChain is a framework for developing applications powered by language models.") print(response) |
Summary
- No Token Needed: For public models used locally via transformers.
- Token Needed: For gated models, private models, or when using the Hugging Face Inference API (HuggingFaceHub in LangChain).
Security Tips:
- Keep your token private and avoid sharing it publicly.
- Use environment variables or secure storage to manage tokens.
- Regenerate tokens if compromised.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
- LangChain Tutorial
- RAG Tutorial
- Generative AI Tutorial
- Machine Learning Tutorial
- Deep Learning Tutorial
- Ollama Tutorial
- Retrieval Augmented Generation (RAG) Tutorial
- Copilot Tutorial
- ChatGPT Tutorial
No Comments