Bin
2025-12-17 1442f92732d7c5311a627a7ba3aaa0bb8ffc539f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import pytest
 
from label_studio.tests.sdk.common import LABEL_CONFIG_AND_TASKS
 
pytestmark = pytest.mark.django_db
from label_studio_sdk.client import LabelStudio
from label_studio_sdk.label_interface import LabelInterface
from label_studio_sdk.label_interface.objects import PredictionValue, TaskValue
 
 
def test_predictions_CRUD(django_live_url, business_client):
    ls = LabelStudio(base_url=django_live_url, api_key=business_client.api_key)
    li = LabelInterface(LABEL_CONFIG_AND_TASKS['label_config'])
    p = ls.projects.create(title='New Project', label_config=li.config)
 
    task = ls.tasks.create(project=p.id, data={'my_text': 'Test task'})
 
    # create a prediction
    pv = PredictionValue(
        result=[li.get_control('sentiment_class').label(['Positive'])],
        score=0.9,
        model_version='1.0.0',
    )
    prediction = ls.predictions.create(
        task=task.id,
        **pv.model_dump(),
    )
 
    # get a prediction
    prediction = ls.predictions.get(id=prediction.id)
    assert prediction.result[0]['value']['choices'] == ['Positive']
    assert prediction.score == 0.9
    assert prediction.model_version == '1.0.0'
 
    # create another prediction
    pv = PredictionValue(
        result=[
            li.get_control('sentiment_class').label(['Neutral']),
            li.get_control('sentiment_class').label(['Negative']),
        ],
        score=0.8,
        model_version='1.0.1',
    )
 
    another_prediction = ls.predictions.create(task=task.id, **pv.model_dump())
 
    # check that there are two predictions
    predictions = ls.predictions.list(task=task.id)
    assert len(predictions) == 2
 
    # delete one prediction
    ls.predictions.delete(id=prediction.id)
    predictions = ls.predictions.list(task=task.id)
    assert len(predictions) == 1
    assert predictions[0].id == another_prediction.id
 
 
def test_create_predictions_with_import(django_live_url, business_client):
    ls = LabelStudio(base_url=django_live_url, api_key=business_client.api_key)
    li = LabelInterface(LABEL_CONFIG_AND_TASKS['label_config'])
    p = ls.projects.create(title='New Project', label_config=li.config)
 
    # import tasks with predictions
    ls.projects.import_tasks(
        id=p.id,
        request=[
            {'my_text': 'Hello world', 'sentiment_class': 'Positive'},
            {'my_text': 'Goodbye Label Studio', 'sentiment_class': 'Negative'},
            {'my_text': 'What a beautiful day', 'sentiment_class': 'Positive'},
        ],
        preannotated_from_fields=['sentiment_class'],
    )
 
    # check predictions for each class
    task_ids = []
    for task in ls.tasks.list(project=p.id):
        assert len(ls.predictions.list(task=task.id)) == 1
        task_ids.append(task.id)
    assert len(task_ids) == 3
 
    # import more tasks in extended format
    task1 = TaskValue(
        data={'my_text': 'Hello world'},
        predictions=[
            PredictionValue(
                result=[li.get_control('sentiment_class').label(['Positive'])],
                score=0.95,
                model_version='3.4.5',
            )
        ],
    )
    task2 = TaskValue(
        data={'my_text': 'Goodbye Label Studio'},
        predictions=[
            PredictionValue(
                result=[li.get_control('sentiment_class').label(['Negative'])],
                score=0.85,
                model_version='3.4.5',
            )
        ],
    )
 
    ls.projects.import_tasks(
        id=p.id,
        request=[task1.model_dump(), task2.model_dump()],
    )
 
    # check for new predictions
    for task in ls.tasks.list(project=p.id):
        predictions = ls.predictions.list(task=task.id)
        assert len(predictions) == 1
        if task.id not in task_ids:
            assert predictions[0].model_version == '3.4.5'
            task_ids.append(task.id)
 
    assert len(task_ids) == 5
 
    # update project with model_version (RND-113)
    assert p.model_version == ''
    ls.projects.update(id=p.id, model_version='3.4.5')
    project = ls.projects.get(id=p.id)
    assert project.model_version == '3.4.5'
 
    # assert it raises label_studio_sdk.core.api_error.ApiError with validation_errors': {'model_version': ["Model version doesn't exist..." ]}
    from label_studio_sdk.core.api_error import ApiError
 
    with pytest.raises(ApiError) as e:
        ls.projects.update(id=p.id, model_version='3.4.6')
    assert e.value.status_code == 400
    assert e.value.body['validation_errors']['model_version'][0].startswith("Model version doesn't exist")
 
 
def test_projects_import_predictions(django_live_url, business_client):
    """Import multiple predictions via projects.import_predictions
 
    Purpose:
    - Verify that the bulk predictions import endpoint creates predictions for a project
 
    Setup:
    - Create a project with a simple text classification config
    - Create one task in the project
 
    Actions:
    - Call ls.projects.import_predictions with three predictions for the same task
 
    Validations:
    - API returns created == 3
    - Listing predictions for the task returns exactly three items with expected model versions
    """
 
    ls = LabelStudio(base_url=django_live_url, api_key=business_client.api_key)
    li = LabelInterface(LABEL_CONFIG_AND_TASKS['label_config'])
    project = ls.projects.create(title='Predictions Import Project', label_config=li.config)
 
    task = ls.tasks.create(project=project.id, data={'my_text': 'Classify this sentence'})
 
    model_versions = ['humor__gpt-5-mini', 'humor__gpt-4.1-mini', 'humor__gpt-4o-mini']
    choices = [['Positive'], ['Neutral'], ['Negative']]
 
    predictions_payload = []
    for mv, ch in zip(model_versions, choices):
        predictions_payload.append(
            {
                'result': [
                    {
                        'from_name': 'sentiment_class',
                        'to_name': 'message',
                        'type': 'choices',
                        'value': {'choices': ch},
                    }
                ],
                'model_version': mv,
                'score': 1,
                'task': task.id,
            }
        )
 
    response = ls.projects.import_predictions(id=project.id, request=predictions_payload)
    assert response.created == 3
 
    preds = ls.predictions.list(task=task.id)
    assert len(preds) == 3
    returned_versions = sorted([p.model_version for p in preds])
    assert returned_versions == sorted(model_versions)