Skip to content

Commit 7239f42

Browse files
authored
Merge pull request #2024 from kili-technology/feature/sup-2586-enabled-us-cloud-extra-label-data-being-returned-for-video
fix(SUP-2586): remove annotations as we now have it in the jsonResponseUrl
2 parents c3ed9e9 + 4cad8fb commit 7239f42

10 files changed

Lines changed: 125 additions & 296 deletions

File tree

src/kili/adapters/kili_api_gateway/asset/operations.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,3 @@ def get_assets_query(fragment: str) -> str:
3030
external_ids: filterExistingAssets(projectID: $projectID, externalIDs: $externalIDs)
3131
}
3232
"""
33-
34-
GQL_COUNT_ASSET_ANNOTATIONS = """
35-
query countAssetAnnotations($where: AssetWhere!) {
36-
data: countAssetAnnotations(where: $where)
37-
}
38-
"""

src/kili/adapters/kili_api_gateway/asset/operations_mixin.py

Lines changed: 15 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,11 @@
22

33
from collections.abc import Generator
44

5-
from kili_formats.tool.annotations_to_json_response import (
6-
AnnotationsToJsonResponseConverter,
7-
)
8-
95
from kili.adapters.kili_api_gateway.asset.formatters import (
106
load_asset_json_fields,
117
)
128
from kili.adapters.kili_api_gateway.asset.mappers import asset_where_mapper
139
from kili.adapters.kili_api_gateway.asset.operations import (
14-
GQL_COUNT_ASSET_ANNOTATIONS,
1510
GQL_COUNT_ASSETS,
1611
GQL_CREATE_UPLOAD_BUCKET_SIGNED_URLS,
1712
GQL_FILTER_EXISTING_ASSETS,
@@ -23,18 +18,11 @@
2318
QueryOptions,
2419
fragment_builder,
2520
)
26-
from kili.adapters.kili_api_gateway.label.common import get_annotation_fragment
2721
from kili.adapters.kili_api_gateway.project.common import get_project
2822
from kili.core.graphql.operations.asset.mutations import GQL_SET_ASSET_CONSENSUS
2923
from kili.domain.asset import AssetFilters
3024
from kili.domain.types import ListOrTuple
3125

32-
# Threshold for batching based on number of annotations
33-
# This is used to determine whether to use a single batch or multiple batches
34-
# when fetching assets. If the number of annotations counted exceeds this threshold,
35-
# the asset fetch will be done in multiple smaller batches to avoid performance issues.
36-
THRESHOLD_FOR_BATCHING = 200
37-
3826

3927
class AssetOperationMixin(BaseOperationMixin):
4028
"""Mixin extending Kili API Gateway class with Assets related operations."""
@@ -46,9 +34,6 @@ def list_assets(
4634
options: QueryOptions,
4735
) -> Generator[dict, None, None]:
4836
"""List assets with given options."""
49-
has_labels_url = "labels.jsonResponseUrl" in fields
50-
has_latest_label_url = "latestLabel.jsonResponseUrl" in fields
51-
5237
if "labels.jsonResponse" in fields or "latestLabel.jsonResponse" in fields:
5338
# Check if we can get the jsonResponse of if we need to rebuild it.
5439
project_info = get_project(
@@ -61,10 +46,7 @@ def list_assets(
6146
"LLM_STATIC",
6247
"GEOSPATIAL",
6348
}:
64-
fetch_annotations = not (has_labels_url or has_latest_label_url)
65-
yield from self.list_assets_split(
66-
filters, fields, options, project_info, fetch_annotations
67-
)
49+
yield from self.list_assets_split(filters, fields, options, project_info)
6850
return
6951

7052
fragment = fragment_builder(fields)
@@ -90,35 +72,24 @@ def list_assets_split(
9072
fields: ListOrTuple[str],
9173
options: QueryOptions,
9274
project_info,
93-
fetch_annotations: bool,
9475
) -> Generator[dict, None, None]:
9576
"""List assets with given options."""
96-
nb_annotations = self.count_assets_annotations(filters)
9777
assets_batch_max_amount = 10 if project_info["inputType"] == "VIDEO" else 50
9878
batch_size_to_use = min(options.batch_size, assets_batch_max_amount)
99-
batch_size = (
100-
1 if nb_annotations / batch_size_to_use > THRESHOLD_FOR_BATCHING else batch_size_to_use
101-
)
10279

103-
options = QueryOptions(options.disable_tqdm, options.first, options.skip, batch_size)
104-
105-
static_fragments = {}
106-
if fetch_annotations:
107-
inner_annotation_fragment = get_annotation_fragment()
108-
annotation_fragment = f"""
109-
annotations {{
110-
{inner_annotation_fragment}
111-
}}
112-
"""
113-
static_fragments = {"labels": annotation_fragment, "latestLabel": annotation_fragment}
114-
115-
required_fields = {"content", "jsonContent", "resolution.width", "resolution.height"}
116-
fields = list(fields)
117-
for field in required_fields:
118-
if field not in fields:
119-
fields.append(field)
120-
121-
fragment = fragment_builder(fields, static_fragments if static_fragments else None)
80+
options = QueryOptions(options.disable_tqdm, options.first, options.skip, batch_size_to_use)
81+
82+
required_fields = {"content", "jsonContent", "resolution.width", "resolution.height"}
83+
if "labels.jsonResponse" in fields:
84+
required_fields.add("labels.jsonResponseUrl")
85+
if "latestLabel.jsonResponse" in fields:
86+
required_fields.add("latestLabel.jsonResponseUrl")
87+
fields = list(fields)
88+
for field in required_fields:
89+
if field not in fields:
90+
fields.append(field)
91+
92+
fragment = fragment_builder(fields)
12293
query = get_assets_query(fragment)
12394
where = asset_where_mapper(filters)
12495
assets_gen = PaginatedGraphQLQuery(self.graphql_client).execute_query_from_paginated_call(
@@ -128,28 +99,7 @@ def list_assets_split(
12899
load_asset_json_fields(asset, fields, self.http_client) for asset in assets_gen
129100
)
130101

131-
if fetch_annotations:
132-
converter = AnnotationsToJsonResponseConverter(
133-
json_interface=project_info["jsonInterface"],
134-
project_input_type=project_info["inputType"],
135-
)
136-
is_requesting_annotations = any("annotations." in element for element in fields)
137-
for asset in assets_gen:
138-
if "latestLabel.jsonResponse" in fields and asset.get("latestLabel"):
139-
converter.patch_label_json_response(
140-
asset, asset["latestLabel"], asset["latestLabel"]["annotations"]
141-
)
142-
if not is_requesting_annotations:
143-
asset["latestLabel"].pop("annotations")
144-
145-
if "labels.jsonResponse" in fields:
146-
for label in asset.get("labels", []):
147-
converter.patch_label_json_response(asset, label, label["annotations"])
148-
if not is_requesting_annotations:
149-
label.pop("annotations")
150-
yield asset
151-
else:
152-
yield from assets_gen
102+
yield from assets_gen
153103

154104
def count_assets(self, filters: AssetFilters) -> int:
155105
"""Send a GraphQL request calling countIssues resolver."""
@@ -176,14 +126,6 @@ def filter_existing_assets(self, project_id: str, assets_external_ids: ListOrTup
176126
external_id_response = self.graphql_client.execute(GQL_FILTER_EXISTING_ASSETS, payload)
177127
return external_id_response["external_ids"]
178128

179-
def count_assets_annotations(self, filters: AssetFilters) -> int:
180-
"""Count the number of annotations for assets matching the filters."""
181-
where = asset_where_mapper(filters)
182-
payload = {"where": where}
183-
count_result = self.graphql_client.execute(GQL_COUNT_ASSET_ANNOTATIONS, payload)
184-
count: int = count_result["data"]
185-
return count
186-
187129
def update_asset_consensus(
188130
self,
189131
project_id: str,

src/kili/adapters/kili_api_gateway/label/common.py

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
from kili.adapters.kili_api_gateway.asset.formatters import load_asset_json_fields
55
from kili.adapters.kili_api_gateway.asset.operations import get_assets_query
66
from kili.adapters.kili_api_gateway.helpers.queries import fragment_builder
7-
from kili.adapters.kili_api_gateway.label.operations import (
8-
get_annotations_partial_query,
9-
)
107
from kili.core.graphql.graphql_client import GraphQLClient
118
from kili.domain.asset.asset import AssetId
129
from kili.domain.types import ListOrTuple
@@ -32,47 +29,3 @@ def get_asset(
3229
f"asset ID: {asset_id}. The asset does not exist or you do not have access to it."
3330
)
3431
return load_asset_json_fields(assets[0], fields, http_client=http_client)
35-
36-
37-
def get_annotation_fragment():
38-
"""Generates a fragment to get all annotations and their values."""
39-
return get_annotations_partial_query(
40-
annotation_fragment=fragment_builder(("__typename", "id", "job", "path", "labelId")),
41-
classification_annotation_fragment=fragment_builder(
42-
("annotationValue.categories", "chatItemId")
43-
),
44-
comparison_annotation_fragment=fragment_builder(
45-
(
46-
"annotationValue.choice.code",
47-
"annotationValue.choice.firstId",
48-
"annotationValue.choice.secondId",
49-
"chatItemId",
50-
)
51-
),
52-
transcription_annotation_fragment=fragment_builder(("annotationValue.text", "chatItemId")),
53-
video_annotation_fragment=fragment_builder(
54-
(
55-
"frames.start",
56-
"frames.end",
57-
"keyAnnotations.id",
58-
"keyAnnotations.frame",
59-
)
60-
),
61-
video_classification_annotation_fragment=fragment_builder(
62-
("keyAnnotations.annotationValue.categories",)
63-
),
64-
video_object_detection_annotation_fragment=fragment_builder(
65-
(
66-
"keyAnnotations.annotationValue.vertices: verticesScalar",
67-
"name",
68-
"mid",
69-
"category",
70-
)
71-
),
72-
video_transcription_annotation_fragment=fragment_builder(
73-
("keyAnnotations.annotationValue.text",)
74-
),
75-
object_detection_annotation_fragment=fragment_builder(
76-
("category", "mid", "name", "annotationValue.vertices: verticesScalar")
77-
),
78-
)

src/kili/adapters/kili_api_gateway/label/operations.py

Lines changed: 0 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -71,82 +71,6 @@ def get_append_to_labels_mutation(fragment: str) -> str:
7171
"""
7272

7373

74-
def get_annotations_partial_query(
75-
annotation_fragment: str,
76-
classification_annotation_fragment: str,
77-
comparison_annotation_fragment: str,
78-
transcription_annotation_fragment: str,
79-
video_annotation_fragment: str,
80-
video_object_detection_annotation_fragment: str,
81-
video_classification_annotation_fragment: str,
82-
video_transcription_annotation_fragment: str,
83-
object_detection_annotation_fragment: str,
84-
) -> str:
85-
"""Get the gql annotations query."""
86-
inline_fragments = ""
87-
88-
if classification_annotation_fragment.strip():
89-
inline_fragments += f"""
90-
... on ClassificationAnnotation {{
91-
{classification_annotation_fragment}
92-
}}
93-
"""
94-
95-
if comparison_annotation_fragment.strip():
96-
inline_fragments += f"""
97-
... on ComparisonAnnotation {{
98-
{comparison_annotation_fragment}
99-
}}
100-
"""
101-
102-
if transcription_annotation_fragment.strip():
103-
inline_fragments += f"""
104-
... on TranscriptionAnnotation {{
105-
{transcription_annotation_fragment}
106-
}}
107-
"""
108-
109-
if video_annotation_fragment.strip():
110-
inline_fragments += f"""
111-
... on VideoAnnotation {{
112-
{video_annotation_fragment}
113-
}}
114-
"""
115-
116-
if video_object_detection_annotation_fragment.strip():
117-
inline_fragments += f"""
118-
... on VideoObjectDetectionAnnotation {{
119-
{video_object_detection_annotation_fragment}
120-
}}
121-
"""
122-
123-
if video_classification_annotation_fragment.strip():
124-
inline_fragments += f"""
125-
... on VideoClassificationAnnotation {{
126-
{video_classification_annotation_fragment}
127-
}}
128-
"""
129-
130-
if video_transcription_annotation_fragment.strip():
131-
inline_fragments += f"""
132-
... on VideoTranscriptionAnnotation {{
133-
{video_transcription_annotation_fragment}
134-
}}
135-
"""
136-
137-
if object_detection_annotation_fragment.strip():
138-
inline_fragments += f"""
139-
... on ObjectDetectionAnnotation {{
140-
{object_detection_annotation_fragment}
141-
}}
142-
"""
143-
144-
return f"""
145-
{annotation_fragment}
146-
{inline_fragments}
147-
"""
148-
149-
15074
GQL_COPY_LABELS = """
15175
mutation CopyLabels($data: CopyLabelsInput!) {
15276
copyLabels(data: $data)

0 commit comments

Comments
 (0)