Spaces:
Runtime error
Runtime error
Commit
·
d690b2a
1
Parent(s):
37d60b5
Transcribe in chunks
Browse filesAvoid OOM on large audio files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import Wav2Vec2ForCTC, AutoProcessor
|
3 |
import torch
|
|
|
4 |
import librosa
|
5 |
import json
|
6 |
|
@@ -24,20 +25,30 @@ def transcribe(audio_file_mic=None, audio_file_upload=None, language="English (e
|
|
24 |
# Make sure audio is 16kHz
|
25 |
speech, sample_rate = librosa.load(audio_file)
|
26 |
if sample_rate != 16000:
|
|
|
27 |
speech = librosa.resample(speech, orig_sr=sample_rate, target_sr=16000)
|
28 |
|
29 |
-
#
|
|
|
|
|
|
|
|
|
30 |
language_code = iso_codes[language]
|
31 |
processor.tokenizer.set_target_lang(language_code)
|
32 |
model.load_adapter(language_code)
|
33 |
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
37 |
-
|
|
|
38 |
|
39 |
-
|
40 |
-
transcription = processor.decode(ids)
|
41 |
return transcription
|
42 |
|
43 |
examples = [
|
@@ -50,14 +61,15 @@ examples = [
|
|
50 |
description = '''Automatic Speech Recognition with [MMS](https://ai.facebook.com/blog/multilingual-model-speech-recognition/) (Massively Multilingual Speech) by Meta.
|
51 |
Supports [1162 languages](https://dl.fbaipublicfiles.com/mms/misc/language_coverage_mms.html). Read the paper for more details: [Scaling Speech Technology to 1,000+ Languages](https://arxiv.org/abs/2305.13516).'''
|
52 |
|
53 |
-
iface = gr.Interface(
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
63 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import Wav2Vec2ForCTC, AutoProcessor
|
3 |
import torch
|
4 |
+
import numpy as np
|
5 |
import librosa
|
6 |
import json
|
7 |
|
|
|
25 |
# Make sure audio is 16kHz
|
26 |
speech, sample_rate = librosa.load(audio_file)
|
27 |
if sample_rate != 16000:
|
28 |
+
print('resampling')
|
29 |
speech = librosa.resample(speech, orig_sr=sample_rate, target_sr=16000)
|
30 |
|
31 |
+
# Cut speech into chunks
|
32 |
+
chunk_size = 30 * 16000 # 30s * 16000Hz
|
33 |
+
chunks = np.split(speech, np.arange(chunk_size, len(speech), chunk_size))
|
34 |
+
|
35 |
+
# load model adapter for this language
|
36 |
language_code = iso_codes[language]
|
37 |
processor.tokenizer.set_target_lang(language_code)
|
38 |
model.load_adapter(language_code)
|
39 |
|
40 |
+
transcriptions = []
|
41 |
+
for chunk in chunks:
|
42 |
+
inputs = processor(chunk, sampling_rate=16_000, return_tensors="pt")
|
43 |
+
|
44 |
+
with torch.no_grad():
|
45 |
+
outputs = model(**inputs).logits
|
46 |
|
47 |
+
ids = torch.argmax(outputs, dim=-1)[0]
|
48 |
+
transcription = processor.decode(ids)
|
49 |
+
transcriptions.append(transcription)
|
50 |
|
51 |
+
transcription = ' '.join(transcriptions)
|
|
|
52 |
return transcription
|
53 |
|
54 |
examples = [
|
|
|
61 |
description = '''Automatic Speech Recognition with [MMS](https://ai.facebook.com/blog/multilingual-model-speech-recognition/) (Massively Multilingual Speech) by Meta.
|
62 |
Supports [1162 languages](https://dl.fbaipublicfiles.com/mms/misc/language_coverage_mms.html). Read the paper for more details: [Scaling Speech Technology to 1,000+ Languages](https://arxiv.org/abs/2305.13516).'''
|
63 |
|
64 |
+
iface = gr.Interface(
|
65 |
+
fn=transcribe,
|
66 |
+
inputs=[
|
67 |
+
gr.Audio(source="microphone", type="filepath", label="Record Audio"),
|
68 |
+
gr.Audio(source="upload", type="filepath", label="Upload Audio"),
|
69 |
+
gr.Dropdown(choices=languages, label="Language", value="English (eng)")
|
70 |
+
],
|
71 |
+
outputs=gr.Textbox(label="Transcription"),
|
72 |
+
examples=examples,
|
73 |
+
description=description
|
74 |
+
)
|
75 |
iface.launch()
|