View the Code

See the code for this example on Github.

Installation

1

🎁 Create an Account

Create an account on Beam. You’ll get 15 hours of free credit when you signup!

2

🚧 Activate a Python Virtual Environment (Recommended)

Activate a Python virtualenv, which is where you’ll install the Beam SDK.

python3 -m venv .venv && source .venv/bin/activate
3

📝 Register API Token and Install SDK

Retrieve your API token from the dashboard, on the API Keys page.

Paste the token in the command below to install the Beam SDK. Make sure to replace [TOKEN] with your actual token.

pip install beam-client && beam configure default --token [TOKEN]

Your API key will be saved to the ~/.beam/config.ini file on your computer.

Deploy a Simple Web Endpoint

In order to run your code on Beam’s cloud, you’ll add special decorators to your code.

Let’s write a simple function that finds the square of 256.

We include numpy in the container image to show how you can easily add Python packages to your containers.

from beam import endpoint, Image


@endpoint(
    name="quickstart",
    cpu=1,
    memory="1Gi",
    image=Image().add_python_packages(["numpy"]),
)
def predict(**inputs):
    x = inputs.get("x", 256)
    return {"result": x**2}

Save this code to a file on your computer. You can name the file whatever you want, but we’ll call it app.py for this example.

Deployment

With the app.py file saved on your computer, you can deploy this to the cloud using this command:

beam deploy app.py:predict

Call the API

After running beam deploy, a cURL command will print in your shell. You can use this command to invoke the API.

Make sure to replace [TOKEN] with your actual token.

curl -X POST 'https://app.beam.cloud/endpoint/inference' -H 'Authorization: Bearer [TOKEN]'

The container will start in a few seconds and spin down automatically after the request!

What Next?

Here are some other things you can try: