janaab commited on
Commit
0b203ab
·
verified ·
1 Parent(s): 20116ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -14,27 +14,30 @@ def highlight_errors(ground_truth, hypothesis):
14
  gt_index = 0
15
  hyp_index = 0
16
 
17
- for op in measures['ops']:
18
- for chunk in op:
19
- if chunk.type == 'equal':
 
20
  # Add equal words without highlighting
21
- highlighted_hyp.extend(gt_words[gt_index:gt_index + (chunk.ref_end_idx - chunk.ref_start_idx)])
22
- gt_index += (chunk.ref_end_idx - chunk.ref_start_idx)
23
- hyp_index += (chunk.hyp_end_idx - chunk.hyp_start_idx)
24
- elif chunk.type == 'insert':
 
25
  # Highlight inserted words in green
26
- highlighted_hyp.append(f'<span style="color:green;">{hyp_words[hyp_index]}</span>')
27
- hyp_index += 1
28
- elif chunk.type == 'sub':
29
- # Highlight substituted words in purple
 
30
  highlighted_hyp.append(f'<span style="color:purple;">{hyp_words[hyp_index]}</span>')
31
- highlighted_hyp.append(f'<span style="color:red; text-decoration:line-through;">{gt_words[gt_index]}</span>') # Strikethrough for substitution
32
- gt_index += 1 # Move in ground truth
33
- hyp_index += 1 # Move in hypothesis
34
- elif chunk.type == 'delete':
35
  # Highlight deleted words in red with strikethrough
36
  highlighted_hyp.append(f'<span style="color:red; text-decoration:line-through;">{gt_words[gt_index]}</span>')
37
- gt_index += 1 # Move in ground truth
38
 
39
  # Handle any remaining words in hypothesis as insertions
40
  while hyp_index < len(hyp_words):
@@ -70,13 +73,13 @@ interface = gr.Interface(
70
  fn=highlight_errors,
71
  inputs=["text", "text"],
72
  outputs=[
73
- gr.Markdown(label="Highlighted Transcript with Legend"),
74
  gr.Number(label="Word Error Rate"),
75
  gr.Number(label="Substitutions"),
76
  gr.Number(label="Insertions"),
77
  gr.Number(label="Deletions")
78
  ],
79
- title="WER Calculator with Error Highlighting and Legend"
80
  )
81
 
82
  interface.launch()
 
14
  gt_index = 0
15
  hyp_index = 0
16
 
17
+ # Process each chunk of alignment (e.g., equal, insert, substitute, delete)
18
+ for chunk in measures['ops']:
19
+ for alignment in chunk:
20
+ if alignment.type == 'equal':
21
  # Add equal words without highlighting
22
+ for i in range(alignment.ref_start_idx, alignment.ref_end_idx):
23
+ highlighted_hyp.append(gt_words[i]) # Add ground truth word as is
24
+ gt_index = alignment.ref_end_idx
25
+ hyp_index = alignment.hyp_end_idx
26
+ elif alignment.type == 'insert':
27
  # Highlight inserted words in green
28
+ for i in range(alignment.hyp_start_idx, alignment.hyp_end_idx):
29
+ highlighted_hyp.append(f'<span style="color:green;">{hyp_words[i]}</span>')
30
+ hyp_index = alignment.hyp_end_idx
31
+ elif alignment.type == 'sub':
32
+ # Highlight substituted words in purple and strikethrough for ground truth in red
33
  highlighted_hyp.append(f'<span style="color:purple;">{hyp_words[hyp_index]}</span>')
34
+ highlighted_hyp.append(f'<span style="color:red; text-decoration:line-through;">{gt_words[gt_index]}</span>')
35
+ gt_index += 1
36
+ hyp_index += 1
37
+ elif alignment.type == 'delete':
38
  # Highlight deleted words in red with strikethrough
39
  highlighted_hyp.append(f'<span style="color:red; text-decoration:line-through;">{gt_words[gt_index]}</span>')
40
+ gt_index += 1
41
 
42
  # Handle any remaining words in hypothesis as insertions
43
  while hyp_index < len(hyp_words):
 
73
  fn=highlight_errors,
74
  inputs=["text", "text"],
75
  outputs=[
76
+ gr.Markdown(label="Highlighted Transcript"),
77
  gr.Number(label="Word Error Rate"),
78
  gr.Number(label="Substitutions"),
79
  gr.Number(label="Insertions"),
80
  gr.Number(label="Deletions")
81
  ],
82
+ title="WER Analysis"
83
  )
84
 
85
  interface.launch()