<jupyter_start><jupyter_text>**MiniASR Tutorial: Recognition**This is a tutorial for recognizing speech with a pre-trained end-to-end automatic speech recognition model using the toolkit [MiniASR](https://github.com/vectominist/MiniASR).  **Installation & Preprocessing**<jupyter_code>! sudo apt-get install sox 
! pip3 install fairseq@git+https://github.com//pytorch/fairseq.git@f2146bdc7abf293186de9449bfa2272775e39e1d#egg=fairseq

! git clone https://github.com/vectominist/MiniASR.git
% cd MiniASR
! pip3 install -e ./
% cd ..
! mkdir -p data
% cd data
! wget https://www.openslr.org/resources/12/dev-clean.tar.gz
! tar zxf dev-clean.tar.gz
! rm dev-clean.tar.gz
% cd ..
! miniasr-preprocess \
    -c LibriSpeech \
    -p data/LibriSpeech \
    -s dev-clean \
    -o data/libri_dev
! wget -O ctc_eng.ckpt https://www.dropbox.com/s/1k3mpngqpinihlo/ctc_ls960_hubert_base_char.ckpt?dl=0
% cd MiniASR/<jupyter_output>/content/MiniASR<jupyter_text>**Load Model**<jupyter_code>from miniasr.utils import load_from_checkpoint, sequence_distance
from miniasr.data.audio import load_waveform

model, args, tokenizer = load_from_checkpoint('../ctc_eng.ckpt', 'cuda')
model.eval()<jupyter_output><empty_output><jupyter_text>**Recognize a Sample**<jupyter_code>waves = [load_waveform('../data/LibriSpeech/dev-clean/6345/93302/6345-93302-0025.flac').to('cuda')]
hyps = model.recognize(waves)
print(hyps[0])
ref = 'ARE YOU REALLY GOING TO THROW ME OVER FOR A THING LIKE THIS'
res_cer = sequence_distance(ref, hyps[0], mode='char')
res_wer = sequence_distance(ref, hyps[0], mode='word')
print('CER = {:.2f}%'.format(100. * res_cer['distance'] / res_cer['length']))
print('WER = {:.2f}%'.format(100. * res_wer['distance'] / res_wer['length']))<jupyter_output>ARE YOU REALLY GOING TO THROW ME OVER FOR A THING LIKE THIS
CER = 0.00%
WER = 0.00%<jupyter_text>**Record Your Own Voice**<jupyter_code># from https://gist.github.com/korakot/c21c3476c024ad6d56d5f48b0bca92be

from IPython.display import Javascript
from google.colab import output
from base64 import b64decode

RECORD = """
const sleep = time => new Promise(resolve => setTimeout(resolve, time))
const b2text = blob => new Promise(resolve => {
  const reader = new FileReader()
  reader.onloadend = e => resolve(e.srcElement.result)
  reader.readAsDataURL(blob)
})
var record = time => new Promise(async resolve => {
  stream = await navigator.mediaDevices.getUserMedia({ audio: true })
  recorder = new MediaRecorder(stream)
  chunks = []
  recorder.ondataavailable = e => chunks.push(e.data)
  recorder.start()
  await sleep(time)
  recorder.onstop = async ()=>{
    blob = new Blob(chunks)
    text = await b2text(blob)
    resolve(text)
  }
  recorder.stop()
})
"""

def record(sec, filename='audio.wav'):
  display(Javascript(RECORD))
  s = output.eval_js('record(%d)' % (sec * 1000))
  b = b64decode(s.split(',')[1])
  with open(filename, 'wb+') as f:
    f.write(b)

audio = 'audio.wav'
second = 5
print(f"Speak to your microphone {second} sec...")
record(second, audio)
print("Done!")


import librosa
import librosa.display
speech, rate = librosa.load(audio, sr=16000)
librosa.display.waveplot(speech, sr=rate)

import matplotlib.pyplot as plt
plt.show()

import pysndfile
pysndfile.sndio.write('audio_ds.wav', speech, rate=rate, format='wav', enc='pcm16')

from IPython.display import display, Audio
display(Audio(speech, rate=rate))
waves = [load_waveform('audio_ds.wav').to('cuda')]
hyps = model.recognize(waves)
print(hyps[0])<jupyter_output>I LIKED MATH UNTIL THEY ADDED THE ALPHABET IN IT
{'fraction_non_alphanumeric': 0.14684329199549043, 'fraction_numerical': 0.032130777903043965, 'mean_word_length': 5.193717277486911, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 23, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '631281', 'license': 'MIT', 'n_tokens_mistral': 1424, 'n_tokens_neox': 1340, 'n_words': 275, 'path': 'example/recognition.ipynb', 'repo_name': 'shinyuchen/MiniASR', 'size': '3548'}