Using Secrets
Inject your Environment Variables and API Keys.
Function provides functionality for injecting environment variables into predictors while they are being provisioned. These are primarily used to pass secrets to the predictor, including API keys and other secure information.
In this guide, let's make a predictor uses OpenAI's managed Whisper service to transcribe an audio file:
Implementing the Predictor
Create a transcribe.ipynb
notebook and add the following cell to install a few dependencies we'll need:
# Install OpenAI library
%pip install openai
Next, add a new code cell and paste the following implementation:
import openai
from pathlib import Path
def predict (audio: Path) -> str:
# Make sure that the audio file has a ".wav" extension
audio = audio.rename(audio.with_suffix(f".wav"))
# Transcribe
with open(audio, "rb") as f:
transcript = openai.Audio.transcribe("whisper-1", f)
# Return
return transcript["text"]
This predictor accepts an audio
path pointing to an audio file on the system. It then transcribes the file using OpenAI's hosted Whisper service, then returns the transcribed text.
audio
input value to a Path
which points to the audio file. See more on Value → Using Secrets in Code
We have to specify our OpenAI API key to use the Whisper service in our code. In Function, environment variables are accessed through the os.environ
dictionary. Let's now update our code to set the API key:
import openai
from os import environ
from pathlib import Path
# Specify our OpenAI API key
openai.api_key = environ.get("OPENAI_API_KEY")
def predict (audio: Path) -> str:
# Make sure that the audio file has a ".wav" extension
audio = audio.rename(audio.with_suffix(f".wav"))
# Transcribe
with open(audio, "rb") as f:
transcript = openai.Audio.transcribe("whisper-1", f)
# Return
return transcript["text"]
OPENAI_API_KEY
↗ env. Defining Secrets
Function provides two ways to define environment variables to predictors:
Global Secrets
Global secrets are available to all predictors under a given account. To define a global secret, use the Function CLI:
# Open a terminal and run the following command:
fxn env create OPENAI_API_KEY 1234abcd
--organization
flag to create a global secret for an organization account. You can also define global secrets on fxn.ai ↗
Predictor Secrets
As the name implies, predictor secrets are specific to a given predictor. To define a predictor secret, use the --env
flag when creating the predictor in the Function CLI:
# Open a terminal and run this command to create our predictor
fxn create @username/transcribe transcribe.ipynb --env OPENAI_API_KEY=1234abcd
@username
with your Function username. --env NAME=VALUE
. Making a Prediction
If you haven't already, head over to OpenAI ↗ to generate a new API key. Then define a new global secret:
# Open a terminal and run the following command:
fxn env create OPENAI_API_KEY <YOUR OPENAI API KEY>
Now, let's create the predictor:
# Create the predictor
fxn create @username/transcribe transcribe.ipynb
@username
with your Function username. Once the predictor has been provisioned, head over to voice-recorder-online.com ↗ to record your voice to a waveform audio file.
Next, open a terminal and run the following command:
# Test out our transcription predictor
fxn predict @username/transcribe --audio @record.wav
After a few moments, you should see the transcription: