FastAPI
Warning
⚠️ Before we begin, please make sure that you have authentication covered.
Info
This example shows the general structure of how to build a FastAPI application with encord-agents
.
For concrete implementations of agents with specific abilities, please see the examples section.
Create a project¶
Create a new project:
Create and source a new virtual environment.
Install the dependencies.
Develop your agent¶
Create a main.py
file with the following template:
from typing_extensions import Annotated
from encord.objects.ontology_labels_impl import LabelRowV2
from encord_agents import FrameData
from encord_agents.fastapi import dep_label_row
from encord_agents.fastapi.cors import EncordCORSMiddleware
from fastapi import FastAPI, Depends, Form
app = FastAPI()
app.add_middleware(EncordCORSMiddleware)
@app.post("/my_agent")
def my_agent(
frame_data: FrameData,
label_row: Annotated[LabelRowV2, Depends(dep_label_row)],
):
# ... Do your edits to the labels
label_row.save()
Fill in the function my_agent
with what you want to happen when your agent is triggered.
Tip
💡 Notice that you can inject multiple different dependencies into the function if you want.
You can find multiple examples of what can be done with editor agents here.
Test the agent¶
First, run the agent locally, such that it can be triggered.
Info
Effectively, this means starting an API that lives at localhost:8080/my_agent
and expects a POST request with JSON
data of the following format:
To hit that agent endpoint, open the Label Editor in your browser on a frame for which you want to test your agent. Copy the URL.
Open a new terminal in the my_project
directory.
Then, run
Warning
Notice the single quotes around <the_pasted_url>
. They are important and should be there because you might copy a url with, e.g., an &
character that have a special meaning if it is not within a string (or escaped).
Refresh the label editor in your browser to see the effect that you applied to the label_row: LabelRowV2
happening.
Deployment¶
Info
This section is under construction.
Meanwhile, please refer to the official FastAPI documentation.