chenhaodev's picture
Initial commit
e793dbd
# app.py
import gradio as gr
import jiwer
import pandas as pd
def calculate_metrics(hypothesis, reference):
"""
Calculate WER, MER, WIL and CER metrics using jiwer
Args:
hypothesis (str): ASR output text
reference (str): Ground truth reference text
Returns:
dict: Dictionary containing the metrics
"""
# Calculate WER and related metrics
transformation = jiwer.Compose([
jiwer.ToLowerCase(),
jiwer.RemoveMultipleSpaces(),
jiwer.Strip(),
jiwer.RemovePunctuation()
])
wer = jiwer.wer(reference, hypothesis, truth_transform=transformation, hypothesis_transform=transformation)
mer = jiwer.mer(reference, hypothesis, truth_transform=transformation, hypothesis_transform=transformation)
wil = jiwer.wil(reference, hypothesis, truth_transform=transformation, hypothesis_transform=transformation)
# Calculate CER
cer = jiwer.cer(reference, hypothesis, truth_transform=transformation, hypothesis_transform=transformation)
return {
"WER (Word Error Rate)": f"{wer:.4f}",
"MER (Match Error Rate)": f"{mer:.4f}",
"WIL (Word Information Lost)": f"{wil:.4f}",
"CER (Character Error Rate)": f"{cer:.4f}"
}
def process_file_input(hypothesis_file, reference_file):
"""
Process uploaded text files and calculate metrics
Args:
hypothesis_file (str): Path to ASR output file
reference_file (str): Path to reference file
Returns:
dict: Dictionary containing the metrics
"""
try:
with open(hypothesis_file.name, 'r', encoding='utf-8') as f:
hypothesis = f.read()
with open(reference_file.name, 'r', encoding='utf-8') as f:
reference = f.read()
return calculate_metrics(hypothesis, reference)
except Exception as e:
return f"Error processing files: {str(e)}"
# Text input interface
text_interface = gr.Interface(
fn=calculate_metrics,
inputs=[
gr.Textbox(label="ASR Output Text", placeholder="Enter ASR output text here...", lines=5),
gr.Textbox(label="Reference Text", placeholder="Enter reference text here...", lines=5)
],
outputs=gr.JSON(label="Metrics"),
title="Speech Recognition Metrics Calculator",
description="Calculate WER (Word Error Rate) and other metrics between ASR output and reference text.",
examples=[
["the quick brown fox jumps over the lazy dog",
"the quick brown fox jumped over the lazy dog"],
["hello wrld how are you",
"hello world how are you"]
]
)
# File input interface
file_interface = gr.Interface(
fn=process_file_input,
inputs=[
gr.File(label="ASR Output File (txt)"),
gr.File(label="Reference File (txt)")
],
outputs=gr.JSON(label="Metrics"),
title="Speech Recognition Metrics Calculator (File Upload)",
description="Calculate WER (Word Error Rate) and other metrics from text files."
)
# Create the demo
demo = gr.TabbedInterface(
[text_interface, file_interface],
tab_names=["Text Input", "File Upload"]
)
# Launch the demo
demo.queue().launch()