Spaces:
Build error
Build error
File size: 1,956 Bytes
4671727 3313875 4671727 ac68576 df83202 ac68576 4671727 8f16942 1d6e986 ac68576 |
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 |
import gradio as gr
import librosa
from optimum.onnxruntime import ORTModelForSeq2SeqLM
from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch
# load model and processor
processor = Wav2Vec2Processor.from_pretrained("jonatasgrosman/wav2vec2-large-xlsr-53-english")
model = Wav2Vec2ForCTC.from_pretrained("jonatasgrosman/wav2vec2-large-xlsr-53-english")
tokenizer = AutoTokenizer.from_pretrained("icon-it-tdtu/mt-en-vi-optimum")
model_lm = ORTModelForSeq2SeqLM.from_pretrained("icon-it-tdtu/mt-en-vi-optimum")
def process_audio_file(file):
data, sr = librosa.load(file)
if sr != 16000:
data = librosa.resample(data, sr, 16000)
inputs = processor(data, sampling_rate=16000, return_tensors="pt", padding=True)
return inputs
def transcribe(file, state=""):
inputs = process_audio_file(file)
with torch.no_grad():
output_logit = model(inputs.input_values).logits
pred_ids = torch.argmax(output_logit, dim=-1)
text = processor.batch_decode(pred_ids)[0].lower()
print(text)
text = translate(text)
state += text + " "
return state, state
def translate(text):
batch = tokenizer([text], return_tensors="pt")
generated_ids = model_lm.generate(**batch)
translated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
return translated_text
# Set the starting state to an empty string
iface = gr.Interface(
fn=transcribe,
title="Streaming interpret English to Vietnamese",
description="A simple interface to streaming interpret from spoken English to Vietnamese.",
article="Author: <a href=\"https://huggingface.co./vumichien\">Vu Minh Chien</a>.",
inputs=[
gr.Audio(source="microphone", type="filepath", streaming=True),
"state"
],
outputs=[
"textbox",
"state"
])
iface.launch(enable_queue=True, debug=True) |