Spaces:
Runtime error
Runtime error
File size: 9,227 Bytes
607348e 5c35238 607348e e8eafba 5c35238 e8eafba 5c35238 e8eafba 5c35238 e8eafba 2fcacda e8eafba 5c35238 e8eafba 5c35238 e8eafba 5c35238 e8eafba 5c35238 607348e 3008991 5c35238 607348e 5c35238 3008991 e8eafba 5c35238 530a947 3008991 15dadb3 3008991 5c35238 3008991 5c35238 3008991 5c35238 3008991 5c35238 ad6ff47 5c35238 ad6ff47 5c35238 ad6ff47 e4bd4d5 ad6ff47 5c35238 855340c e4bd4d5 5c35238 e4bd4d5 5c35238 e00f0bb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
import gradio as gr
import nltk
import librosa
from transformers import pipeline
from transformers.file_utils import cached_path, hf_bucket_url
import os, zipfile
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC, Wav2Vec2Tokenizer
from datasets import load_dataset
import torch
import kenlm
import torchaudio
from pyctcdecode import Alphabet, BeamSearchDecoderCTC, LanguageModel
"""Vietnamese speech2text"""
cache_dir = './cache/'
processor = Wav2Vec2Processor.from_pretrained("nguyenvulebinh/wav2vec2-base-vietnamese-250h", cache_dir=cache_dir)
vi_model = Wav2Vec2ForCTC.from_pretrained("nguyenvulebinh/wav2vec2-base-vietnamese-250h", cache_dir=cache_dir)
lm_file = hf_bucket_url("nguyenvulebinh/wav2vec2-base-vietnamese-250h", filename='vi_lm_4grams.bin.zip')
lm_file = cached_path(lm_file,cache_dir=cache_dir)
with zipfile.ZipFile(lm_file, 'r') as zip_ref:
zip_ref.extractall(cache_dir)
lm_file = cache_dir + 'vi_lm_4grams.bin'
def get_decoder_ngram_model(tokenizer, ngram_lm_path):
vocab_dict = tokenizer.get_vocab()
sort_vocab = sorted((value, key) for (key, value) in vocab_dict.items())
vocab = [x[1] for x in sort_vocab][:-2]
vocab_list = vocab
# convert ctc blank character representation
vocab_list[tokenizer.pad_token_id] = ""
# replace special characters
vocab_list[tokenizer.unk_token_id] = ""
# vocab_list[tokenizer.bos_token_id] = ""
# vocab_list[tokenizer.eos_token_id] = ""
# convert space character representation
vocab_list[tokenizer.word_delimiter_token_id] = " "
# specify ctc blank char index, since conventially it is the last entry of the logit matrix
alphabet = Alphabet.build_alphabet(vocab_list, ctc_token_idx=tokenizer.pad_token_id)
lm_model = kenlm.Model(ngram_lm_path)
decoder = BeamSearchDecoderCTC(alphabet,
language_model=LanguageModel(lm_model))
return decoder
ngram_lm_model = get_decoder_ngram_model(processor.tokenizer, lm_file)
# define function to read in sound file
def speech_file_to_array_fn(path, max_seconds=10):
batch = {"file": path}
speech_array, sampling_rate = torchaudio.load(batch["file"])
if sampling_rate != 16000:
transform = torchaudio.transforms.Resample(orig_freq=sampling_rate,
new_freq=16000)
speech_array = transform(speech_array)
speech_array = speech_array[0]
if max_seconds > 0:
speech_array = speech_array[:max_seconds*16000]
batch["speech"] = speech_array.numpy()
batch["sampling_rate"] = 16000
return batch
# tokenize
def speech2text_vi(audio):
# read in sound file
# load dummy dataset and read soundfiles
ds = speech_file_to_array_fn(audio.name)
# infer model
input_values = processor(
ds["speech"],
sampling_rate=ds["sampling_rate"],
return_tensors="pt"
).input_values
# decode ctc output
logits = vi_model(input_values).logits[0]
pred_ids = torch.argmax(logits, dim=-1)
greedy_search_output = processor.decode(pred_ids)
beam_search_output = ngram_lm_model.decode(logits.cpu().detach().numpy(), beam_width=500)
return beam_search_output
"""English speech2text"""
nltk.download("punkt")
# Loading the model and the tokenizer
model_name = "facebook/wav2vec2-base-960h"
eng_tokenizer = Wav2Vec2Tokenizer.from_pretrained(model_name)
eng_model = Wav2Vec2ForCTC.from_pretrained(model_name)
def load_data(input_file):
""" Function for resampling to ensure that the speech input is sampled at 16KHz.
"""
# read the file
speech, sample_rate = librosa.load(input_file)
# make it 1-D
if len(speech.shape) > 1:
speech = speech[:, 0] + speech[:, 1]
# Resampling at 16KHz since wav2vec2-base-960h is pretrained and fine-tuned on speech audio sampled at 16 KHz.
if sample_rate != 16000:
speech = librosa.resample(speech, sample_rate, 16000)
return speech
def correct_casing(input_sentence):
""" This function is for correcting the casing of the generated transcribed text
"""
sentences = nltk.sent_tokenize(input_sentence)
return (' '.join([s.replace(s[0], s[0].capitalize(), 1) for s in sentences]))
def speech2text_en(input_file):
"""This function generates transcripts for the provided audio input
"""
speech = load_data(input_file)
# Tokenize
input_values = eng_tokenizer(speech, return_tensors="pt").input_values
# Take logits
logits = eng_model(input_values).logits
# Take argmax
predicted_ids = torch.argmax(logits, dim=-1)
# Get the words from predicted word ids
transcription = eng_tokenizer.decode(predicted_ids[0])
# Output is all upper case
transcription = correct_casing(transcription.lower())
return transcription
"""Machine translation"""
vien_model_checkpoint = "datnth1709/finetuned_HelsinkiNLP-opus-mt-vi-en_PhoMT"
envi_model_checkpoint = "datnth1709/finetuned_HelsinkiNLP-opus-mt-en-vi_PhoMT"
vien_translator = pipeline("translation", model=vien_model_checkpoint)
envi_translator = pipeline("translation", model=envi_model_checkpoint)
def translate_vi2en(Vietnamese):
return vien_translator(Vietnamese)[0]['translation_text']
def translate_en2vi(English):
return envi_translator(English)[0]['translation_text']
""" Inference"""
def inference_vien(audio):
vi_text = speech2text_vi(audio)
en_text = translate_vi2en(vi_text)
return en_text
def inference_envi(audio):
en_text = speech2text_en(audio)
vi_text = translate_en2vi(en_text)
return vi_text
"""Gradio demo"""
vi_example_text = ["Có phải bạn đang muốn tìm mua nhà ở ngoại ô thành phố Hồ Chí Minh không?",
"Ánh mắt ta chạm nhau. Chỉ muốn ngắm anh lâu thật lâu.",
"Nếu như một câu nói có thể khiến em vui."]
vi_example_voice =[['vi_speech_01.wav'], ['vi_speech_02.wav'], ['vi_speech_03.wav']]
en_example_text = ["According to a study by Statista, the global AI market is set to grow up to 54 percent every single year.",
"As one of the world's greatest cities, Air New Zealand is proud to add the Big Apple to its list of 29 international destinations.",
"And yet, earlier this month, I found myself at Halloween Horror Nights at Universal Orlando Resort, one of the most popular Halloween events in the US among hardcore horror buffs."
]
en_example_voice =[['en_speech_01.wav'], ['en_speech_02.wav'], ['en_speech_03.wav']]
with gr.Blocks() as demo:
with gr.Tabs():
with gr.TabItem("Translation: Vietnamese to English"):
with gr.Row():
with gr.Column():
vietnamese_text = gr.Textbox(label="Vietnamese Text")
translate_button_vien_1 = gr.Button(value="Translate To English")
with gr.Column():
english_out_1 = gr.Textbox(label="English Text")
translate_button_vien_1.click(lambda text: translate_vi2en(text), inputs=vietnamese_text, outputs=english_out_1)
gr.Examples(examples=vi_example_text,
inputs=[vietnamese_text])
with gr.TabItem("Speech2text and Vi-En Translation"):
with gr.Row():
with gr.Column():
vi_audio = gr.Audio(source="microphone", label="Input Audio", type="file", streaming=False)
translate_button_vien_2 = gr.Button(value="Translate To English")
with gr.Column():
english_out_2 = gr.Textbox(label="English Text")
translate_button_vien_2.click(lambda vi_voice: inference_vien(vi_voice), inputs=vi_audio, outputs=english_out_2)
gr.Examples(examples=vi_example_voice,
inputs=[vi_audio])
with gr.Tabs():
with gr.TabItem("Translation: English to Vietnamese"):
with gr.Row():
with gr.Column():
english_text = gr.Textbox(label="English Text")
translate_button_envi_1 = gr.Button(value="Translate To Vietnamese")
with gr.Column():
vietnamese_out_1 = gr.Textbox(label="Vietnamese Text")
translate_button_envi_1.click(lambda text: translate_en2vi(text), inputs=english_text, outputs=vietnamese_out_1)
gr.Examples(examples=en_example_text,
inputs=[english_text])
with gr.TabItem("Speech2text and En-Vi Translation"):
with gr.Row():
with gr.Column():
en_audio = gr.Audio(source="microphone", label="Input Audio", type="filepath", streaming=False)
translate_button_envi_2 = gr.Button(value="Translate To Vietnamese")
with gr.Column():
vietnamese_out_2 = gr.Textbox(label="English Text")
translate_button_envi_2.click(lambda en_voice: inference_envi(en_voice), inputs=en_audio, outputs=vietnamese_out_2)
gr.Examples(examples=en_example_voice,
inputs=[en_audio])
if __name__ == "__main__":
demo.launch() |