others-how to solve HUGGINGFACEHUB_API_TOKEN error when trying to access huggingface models?

1. Purpose

In this post, I will show you how to solve HUGGINGFACEHUB_API_TOKEN error when trying to access huggingface models, here is the detailed error messages:

error:

(langchain_helloworld_env) ➜  helloworld git:(master) ✗ python test2.py                
/Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages/langchain/__init__.py:34: UserWarning: Importing HuggingFaceHub from langchain root module is no longer supported. Please use langchain.llms.HuggingFaceHub instead.
  warnings.warn(
Traceback (most recent call last):
  File "/Users/bswen/work/python/myutils/learn_langchain/helloworld/test2.py", line 7, in <module>
    llm = HuggingFaceHub(repo_id="bigscience/bloom-1b7")
  File "/Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages/langchain/load/serializable.py", line 97, in __init__
    super().__init__(**kwargs)
  File "/Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages/pydantic/v1/main.py", line 341, in __init__
    raise validation_error
pydantic.v1.error_wrappers.ValidationError: 1 validation error for HuggingFaceHub
__root__
  Did not find huggingfacehub_api_token, please add an environment variable `HUGGINGFACEHUB_API_TOKEN` which contains it, or pass  `huggingfacehub_api_token` as a named parameter. (type=value_error)
(langchain_helloworld_env) ➜  helloworld git:(master)

core error message:

Did not find huggingfacehub_api_token, please add an environment variable `HUGGINGFACEHUB_API_TOKEN` which contains it, or pass  `huggingfacehub_api_token` as a named parameter. (type=value_error)

Here is the code:

import os  
from langchain import HuggingFaceHub  
llm = HuggingFaceHub(repo_id="bigscience/bloom-1b7")  
resp = llm.predict("Please name my flower shop")  
print(resp)

You can see that I am trying to load the huggingface model bigscience/bloom-1b7, but the error occurred.



2. Solution

You can open huggingface, logon the website and then create an api token as following picture showing:

Then change your code as follows:

import os  
os.environ["HUGGINGFACEHUB_API_TOKEN"] = "hf_nJnjdwfvR......"  
from langchain import HuggingFaceHub  
llm = HuggingFaceHub(repo_id="bigscience/bloom-1b7")  
resp = llm.predict("Please name my flower shop")  
print(resp)

Now let’s execute the script again, here is the result:

(langchain_helloworld_env) ➜  helloworld git:(master) ✗ python test2.py                
/Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages/langchain/__init__.py:34: UserWarning: Importing HuggingFaceHub from langchain root module is no longer supported. Please use langchain.llms.HuggingFaceHub instead.
  warnings.warn(
Traceback (most recent call last):
  File "/Users/bswen/work/python/myutils/learn_langchain/helloworld/test2.py", line 7, in <module>
    llm = HuggingFaceHub(repo_id="bigscience/bloom-1b7")
  File "/Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages/langchain/load/serializable.py", line 97, in __init__
    super().__init__(**kwargs)
  File "/Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages/pydantic/v1/main.py", line 341, in __init__
    raise validation_error
pydantic.v1.error_wrappers.ValidationError: 1 validation error for HuggingFaceHub
__root__
  Could not import huggingface_hub python package. Please install it with `pip install huggingface_hub`. (type=value_error)
(langchain_helloworld_env) ➜  helloworld git:(master) ✗ pip install huggingface_hub       
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting huggingface_hub
  Downloading http://mirrors.aliyun.com/pypi/packages/ef/b5/b6107bd65fa4c96fdf00e4733e2fe5729bb9e5e09997f63074bb43d3ab28/huggingface_hub-0.18.0-py3-none-any.whl (301 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 302.0/302.0 kB 595.8 kB/s eta 0:00:00
Collecting filelock (from huggingface_hub)
  Downloading http://mirrors.aliyun.com/pypi/packages/81/54/84d42a0bee35edba99dee7b59a8d4970eccdd44b99fe728ed912106fc781/filelock-3.13.1-py3-none-any.whl (11 kB)
Collecting fsspec>=2023.5.0 (from huggingface_hub)
  Downloading http://mirrors.aliyun.com/pypi/packages/e8/f6/3eccfb530aac90ad1301c582da228e4763f19e719ac8200752a4841b0b2d/fsspec-2023.10.0-py3-none-any.whl (166 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 166.4/166.4 kB 417.7 kB/s eta 0:00:00
Requirement already satisfied: requests in /Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages (from huggingface_hub) (2.31.0)
Requirement already satisfied: tqdm>=4.42.1 in /Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages (from huggingface_hub) (4.66.1)
Requirement already satisfied: pyyaml>=5.1 in /Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages (from huggingface_hub) (6.0.1)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages (from huggingface_hub) (4.8.0)
Requirement already satisfied: packaging>=20.9 in /Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages (from huggingface_hub) (23.2)
Requirement already satisfied: charset-normalizer<4,>=2 in /Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages (from requests->huggingface_hub) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages (from requests->huggingface_hub) (3.4)
Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages (from requests->huggingface_hub) (2.0.7)
Requirement already satisfied: certifi>=2017.4.17 in /Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages (from requests->huggingface_hub) (2023.7.22)
Installing collected packages: fsspec, filelock, huggingface_hub
Successfully installed filelock-3.13.1 fsspec-2023.10.0 huggingface_hub-0.18.0
(langchain_helloworld_env) ➜  helloworld git:(master) ✗ python test2.py            
/Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages/langchain/__init__.py:34: UserWarning: Importing HuggingFaceHub from langchain root module is no longer supported. Please use langchain.llms.HuggingFaceHub instead.
  warnings.warn(
/Users/bswen/miniconda3/envs/langchain_helloworld_env/lib/python3.10/site-packages/huggingface_hub/utils/_deprecation.py:127: FutureWarning: '__init__' (from 'huggingface_hub.inference_api') is deprecated and will be removed from version '0.19.0'. `InferenceApi` client is deprecated in favor of the more feature-complete `InferenceClient`. Check out this guide to learn how to convert your script to use it: https://huggingface.co/docs/huggingface_hub/guides/inference#legacy-inferenceapi-client.
  warnings.warn(warning_message, FutureWarning)
, Just call it "love of flowers".
(langchain_helloworld_env) ➜  helloworld git:(master)

Success!

3. Summary

In this post, I demonstrated how to solve HUGGINGFACEHUB_API_TOKEN error when trying to access huggingface models, the key point is to create a token for your script on huggingface website. That’s it, thanks for your reading.