Karthikraj Sivakumar commited on
Commit
7e07095
·
1 Parent(s): c9c30b5

fix biased uncertainty problem

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -379,16 +379,35 @@ def predict_captcha(image):
379
  prediction, confidence = ctc_decode_with_confidence(log_probs, idx_to_char)
380
  confidence_pct = confidence * 100
381
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
382
  # Format output
383
  output = f"**Primary Prediction:** {prediction}\n\n"
384
 
385
- # Add status indicator
386
- if confidence < 0.6:
387
- status = "⚠️ Low Confidence"
388
- note = "Visual ambiguity detected (e.g., 0/o, i/1/l confusion)"
 
 
 
 
389
 
390
- # Get alternative predictions when confidence is low
391
- top_predictions = ctc_decode_top_k(log_probs, idx_to_char, k=3)
392
 
393
  output += f"{status} — {confidence_pct:.1f}%\n"
394
  output += f"{note}\n\n"
@@ -396,7 +415,8 @@ def predict_captcha(image):
396
 
397
  for i, (text, conf) in enumerate(top_predictions, 1):
398
  conf_pct = conf * 100
399
- output += f"{i}. `{text}` {conf_pct:.1f}%\n"
 
400
 
401
  output += "\n💡 *Tip: Check which makes sense in context*"
402
 
 
379
  prediction, confidence = ctc_decode_with_confidence(log_probs, idx_to_char)
380
  confidence_pct = confidence * 100
381
 
382
+ # Get top-k predictions to check uncertainty
383
+ top_predictions = ctc_decode_top_k(log_probs, idx_to_char, k=3)
384
+
385
+ # Check if alternatives are close (uncertainty margin)
386
+ show_alternatives = False
387
+ if len(top_predictions) >= 2:
388
+ top1_conf = top_predictions[0][1]
389
+ top2_conf = top_predictions[1][1]
390
+ margin = top1_conf - top2_conf
391
+
392
+ # Show alternatives if:
393
+ # 1. Low confidence (< 70%), OR
394
+ # 2. Top 2 predictions are very close (margin < 0.1)
395
+ if confidence < 0.70 or margin < 0.1:
396
+ show_alternatives = True
397
+
398
  # Format output
399
  output = f"**Primary Prediction:** {prediction}\n\n"
400
 
401
+ # Add status and alternatives based on confidence and margin
402
+ if show_alternatives:
403
+ if confidence < 0.6:
404
+ status = "⚠️ Low Confidence"
405
+ elif confidence < 0.70:
406
+ status = "⚡ Medium Confidence"
407
+ else:
408
+ status = "⚠️ Uncertain" # High confidence but close alternatives
409
 
410
+ note = "Visual ambiguity detected (e.g., 0/o, i/1/l confusion)"
 
411
 
412
  output += f"{status} — {confidence_pct:.1f}%\n"
413
  output += f"{note}\n\n"
 
415
 
416
  for i, (text, conf) in enumerate(top_predictions, 1):
417
  conf_pct = conf * 100
418
+ marker = "→" if i == 1 else " "
419
+ output += f"{marker} {i}. `{text}` — {conf_pct:.1f}%\n"
420
 
421
  output += "\n💡 *Tip: Check which makes sense in context*"
422