File size: 6,686 Bytes
63da647
a611f4b
63da647
 
1f97be9
 
63da647
 
 
8cc7c73
63da647
 
 
 
 
a635c25
63da647
 
 
 
1f97be9
a635c25
 
 
 
 
 
 
63da647
 
 
 
 
a635c25
63da647
a635c25
63da647
 
 
 
a635c25
 
 
 
 
 
 
63da647
 
 
 
 
a635c25
63da647
 
a635c25
 
 
1f97be9
f951aed
a635c25
 
 
 
 
 
 
 
 
 
 
 
 
1f97be9
a368f0c
 
 
 
 
 
 
 
 
 
 
1f97be9
 
a635c25
1f97be9
 
 
 
 
 
 
 
 
a635c25
 
 
1f97be9
63da647
a635c25
99eb4a3
 
 
 
 
1f97be9
63da647
 
70f71c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63da647
a635c25
 
 
 
 
 
 
 
b6c2d8b
a635c25
b6c2d8b
 
 
 
 
 
 
 
a368f0c
a635c25
 
 
 
 
 
 
 
b6c2d8b
 
 
 
 
 
 
 
a368f0c
a635c25
 
 
63da647
8cc7c73
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import gradio as gr
import spaces
import random
import json
import os
import string
from difflib import SequenceMatcher
from jiwer import wer
import torchaudio
from transformers import pipeline

# Load metadata
with open("common_voice_en_validated_249_hf_ready.json") as f:
    data = json.load(f)

# Prepare dropdown options
ages = sorted(set(entry["age"] for entry in data))
genders = sorted(set(entry["gender"] for entry in data))
accents = sorted(set(entry["accent"] for entry in data))

# Utility functions
def convert_to_wav(file_path):
    wav_path = file_path.replace(".mp3", ".wav")
    if not os.path.exists(wav_path):
        waveform, sample_rate = torchaudio.load(file_path)
        waveform = waveform.mean(dim=0, keepdim=True)
        torchaudio.save(wav_path, waveform, sample_rate)
    return wav_path

def highlight_differences(ref, hyp):
    sm = SequenceMatcher(None, ref.split(), hyp.split())
    result = []
    for opcode, i1, i2, j1, j2 in sm.get_opcodes():
        if opcode == "equal":
            result.extend(hyp.split()[j1:j2])
        else:
            wrong = hyp.split()[j1:j2]
            result.extend([f"<span style='color:red'>{w}</span>" for w in wrong])
    return " ".join(result)

def normalize(text):
    text = text.lower()
    text = text.translate(str.maketrans('', '', string.punctuation))
    return text.strip()

# Generate Audio
def generate_audio(age, gender, accent):
    filtered = [
        entry for entry in data
        if entry["age"] == age and entry["gender"] == gender and entry["accent"] == accent
    ]
    if not filtered:
        return None, "No matching sample."
    sample = random.choice(filtered)
    file_path = os.path.join("common_voice_en_validated_249", sample["path"])
    wav_file_path = convert_to_wav(file_path)
    return wav_file_path, wav_file_path

# Transcribe & Compare (GPU Decorated)
# @spaces.GPU
def transcribe_audio(file_path):
    if not file_path:
        return "No file selected.", "", "", "", "", "", ""

    filename_mp3 = os.path.basename(file_path).replace(".wav", ".mp3")
    gold = ""
    for entry in data:
        if entry["path"].endswith(filename_mp3):
            gold = normalize(entry["sentence"])
            break
    if not gold:
        return "Reference not found.", "", "", "", "", "", ""

    model_ids = [
        "openai/whisper-tiny", # Smallest, multilingual
        "openai/whisper-tiny.en", # Tiny, English-only
        "openai/whisper-base", # Balanced, multilingual
        "openai/whisper-base.en", # Base, English-only
        "openai/whisper-medium", # Medium, multilingual
        "openai/whisper-medium.en", # Medium, English-only
        "distil-whisper/distil-large-v3.5", # Distilled from Whisper large, Faster & More accurate
        "facebook/wav2vec2-base-960h", # Base model trained on 960h LibriSpeech (monolingual, English)
        "facebook/wav2vec2-large-960h", #Larger model, better performance (monolingual, English)
        "facebook/wav2vec2-large-960h-lv60-self", # Fine-tuned on 60k LibriLight hours
        "facebook/hubert-large-ls960-ft", # Fine-tuned on LibriSpeech
    ]

    outputs = {}
    for model_id in model_ids:
        try:
            pipe = pipeline("automatic-speech-recognition", model=model_id)
            text = pipe(file_path)["text"].strip().lower()
            clean = normalize(text)
            wer_score = wer(gold, clean)
            outputs[model_id] = f"<b>{model_id} (WER: {wer_score:.2f}):</b><br>{highlight_differences(gold, clean)}"
        except Exception as e:
            outputs[model_id] = f"<b>{model_id}:</b><br><span style='color:red'>Error: {str(e)}</span>"

    return (gold, *outputs.values())

# Gradio UI
with gr.Blocks() as demo:
    gr.Markdown("# Comparing ASR Models on Diverse English Speech Samples")
    gr.Markdown("""
        This demo compares the transcription performance of several automatic speech recognition (ASR) models.
        Users can select age, gender, and accent to generate diverse English audio samples.
        The models are evaluated on their ability to transcribe those samples.
        Data is sourced from 249 validated entries in the Common Voice English Delta Segment 21.0 release.
    """)

    with gr.Row():
        accent = gr.Dropdown(choices=accents, label="Accent", interactive=True)
        gender = gr.Dropdown(choices=[], label="Gender", interactive=True)
        age = gr.Dropdown(choices=[], label="Age", interactive=True)

    def update_gender_options(selected_accent):
        options = sorted(set(entry["gender"] for entry in data if entry["accent"] == selected_accent))
        return gr.update(choices=options, value=None)

    def update_age_options(selected_accent, selected_gender):
        options = sorted(set(
            entry["age"] for entry in data
            if entry["accent"] == selected_accent and entry["gender"] == selected_gender
        ))
        return gr.update(choices=options, value=None)

    accent.change(update_gender_options, inputs=[accent], outputs=[gender])
    gender.change(update_age_options, inputs=[accent, gender], outputs=[age])

    generate_btn = gr.Button("Get Audio")
    audio_output = gr.Audio(label="Audio", type="filepath", interactive=False)
    file_path_output = gr.Textbox(label="Audio File Path", visible=False)

    generate_btn.click(generate_audio, [age, gender, accent], [audio_output, file_path_output])

    transcribe_btn = gr.Button("Transcribe with All Models")
    gold_text = gr.Textbox(label="Reference (Gold Standard)")
    
    whisper_tiny_html = gr.HTML(label="Whisper Tiny")
    whisper_tiny_en_html = gr.HTML(label="Whisper Tiny English")
    whisper_base_html = gr.HTML(label="Whisper Base")
    whisper_base_en_html = gr.HTML(label="Whisper Base English")
    whisper_medium_html = gr.HTML(label="Whisper Medium")
    whisper_medium_en_html = gr.HTML(label="Whisper Medium English")
    distil_html = gr.HTML(label="Distil-Whisper Large")
    wav2vec_base_html = gr.HTML(label="Wav2Vec2 Base")
    wav2vec_large_html = gr.HTML(label="Wav2Vec2 Large")
    wav2vec_lv60_html = gr.HTML(label="Wav2Vec2 Large + LibriLight")
    hubert_html = gr.HTML(label="HuBERT Large")

    transcribe_btn.click(
        transcribe_audio,
        inputs=[file_path_output],
        outputs=[
            gold_text,
            whisper_tiny_html,
            whisper_tiny_en_html,
            whisper_base_html,
            whisper_base_en_html,
            whisper_medium_html,
            whisper_medium_en_html,
            distil_html,
            wav2vec_base_html,
            wav2vec_large_html,
            wav2vec_lv60_html,
            hubert_html,
        ],
    )

demo.launch()