Skip to content

Commit b364c6f

Browse files
committed
Support creating text-mode detectors
1 parent 04ac2ea commit b364c6f

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

src/groundlight/experimental_api.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from groundlight_openapi_client.model.payload_template_request import PayloadTemplateRequest
3131
from groundlight_openapi_client.model.rule_request import RuleRequest
3232
from groundlight_openapi_client.model.status_enum import StatusEnum
33+
from groundlight_openapi_client.model.text_mode_configuration import TextModeConfiguration
3334
from groundlight_openapi_client.model.webhook_action_request import WebhookActionRequest
3435
from model import (
3536
ROI,
@@ -1053,6 +1054,60 @@ def create_bounding_box_detector( # noqa: PLR0913 # pylint: disable=too-many-ar
10531054
obj = self.detectors_api.create_detector(detector_creation_input, _request_timeout=DEFAULT_REQUEST_TIMEOUT)
10541055
return Detector.parse_obj(obj.to_dict())
10551056

1057+
def create_text_recognition_detector(
1058+
self,
1059+
name: str,
1060+
query: str,
1061+
*,
1062+
group_name: Optional[str] = None,
1063+
confidence_threshold: Optional[float] = None,
1064+
patience_time: Optional[float] = None,
1065+
pipeline_config: Optional[str] = None,
1066+
metadata: Union[dict, str, None] = None,
1067+
) -> Detector:
1068+
"""
1069+
Creates a text recognition detector that can read specified spans of text from images.
1070+
1071+
**Example usage**::
1072+
1073+
gl = ExperimentalApi()
1074+
1075+
# Create a text recognition detector
1076+
detector = gl.create_text_recognition_detector(
1077+
name="date_and_time_detector",
1078+
query="Read the date and time from the bottom left corner of the image.",
1079+
)
1080+
1081+
:param name: A short, descriptive name for the detector.
1082+
:param query: A question about the object to detect in the image.
1083+
:param group_name: Optional name of a group to organize related detectors together.
1084+
:param confidence_threshold: A value that sets the minimum confidence level required for the ML model's
1085+
predictions. If confidence is below this threshold, the query may be sent for human review.
1086+
:param patience_time: The maximum time in seconds that Groundlight will attempt to generate a
1087+
confident prediction before falling back to human review. Defaults to 30 seconds.
1088+
:param pipeline_config: Advanced usage only. Configuration string needed to instantiate a specific
1089+
prediction pipeline for this detector.
1090+
:param metadata: A dictionary or JSON string containing custom key/value pairs to associate with
1091+
1092+
:return: The created Detector object
1093+
"""
1094+
1095+
detector_creation_input = self._prep_create_detector(
1096+
name=name,
1097+
query=query,
1098+
group_name=group_name,
1099+
confidence_threshold=confidence_threshold,
1100+
patience_time=patience_time,
1101+
pipeline_config=pipeline_config,
1102+
metadata=metadata,
1103+
)
1104+
detector_creation_input.mode = ModeEnum.TEXT
1105+
mode_config = TextModeConfiguration()
1106+
1107+
detector_creation_input.mode_configuration = mode_config
1108+
obj = self.detectors_api.create_detector(detector_creation_input, _request_timeout=DEFAULT_REQUEST_TIMEOUT)
1109+
return Detector.parse_obj(obj.to_dict())
1110+
10561111
def _download_mlbinary_url(self, detector: Union[str, Detector]) -> EdgeModelInfo:
10571112
"""
10581113
Gets a temporary presigned URL to download the model binaries for the given detector, along

test/unit/test_experimental.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ def test_multiclass_detector(gl_experimental: ExperimentalApi):
147147
assert mc_iq.result.label in class_names
148148

149149

150+
@pytest.mark.skip(
151+
reason=(
152+
"General users currently currently can't use text recognition detectors. If you have questions, reach out"
153+
" to Groundlight support, or upgrade your plan."
154+
)
155+
)
156+
def test_text_recognition_detector(gl_experimental: ExperimentalApi):
157+
"""
158+
verify that we can create and submit to a text recognition detector
159+
"""
160+
name = f"Test {datetime.utcnow()}"
161+
created_detector = gl_experimental.create_text_recognition_detector(name, "What is the date and time?")
162+
assert created_detector is not None
163+
mc_iq = gl_experimental.submit_image_query(created_detector, "test/assets/dog.jpeg")
164+
assert mc_iq.result.label is not None
165+
166+
150167
@pytest.mark.skip(
151168
reason=(
152169
"General users currently currently can't use bounding box detectors. If you have questions, reach out"

test/unit/test_labels.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,21 @@ def test_multiclass_labels(gl_experimental: ExperimentalApi):
6464
assert iq1.result.label == "cherry"
6565
with pytest.raises(ApiException) as _:
6666
gl_experimental.add_label(iq1, "MAYBE")
67+
68+
69+
def test_text_recognition_labels(gl_experimental: ExperimentalApi):
70+
name = f"Test text recognition labels{datetime.utcnow()}"
71+
det = gl_experimental.create_text_recognition_detector(name, "test_query")
72+
iq1 = gl_experimental.submit_image_query(det, "test/assets/cat.jpeg")
73+
gl_experimental.add_label(iq1, "apple text")
74+
iq1 = gl_experimental.get_image_query(iq1.id)
75+
assert iq1.result.label == "apple text"
76+
gl_experimental.add_label(iq1, "banana text")
77+
iq1 = gl_experimental.get_image_query(iq1.id)
78+
assert iq1.result.label == "banana text"
79+
gl_experimental.add_label(iq1, "")
80+
iq1 = gl_experimental.get_image_query(iq1.id)
81+
assert iq1.result.label == ""
82+
83+
with pytest.raises(ApiException) as _:
84+
gl_experimental.add_label(iq1, "MAYBE")

0 commit comments

Comments
 (0)