import gradio as gr import torch import time import librosa import soundfile import nemo.collections.asr as nemo_asr import tempfile import os import uuid SAMPLE_RATE = 16000 model = nemo_asr.models.EncDecRNNTBPEModel.from_pretrained("nvidia/stt_en_conformer_transducer_xlarge") model.change_decoding_strategy(None) model.eval() def process_audio_file(file): data, sr = librosa.load(file) if sr != SAMPLE_RATE: data = librosa.resample(data, orig_sr=sr, target_sr=SAMPLE_RATE) # monochannel data = librosa.to_mono(data) return data def transcribe(audio, state=""): # Grant additional context # time.sleep(1) if state is None: state = "" audio_data = process_audio_file(audio) with tempfile.TemporaryDirectory() as tmpdir: # Filepath transcribe audio_path = os.path.join(tmpdir, f'audio_{uuid.uuid4()}.wav') soundfile.write(audio_path, audio_data, SAMPLE_RATE) transcriptions = model.transcribe([audio_path]) # Direct transcribe # transcriptions = model.transcribe([audio]) # if transcriptions form a tuple (from RNNT), extract just "best" hypothesis if type(transcriptions) == tuple and len(transcriptions) == 2: transcriptions = transcriptions[0] transcriptions = transcriptions[0] state = state + transcriptions + " " return state, state iface = gr.Interface( fn=transcribe, inputs=[ gr.Audio(source="microphone", type='filepath', streaming=True), "state", ], outputs=[ "textbox", "state", ], layout="horizontal", theme="huggingface", title="NeMo Streaming Conformer Transducer Large - English", description="Demo for English speech recognition using Conformer Transducers", allow_flagging='never', live=True, ) iface.launch()