Migrate from Cog
This tutorial will show you how to migrate a Cog Hello World app to Beam.
Beam vs. Cog
Beam and Cog are fundamentally different products. Cog is a wrapper around Docker, whereas Beam provides a custom container runtime. Moreover, the interface for Cog is yaml
, whereas the interface for Beam is pure Python.
Beam’s custom container runtime is the magic behind fast cold-start. In addition, Beam provides other utilities for developers building AI apps, like the ability to cache models in storage volumes and run development previews.
You don’t need Docker to use Beam. The only requirement is having the Beam CLI and SDK installed.
Refactoring a Cog to Beam
The structure of a Cog is two-fold. There is a predict.py
, which is the entry-point to your model, and a cog.yaml
which defines the libraries and dependencies to package with your app.
With Beam, your predict.py
and cog.yaml
will be combined into the same Python file.
You don’t necessarily need to keep your entire Beam app in a single file; feel free to split your code into different files and import the modules accordingly.
Migrate the logic in predict.py
The first thing we’ll do is refactor the predict.py
portion of the Cog to use Beam primitives.
This is the original predict.py
file:
To migrate predict.py
to Beam, create a new Python file. You can call it whatever you want, but for this example, we will name it app.py
.
Migrate the image and dependencies in cog.yaml
Now, we’ll move the dependencies in cog.yaml
to the app.py
file with your Beam app:
These dependencies go in the Image
class.
Adding System Commands & Python Packages
This is a simple Hello World example, but your real-world Cog app probably has additional dependencies. Suppose your cog.yaml
looks like this:
To add these dependencies to a Beam app, you can use the python_packages
and commands
field in Image()
:
Test Your Beam Code
To test your Beam code, you can spin up a temporary dev server:
This will spin up a dev server on the cloud, so you can test your code changes live as you work:
Deploy to Beam
When you are ready to deploy your app, run this command:
This will package your app and spin up a persistent web endpoint to invoke it. You can then invoke your deployed API with a cURL command:
Next Steps
After you’ve refactored your app to run on Beam, there are more optimizations you can take advantage of.
Was this page helpful?