Spaces:
Sleeping
Sleeping
add app file
Browse files
app.py
ADDED
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
def calculate_wer(reference, hypothesis):
|
5 |
+
reference_words = reference.split()
|
6 |
+
hypothesis_words = hypothesis.split()
|
7 |
+
|
8 |
+
m = len(reference_words)
|
9 |
+
n = len(hypothesis_words)
|
10 |
+
|
11 |
+
# Initialize DP table
|
12 |
+
dp = np.zeros((m+1, n+1), dtype=np.int32)
|
13 |
+
|
14 |
+
# Base cases
|
15 |
+
for i in range(m+1):
|
16 |
+
dp[i][0] = i
|
17 |
+
for j in range(n+1):
|
18 |
+
dp[0][j] = j
|
19 |
+
|
20 |
+
# Fill DP table
|
21 |
+
for i in range(1, m+1):
|
22 |
+
for j in range(1, n+1):
|
23 |
+
cost = 0 if reference_words[i-1] == hypothesis_words[j-1] else 1
|
24 |
+
dp[i][j] = min(dp[i-1][j] + 1, # Deletion
|
25 |
+
dp[i][j-1] + 1, # Insertion
|
26 |
+
dp[i-1][j-1] + cost) # Substitution or no cost
|
27 |
+
|
28 |
+
wer = dp[m][n] / m
|
29 |
+
return wer
|
30 |
+
|
31 |
+
def calculate_cer(reference, hypothesis):
|
32 |
+
reference = reference.replace(" ", "")
|
33 |
+
hypothesis = hypothesis.replace(" ", "")
|
34 |
+
|
35 |
+
m = len(reference)
|
36 |
+
n = len(hypothesis)
|
37 |
+
|
38 |
+
# Initialize DP table
|
39 |
+
dp = np.zeros((m+1, n+1), dtype=np.int32)
|
40 |
+
|
41 |
+
# Base cases
|
42 |
+
for i in range(m+1):
|
43 |
+
dp[i][0] = i
|
44 |
+
for j in range(n+1):
|
45 |
+
dp[0][j] = j
|
46 |
+
|
47 |
+
# Fill DP table
|
48 |
+
for i in range(1, m+1):
|
49 |
+
for j in range(1, n+1):
|
50 |
+
cost = 0 if reference[i-1] == hypothesis[j-1] else 1
|
51 |
+
dp[i][j] = min(dp[i-1][j] + 1, # Deletion
|
52 |
+
dp[i][j-1] + 1, # Insertion
|
53 |
+
dp[i-1][j-1] + cost) # Substitution or no cost
|
54 |
+
|
55 |
+
cer = dp[m][n] / m
|
56 |
+
return cer
|
57 |
+
|
58 |
+
def calculate_mer(reference, hypothesis):
|
59 |
+
# Custom mission error rate calculation
|
60 |
+
# Replace this with your specific logic
|
61 |
+
return 0.0
|
62 |
+
|
63 |
+
def process_files(reference_file, hypothesis_file):
|
64 |
+
try:
|
65 |
+
with open(reference_file.name, 'r') as f:
|
66 |
+
reference_text = f.read()
|
67 |
+
|
68 |
+
with open(hypothesis_file.name, 'r') as f:
|
69 |
+
hypothesis_text = f.read()
|
70 |
+
|
71 |
+
wer_value = calculate_wer(reference_text, hypothesis_text)
|
72 |
+
cer_value = calculate_cer(reference_text, hypothesis_text)
|
73 |
+
mer_value = calculate_mer(reference_text, hypothesis_text)
|
74 |
+
|
75 |
+
return {
|
76 |
+
"WER": wer_value,
|
77 |
+
"CER": cer_value,
|
78 |
+
"MER": mer_value
|
79 |
+
}
|
80 |
+
except Exception as e:
|
81 |
+
return {"error": str(e)}
|
82 |
+
|
83 |
+
def main():
|
84 |
+
with gr.Blocks() as demo:
|
85 |
+
gr.Markdown("# ASR Metrics Calculator")
|
86 |
+
|
87 |
+
with gr.Row():
|
88 |
+
reference_file = gr.File(label="Upload Reference File")
|
89 |
+
hypothesis_file = gr.File(label="Upload Hypothesis File")
|
90 |
+
|
91 |
+
with gr.Row():
|
92 |
+
reference_preview = gr.Textbox(label="Reference Preview", lines=3)
|
93 |
+
hypothesis_preview = gr.Textbox(label="Hypothesis Preview", lines=3)
|
94 |
+
|
95 |
+
with gr.Row():
|
96 |
+
compute_button = gr.Button("Compute Metrics")
|
97 |
+
results_output = gr.JSON(label="Results")
|
98 |
+
|
99 |
+
# Update previews when files are uploaded
|
100 |
+
def update_previews(ref_file, hyp_file):
|
101 |
+
if ref_file:
|
102 |
+
with open(ref_file.name, 'r') as f:
|
103 |
+
ref_text = f.read()
|
104 |
+
return ref_text[:200] # Show first 200 characters
|
105 |
+
if hyp_file:
|
106 |
+
with open(hyp_file.name, 'r') as f:
|
107 |
+
hyp_text = f.read()
|
108 |
+
return hyp_text[:200] # Show first 200 characters
|
109 |
+
return "", ""
|
110 |
+
|
111 |
+
reference_file.change(
|
112 |
+
fn=update_previews,
|
113 |
+
inputs=[reference_file, hypothesis_file],
|
114 |
+
outputs=[reference_preview, hypothesis_preview]
|
115 |
+
)
|
116 |
+
hypothesis_file.change(
|
117 |
+
fn=update_previews,
|
118 |
+
inputs=[reference_file, hypothesis_file],
|
119 |
+
outputs=[reference_preview, hypothesis_preview]
|
120 |
+
)
|
121 |
+
|
122 |
+
compute_button.click(
|
123 |
+
fn=process_files,
|
124 |
+
inputs=[reference_file, hypothesis_file],
|
125 |
+
outputs=results_output
|
126 |
+
)
|
127 |
+
|
128 |
+
demo.launch()
|
129 |
+
|
130 |
+
if __name__ == "__main__":
|
131 |
+
main()
|