def test_text_classification_input_string(): assert TextClassificationRecord( inputs="A text") == TextClassificationRecord(inputs=dict( text="A text")) assert TextClassificationRecord( inputs=["A text", "another text"]) == TextClassificationRecord( inputs=dict(text=["A text", "another text"]))
def test_metadata_values_length(): text = "oh yeah!" metadata = {"too_long": "a" * 200} record = TextClassificationRecord(inputs={"text": text}, metadata=metadata) assert len(record.metadata["too_long"]) == MAX_KEYWORD_LENGTH record = TokenClassificationRecord(text=text, tokens=text.split(), metadata=metadata) assert len(record.metadata["too_long"]) == MAX_KEYWORD_LENGTH
def _text_classification_sdk_to_record( sdk: Union[models.TextClassificationRecord, Dict[str, Any]] ) -> TextClassificationRecord: """Transforms and returns the sdk model as a `TextClassificationRecord` record""" if isinstance(sdk, models.TextClassificationRecord): sdk = sdk.to_dict() record = TextClassificationRecord( id=sdk.get("id"), event_timestamp=sdk.get("event_timestamp"), inputs=sdk.get("text", sdk.get("inputs")), multi_label=sdk.get("multi_label"), status=sdk.get("status"), ) prediction = sdk.get("prediction") if prediction: record.prediction = [(label["class"], label["confidence"]) for label in prediction["labels"]] record.prediction_agent = prediction["agent"] annotation = sdk.get("annotation") if annotation: # TODO(dfidalgo): it's depends on multilabel field? record.annotation = [ label["class"] for label in annotation["labels"] ] record.annotation_agent = annotation["agent"] explanation = sdk.get("explanation") if explanation: record.explanation = { key: [ TokenAttributions(**attribution) for attribution in attributions ] for key, attributions in explanation } metadata = sdk.get("metadata") if metadata: record.metadata = metadata return record
def test_text_classification_record(annotation, status, expected_status): """Just testing its dynamic defaults""" record = TextClassificationRecord(inputs={"text": "test"}, annotation=annotation, status=status) assert record.status == expected_status
def test_text_classification_record_none_inputs(): """Test validation error for None in inputs""" with pytest.raises(ValidationError): TextClassificationRecord(inputs={"text": None})