Spaces:
Running
on
Zero
Running
on
Zero
add cer
Browse files
app.py
CHANGED
@@ -9,9 +9,7 @@ def split_into_sentences(text):
|
|
9 |
Simple sentence tokenizer using regular expressions.
|
10 |
Splits text into sentences based on punctuation.
|
11 |
"""
|
12 |
-
# Split text into sentences using regex
|
13 |
sentences = re.split(r'(?<=[.!?])\s*', text)
|
14 |
-
# Clean up empty strings and whitespace
|
15 |
sentences = [s.strip() for s in sentences if s.strip()]
|
16 |
return sentences
|
17 |
|
@@ -32,9 +30,9 @@ def calculate_cer(reference, hypothesis):
|
|
32 |
return cer
|
33 |
|
34 |
@spaces.GPU()
|
35 |
-
def
|
36 |
"""
|
37 |
-
Calculate WER for each sentence and overall statistics.
|
38 |
"""
|
39 |
try:
|
40 |
reference_sentences = split_into_sentences(reference)
|
@@ -44,24 +42,35 @@ def calculate_sentence_wer(reference, hypothesis):
|
|
44 |
raise ValueError("Reference and hypothesis must contain the same number of sentences")
|
45 |
|
46 |
sentence_wers = []
|
|
|
47 |
for ref, hyp in zip(reference_sentences, hypothesis_sentences):
|
48 |
-
|
49 |
-
|
|
|
|
|
50 |
|
51 |
-
if not sentence_wers:
|
52 |
return {
|
53 |
"sentence_wers": [],
|
|
|
54 |
"average_wer": 0.0,
|
55 |
-
"
|
|
|
|
|
56 |
}
|
57 |
|
58 |
average_wer = np.mean(sentence_wers)
|
59 |
-
|
|
|
|
|
60 |
|
61 |
return {
|
62 |
"sentence_wers": sentence_wers,
|
|
|
63 |
"average_wer": average_wer,
|
64 |
-
"
|
|
|
|
|
65 |
}
|
66 |
except Exception as e:
|
67 |
raise e
|
@@ -75,30 +84,43 @@ def process_files(reference_file, hypothesis_file):
|
|
75 |
with open(hypothesis_file.name, 'r') as f:
|
76 |
hypothesis_text = f.read()
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
|
82 |
return {
|
83 |
-
"WER":
|
84 |
-
"CER":
|
85 |
-
"Sentence WERs":
|
86 |
-
"
|
87 |
-
"
|
|
|
|
|
|
|
88 |
}
|
89 |
except Exception as e:
|
90 |
return {"error": str(e)}
|
91 |
|
92 |
-
def
|
93 |
-
if not sentence_wers:
|
94 |
return "All sentences match perfectly!"
|
95 |
|
96 |
-
md = "### Sentence-level
|
|
|
97 |
md += f"* Average WER: {average_wer:.2f}\n"
|
98 |
-
md += f"* Standard Deviation: {
|
|
|
|
|
|
|
|
|
99 |
md += "### WER for Each Sentence\n\n"
|
100 |
for i, wer in enumerate(sentence_wers):
|
101 |
md += f"* Sentence {i+1}: {wer:.2f}\n"
|
|
|
|
|
|
|
|
|
|
|
102 |
return md
|
103 |
|
104 |
def main():
|
@@ -116,7 +138,7 @@ def main():
|
|
116 |
with gr.Row():
|
117 |
compute_button = gr.Button("Compute Metrics")
|
118 |
results_output = gr.JSON(label="Results")
|
119 |
-
|
120 |
|
121 |
# Update previews when files are uploaded
|
122 |
def update_previews(ref_file, hyp_file):
|
@@ -149,22 +171,25 @@ def main():
|
|
149 |
return {}, {}, "Error: " + result["error"]
|
150 |
|
151 |
metrics = {
|
152 |
-
"WER": result["WER"],
|
153 |
-
"CER": result["CER"]
|
154 |
}
|
155 |
|
156 |
-
|
157 |
result["Sentence WERs"],
|
|
|
158 |
result["Average WER"],
|
159 |
-
result["
|
|
|
|
|
160 |
)
|
161 |
|
162 |
-
return metrics,
|
163 |
|
164 |
compute_button.click(
|
165 |
fn=process_and_display,
|
166 |
inputs=[reference_file, hypothesis_file],
|
167 |
-
outputs=[results_output,
|
168 |
)
|
169 |
|
170 |
demo.launch()
|
|
|
9 |
Simple sentence tokenizer using regular expressions.
|
10 |
Splits text into sentences based on punctuation.
|
11 |
"""
|
|
|
12 |
sentences = re.split(r'(?<=[.!?])\s*', text)
|
|
|
13 |
sentences = [s.strip() for s in sentences if s.strip()]
|
14 |
return sentences
|
15 |
|
|
|
30 |
return cer
|
31 |
|
32 |
@spaces.GPU()
|
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)
|
|
|
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,
|
69 |
+
"sentence_cers": sentence_cers,
|
70 |
"average_wer": average_wer,
|
71 |
+
"average_cer": average_cer,
|
72 |
+
"std_dev_wer": std_dev_wer,
|
73 |
+
"std_dev_cer": std_dev_cer
|
74 |
}
|
75 |
except Exception as e:
|
76 |
raise e
|
|
|
84 |
with open(hypothesis_file.name, 'r') as f:
|
85 |
hypothesis_text = f.read()
|
86 |
|
87 |
+
overall_wer = calculate_wer(reference_text, hypothesis_text)
|
88 |
+
overall_cer = calculate_cer(reference_text, hypothesis_text)
|
89 |
+
sentence_metrics = calculate_sentence_metrics(reference_text, hypothesis_text)
|
90 |
|
91 |
return {
|
92 |
+
"Overall WER": overall_wer,
|
93 |
+
"Overall CER": overall_cer,
|
94 |
+
"Sentence WERs": sentence_metrics["sentence_wers"],
|
95 |
+
"Sentence CERs": sentence_metrics["sentence_cers"],
|
96 |
+
"Average WER": sentence_metrics["average_wer"],
|
97 |
+
"Average CER": sentence_metrics["average_cer"],
|
98 |
+
"Standard Deviation WER": sentence_metrics["std_dev_wer"],
|
99 |
+
"Standard Deviation CER": sentence_metrics["std_dev_cer"]
|
100 |
}
|
101 |
except Exception as e:
|
102 |
return {"error": str(e)}
|
103 |
|
104 |
+
def format_sentence_metrics(sentence_wers, sentence_cers, average_wer, average_cer, std_dev_wer, std_dev_cer):
|
105 |
+
if not sentence_wers and not sentence_cers:
|
106 |
return "All sentences match perfectly!"
|
107 |
|
108 |
+
md = "### Sentence-level Metrics\n\n"
|
109 |
+
md += "#### Word Error Rate (WER)\n"
|
110 |
md += f"* Average WER: {average_wer:.2f}\n"
|
111 |
+
md += f"* Standard Deviation: {std_dev_wer:.2f}\n\n"
|
112 |
+
md += "#### Character Error Rate (CER)\n"
|
113 |
+
md += f"* Average CER: {average_cer:.2f}\n"
|
114 |
+
md += f"* Standard Deviation: {std_dev_cer:.2f}\n\n"
|
115 |
+
|
116 |
md += "### WER for Each Sentence\n\n"
|
117 |
for i, wer in enumerate(sentence_wers):
|
118 |
md += f"* Sentence {i+1}: {wer:.2f}\n"
|
119 |
+
|
120 |
+
md += "\n### CER for Each Sentence\n\n"
|
121 |
+
for i, cer in enumerate(sentence_cers):
|
122 |
+
md += f"* Sentence {i+1}: {cer:.2f}\n"
|
123 |
+
|
124 |
return md
|
125 |
|
126 |
def main():
|
|
|
138 |
with gr.Row():
|
139 |
compute_button = gr.Button("Compute Metrics")
|
140 |
results_output = gr.JSON(label="Results")
|
141 |
+
metrics_output = gr.Markdown(label="Sentence Metrics")
|
142 |
|
143 |
# Update previews when files are uploaded
|
144 |
def update_previews(ref_file, hyp_file):
|
|
|
171 |
return {}, {}, "Error: " + result["error"]
|
172 |
|
173 |
metrics = {
|
174 |
+
"Overall WER": result["Overall WER"],
|
175 |
+
"Overall CER": result["Overall CER"]
|
176 |
}
|
177 |
|
178 |
+
metrics_md = format_sentence_metrics(
|
179 |
result["Sentence WERs"],
|
180 |
+
result["Sentence CERs"],
|
181 |
result["Average WER"],
|
182 |
+
result["Average CER"],
|
183 |
+
result["Standard Deviation WER"],
|
184 |
+
result["Standard Deviation CER"]
|
185 |
)
|
186 |
|
187 |
+
return metrics, metrics_md
|
188 |
|
189 |
compute_button.click(
|
190 |
fn=process_and_display,
|
191 |
inputs=[reference_file, hypothesis_file],
|
192 |
+
outputs=[results_output, metrics_output]
|
193 |
)
|
194 |
|
195 |
demo.launch()
|