SsebaA commited on
Commit
33cafa8
·
verified ·
1 Parent(s): 806edf7

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +172 -230
utils.py CHANGED
@@ -1,264 +1,206 @@
1
  """
2
  VoiceNote AI - Utilities
3
- =========================
4
- WER calculator and HTML formatting functions.
5
  """
6
 
7
- import os
8
- import csv
9
  import logging
10
- from typing import Optional, Dict
11
  from datetime import datetime
12
- from jiwer import wer as compute_wer
13
  from config import Config
14
 
15
  logger = logging.getLogger(__name__)
16
 
17
 
18
- # ══════════════════════════════════════════════════════════
19
- # WER CALCULATOR
20
- # ══════════════════════════════════════════════════════════
21
-
22
- class WERCalculator:
23
- """Word Error Rate calculator with validation"""
24
 
25
- @staticmethod
26
- def calculate(reference: str, hypothesis: str) -> Optional[float]:
27
- """
28
- Calculate WER score.
29
- Returns percentage (0-100) or None if invalid input.
30
- """
31
- if not reference or not reference.strip():
32
- return None
33
-
34
- if not hypothesis or not hypothesis.strip():
35
- logger.warning("Empty hypothesis for WER calculation")
36
- return 100.0 # All words are errors
37
-
38
- try:
39
- score = compute_wer(
40
- reference.lower().strip(),
41
- hypothesis.lower().strip()
42
- )
43
- percentage = round(score * 100, 1)
44
- logger.info(f"WER calculated: {percentage}%")
45
- return percentage
46
- except Exception as e:
47
- logger.error(f"WER calculation error: {e}")
48
- return None
49
 
50
- @staticmethod
51
- def get_quality_label(wer: Optional[float]) -> str:
52
- """Get quality label for WER score"""
53
- if wer is None:
54
- return "N/A"
55
 
56
- if wer < Config.WER_EXCELLENT:
57
- return "Utmärkt"
58
- elif wer < Config.WER_GOOD:
59
- return "Bra"
60
- elif wer < Config.WER_ACCEPTABLE:
61
- return "Godkänd"
62
- else:
63
- return "Behöver förbättras"
64
-
65
-
66
- # ══════════════════════════════════════════════════════════
67
- # HTML FORMATTERS
68
- # ══════════════════════════════════════════════════════════
69
-
70
- def formatera_vips_html(vips: dict) -> str:
71
- """Format VIPS dictionary as colored HTML"""
72
- colors = {
73
- 'V': ('#10B981', '#ECFDF5'), # Green
74
- 'I': ('#F59E0B', '#FFFBEB'), # Amber
75
- 'P': ('#3B82F6', '#EFF6FF'), # Blue
76
- 'S': ('#EF4444', '#FEF2F2'), # Red
77
- }
78
 
79
- categories = {
80
- 'V': 'Välbefinnande',
81
- 'I': 'Integritet',
82
- 'P': 'Prevention',
83
- 'S': 'Säkerhet',
84
- }
85
 
86
- html_parts = []
87
- for key in ['V', 'I', 'P', 'S']:
88
- fg, bg = colors[key]
89
- cat = categories[key]
90
- text = vips.get(key, "Ingen relevant information.")
91
-
92
- html_parts.append(f"""
93
- <div style='background:{bg};border-left:4px solid {fg};padding:12px 16px;margin-bottom:10px;border-radius:8px;'>
94
- <div style='color:{fg};font-weight:700;font-size:13px;margin-bottom:4px;'>{key} — {cat}</div>
95
- <div style='color:#1F2937;font-size:14px;line-height:1.6;'>{text}</div>
96
- </div>
97
- """)
98
 
99
- return "".join(html_parts)
100
-
101
-
102
- def wer_badge(wer_poang: Optional[float]) -> str:
103
- """Create WER badge HTML"""
104
- if wer_poang is None:
105
- return ""
106
 
107
- if wer_poang < Config.WER_EXCELLENT:
108
- color, bg = "#059669", "#ECFDF5"
109
- label = "Utmärkt ✅"
110
- elif wer_poang < Config.WER_GOOD:
111
- color, bg = "#0369A1", "#EFF6FF"
112
- label = "Bra ✅"
113
- elif wer_poang < Config.WER_ACCEPTABLE:
114
- color, bg = "#D97706", "#FFFBEB"
115
- label = "Godkänd ⚠️"
116
- else:
117
- color, bg = "#DC2626", "#FEF2F2"
118
- label = "Behöver förbättras ❌"
119
 
120
- return f"""
121
- <div style='background:{bg};border:2px solid {color}55;border-radius:14px;
122
- padding:20px;margin-bottom:12px;text-align:center;'>
123
- <div style='font-size:42px;font-weight:900;color:{color};'>{wer_poang:.1f}%</div>
124
- <div style='color:{color};font-size:16px;font-weight:700;margin-top:4px;'>WER: {label}</div>
125
- </div>"""
126
 
127
 
128
- def formatera_historik_html(historik: list) -> str:
129
- """Format history list as HTML"""
130
- if not historik:
131
- return "<p style='color:#64748B;text-align:center;padding:40px;'>Ingen historik ännu.</p>"
132
 
133
- items = []
134
- for post in reversed(historik[-5:]): # Last 5 entries
135
- tid = post.get('tid', 'N/A')
136
- wer = post.get('wer_poang')
137
- wer_text = f"{wer:.1f}%" if wer is not None else "N/A"
138
 
139
- technique = post.get('prompt_technique', 'unknown')
140
- technique_emoji = "📚" if technique == "few_shot" else "🧠"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
 
142
- items.append(f"""
143
- <div style='background:#F8FAFC;border:1.5px solid #E2E8F0;border-radius:10px;
144
- padding:12px;margin-bottom:8px;'>
145
- <div style='display:flex;justify-content:space-between;align-items:center;'>
146
- <span style='color:#475569;font-weight:600;'>{technique_emoji} {tid}</span>
147
- <span style='color:#64748B;font-size:13px;'>WER: {wer_text}</span>
148
- </div>
149
- </div>
150
- """)
151
 
152
- return "".join(items)
 
 
 
 
153
 
 
154
 
155
- # ══════════════════════════════════════════════════════════
156
- # FILE EXPORT FUNCTIONS
157
- # ══════════════════════════════════════════════════════════
158
 
159
- def spara_nedladdning(innehall: str) -> Optional[str]:
160
- """Save text file for download"""
161
- # Check if input is valid
162
- if not innehall or not isinstance(innehall, str):
163
- logger.warning("Invalid input for file download")
164
- return None
165
-
166
- try:
167
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
168
- path = os.path.join(Config.TEMP_DIR, f"vips_anteckning_{timestamp}.txt")
169
- with open(path, "w", encoding="utf-8") as f:
170
- f.write(innehall)
171
- logger.info(f"Saved text file: {path}")
172
- return path
173
- except Exception as e:
174
- logger.error(f"Error saving text file: {e}")
175
- return None
176
-
177
- def exportera_csv(alla_resultat: list) -> Optional[str]:
178
- """Export all results as CSV"""
179
- if not alla_resultat:
180
- return None
181
- try:
182
- path = os.path.join(Config.TEMP_DIR, "voicenote_resultat.csv")
183
- with open(path, "w", newline="", encoding="utf-8") as f:
184
- fieldnames = ["tid", "prompt_technique", "asr_tid", "llm_tid", "total_tid", "wer_poang", "transkription", "referens", "vips"]
185
- writer = csv.DictWriter(f, fieldnames=fieldnames, extrasaction="ignore")
186
- writer.writeheader()
187
- writer.writerows(alla_resultat)
188
- logger.info(f"Exported CSV: {path}")
189
- return path
190
- except Exception as e:
191
- logger.error(f"CSV export error: {e}")
192
- return None
193
 
 
 
 
 
194
 
195
- # ══════════════════════════════════════════════════════════
196
- # USABILITY CALCULATORS
197
- # ══════════════════════════════════════════════════════════
198
 
199
- def berakna_sus(*svar) -> str:
200
- """Calculate SUS score and return HTML"""
201
- try:
202
- pl = []
203
- for i, val in enumerate(svar):
204
- try:
205
- v = int(val)
206
- except (TypeError, ValueError):
207
- return "<div style='color:#DC2626;padding:14px;background:#FEF2F2;border-radius:10px;font-weight:600;'>⚠️ Fyll i alla 10 frågor.</div>"
208
- pl.append(v - 1 if i % 2 == 0 else 5 - v)
209
-
210
- total = sum(pl) * 2.5
211
-
212
- # Determine color and label
213
- if total >= Config.SUS_GOOD:
214
- f, b = "#059669", "#ECFDF5"
215
- elif total >= Config.SUS_PASS:
216
- f, b = "#D97706", "#FFFBEB"
217
- else:
218
- f, b = "#DC2626", "#FEF2F2"
219
 
220
- e = ("Utmärkt ✅" if total >= Config.SUS_EXCELLENT else
221
- "Bra ✅" if total >= Config.SUS_GOOD else
222
- "Godkänd ⚠️" if total >= Config.SUS_PASS else
223
- "Underkänd ")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
 
225
- return f"""<div style='background:{b};border:2px solid {f}55;border-radius:14px;padding:28px;text-align:center;'>
226
- <div style='font-size:60px;font-weight:900;color:{f};'>{total:.1f}</div>
227
- <div style='color:{f};font-size:18px;font-weight:700;margin-top:6px;'>{e}</div>
228
- </div>"""
229
- except Exception as ex:
230
- logger.error(f"SUS calculation error: {ex}")
231
- return "<div style='color:#DC2626;'>Fel vid beräkning</div>"
232
-
233
-
234
- def berakna_nasa(*svar) -> str:
235
- """Calculate NASA-TLX score and return HTML"""
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  try:
237
- values = []
238
- for val in svar:
239
- try:
240
- v = int(val)
241
- values.append(v)
242
- except (TypeError, ValueError):
243
- return "<div style='color:#DC2626;padding:14px;background:#FEF2F2;border-radius:10px;font-weight:600;'>⚠️ Fyll i alla 6 dimensioner.</div>"
244
-
245
- total = sum(values)
246
-
247
- if total <= Config.NASA_LOW:
248
- f, b = "#059669", "#ECFDF5"
249
- e = "Låg belastning ✅"
250
- elif total <= Config.NASA_MEDIUM:
251
- f, b = "#D97706", "#FFFBEB"
252
- e = "Medel belastning ⚠️"
253
- else:
254
- f, b = "#DC2626", "#FEF2F2"
255
- e = "Hög belastning ❌"
256
-
257
- return f"""<div style='background:{b};border:2px solid {f}55;border-radius:14px;padding:28px;text-align:center;'>
258
- <div style='font-size:60px;font-weight:900;color:{f};'>{total}</div>
259
- <div style='color:{f};font-size:14px;margin-top:4px;'>/ 120 poäng</div>
260
- <div style='color:{f};font-size:18px;font-weight:700;margin-top:10px;'>{e}</div>
261
- </div>"""
262
- except Exception as ex:
263
- logger.error(f"NASA-TLX calculation error: {ex}")
264
- return "<div style='color:#DC2626;'>Fel vid beräkning</div>"
 
1
  """
2
  VoiceNote AI - Utilities
3
+ Helper functions for WER calculation, formatting, and export
 
4
  """
5
 
 
 
6
  import logging
 
7
  from datetime import datetime
 
8
  from config import Config
9
 
10
  logger = logging.getLogger(__name__)
11
 
12
 
13
+ def calculate_wer(reference: str, hypothesis: str) -> float:
14
+ """
15
+ Calculate Word Error Rate (WER)
 
 
 
16
 
17
+ WER = (S + D + I) / N
18
+ where:
19
+ S = substitutions
20
+ D = deletions
21
+ I = insertions
22
+ N = total words in reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
+ Args:
25
+ reference: Ground truth text
26
+ hypothesis: Predicted text
 
 
27
 
28
+ Returns:
29
+ WER as percentage (0-100)
30
+ """
31
+ if not reference or not reference.strip():
32
+ return 0.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
+ ref_words = reference.strip().split()
35
+ hyp_words = hypothesis.strip().split()
 
 
 
 
36
 
37
+ # Initialize distance matrix
38
+ d = [[0] * (len(hyp_words) + 1) for _ in range(len(ref_words) + 1)]
 
 
 
 
 
 
 
 
 
 
39
 
40
+ # Initialize first row and column
41
+ for i in range(len(ref_words) + 1):
42
+ d[i][0] = i
43
+ for j in range(len(hyp_words) + 1):
44
+ d[0][j] = j
 
 
45
 
46
+ # Calculate edit distance
47
+ for i in range(1, len(ref_words) + 1):
48
+ for j in range(1, len(hyp_words) + 1):
49
+ if ref_words[i-1] == hyp_words[j-1]:
50
+ d[i][j] = d[i-1][j-1]
51
+ else:
52
+ substitution = d[i-1][j-1] + 1
53
+ insertion = d[i][j-1] + 1
54
+ deletion = d[i-1][j] + 1
55
+ d[i][j] = min(substitution, insertion, deletion)
 
 
56
 
57
+ # Calculate WER
58
+ wer = (d[len(ref_words)][len(hyp_words)] / len(ref_words)) * 100
59
+ return round(wer, 2)
 
 
 
60
 
61
 
62
+ def format_vips_output(vips: dict) -> str:
63
+ """
64
+ Format VIPS dictionary as readable text
 
65
 
66
+ Args:
67
+ vips: Dictionary with V, I, P, S categories
 
 
 
68
 
69
+ Returns:
70
+ Formatted VIPS text
71
+ """
72
+ output = []
73
+ for category in ["V", "I", "P", "S"]:
74
+ if category in vips:
75
+ output.append(f"{category}: {vips[category]}")
76
+ return "\n".join(output)
77
+
78
+
79
+ def format_timestamp() -> str:
80
+ """Get current timestamp in HH:MM:SS format"""
81
+ return datetime.now().strftime("%H:%M:%S")
82
+
83
+
84
+ def format_report(
85
+ transcription: str,
86
+ vips: dict,
87
+ reference: str,
88
+ wer: float,
89
+ asr_time: float,
90
+ llm_time: float,
91
+ total_time: float
92
+ ) -> str:
93
+ """
94
+ Format complete report for display
95
+
96
+ Args:
97
+ transcription: Transcribed text
98
+ vips: VIPS classifications
99
+ reference: Reference text for WER
100
+ wer: Word Error Rate
101
+ asr_time: ASR processing time
102
+ llm_time: LLM processing time
103
+ total_time: Total processing time
104
 
105
+ Returns:
106
+ Formatted report text
107
+ """
108
+ technique_name = "Few-shot Prompting" if Config.PROMPT_TECHNIQUE == "few_shot" else "Chain-of-Thought Prompting"
 
 
 
 
 
109
 
110
+ report = f"""VoiceNote AI — VIPS Journalanteckning
111
+ Tid: {format_timestamp()}
112
+ ASR Model: OpenAI Whisper ({Config.ASR_MODEL_NAME})
113
+ Prompt: {technique_name} ({Config.PROMPT_TECHNIQUE})
114
+ GDPR: Dubbel anonymisering · Mistral AI EU-servrar
115
 
116
+ ASR: {asr_time:.2f}s | LLM: {llm_time:.2f}s | Total: {total_time:.2f}s | WER: {wer if wer else 'N/A'}%
117
 
118
+ Transkription:
119
+ {transcription}
 
120
 
121
+ VIPS-Dokumentation:
122
+ {format_vips_output(vips)}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123
 
124
+ Referenstext (för WER):
125
+ {reference if reference else 'Ingen referenstext angiven'}
126
+ """
127
+ return report
128
 
 
 
 
129
 
130
+ def export_to_csv(data: dict) -> str:
131
+ """
132
+ Export result to CSV format
133
+
134
+ Args:
135
+ data: Dictionary with result data
 
 
 
 
 
 
 
 
 
 
 
 
 
 
136
 
137
+ Returns:
138
+ CSV row as string
139
+ """
140
+ csv_header = "Tid,Prompt_Technique,ASR_Tid,LLM_Tid,Total_Tid,WER,Transkription,V,I,P,S,Referens\n"
141
+
142
+ csv_row = f"{data.get('tid', '')},{data.get('technique', '')}," \
143
+ f"{data.get('asr_tid', '')},{data.get('llm_tid', '')}," \
144
+ f"{data.get('total_tid', '')},{data.get('wer', '')}," \
145
+ f"\"{data.get('transkription', '')}\",\"{data.get('V', '')}\"," \
146
+ f"\"{data.get('I', '')}\",\"{data.get('P', '')}\"," \
147
+ f"\"{data.get('S', '')}\",\"{data.get('referens', '')}\"\n"
148
+
149
+ return csv_header + csv_row
150
+
151
+
152
+ def spara_nedladdning(
153
+ transcription: str,
154
+ vips: dict,
155
+ reference: str,
156
+ wer: float,
157
+ asr_time: float,
158
+ llm_time: float,
159
+ total_time: float
160
+ ) -> str:
161
+ """
162
+ Save results to downloadable file
163
+
164
+ CRITICAL FIX: Return a valid filepath instead of multiline string
165
+
166
+ Args:
167
+ transcription: Transcribed text
168
+ vips: VIPS classifications
169
+ reference: Reference text
170
+ wer: Word Error Rate
171
+ asr_time: ASR time
172
+ llm_time: LLM time
173
+ total_time: Total time
174
 
175
+ Returns:
176
+ Path to saved file
177
+ """
178
+ # Validate inputs
179
+ if not transcription or not isinstance(transcription, str):
180
+ logger.warning("Invalid input for file download")
181
+ return None
182
+
183
+ # Generate filename with timestamp
184
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
185
+ filename = f"/tmp/voicenote_ai_{timestamp}.txt"
186
+
187
+ # Generate report content
188
+ report = format_report(
189
+ transcription=transcription,
190
+ vips=vips,
191
+ reference=reference,
192
+ wer=wer,
193
+ asr_time=asr_time,
194
+ llm_time=llm_time,
195
+ total_time=total_time
196
+ )
197
+
198
+ # Write to file
199
  try:
200
+ with open(filename, 'w', encoding='utf-8') as f:
201
+ f.write(report)
202
+ logger.info(f"Report saved to {filename}")
203
+ return filename
204
+ except Exception as e:
205
+ logger.error(f"Failed to save report: {e}")
206
+ return None