Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: DIA-1685: [sdk] Create example predictions and annotations from a LabelConfig #360

Merged
merged 8 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 150 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ typing_extensions = ">= 4.0.0"
ujson = ">=5.8.0"
xmljson = "0.2.1"

jsf = "^0.11.2"
[tool.poetry.dev-dependencies]
mypy = "1.0.1"
pytest = "^7.4.0"
Expand Down
16 changes: 16 additions & 0 deletions src/label_studio_sdk/label_interface/control_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,13 @@ class SpanSelectionOffsets(SpanSelection):
class ChoicesValue(BaseModel):
choices: List[str]

# I don't know how Choices predictions with choice != 'multiple' was working without this...
matt-bernstein marked this conversation as resolved.
Show resolved Hide resolved
@validator("choices", pre=True, always=True)
def coerce_to_list(cls, value: Union[str, List[str]]):
if isinstance(value, str):
return [value]
return value


class ChoicesTag(ControlTag):
""" """
Expand Down Expand Up @@ -540,6 +547,15 @@ def to_json_schema(self):
"description": f"Choices for {self.to_name[0]}"
}

def _validate_labels(self, labels):
if super()._validate_labels(labels):
return True

# HACK to continue to support single-item output in json schema
if not self.is_multiple_choice and isinstance(labels, str):
return super()._validate_labels([labels])



class LabelsValue(SpanSelection):
labels: List[str]
Expand Down
21 changes: 18 additions & 3 deletions src/label_studio_sdk/label_interface/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from collections import defaultdict, OrderedDict
from lxml import etree
import xmljson
from jsf import JSF

from label_studio_sdk._legacy.exceptions import (
LSConfigParseException,
Expand Down Expand Up @@ -770,7 +771,7 @@ def validate_region(self, region) -> bool:
return False

# type of the region should match the tag name
if control.tag.lower() != region["type"]:
if control.tag.lower() != region["type"].lower():
return False

# make sure that in config it connects to the same tag as
Expand Down Expand Up @@ -839,9 +840,23 @@ def generate_sample_task(self, mode="upload", secure_mode=False):

return task

def generate_sample_annotation(self):
def generate_sample_prediction(self):
matt-bernstein marked this conversation as resolved.
Show resolved Hide resolved
""" """
raise NotImplemented()
prediction = PredictionValue(
model_version='sample model version',
result=[
{
'from_name': control.name,
'to_name': control.to_name[0],
'type': control.tag,
# TODO: put special case for choices in generation instead of validation
'value': {control._label_attr_name: JSF(control.to_json_schema()).generate()}
} for control in self.controls
]
)
prediction_dct = prediction.model_dump()
assert self.validate_prediction(prediction_dct), 'could not generate a sample prediction'
matt-bernstein marked this conversation as resolved.
Show resolved Hide resolved
return prediction_dct

#####
##### COMPATIBILITY LAYER
Expand Down
Loading