Building Gen AI Applications with Gradio

Hi Team,

I’m trying to run the training material in my local Jupyter notebook but I’m stuck with the below error.

Cell In[8], line 8
6 from dotenv import load_dotenv, find_dotenv
7 _ = load_dotenv(find_dotenv()) # read local .env file
----> 8 hf_api_key = os.environ[‘HF_API_KEY’]

KeyError: ‘HF_API_KEY’

Thanks Viviana

Generate this key from hugging face portal and store it in your bash_profile.

export HF_API_KEY = <your generated key on hugging face website>

Hi everyone at Deeplearning.ai

I would like to report that the notebooks from the course don’t seem to be running for me. It looks like something needs to be updated (not sure what exactly). All the requests that are made to the endpoints give an error {‘error’: ‘Bad Request: Invalid state’} ← for example.

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:

  1. Sign Up or Log In to HuggingFace

  2. 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”.

  1. 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”.

  1. 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.

  1. 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)

Oh and 1 more thing, you need to update this line as well: hf_api_summary_base = os.environ[‘HF_API_SUMMARY_BASE’]

I am unable to provide link here but please use ChatGPT for suggest link for Huggingface bart large cnn model

HF_API_SUMMARY_BASE={Huggingface bart large cnn model link}