Delete utils.py
Browse files
utils.py
DELETED
|
@@ -1,87 +0,0 @@
|
|
| 1 |
-
"""
|
| 2 |
-
VoiceNote AI - Utilities
|
| 3 |
-
WER calculation, VIPS formatting, evaluation export
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import json
|
| 7 |
-
import logging
|
| 8 |
-
from datetime import datetime
|
| 9 |
-
from config import Config
|
| 10 |
-
|
| 11 |
-
logger = logging.getLogger(__name__)
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
def calculate_wer(reference: str, hypothesis: str) -> float:
|
| 15 |
-
"""
|
| 16 |
-
Calculate Word Error Rate (WER).
|
| 17 |
-
|
| 18 |
-
WER = (Substitutions + Deletions + Insertions) / N
|
| 19 |
-
where N = total words in reference.
|
| 20 |
-
|
| 21 |
-
Args:
|
| 22 |
-
reference: Ground truth transcription
|
| 23 |
-
hypothesis: Whisper output
|
| 24 |
-
|
| 25 |
-
Returns:
|
| 26 |
-
WER as percentage (0–100). Returns 0.0 if reference is empty.
|
| 27 |
-
"""
|
| 28 |
-
if not reference or not reference.strip():
|
| 29 |
-
return 0.0
|
| 30 |
-
|
| 31 |
-
ref_words = reference.strip().lower().split()
|
| 32 |
-
hyp_words = hypothesis.strip().lower().split()
|
| 33 |
-
|
| 34 |
-
# Dynamic programming edit distance
|
| 35 |
-
d = [[0] * (len(hyp_words) + 1) for _ in range(len(ref_words) + 1)]
|
| 36 |
-
for i in range(len(ref_words) + 1):
|
| 37 |
-
d[i][0] = i
|
| 38 |
-
for j in range(len(hyp_words) + 1):
|
| 39 |
-
d[0][j] = j
|
| 40 |
-
|
| 41 |
-
for i in range(1, len(ref_words) + 1):
|
| 42 |
-
for j in range(1, len(hyp_words) + 1):
|
| 43 |
-
if ref_words[i - 1] == hyp_words[j - 1]:
|
| 44 |
-
d[i][j] = d[i - 1][j - 1]
|
| 45 |
-
else:
|
| 46 |
-
d[i][j] = 1 + min(d[i - 1][j - 1], d[i][j - 1], d[i - 1][j])
|
| 47 |
-
|
| 48 |
-
wer = (d[len(ref_words)][len(hyp_words)] / len(ref_words)) * 100
|
| 49 |
-
return round(wer, 2)
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
def format_vips_output(vips: dict) -> str:
|
| 53 |
-
"""Format a VIPS dict as readable Swedish clinical text."""
|
| 54 |
-
labels = {
|
| 55 |
-
"V": "V (Välbefinnande)",
|
| 56 |
-
"I": "I (Integritet)",
|
| 57 |
-
"P": "P (Prevention)",
|
| 58 |
-
"S": "S (Säkerhet)",
|
| 59 |
-
}
|
| 60 |
-
lines = [f"{labels.get(k, k)}: {vips.get(k, 'Ingen relevant information.')}"
|
| 61 |
-
for k in ["V", "I", "P", "S"]]
|
| 62 |
-
return "\n".join(lines)
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
def save_evaluation(entry: dict) -> str:
|
| 66 |
-
"""
|
| 67 |
-
Append an evaluation entry to the JSONL file.
|
| 68 |
-
|
| 69 |
-
Args:
|
| 70 |
-
entry: Dict with evaluation answers
|
| 71 |
-
|
| 72 |
-
Returns:
|
| 73 |
-
Status message
|
| 74 |
-
"""
|
| 75 |
-
entry["timestamp"] = datetime.utcnow().isoformat() + "Z"
|
| 76 |
-
try:
|
| 77 |
-
with open(Config.EVAL_FILE, "a", encoding="utf-8") as f:
|
| 78 |
-
f.write(json.dumps(entry, ensure_ascii=False) + "\n")
|
| 79 |
-
logger.info(f"Evaluation saved: {entry}")
|
| 80 |
-
return "✅ Utvärdering sparad!"
|
| 81 |
-
except Exception as e:
|
| 82 |
-
logger.error(f"Failed to save evaluation: {e}")
|
| 83 |
-
return f"⚠️ Kunde inte spara: {e}"
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
def format_timestamp() -> str:
|
| 87 |
-
return datetime.now().strftime("%H:%M:%S")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|