Spaces:
Runtime error
Runtime error
app commit with wav2vec-base-960h
Browse files
app.py
CHANGED
|
@@ -1,7 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import soundfile as sf
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
| 4 |
+
from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
|
| 5 |
import gradio as gr
|
| 6 |
+
import sox
|
| 7 |
|
| 8 |
+
def convert(inputfile, outfile):
|
| 9 |
+
sox_tfm = sox.Transformer()
|
| 10 |
+
sox_tfm.set_output_format(
|
| 11 |
+
file_type="wav", channels=1, encoding="signed-integer", rate=16000, bits=16
|
| 12 |
+
)
|
| 13 |
+
sox_tfm.build(inputfile, outfile)
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
|
| 18 |
+
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
|
| 19 |
|
| 20 |
+
def parse_transcription(wav_file):
|
| 21 |
+
filename = wav_file.name.split('.')[0]
|
| 22 |
+
convert(wav_file.name, filename + "16k.wav")
|
| 23 |
+
speech, _ = sf.read(filename + "16k.wav")
|
| 24 |
+
input_values = processor(speech, sampling_rate=16_000, return_tensors="pt").input_values
|
| 25 |
+
logits = model(input_values).logits
|
| 26 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
| 27 |
+
transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
|
| 28 |
+
return transcription,
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
output1 = gr.outputs.Textbox(label="Transcription in English: ")
|
| 33 |
+
output2 = gr.outputs.Textbox(label="Validated Transcription in English")
|
| 34 |
+
|
| 35 |
+
input_ = gr.inputs.Audio(source="microphone", type="file")
|
| 36 |
+
#gr.Interface(parse_transcription, inputs = input_, outputs="text",
|
| 37 |
+
# analytics_enabled=False, show_tips=False, enable_queue=True).launch(inline=False);
|
| 38 |
+
|
| 39 |
+
gr.Interface(parse_transcription, inputs = input_, outputs=[output1, output2], analytics_enabled=False,
|
| 40 |
+
show_tips=False,
|
| 41 |
+
theme='huggingface',
|
| 42 |
+
layout='vertical',
|
| 43 |
+
title="Piecurus Test on Speech Transcription",
|
| 44 |
+
description="This is a live demo for Speech to Text Translation. Models used: facebook/wav2vec2-base-960h", enable_queue=True).launch( inline=False)
|