File size: 14,087 Bytes
20960bc
 
 
 
 
 
 
 
 
 
 
 
 
fbaac12
 
20960bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52f6fee
20960bc
 
2b3994d
20960bc
 
 
 
 
 
 
52f6fee
20960bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a51f3f5
20960bc
 
 
 
 
 
 
 
 
 
52f6fee
20960bc
 
52f6fee
20960bc
a51f3f5
20960bc
 
 
 
5ed22d6
52f6fee
bb3d9e1
20960bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ed22d6
20960bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ed22d6
20960bc
5ed22d6
 
20960bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# import os
# # os.system("pip install git+https://github.com/openai/whisper.git")
# # import gradio as gr
# # import whisper
# # from huggingface_hub import from_pretrained_keras
# # from transformers import AutoTokenizer, AutoModelForSequenceClassification
# # from transformers import pipeline
# # from sklearn.preprocessing import StandardScaler
# # import logging
# # import librosa
# # import numpy as np
# # import pickle

# import gradio as gr

# # gr.Interface.load("models/kanyekuthi/AfriSpeech-whisper-tiny").launch()
# # gr.Interface.load("models/kanyekuthi/AfriSpeech-whisper-tiny")
# # gr.launch()

# distil_transcription = gr.components.Textbox(label="Distil-Whisper Transcription", show_copy_button=True)
# transcription = gr.components.Textbox(label="Whisper Transcription", show_copy_button=True)

# demo = gr.Interface(
#     # main_note,
#     gr.Audio(sources=["microphone"]),
#     # gr.Label(num_top_classes=4),
#     # examples=[
#     #     [os.path.join(os.path.dirname(__file__),"audio/recording1.wav")],
#     #     [os.path.join(os.path.dirname(__file__),"audio/cantina.wav")],
#     # ],
#     outputs=[distil_transcription, transcription]
# )

# demo.load("models/kanyekuthi/AfriSpeech-whisper-tiny")

# if __name__ == "__main__":
#     demo.launch()

from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from transformers.utils import is_flash_attn_2_available
from transformers.pipelines.audio_utils import ffmpeg_read
import torch
import gradio as gr
import time

BATCH_SIZE = 16
MAX_AUDIO_MINS = 30  # maximum audio input in minutes

device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
use_flash_attention_2 = is_flash_attn_2_available()

model = AutoModelForSpeechSeq2Seq.from_pretrained(
    "models/kanyekuthi/AfriSpeech-whisper-tiny", torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True, use_flash_attention_2=use_flash_attention_2
)
distilled_model = AutoModelForSpeechSeq2Seq.from_pretrained(
    "distil-whisper/distil-small", torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True, use_flash_attention_2=use_flash_attention_2
)

if not use_flash_attention_2:
    # use flash attention from pytorch sdpa
    model = model.to_bettertransformer()
    distilled_model = distilled_model.to_bettertransformer()

processor = AutoProcessor.from_pretrained("models/kanyekuthi/AfriSpeech-whisper-tiny")

model.to(device)
distilled_model.to(device)

pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    chunk_length_s=30,
    torch_dtype=torch_dtype,
    device=device,
    generate_kwargs={"language": "en", "task": "transcribe"},
    return_timestamps=True
)
pipe_forward = pipe._forward

distil_pipe = pipeline(
    "automatic-speech-recognition",
    model=distilled_model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    chunk_length_s=15,
    torch_dtype=torch_dtype,
    device=device,
    generate_kwargs={"language": "en", "task": "transcribe"},
)
distil_pipe_forward = distil_pipe._forward

def transcribe(inputs):
    if inputs is None:
        raise gr.Error("No audio file submitted! Please record or upload an audio file before submitting your request.")

    with open(inputs, "rb") as f:
        inputs = f.read()

    inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
    audio_length_mins = len(inputs) / pipe.feature_extractor.sampling_rate / 60

    if audio_length_mins > MAX_AUDIO_MINS:
        raise gr.Error(
            f"To ensure fair usage of the Space, the maximum audio length permitted is {MAX_AUDIO_MINS} minutes."
            f"Got an audio of length {round(audio_length_mins, 3)} minutes."
        )

    inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}

    def _forward_distil_time(*args, **kwargs):
        global distil_runtime
        start_time = time.time()
        result = distil_pipe_forward(*args, **kwargs)
        distil_runtime = time.time() - start_time
        distil_runtime = round(distil_runtime, 2)
        return result

    distil_pipe._forward = _forward_distil_time
    distil_text = distil_pipe(inputs.copy(), batch_size=BATCH_SIZE)["text"]
    yield distil_text, distil_runtime, None, None, None

    def _forward_time(*args, **kwargs):
        global runtime
        start_time = time.time()
        result = pipe_forward(*args, **kwargs)
        runtime = time.time() - start_time
        runtime = round(runtime, 2)
        return result

    pipe._forward = _forward_time
    text = pipe(inputs, batch_size=BATCH_SIZE)["text"]

    yield distil_text, distil_runtime, text, runtime

if __name__ == "__main__":
    with gr.Blocks() as demo:
        gr.HTML(
            """
                <div style="text-align: center; max-width: 700px; margin: 0 auto;">
                  <div
                    style="
                      display: inline-flex; align-items: center; gap: 0.8rem; font-size: 1.75rem;
                    "
                  >
                    <h1 style="font-weight: 900; margin-bottom: 7px; line-height: normal;">
                      Whisper vs Distil-Whisper: Speed Comparison
                    </h1>
                  </div>
                </div>
            """
        )
        gr.HTML(
            f"""
            <p><a href="https://huggingface.co/distil-whisper/distil-small"> Distil-Whisper</a> is a distilled variant 
            of the <a href="https://huggingface.co/openai/whisper-small"> Whisper</a> model by OpenAI. Compared to Whisper, 
            Distil-Whisper runs 6x faster with 50% fewer parameters, while performing to within 1% word error rate (WER) on
            out-of-distribution evaluation data.</p>
            
            <p>In this demo, we perform a speed comparison between Whisper and Distil-Whisper in order to test this claim.
            Both models use the <a href="https://huggingface.co/distil-whisper/distil-small#long-form-transcription"> chunked long-form transcription algorithm</a> 
            in 🤗 Transformers, as well as Flash Attention. To use Distil-Whisper yourself, check the code examples on the
            <a href="https://github.com/huggingface/distil-whisper#1-usage"> Distil-Whisper repository</a>. To ensure fair 
            usage of the Space, we ask that audio file inputs are kept to < 30 mins.</p>
            """
        )
        audio = gr.components.Audio(type="filepath", label="Audio input")
        button = gr.Button("Transcribe")
        with gr.Row():
            distil_runtime = gr.components.Textbox(label="Distil-Whisper Transcription Time (s)")
            runtime = gr.components.Textbox(label="Whisper Transcription Time (s)")
        with gr.Row():
            distil_transcription = gr.components.Textbox(label="Distil-Whisper Transcription", show_copy_button=True)
            transcription = gr.components.Textbox(label="Whisper Transcription", show_copy_button=True)
        button.click(
            fn=transcribe,
            inputs=audio,
            outputs=[distil_transcription, distil_runtime, transcription, runtime],
        )
        gr.Markdown("## Examples")
        gr.Examples(
            [["./assets/example_1.wav"], ["./assets/example_2.wav"]],
            audio,
            outputs=[distil_transcription, distil_runtime, transcription, runtime],
            fn=transcribe,
            cache_examples=False,
        )
    demo.queue(max_size=10).launch()
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
from transformers.utils import is_flash_attn_2_available
from transformers.pipelines.audio_utils import ffmpeg_read
import torch
import gradio as gr
import time

BATCH_SIZE = 16
MAX_AUDIO_MINS = 30  # maximum audio input in minutes

device = "cuda:0" if torch.cuda.is_available() else "cpu"
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
use_flash_attention_2 = is_flash_attn_2_available()

model = AutoModelForSpeechSeq2Seq.from_pretrained(
    "models/kanyekuthi/AfriSpeech-whisper-tiny", torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True, use_flash_attention_2=use_flash_attention_2
)
distilled_model = AutoModelForSpeechSeq2Seq.from_pretrained(
    "models/kanyekuthi/AfriSpeech-whisper-tiny", torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True, use_flash_attention_2=use_flash_attention_2
)

if not use_flash_attention_2:
    # use flash attention from pytorch sdpa
    model = model.to_bettertransformer()
    distilled_model = distilled_model.to_bettertransformer()

processor = AutoProcessor.from_pretrained("models/kanyekuthi/AfriSpeech-whisper-tiny")

model.to(device)
distilled_model.to(device)

pipe = pipeline(
    "automatic-speech-recognition",
    model=model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    chunk_length_s=30,
    torch_dtype=torch_dtype,
    device=device,
    generate_kwargs={"language": "en", "task": "transcribe"},
    return_timestamps=True
)
pipe_forward = pipe._forward

distil_pipe = pipeline(
    "automatic-speech-recognition",
    model=distilled_model,
    tokenizer=processor.tokenizer,
    feature_extractor=processor.feature_extractor,
    max_new_tokens=128,
    chunk_length_s=15,
    torch_dtype=torch_dtype,
    device=device,
    generate_kwargs={"language": "en", "task": "transcribe"},
)
distil_pipe_forward = distil_pipe._forward

def transcribe(inputs):
    if inputs is None:
        raise gr.Error("No audio file submitted! Please record or upload an audio file before submitting your request.")

    with open(inputs, "rb") as f:
        inputs = f.read()

    inputs = ffmpeg_read(inputs, pipe.feature_extractor.sampling_rate)
    audio_length_mins = len(inputs) / pipe.feature_extractor.sampling_rate / 60

    if audio_length_mins > MAX_AUDIO_MINS:
        raise gr.Error(
            f"To ensure fair usage of the Space, the maximum audio length permitted is {MAX_AUDIO_MINS} minutes."
            f"Got an audio of length {round(audio_length_mins, 3)} minutes."
        )

    inputs = {"array": inputs, "sampling_rate": pipe.feature_extractor.sampling_rate}

    def _forward_distil_time(*args, **kwargs):
        global distil_runtime
        start_time = time.time()
        result = distil_pipe_forward(*args, **kwargs)
        distil_runtime = time.time() - start_time
        distil_runtime = round(distil_runtime, 2)
        return result

    distil_pipe._forward = _forward_distil_time
    distil_text = distil_pipe(inputs.copy(), batch_size=BATCH_SIZE)["text"]
    yield distil_text, distil_runtime, None, None, None

    def _forward_time(*args, **kwargs):
        global runtime
        start_time = time.time()
        result = pipe_forward(*args, **kwargs)
        runtime = time.time() - start_time
        runtime = round(runtime, 2)
        return result

    pipe._forward = _forward_time
    text = pipe(inputs, batch_size=BATCH_SIZE)["text"]

    yield distil_text, distil_runtime, text, runtime

if __name__ == "__main__":
    with gr.Blocks() as demo:
        gr.HTML(
            """
                <div style="text-align: center; max-width: 700px; margin: 0 auto;">
                  <div
                    style="
                      display: inline-flex; align-items: center; gap: 0.8rem; font-size: 1.75rem;
                    "
                  >
                    <h1 style="font-weight: 900; margin-bottom: 7px; line-height: normal;">
                      Whisper vs Distil-Whisper: Speed Comparison
                    </h1>
                  </div>
                </div>
            """
        )
        gr.HTML(
            f"""
            <p><a href="https://huggingface.co/distil-whisper/distil-small"> Distil-Whisper</a> is a distilled variant 
            of the <a href="https://huggingface.co/openai/whisper-small"> Whisper</a> model by OpenAI. Compared to Whisper, 
            Distil-Whisper runs 6x faster with 50% fewer parameters, while performing to within 1% word error rate (WER) on
            out-of-distribution evaluation data.</p>
            
            <p>In this demo, we perform a speed comparison between Whisper and Distil-Whisper in order to test this claim.
            Both models use the <a href="https://huggingface.co/distil-whisper/distil-small#long-form-transcription"> chunked long-form transcription algorithm</a> 
            in 🤗 Transformers, as well as Flash Attention. To use Distil-Whisper yourself, check the code examples on the
            <a href="https://github.com/huggingface/distil-whisper#1-usage"> Distil-Whisper repository</a>. To ensure fair 
            usage of the Space, we ask that audio file inputs are kept to < 30 mins.</p>
            """
        )
        audio = gr.components.Audio(type="filepath", label="Audio input")
        button = gr.Button("Transcribe")
        with gr.Row():
            distil_runtime = gr.components.Textbox(label="Distil-Whisper Transcription Time (s)")
            runtime = gr.components.Textbox(label="Whisper Transcription Time (s)")
        with gr.Row():
            distil_transcription = gr.components.Textbox(label="Distil-Whisper Transcription", show_copy_button=True)
            transcription = gr.components.Textbox(label="Whisper Transcription", show_copy_button=True)
        button.click(
            fn=transcribe,
            inputs=audio,
            outputs=[distil_transcription, distil_runtime, transcription, runtime],
        )
        gr.Markdown("## Examples")
        gr.Examples(
            [["./assets/example_1.wav"], ["./assets/example_2.wav"]],
            audio,
            outputs=[distil_transcription, distil_runtime, transcription, runtime],
            fn=transcribe,
            cache_examples=False,
        )
    demo.queue(max_size=10).launch()