Spaces:
Runtime error
Runtime error
Vaibhav Srivastav
commited on
Commit
Β·
3b8d409
1
Parent(s):
d32240b
removing unused code
Browse files
app.py
CHANGED
|
@@ -12,10 +12,10 @@ model_name = "facebook/wav2vec2-base-960h"
|
|
| 12 |
processor = Wav2Vec2Processor.from_pretrained(model_name)
|
| 13 |
model = Wav2Vec2ForCTC.from_pretrained(model_name)
|
| 14 |
|
| 15 |
-
def
|
| 16 |
#read the file
|
| 17 |
speech, sample_rate = librosa.load(input_file)
|
| 18 |
-
#make it
|
| 19 |
if len(speech.shape) > 1:
|
| 20 |
speech = speech[:,0] + speech[:,1]
|
| 21 |
#resampling to 16KHz
|
|
@@ -29,26 +29,23 @@ def fix_transcription_casing(input_sentence):
|
|
| 29 |
return (' '.join([s.replace(s[0],s[0].capitalize(),1) for s in sentences]))
|
| 30 |
|
| 31 |
def predict_and_decode(input_file):
|
| 32 |
-
speech =
|
| 33 |
-
|
| 34 |
input_values = processor(speech, return_tensors="pt", sampling_rate=16000).input_values
|
| 35 |
logits = model(input_values).logits.cpu().detach().numpy()[0]
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
# predicted_ids = torch.argmax(logits, dim=-1)
|
| 39 |
-
# #Get the words from predicted word ids
|
| 40 |
-
# transcription = tokenizer.decode(predicted_ids[0])
|
| 41 |
decoder = build_ctcdecoder(vocab_list)
|
| 42 |
pred = decoder.decode(logits)
|
| 43 |
|
| 44 |
-
#Output is all upper case
|
| 45 |
transcribed_text = fix_transcription_casing(pred.lower())
|
|
|
|
| 46 |
return transcribed_text
|
| 47 |
|
| 48 |
gr.Interface(predict_and_decode,
|
| 49 |
-
inputs = gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="
|
| 50 |
outputs = gr.outputs.Textbox(label="Output Text"),
|
| 51 |
title="ASR using Wav2Vec 2.0 & pyctcdecode",
|
| 52 |
-
description = "
|
| 53 |
layout = "horizontal",
|
| 54 |
examples = [["test.wav"]], theme="huggingface").launch()
|
|
|
|
| 12 |
processor = Wav2Vec2Processor.from_pretrained(model_name)
|
| 13 |
model = Wav2Vec2ForCTC.from_pretrained(model_name)
|
| 14 |
|
| 15 |
+
def load_and_fix_data(input_file):
|
| 16 |
#read the file
|
| 17 |
speech, sample_rate = librosa.load(input_file)
|
| 18 |
+
#make it 1D
|
| 19 |
if len(speech.shape) > 1:
|
| 20 |
speech = speech[:,0] + speech[:,1]
|
| 21 |
#resampling to 16KHz
|
|
|
|
| 29 |
return (' '.join([s.replace(s[0],s[0].capitalize(),1) for s in sentences]))
|
| 30 |
|
| 31 |
def predict_and_decode(input_file):
|
| 32 |
+
speech = load_and_fix_data(input_file)
|
| 33 |
+
|
| 34 |
input_values = processor(speech, return_tensors="pt", sampling_rate=16000).input_values
|
| 35 |
logits = model(input_values).logits.cpu().detach().numpy()[0]
|
| 36 |
+
|
| 37 |
+
vocab_list = list(processor.tokenizer.get_vocab().keys())
|
|
|
|
|
|
|
|
|
|
| 38 |
decoder = build_ctcdecoder(vocab_list)
|
| 39 |
pred = decoder.decode(logits)
|
| 40 |
|
|
|
|
| 41 |
transcribed_text = fix_transcription_casing(pred.lower())
|
| 42 |
+
|
| 43 |
return transcribed_text
|
| 44 |
|
| 45 |
gr.Interface(predict_and_decode,
|
| 46 |
+
inputs = gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Record/ Drop audio"),
|
| 47 |
outputs = gr.outputs.Textbox(label="Output Text"),
|
| 48 |
title="ASR using Wav2Vec 2.0 & pyctcdecode",
|
| 49 |
+
description = "Extending HF ASR models with pyctcdecode decoder",
|
| 50 |
layout = "horizontal",
|
| 51 |
examples = [["test.wav"]], theme="huggingface").launch()
|