wchen22 commited on
Commit
b831ff9
·
verified ·
1 Parent(s): 4d2c01b

feat: add compression receipt drop summaries

Browse files
Files changed (1) hide show
  1. app.py +27 -13
app.py CHANGED
@@ -248,10 +248,15 @@ def _safe_classifier_drop_ranges(
248
  *,
249
  text_length: int,
250
  min_score: float,
251
- ) -> tuple[list[tuple[int, int]], int, int, int]:
 
 
 
 
 
 
252
  ranges: list[tuple[int, int]] = []
253
  drop_labels = 0
254
- blocked = 0
255
  for item in labels:
256
  raw_drop_score = item.get("drop_score")
257
  try:
@@ -269,19 +274,20 @@ def _safe_classifier_drop_ranges(
269
  end = int(item["end"])
270
  score = drop_score if drop_score is not None else float(item.get("score", 1.0))
271
  except Exception:
272
- blocked += 1
273
  continue
274
- if (
275
- start < 0
276
- or end > text_length
277
- or end <= start
278
- or score < min_score
279
- or _overlaps(protected, start, end)
280
- ):
281
- blocked += 1
282
  continue
283
  ranges.append((start, end))
284
- return _merge(ranges), drop_labels, len(ranges), blocked
 
285
 
286
 
287
  def _is_subsequence(candidate: str, original: str) -> bool:
@@ -368,7 +374,7 @@ def _compress_text(payload: dict[str, Any]) -> dict[str, Any]:
368
  preserve_json=preserve_json,
369
  )
370
  classifier_status, classifier_labels, classifier_error = _manifest_drop_labels(text)
371
- classifier_ranges, classifier_drop_labels, classifier_applied, classifier_blocked = (
372
  _safe_classifier_drop_ranges(
373
  classifier_labels,
374
  protected,
@@ -431,6 +437,7 @@ def _compress_text(payload: dict[str, Any]) -> dict[str, Any]:
431
  {"reason": reason, "preview": preview, "start": start, "end": end}
432
  for start, end, reason, preview in drops[:20]
433
  ]
 
434
  receipt = {
435
  "protected_spans_checked": len(protected_values),
436
  "protected_spans_missing": len(missing),
@@ -455,7 +462,14 @@ def _compress_text(payload: dict[str, Any]) -> dict[str, Any]:
455
  "drop_labels": classifier_drop_labels,
456
  "drop_spans_applied": classifier_applied,
457
  "drop_spans_blocked_by_safety": classifier_blocked,
 
458
  },
 
 
 
 
 
 
459
  "dropped_segments_count": len(drops),
460
  "dropped_segments": dropped_segments,
461
  }
 
248
  *,
249
  text_length: int,
250
  min_score: float,
251
+ ) -> tuple[list[tuple[int, int]], int, int, dict[str, int]]:
252
+ blocked_by_reason = {
253
+ "malformed_or_missing_offsets": 0,
254
+ "out_of_bounds": 0,
255
+ "below_min_score": 0,
256
+ "protected_span_overlap": 0,
257
+ }
258
  ranges: list[tuple[int, int]] = []
259
  drop_labels = 0
 
260
  for item in labels:
261
  raw_drop_score = item.get("drop_score")
262
  try:
 
274
  end = int(item["end"])
275
  score = drop_score if drop_score is not None else float(item.get("score", 1.0))
276
  except Exception:
277
+ blocked_by_reason["malformed_or_missing_offsets"] += 1
278
  continue
279
+ if start < 0 or end > text_length or end <= start:
280
+ blocked_by_reason["out_of_bounds"] += 1
281
+ continue
282
+ if score < min_score:
283
+ blocked_by_reason["below_min_score"] += 1
284
+ continue
285
+ if _overlaps(protected, start, end):
286
+ blocked_by_reason["protected_span_overlap"] += 1
287
  continue
288
  ranges.append((start, end))
289
+ merged = _merge(ranges)
290
+ return merged, drop_labels, len(merged), blocked_by_reason
291
 
292
 
293
  def _is_subsequence(candidate: str, original: str) -> bool:
 
374
  preserve_json=preserve_json,
375
  )
376
  classifier_status, classifier_labels, classifier_error = _manifest_drop_labels(text)
377
+ classifier_ranges, classifier_drop_labels, classifier_applied, classifier_blocked_by_reason = (
378
  _safe_classifier_drop_ranges(
379
  classifier_labels,
380
  protected,
 
437
  {"reason": reason, "preview": preview, "start": start, "end": end}
438
  for start, end, reason, preview in drops[:20]
439
  ]
440
+ classifier_blocked = sum(classifier_blocked_by_reason.values())
441
  receipt = {
442
  "protected_spans_checked": len(protected_values),
443
  "protected_spans_missing": len(missing),
 
462
  "drop_labels": classifier_drop_labels,
463
  "drop_spans_applied": classifier_applied,
464
  "drop_spans_blocked_by_safety": classifier_blocked,
465
+ "drop_spans_blocked_by_reason": classifier_blocked_by_reason,
466
  },
467
+ "removed_spans_count": len(drop_ranges),
468
+ "removed_chars": sum(end - start for start, end in drop_ranges),
469
+ "rule_drop_spans_applied": len(drops),
470
+ "rule_drop_chars": sum(end - start for start, end, _, _ in drops),
471
+ "classifier_drop_spans_applied": len(classifier_ranges),
472
+ "classifier_drop_chars": sum(end - start for start, end in classifier_ranges),
473
  "dropped_segments_count": len(drops),
474
  "dropped_segments": dropped_segments,
475
  }