I was able to solve this problem by creating my own API key on Huggingface and then it and specifying the base model in the code as follows:
Creating an API key for Hugging Face involves signing up for an account and then generating the key from the user dashboard. Here are the steps to create a Hugging Face API key:
-
Sign Up or Log In to HuggingFace
-
Navigate to API Tokens:
• Once logged in, click on your profile picture or username at the top right corner of the page to open the user menu.
• From the dropdown menu, select “Settings”.
- Generate an API Key:
• In the settings menu, look for the “Access Tokens” section.
• Click on “New token” to generate a new API key.
• Give your token a name and select the appropriate scope (usually, the default scopes are fine).
• Click “Generate”.
- Copy Your API Key:
• Once the API key is generated, copy it. This is the HF_API_KEY that you will use to authenticate your requests to the Hugging Face API.
- Store the API Key Securely:
• Create a .env file in your project directory if you don’t already have one.
• Add your API key to the .env file like this:
import requests
import json
import os
from dotenv import load_dotenv, find_dotenv
Load environment variables from .env file
_ = load_dotenv(find_dotenv())
HF_API_KEY=your_hugging_face_api_key
HF_API_SUMMARY_BASE=os.environ[‘HF_API_SUMMARY_BASE’]
def get_completion(inputs, parameters=None, ENDPOINT_URL=hf_api_summary_base):
headers = {
“Authorization”: f"Bearer {hf_api_key}",
“Content-Type”: “application/json”
}
data = { “inputs”: inputs }
if parameters is not None:
data.update({“parameters”: parameters})
response = requests.post(ENDPOINT_URL, headers=headers, data=json.dumps(data))
if response.status_code == 200:
return response.json()
else:
print(f"Error: {response.status_code}")
print(response.json())
return None
Example text to summarize
text = (‘’‘The tower is 324 metres (1,063 ft) tall, about the same height
as an 81-storey building, and the tallest structure in Paris.
Its base is square, measuring 125 metres (410 ft) on each side.
During its construction, the Eiffel Tower surpassed the Washington
Monument to become the tallest man-made structure in the world,
a title it held for 41 years until the Chrysler Building
in New York City was finished in 1930. It was the first structure
to reach a height of 300 metres. Due to the addition of a broadcasting
aerial at the top of the tower in 1957, it is now taller than the
Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the
Eiffel Tower is the second tallest free-standing structure in France
after the Millau Viaduct.’‘’)
Call the summarization function
summary = get_completion(inputs=text)
Print the summary
if summary:
print(summary)