Skip to navigation

Add Model Predictions

You can add predictions to existing tasks in Label Studio in your Python script.

If you would prefer to set them directly in the script, see Create a Project for an example.

This demo uses the example project from Create a Project. You would need to adjust this to match your labeling configuration.

For example, sentiment maps to <Choices name="sentiment" toName="text" choice="single"> in the labeling configuration we used earlier.

For information on finding your API key and base URL, see Authenticate and Connect to the API.

LABEL_STUDIO_URL = 'YOUR_BASE_URL'
LABEL_STUDIO_API_KEY = 'YOUR_API_KEY'
PROJECT_ID=1 # replace with your project ID
from label_studio_sdk import LabelStudio
from label_studio_sdk.label_interface.objects import PredictionValue
client = LabelStudio(base_url=LABEL_STUDIO_URL, api_key=LABEL_STUDIO_API_KEY)
project = client.projects.get(id=PROJECT_ID)
# Get the parsed labeling interface to build valid prediction payloads
li = project.get_label_interface()
# Iterate tasks and attach predictions
for task in client.tasks.list(project=project.id, include=["id"]):
# 'label' must match the control tag name in your labeling config
predicted_label = li.get_control('sentiment').label(['Positive'])
prediction = PredictionValue(
model_version="my-super-ai",
score=0.99, # optional
result=[predicted_label]
)
client.predictions.create(task=task.id, **prediction.model_dump())

For another example, see the Jupyter notebook example of importing pre-annotated data.