Skip to content

Commit 41fa6f7

Browse files
authored
order spans by their start index, return start index to the UI (#259)
Addresses allenai/playground-issues-repo#156 This PR ensures that spans are sorted by their `start_index` (left) before being returned to the UI. This allows the UI to iterate through the spans one at a time, only adding highlights for the first span. It also returns the `start_index` to the UI. That's not essential, but may be nice in the future or while debugging!
1 parent d67f76b commit 41fa6f7

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/attribution/attribution_service.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class GetAttributionRequest(APIInterface):
123123
@dataclass
124124
class ResponseAttributionSpan:
125125
text: str
126+
start_index: int
126127
documents: list[int] = field(default_factory=list)
127128

128129

@@ -138,9 +139,11 @@ def from_flattened_span(cls, span: FlattenedSpan) -> Self:
138139
ResponseAttributionSpan(
139140
text=nested_span.text,
140141
documents=[document.document_index for document in nested_span.documents],
142+
start_index=nested_span.left,
141143
)
142144
for nested_span in span.nested_spans
143145
],
146+
start_index=span.left,
144147
)
145148

146149

@@ -191,9 +194,7 @@ def get_attribution(
191194
)
192195
except UnexpectedStatus as e:
193196
msg = f"Something went wrong when calling the infini-gram API: {e.status_code} {e.content.decode()}"
194-
raise exceptions.BadGateway(
195-
msg
196-
)
197+
raise exceptions.BadGateway(msg) from e
197198

198199
if isinstance(attribution_response, HTTPValidationError):
199200
# validation error handling
@@ -273,12 +274,14 @@ def get_attribution(
273274

274275
return {
275276
"index": index,
277+
# The UI uses this sort order to show the documents in desc relevance
276278
"documents": sorted(
277279
mapped_documents.values(),
278280
key=lambda document: document.relevance_score,
279281
reverse=True,
280282
),
281-
"spans": list(mapped_spans.values()),
283+
# The UI uses this sort order to highlight spans in order
284+
"spans": sorted(mapped_spans.values(), key=lambda span: span.start_index),
282285
}
283286

284287

0 commit comments

Comments
 (0)