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.

1LABEL_STUDIO_URL = 'YOUR_BASE_URL'
2LABEL_STUDIO_API_KEY = 'YOUR_API_KEY'
3PROJECT_ID=1 # replace with your project ID
4
5from label_studio_sdk import LabelStudio
6from label_studio_sdk.label_interface.objects import PredictionValue
7
8client = LabelStudio(base_url=LABEL_STUDIO_URL, api_key=LABEL_STUDIO_API_KEY)
9
10project = client.projects.get(id=PROJECT_ID)
11
12# Get the parsed labeling interface to build valid prediction payloads
13li = project.get_label_interface()
14
15# Iterate tasks and attach predictions
16for task in client.tasks.list(project=project.id, include=["id"]):
17 # 'label' must match the control tag name in your labeling config
18 predicted_label = li.get_control('sentiment').label(['Positive'])
19
20 prediction = PredictionValue(
21 model_version="my-super-ai",
22 score=0.99, # optional
23 result=[predicted_label]
24 )
25
26 client.predictions.create(task=task.id, **prediction.model_dump())

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