Spaces:
Running
Running
File size: 2,909 Bytes
9e50ae2 7bc29cd 9e50ae2 7bc29cd 9e50ae2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
"""Confidence calculation and visualization utilities.
Provides normalized softmax confidence and color-coded badges"""
from typing import Tuple, List
import numpy as np
import torch
import torch.nn.functional as F
def calculate_softmax_confidence(
logits: torch.Tensor,
) -> Tuple[np.ndarray, float, str, str]:
"""Calculate normalized confidence using softmax
Args:
logits: Raw model logits tensor
Returns:
Tuple of (probabilities, max_confidence, confidence_level, confidence_emoji)
"""
# ===Apply softmax to get probabilities===
probs_np = F.softmax(logits, dim=1).cpu().numpy().flatten()
# ===Get maximum probability as confidence===
max_confidence = float(np.max(probs_np))
# ===Determine confidence level and emoji===
if max_confidence >= 0.80:
confidence_level = "HIGH"
confidence_emoji = "π’"
elif max_confidence >= 0.60:
confidence_level = "MEDIUM"
confidence_emoji = "π‘"
else:
confidence_level = "LOW"
confidence_emoji = "π΄"
return probs_np, max_confidence, confidence_level, confidence_emoji
def get_confidence_badge(confidence: float) -> Tuple[str, str]:
"""Get confidence badge emoji and level description
Args:
confidence: Confidence value (0-1)
Returns:
Tuple of (emoji, level)
"""
if confidence >= 0.80:
return "π’", "HIGH"
elif confidence >= 0.60:
return "π‘", "MEDIUM"
else:
return "π΄", "LOW"
def format_confidence_display(confidence: float, level: str, emoji: str) -> str:
"""
Format confidence for display in UI
Args:
confidence: Confidence value (0-1)
level: Confidence level (HIGH/MEDIUM/LOW)
emoji: Confidence emoji
Returns:
Formatted confidence string
"""
return f"{emoji} **{level}** ({confidence:.1%})"
def calculate_legacy_confidence(logits_list: List[float]) -> Tuple[float, str, str]:
"""
Calculate confidence using legacy logit margin method for backward compatibility
Args:
logits_list: List of raw logits
Returns:
Tuple of (margin, confidence_level, confidence_emoji)
"""
if len(logits_list) < 2:
return 0.0, "LOW", "π΄"
logits_array = np.array(logits_list)
sorted_logits = np.sort(logits_array)[::-1] # Descending order
margin = sorted_logits[0] - sorted_logits[1]
# ===Define thresholds for margin-based confidence===
if margin >= 2.0:
confidence_level = "HIGH"
confidence_emoji = "π’"
elif margin >= 1.0:
confidence_level = "MEDIUM"
confidence_emoji = "π‘"
else:
confidence_level = "LOW"
confidence_emoji = "π΄"
return margin, confidence_level, confidence_emoji
|