how to use openai api key?

#23
by magupon - opened

After deplicating the space, where to use my own api key?

After duplicating the space, head over to Repository Secrets under Settings and add a new secret with name as "OPENAI_API_KEY" and the value as your key

To use your environment variables in a secure way you need to do this:

  1. Go to https://huggingface.co./spaces/USER/SPACE_ID/settings [USER = Your hugging face user name ; SPACE ID = space name as in space home

image.png

  1. Create Private Secret

image.png

  1. Go to your app.py script and add these lines:

'''
... your code....
import os

api_key = os.getenv("OPENAI_API_KEY")
client = OpenAI(api_key=api_key)

... the rest of your code.....
'''

image.png

That should do the trick.

Would be nice if the huggingface's team adds this little detail to the docs: https://huggingface.co./docs/hub/spaces-overview#managing-secrets

Sign up or log in to comment