Notice how the workflow has a purple Agent node called "prioritize". This node will allow every piece of data to be prioritized by the custom code written in this example.
Installation¶
Please ensure that you have the encord-agents
library installed:
!python -m pip install encord-agents
Authentication¶
The library authenticates via ssh-keys. Below, is a code cell for setting the ENCORD_SSH_KEY
environment variable. It should contain the raw content of your private ssh key file.
If you have not yet setup an ssh key, please follow the documentation.
💡 Colab users: In colab, you can set the key once in the secrets in the left sidebar and load it in new notebooks with
from google.colab import userdata key_content = userdata.get("ENCORD_SSH_KEY")
import os
os.environ["ENCORD_SSH_KEY"] = "private_key_file_content"
# or you can set a path to a file
# os.environ["ENCORD_SSH_KEY_FILE"] = "/path/to/your/private/key"
[Alternative] Temporary Key¶
There's also the option of generating a temporary (fresh) ssh key pair via the code cell below. Please follow the instructions printed when executing the code.
# ⚠️ Safe to skip if you have authenticated already
import os
from encord_agents.utils.colab import generate_public_private_key_pair_with_instructions
private_key_path, public_key_path = generate_public_private_key_pair_with_instructions()
os.environ["ENCORD_SSH_KEY_FILE"] = private_key_path.as_posix()
Workflow creation¶
Before we dive into the code, we need to set up a workflow similar to the example shown above. The important thing here is that it begins with an agent node that routes to the annotation node. Naming is not important.
📖 here is the documentation for creating a workflow with Encord.
Agent definition¶
In the following code cell, we define the custom code that will put a priority on a task.
Please fill in
<your_project_hash>
: You obtain it from the project page in the Encord platform.<your_agent_stage_uuid>
: It would be "1e775..." in the example workflow above.<your_pathway_uuid>
: It will be listed when you expand the agent node of the workflowe on the project page. (You can also use the pathway name that you gave it)
from encord.objects.ontology_labels_impl import LabelRowV2
from encord_agents.tasks import Runner
runner = Runner(project_hash="<your_project_hash>")
@runner.stage("<your_agent_stage_uuid>")
def by_file_name(lr: LabelRowV2) -> str | None:
# Assuming the data_title is of the format "%d.jpg"
# and in the range [0; 100]
priority = int(lr.data_title.split(".")[0]) / 100
lr.set_priority(priority=priority)
return "<your_pathway_uuid>"
- Task Runner: The code initializes a runner to process tasks.
- Priority Assignment: It defines a stage implementation that:
- Extracts the data title of a task.
- Parses the stem of the data title as an integer.
- Assigns a priority as a number between
0
and1
.
- Task Routing: Passes the task to the annotation stage by returning the correct pathway
UUID
.
Running the agent¶
Now that we've defined the project, workflow, and the agent, it's time to try it out.
The runner
object is callable which means that you can just call it to prioritize your tasks.
# Run the agent
runner()
Your agent now assigns priorities to tasks based on their file names and routes them appropriately through the Workflow.
💡Hint: If you were to execute this as a python script, you can run it as a command line interface by putting the above code in an
agents.py
file and replacingrunner()with
if __name__ == "__main__": runner.run()Which will allow you set, e.g., the project hash via the command line:
python agent.py --project-hash "..."