Modal provides a serverless cloud for engineers and researchers who want to build compute-intensive applications without thinking about infrastructure. The cloud service is well suited for building and hosting Encord Editor Agents.
This example places a bitmask “ball” in the center of an image in the Label Editor.
See the Editor Agent examples for more sophisticated use cases.
Everything in the FastAPI examples can also be done using Modal, as Modal acts as a wrapper around FastAPI.
This page is not intended to explain everything that Modal can do — as it does a lot —, but it does provide a working example.
For a complete code example, go here.
You need to authenticate with Encord first. Once you have a private ssh key (preferably corresponding to a service account), you should also ensure that you have signed up for Modal.
Now you can configure the secret:
encord-ssh-key
(you can choose the name but it needs to match the name in the code below)ENCORD_SSH_KEY
with the content of your private ssh key file. Similar to the figure below.This setup allows encord-agents
to authenticate with Encord using the provided key.
Ensure that you have installed encord-agents
and modal
:
Now you need a template for creating an endpoint that can be used for defining an Editor Agent.
The following example:
Creates a Docker Image – We define a container that includes all necessary dependencies:
encord-agents
: The library for building agentsmodal
: For hosting the agentlibgl
: Required by opencv
Modal automatically handles setting up this container for you.
Defines an App – We create a FastAPI application to manage the agent’s endpoints.
Sets Up Authentication – We add an endpoint that uses an SSH key for authentication.
ENCORD_SSH_KEY
.encord-agents
automatically detects this key and use it to authenticate with the Encord SDK.Implements the Agent Logic – defines what the agent should do when triggered.
Since this is a FastAPI route, you can also take advantage of built-in dependencies from encord_agents.fastapi.dependencies
to simplify your setup.
Next, you must define some logic to go into the agent.
The following example adds a circular bitmask in the middle of the current frame. However, it could easily be running your own model (even on the GPU).
In the function definition of your route, add the following code to:
Upon completion, the encord_agents
library sends a 200 response to the Label Editor, causing the editor to refresh its state. The result is a sphere in the middle of the image.
To test the agent, there is a dedicated CLI tool.
You must first run the agent locally (with your Python environment sourced).
Assuming that the python file you created is named example.py
, you can run:
The command should print a url similar to this
Let’s call it <agent_url>
.
Now, go to the Encord Label Editor in your browser, viewing an asset for which you would like to test your agent.
Copy the url; let’s call it <editor_url>
.
It should follow this pattern:
With the endpoint running, you can from another terminal window run:
Where you replace the <agent_url>
and the <editor_url>
with the relevant urls.
You should see a green box detaining the request and the response.
If the status code is 200, refresh your browser to see the new bitmask in the Label Editor.
When you are done developing your agent, run the following to deploy your agent.
Copy the displayed url and configure it in the Encord platform by following the documentation.
Once that is done, you can right-click the frame in the Label Editor to trigger the agent.