Spaces:
Build error
Build error
Commit
·
62a78c6
1
Parent(s):
63914a6
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,29 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
from transformers import SpeechEncoderDecoder, Wav2Vec2Processor
|
| 3 |
import gradio as gr
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
def
|
| 14 |
-
|
| 15 |
-
input_values = processor(speech, sampling_rate=16_000, return_tensors="pt").input_values
|
| 16 |
-
logits = model(input_values).logits
|
| 17 |
-
predicted_ids = torch.argmax(logits, dim=-1)
|
| 18 |
-
transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
|
| 19 |
-
return transcription
|
| 20 |
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import librosa
|
| 3 |
+
from transformers import Wav2Vec2Processor, SpeechEncoderDecoder
|
| 4 |
+
|
| 5 |
+
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-xls-r-300m-en-to-15", use_auth_token="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")
|
| 6 |
+
model = SpeechEncoderDecoder.from_pretrained("facebook/wav2vec2-xls-r-300m-en-to-15", use_auth_token="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")
|
| 7 |
|
| 8 |
+
def process_audio_file(file):
|
| 9 |
+
data, sr = librosa.load(file)
|
| 10 |
+
if sr != 16000:
|
| 11 |
+
data = librosa.resample(data, sr, 16000)
|
| 12 |
+
print(data.shape)
|
| 13 |
+
input_values = processor(data, return_tensors="pt").input_values
|
| 14 |
+
return input_values
|
| 15 |
|
| 16 |
+
def transcribe(file):
|
| 17 |
+
input_values = process_audio_file(file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
sequences = model.generate(input_values, num_beams=1, max_length=30)
|
| 20 |
|
| 21 |
+
transcription = processor.batch_decode(sequences)
|
| 22 |
+
return transcription[0]
|
| 23 |
|
| 24 |
+
iface = gr.Interface(
|
| 25 |
+
fn=speech_recognize,
|
| 26 |
+
inputs=gr.inputs.Audio(source="microphone", type='filepath'),
|
| 27 |
+
outputs="text",
|
| 28 |
+
)
|
| 29 |
+
iface.launch()
|