Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from jiwer import wer, compute_measures
|
3 |
+
|
4 |
+
# Function to highlight errors
|
5 |
+
def highlight_errors(ground_truth, hypothesis):
|
6 |
+
measures = compute_measures(ground_truth, hypothesis)
|
7 |
+
gt_words = ground_truth.split()
|
8 |
+
hyp_words = hypothesis.split()
|
9 |
+
|
10 |
+
highlighted_hyp = []
|
11 |
+
for op in measures['ops']:
|
12 |
+
if op[0] == 'hit':
|
13 |
+
highlighted_hyp.append(f'<span>{op[1]}</span>')
|
14 |
+
elif op[0] == 'sub':
|
15 |
+
highlighted_hyp.append(f'<span style="color:orange;">{op[2]}</span>') # Substitution
|
16 |
+
elif op[0] == 'ins':
|
17 |
+
highlighted_hyp.append(f'<span style="color:red;">{op[2]}</span>') # Insertion
|
18 |
+
elif op[0] == 'del':
|
19 |
+
highlighted_hyp.append(f'<span style="color:blue; text-decoration:line-through;">{op[1]}</span>') # Deletion
|
20 |
+
|
21 |
+
highlighted_hyp = ' '.join(highlighted_hyp)
|
22 |
+
error_rate = wer(ground_truth, hypothesis)
|
23 |
+
|
24 |
+
# Color Legend HTML
|
25 |
+
legend_html = """
|
26 |
+
<div style="margin-top: 10px;">
|
27 |
+
<strong>Legend:</strong><br>
|
28 |
+
<span style="color:orange;">Substitution</span>: Words in orange<br>
|
29 |
+
<span style="color:red;">Insertion</span>: Words in red<br>
|
30 |
+
<span style="color:blue; text-decoration:line-through;">Deletion</span>: Words in blue with strikethrough<br>
|
31 |
+
</div>
|
32 |
+
"""
|
33 |
+
|
34 |
+
return highlighted_hyp + legend_html, error_rate, measures['substitutions'], measures['insertions'], measures['deletions']
|
35 |
+
|
36 |
+
# Gradio Interface
|
37 |
+
interface = gr.Interface(
|
38 |
+
fn=highlight_errors,
|
39 |
+
inputs=["text", "text"],
|
40 |
+
outputs=[
|
41 |
+
gr.HTML(label="Highlighted Transcript with Legend"),
|
42 |
+
gr.Number(label="Word Error Rate"),
|
43 |
+
gr.Number(label="Substitutions"),
|
44 |
+
gr.Number(label="Insertions"),
|
45 |
+
gr.Number(label="Deletions")
|
46 |
+
],
|
47 |
+
title="WER Calculator with Error Highlighting and Legend"
|
48 |
+
)
|
49 |
+
|
50 |
+
interface.launch()
|