Spaces:
Runtime error
Runtime error
Commit
·
c71fd53
1
Parent(s):
4ea84df
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#Importing all the necessary packages
|
| 2 |
+
import torch, librosa, torchaudio
|
| 3 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
| 4 |
+
from pyctcdecode import build_ctcdecoder
|
| 5 |
+
|
| 6 |
+
# Define ASR MODEL
|
| 7 |
+
class Speech2Text:
|
| 8 |
+
def __init__(self):
|
| 9 |
+
self.vocab = list(processor.tokenizer.get_vocab().keys())
|
| 10 |
+
self.decoder = build_ctcdecoder(self.vocab, kenlm_model_path=None)
|
| 11 |
+
|
| 12 |
+
def wav2feature(self, path):
|
| 13 |
+
speech_array, sampling_rate = torchaudio.load(path)
|
| 14 |
+
speech_array = librosa.resample(speech_array.squeeze().numpy(),
|
| 15 |
+
sampling_rate, processor.feature_extractor.sampling_rate)
|
| 16 |
+
return processor(speech_array, return_tensors="pt",
|
| 17 |
+
sampling_rate=processor.feature_extractor.sampling_rate)
|
| 18 |
+
|
| 19 |
+
def feature2logits(self, features):
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
return model(features.input_values.to(device),
|
| 22 |
+
attention_mask=features.attention_mask.to(device)).logits.numpy()[0]
|
| 23 |
+
|
| 24 |
+
def __call__(self, path):
|
| 25 |
+
logits = self.feature2logits(self.wav2feature(path))
|
| 26 |
+
return self.decoder.decode(logits)
|
| 27 |
+
|
| 28 |
+
#Loading the model and the tokenizer
|
| 29 |
+
model_name = 'masoudmzb/wav2vec2-xlsr-multilingual-53-fa'
|
| 30 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 31 |
+
wav2vec_model = Wav2Vec2ForCTC.from_pretrained(model_name).to(device).eval()
|
| 32 |
+
processor = Wav2Vec2Processor.from_pretrained(model_name)
|
| 33 |
+
s2t = Speech2Text()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
gr.Interface(s2t,
|
| 38 |
+
inputs = gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Record Your Beautiful Persian Voice"),
|
| 39 |
+
outputs = gr.outputs.Textbox(label="Output Text"),
|
| 40 |
+
title="Persian ASR using Wav2Vec 2.0",
|
| 41 |
+
description = "This application displays transcribed text for given audio input",
|
| 42 |
+
examples = [["Test_File1.wav"]], theme="grass").launch()
|