Spaces:
Running
on
Zero
Running
on
Zero
display misaligned sentences only once
Browse files
app.py
CHANGED
@@ -162,8 +162,8 @@ def format_sentence_metrics(sentence_wers, sentence_cers, average_wer, average_c
|
|
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['
|
166 |
-
md += f"* Hypothesis: {misaligned['
|
167 |
md += f"* Misalignment starts at position: {misaligned['misalignment_start']}\n\n"
|
168 |
else:
|
169 |
md += "\n### Misaligned Sentences\n\n"
|
@@ -171,33 +171,30 @@ def format_sentence_metrics(sentence_wers, sentence_cers, average_wer, average_c
|
|
171 |
|
172 |
return md
|
173 |
|
174 |
-
@spaces.GPU()
|
175 |
-
def process_files(reference_file, hypothesis_file):
|
176 |
-
try:
|
177 |
-
with open(reference_file.name, 'r') as f:
|
178 |
-
reference_text = f.read()
|
179 |
|
180 |
-
|
181 |
-
|
182 |
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
misaligned = identify_misaligned_sentences(reference_text, hypothesis_text)
|
187 |
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
|
|
|
|
|
|
201 |
|
202 |
def process_and_display(ref_file, hyp_file):
|
203 |
result = process_files(ref_file, hyp_file)
|
@@ -233,7 +230,6 @@ def process_and_display(ref_file, hyp_file):
|
|
233 |
|
234 |
return metrics, metrics_md, misaligned_md
|
235 |
|
236 |
-
|
237 |
def main():
|
238 |
with gr.Blocks() as demo:
|
239 |
gr.Markdown("# ASR Metrics")
|
@@ -249,8 +245,7 @@ def main():
|
|
249 |
with gr.Row():
|
250 |
compute_button = gr.Button("Compute Metrics")
|
251 |
results_output = gr.JSON(label="Results")
|
252 |
-
metrics_output = gr.Markdown(label="
|
253 |
-
misaligned_output = gr.Markdown(label="Misaligned Sentences")
|
254 |
|
255 |
# Update previews when files are uploaded
|
256 |
def update_previews(ref_file, hyp_file):
|
@@ -259,10 +254,10 @@ def main():
|
|
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 |
|
@@ -280,10 +275,11 @@ def main():
|
|
280 |
compute_button.click(
|
281 |
fn=process_and_display,
|
282 |
inputs=[reference_file, hypothesis_file],
|
283 |
-
outputs=[results_output, metrics_output
|
284 |
)
|
285 |
|
286 |
demo.launch()
|
287 |
|
|
|
288 |
if __name__ == "__main__":
|
289 |
main()
|
|
|
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['reference']}\n"
|
166 |
+
md += f"* Hypothesis: {misaligned['hypothesis']}\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 |
|
175 |
+
def process_and_display(ref_file, hyp_file):
|
176 |
+
result = process_files(ref_file, hyp_file)
|
177 |
|
178 |
+
if "error" in result:
|
179 |
+
error_msg = result["error"]
|
180 |
+
return {"error": error_msg}, "", ""
|
|
|
181 |
|
182 |
+
metrics = {
|
183 |
+
"Overall WER": result["Overall WER"],
|
184 |
+
"Overall CER": result["Overall CER"]
|
185 |
+
}
|
186 |
+
|
187 |
+
metrics_md = format_sentence_metrics(
|
188 |
+
result["Sentence WERs"],
|
189 |
+
result["Sentence CERs"],
|
190 |
+
result["Average WER"],
|
191 |
+
result["Average CER"],
|
192 |
+
result["Standard Deviation WER"],
|
193 |
+
result["Standard Deviation CER"],
|
194 |
+
result["Misaligned Sentences"]
|
195 |
+
)
|
196 |
+
|
197 |
+
return metrics, metrics_md
|
198 |
|
199 |
def process_and_display(ref_file, hyp_file):
|
200 |
result = process_files(ref_file, hyp_file)
|
|
|
230 |
|
231 |
return metrics, metrics_md, misaligned_md
|
232 |
|
|
|
233 |
def main():
|
234 |
with gr.Blocks() as demo:
|
235 |
gr.Markdown("# ASR Metrics")
|
|
|
245 |
with gr.Row():
|
246 |
compute_button = gr.Button("Compute Metrics")
|
247 |
results_output = gr.JSON(label="Results")
|
248 |
+
metrics_output = gr.Markdown(label="Metrics")
|
|
|
249 |
|
250 |
# Update previews when files are uploaded
|
251 |
def update_previews(ref_file, hyp_file):
|
|
|
254 |
|
255 |
if ref_file:
|
256 |
with open(ref_file.name, 'r') as f:
|
257 |
+
ref_text = f.read()[:200] # Show first 200 characters
|
258 |
if hyp_file:
|
259 |
with open(hyp_file.name, 'r') as f:
|
260 |
+
hyp_text = f.read()[:200] # Show first 200 characters
|
261 |
|
262 |
return ref_text, hyp_text
|
263 |
|
|
|
275 |
compute_button.click(
|
276 |
fn=process_and_display,
|
277 |
inputs=[reference_file, hypothesis_file],
|
278 |
+
outputs=[results_output, metrics_output]
|
279 |
)
|
280 |
|
281 |
demo.launch()
|
282 |
|
283 |
+
|
284 |
if __name__ == "__main__":
|
285 |
main()
|