Error headers = {"Authorization": f"Bearer {api_token}"} NameError: name 'api_token' is not defined

Hello,

How can i resolve this error :
headers = {“Authorization”: f"Bearer {api_token}"}
NameError: name ‘api_token’ is not defined

Here the code :

import json
import requests

API_URL = “https://api-inference.huggingface.co/models/sentence-transformers/all-MiniLM-L6-v2
headers = {“Authorization”: f"Bearer {api_token}"}

def query(payload):
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()

data = query(
{
“inputs”: {
“source_sentence”: “I’m very happy”,
“sentences”:[“I’m filled with happiness”, “I’m happy”]
}
})

Thank you

Hi,
Just checking, do you have a variable declared called api_token? If not, then it will throw a NameError. You can define an api_token variable with the line api_token = "INSERT-API-TOKEN-HERE". Or, you can replace the headers line with
headers = {"Authorization": "Bearer ASHGFsdgfhAurF"}. (Replace the gibberish with your api token.) If you have done this, then that is beyond my fairly basic python knowledge.
Hope this helps.