View the Code
See the code for this example on Github.
Prerequisites
- Request Access: Request access to the model here.
- Retrieve HF Token: Get your Huggingface token from this page.
- Save HF Token on Beam: Use the command
beam secret create HF_TOKEN [TOKEN]to save your token.
Setup Remote Environment
The first thing we’ll do is set up anImage with the Python packages required for this app.
We use the if env.is_remote() flag to conditionally import the Python packages only when the script is running remotely on Beam.
Inference Function
Here’s the inference function. By adding the@endpoint decorator to it, we can expose this function as a RESTful API.
Note the secrets argument which ensures the Huggingface token is loaded into the environment.
Deploy to Production
The following command deploys our code to Beam, and hosts it as a REST API:Invoking the API
Once the API is running, you can invoke it using the following cURL command:[ENDPOINT-ID] with your actual endpoint ID and [AUTH-TOKEN] with your authentication token. You’ll see a response from the API, like this: