akki2825 commited on
Commit
7115c2e
·
verified ·
1 Parent(s): b066284

fix misalignment diplay issue

Browse files
Files changed (1) hide show
  1. app.py +92 -56
app.py CHANGED
@@ -33,36 +33,39 @@ def calculate_cer(reference, hypothesis):
33
  def calculate_sentence_metrics(reference, hypothesis):
34
  """
35
  Calculate WER and CER for each sentence and overall statistics.
 
36
  """
37
  try:
38
  reference_sentences = split_into_sentences(reference)
39
  hypothesis_sentences = split_into_sentences(hypothesis)
40
 
41
- if len(reference_sentences) != len(hypothesis_sentences):
42
- raise ValueError("Reference and hypothesis must contain the same number of sentences")
43
-
44
  sentence_wers = []
45
  sentence_cers = []
46
- for ref, hyp in zip(reference_sentences, hypothesis_sentences):
 
 
 
 
 
47
  wer = jiwer.wer(ref, hyp)
48
  cer = jiwer.cer(ref, hyp)
49
  sentence_wers.append(wer)
50
  sentence_cers.append(cer)
51
 
52
- if not sentence_wers or not sentence_cers:
53
- return {
54
- "sentence_wers": [],
55
- "sentence_cers": [],
56
- "average_wer": 0.0,
57
- "average_cer": 0.0,
58
- "std_dev_wer": 0.0,
59
- "std_dev_cer": 0.0
60
- }
61
-
62
- average_wer = np.mean(sentence_wers)
63
- average_cer = np.mean(sentence_cers)
64
- std_dev_wer = np.std(sentence_wers)
65
- std_dev_cer = np.std(sentence_cers)
66
 
67
  return {
68
  "sentence_wers": sentence_wers,
@@ -74,19 +77,26 @@ def calculate_sentence_metrics(reference, hypothesis):
74
  }
75
  except Exception as e:
76
  raise e
 
77
 
78
  def identify_misaligned_sentences(reference_text, hypothesis_text):
79
  """
80
  Identify sentences that don't match between reference and hypothesis.
 
81
  Returns a dictionary with misaligned sentence pairs, their indices, and misalignment details.
82
  """
83
  reference_sentences = split_into_sentences(reference_text)
84
  hypothesis_sentences = split_into_sentences(hypothesis_text)
85
 
86
  misaligned = []
87
- for i, (ref, hyp) in enumerate(zip(reference_sentences, hypothesis_sentences)):
 
 
 
 
 
 
88
  if ref != hyp:
89
- print(f"Debug: Found misalignment in sentence {i+1}")
90
  # Find the first position where the sentences diverge
91
  min_len = min(len(ref), len(hyp))
92
  misalignment_start = 0
@@ -106,7 +116,29 @@ def identify_misaligned_sentences(reference_text, hypothesis_text):
106
  "context_ref": context_ref,
107
  "context_hyp": context_hyp
108
  })
109
- print(f"Debug: Total misaligned sentences found: {len(misaligned)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  return misaligned
111
 
112
  def format_sentence_metrics(sentence_wers, sentence_cers, average_wer, average_cer, std_dev_wer, std_dev_cer, misaligned_sentences):
@@ -130,8 +162,8 @@ def format_sentence_metrics(sentence_wers, sentence_cers, average_wer, average_c
130
  md += "\n### Misaligned Sentences\n\n"
131
  for misaligned in misaligned_sentences:
132
  md += f"#### Sentence {misaligned['index']}\n"
133
- md += f"* Reference: {misaligned['reference']}\n"
134
- md += f"* Hypothesis: {misaligned['hypothesis']}\n"
135
  md += f"* Misalignment starts at position: {misaligned['misalignment_start']}\n\n"
136
  else:
137
  md += "\n### Misaligned Sentences\n\n"
@@ -139,7 +171,6 @@ def format_sentence_metrics(sentence_wers, sentence_cers, average_wer, average_c
139
 
140
  return md
141
 
142
-
143
  @spaces.GPU()
144
  def process_files(reference_file, hypothesis_file):
145
  try:
@@ -168,6 +199,41 @@ def process_files(reference_file, hypothesis_file):
168
  except Exception as e:
169
  return {"error": str(e)}
170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  def main():
172
  with gr.Blocks() as demo:
173
  gr.Markdown("# ASR Metrics")
@@ -193,10 +259,10 @@ def main():
193
 
194
  if ref_file:
195
  with open(ref_file.name, 'r') as f:
196
- ref_text = f.read()[:200] # Show first 200 characters
197
  if hyp_file:
198
  with open(hyp_file.name, 'r') as f:
199
- hyp_text = f.read()[:200] # Show first 200 characters
200
 
201
  return ref_text, hyp_text
202
 
@@ -211,36 +277,6 @@ def main():
211
  outputs=[reference_preview, hypothesis_preview]
212
  )
213
 
214
- def process_and_display(ref_file, hyp_file):
215
- result = process_files(ref_file, hyp_file)
216
- if "error" in result:
217
- error_msg = result["error"]
218
- return {"error": error_msg}, "", "", {"error": error_msg}
219
-
220
- metrics = {
221
- "Overall WER": result["Overall WER"],
222
- "Overall CER": result["Overall CER"]
223
- }
224
-
225
- metrics_md = format_sentence_metrics(
226
- result["Sentence WERs"],
227
- result["Sentence CERs"],
228
- result["Average WER"],
229
- result["Average CER"],
230
- result["Standard Deviation WER"],
231
- result["Standard Deviation CER"],
232
- result["Misaligned Sentences"]
233
- )
234
-
235
- misaligned_md = "### Misaligned Sentences\n\n"
236
- for misaligned in result["Misaligned Sentences"]:
237
- misaligned_md += f"#### Sentence {misaligned['index']}\n"
238
- misaligned_md += f"* Reference: {misaligned['context_ref']}\n"
239
- misaligned_md += f"* Hypothesis: {misaligned['context_hyp']}\n"
240
- misaligned_md += f"* Misalignment starts at position: {misaligned['misalignment_start']}\n\n"
241
-
242
- return metrics, metrics_md, misaligned_md
243
-
244
  compute_button.click(
245
  fn=process_and_display,
246
  inputs=[reference_file, hypothesis_file],
 
33
  def calculate_sentence_metrics(reference, hypothesis):
34
  """
35
  Calculate WER and CER for each sentence and overall statistics.
36
+ Handles cases where the number of sentences differ.
37
  """
38
  try:
39
  reference_sentences = split_into_sentences(reference)
40
  hypothesis_sentences = split_into_sentences(hypothesis)
41
 
 
 
 
42
  sentence_wers = []
43
  sentence_cers = []
44
+ min_length = min(len(reference_sentences), len(hypothesis_sentences))
45
+
46
+ for i in range(min_length):
47
+ ref = reference_sentences[i]
48
+ hyp = hypothesis_sentences[i]
49
+
50
  wer = jiwer.wer(ref, hyp)
51
  cer = jiwer.cer(ref, hyp)
52
  sentence_wers.append(wer)
53
  sentence_cers.append(cer)
54
 
55
+ # Calculate overall statistics
56
+ if sentence_wers:
57
+ average_wer = np.mean(sentence_wers)
58
+ std_dev_wer = np.std(sentence_wers)
59
+ else:
60
+ average_wer = 0.0
61
+ std_dev_wer = 0.0
62
+
63
+ if sentence_cers:
64
+ average_cer = np.mean(sentence_cers)
65
+ std_dev_cer = np.std(sentence_cers)
66
+ else:
67
+ average_cer = 0.0
68
+ std_dev_cer = 0.0
69
 
70
  return {
71
  "sentence_wers": sentence_wers,
 
77
  }
78
  except Exception as e:
79
  raise e
80
+
81
 
82
  def identify_misaligned_sentences(reference_text, hypothesis_text):
83
  """
84
  Identify sentences that don't match between reference and hypothesis.
85
+ Handles cases where the number of sentences differ.
86
  Returns a dictionary with misaligned sentence pairs, their indices, and misalignment details.
87
  """
88
  reference_sentences = split_into_sentences(reference_text)
89
  hypothesis_sentences = split_into_sentences(hypothesis_text)
90
 
91
  misaligned = []
92
+ min_length = min(len(reference_sentences), len(hypothesis_sentences))
93
+
94
+ # Compare sentences up to the minimum length
95
+ for i in range(min_length):
96
+ ref = reference_sentences[i]
97
+ hyp = hypothesis_sentences[i]
98
+
99
  if ref != hyp:
 
100
  # Find the first position where the sentences diverge
101
  min_len = min(len(ref), len(hyp))
102
  misalignment_start = 0
 
116
  "context_ref": context_ref,
117
  "context_hyp": context_hyp
118
  })
119
+
120
+ # Note any extra sentences as misaligned
121
+ if len(reference_sentences) > len(hypothesis_sentences):
122
+ for i in range(min_length, len(reference_sentences)):
123
+ misaligned.append({
124
+ "index": i+1,
125
+ "reference": reference_sentences[i],
126
+ "hypothesis": "No corresponding sentence",
127
+ "misalignment_start": 0,
128
+ "context_ref": reference_sentences[i],
129
+ "context_hyp": "No corresponding sentence"
130
+ })
131
+ elif len(hypothesis_sentences) > len(reference_sentences):
132
+ for i in range(min_length, len(hypothesis_sentences)):
133
+ misaligned.append({
134
+ "index": i+1,
135
+ "reference": "No corresponding sentence",
136
+ "hypothesis": hypothesis_sentences[i],
137
+ "misalignment_start": 0,
138
+ "context_ref": "No corresponding sentence",
139
+ "context_hyp": hypothesis_sentences[i]
140
+ })
141
+
142
  return misaligned
143
 
144
  def format_sentence_metrics(sentence_wers, sentence_cers, average_wer, average_cer, std_dev_wer, std_dev_cer, misaligned_sentences):
 
162
  md += "\n### Misaligned Sentences\n\n"
163
  for misaligned in misaligned_sentences:
164
  md += f"#### Sentence {misaligned['index']}\n"
165
+ md += f"* Reference: {misaligned['context_ref']}\n"
166
+ md += f"* Hypothesis: {misaligned['context_hyp']}\n"
167
  md += f"* Misalignment starts at position: {misaligned['misalignment_start']}\n\n"
168
  else:
169
  md += "\n### Misaligned Sentences\n\n"
 
171
 
172
  return md
173
 
 
174
  @spaces.GPU()
175
  def process_files(reference_file, hypothesis_file):
176
  try:
 
199
  except Exception as e:
200
  return {"error": str(e)}
201
 
202
+ def process_and_display(ref_file, hyp_file):
203
+ result = process_files(ref_file, hyp_file)
204
+
205
+ if "error" in result:
206
+ error_msg = result["error"]
207
+ return {"error": error_msg}, "", ""
208
+
209
+ metrics = {
210
+ "Overall WER": result["Overall WER"],
211
+ "Overall CER": result["Overall CER"]
212
+ }
213
+
214
+ metrics_md = format_sentence_metrics(
215
+ result["Sentence WERs"],
216
+ result["Sentence CERs"],
217
+ result["Average WER"],
218
+ result["Average CER"],
219
+ result["Standard Deviation WER"],
220
+ result["Standard Deviation CER"],
221
+ result["Misaligned Sentences"]
222
+ )
223
+
224
+ misaligned_md = "### Misaligned Sentences\n\n"
225
+ if result["Misaligned Sentences"]:
226
+ for misaligned in result["Misaligned Sentences"]:
227
+ misaligned_md += f"#### Sentence {misaligned['index']}\n"
228
+ misaligned_md += f"* Reference: {misaligned['context_ref']}\n"
229
+ misaligned_md += f"* Hypothesis: {misaligned['context_hyp']}\n"
230
+ misaligned_md += f"* Misalignment starts at position: {misaligned['misalignment_start']}\n\n"
231
+ else:
232
+ misaligned_md += "* No misaligned sentences found."
233
+
234
+ return metrics, metrics_md, misaligned_md
235
+
236
+
237
  def main():
238
  with gr.Blocks() as demo:
239
  gr.Markdown("# ASR Metrics")
 
259
 
260
  if ref_file:
261
  with open(ref_file.name, 'r') as f:
262
+ ref_text = f.read()[:200]
263
  if hyp_file:
264
  with open(hyp_file.name, 'r') as f:
265
+ hyp_text = f.read()[:200]
266
 
267
  return ref_text, hyp_text
268
 
 
277
  outputs=[reference_preview, hypothesis_preview]
278
  )
279
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
280
  compute_button.click(
281
  fn=process_and_display,
282
  inputs=[reference_file, hypothesis_file],