Upload app_test.py
Browse files- app_test.py +38 -0
app_test.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import soundfile as sf
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import sox
|
| 6 |
+
import os
|
| 7 |
+
def convert(inputfile, outfile):
|
| 8 |
+
sox_tfm = sox.Transformer()
|
| 9 |
+
sox_tfm.set_output_format(
|
| 10 |
+
file_type="wav", channels=1, encoding="signed-integer", rate=16000, bits=16
|
| 11 |
+
)
|
| 12 |
+
sox_tfm.build(inputfile, outfile)
|
| 13 |
+
api_token = os.getenv("API_TOKEN")
|
| 14 |
+
model_name = "new_iloko"
|
| 15 |
+
processor = Wav2Vec2Processor.from_pretrained(model_name, use_auth_token=api_token)
|
| 16 |
+
model = Wav2Vec2ForCTC.from_pretrained(model_name, use_auth_token=api_token)
|
| 17 |
+
def parse_transcription(wav_file):
|
| 18 |
+
filename = wav_file.name.split('.')[0]
|
| 19 |
+
convert(wav_file.name, filename + "16k.wav")
|
| 20 |
+
speech, _ = sf.read(filename + "16k.wav")
|
| 21 |
+
input_values = processor(speech, sampling_rate=16_000, return_tensors="pt").input_values
|
| 22 |
+
logits = model(input_values).logits
|
| 23 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
| 24 |
+
transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
|
| 25 |
+
return transcription
|
| 26 |
+
output = gr.outputs.Textbox(label="The transcript")
|
| 27 |
+
input_ = gr.inputs.Audio(source="microphone", type="file")
|
| 28 |
+
gr.Interface(parse_transcription, inputs=input_, outputs=[output],
|
| 29 |
+
analytics_enabled=False,
|
| 30 |
+
show_tips=False,
|
| 31 |
+
theme='huggingface',
|
| 32 |
+
layout='vertical',
|
| 33 |
+
title="Multilingual Speech Recognition for Indonesian Languages",
|
| 34 |
+
description="Speech Recognition Live Demo for Indonesian, Javanese and Sundanese Language",
|
| 35 |
+
article="This demo was built for the project Multilingual Speech Recognition for Indonesian Languages: https://github.com/indonesian-nlp/multilingual-asr . "
|
| 36 |
+
"It uses the Wav2Vec2 large model https://huggingface.co/indonesian-nlp/wav2vec2-indonesian-javanese-sundanese "
|
| 37 |
+
"which was fine-tuned on Indonesian Common Voice, Javanese and Sundanese OpenSLR speech datasets.",
|
| 38 |
+
enable_queue=True).launch( inline=False)
|