<jupyter_start><jupyter_text>Vakyansh + Hugging Face : Hindi Speech To text Demo  Install requirements<jupyter_code>%%capture

!apt-get -y install sox ffmpeg
!pip install transformers --upgrade 
!pip install ffmpeg-python sox

!wget https://raw.githubusercontent.com/harveenchadha/bol/main/demos/colab/record.py

!pip install pyctcdecode
!pip install https://github.com/kpu/kenlm/archive/master.zip<jupyter_output><empty_output><jupyter_text>Load Hindi Model<jupyter_code>import soundfile as sf
import torch
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor, Wav2Vec2ProcessorWithLM, AutoModelForCTC

def load_model():
    # load pretrained model
    processor_with_lm = Wav2Vec2ProcessorWithLM.from_pretrained("Harveenchadha/vakyansh-wav2vec2-hindi-him-4200")
    processor = Wav2Vec2Processor.from_pretrained("Harveenchadha/vakyansh-wav2vec2-hindi-him-4200")
    model = AutoModelForCTC.from_pretrained("Harveenchadha/vakyansh-wav2vec2-hindi-him-4200")
    return processor_with_lm, processor, model

processor_with_lm, processor, model = load_model()

def parse_transcription(wav_file, use_lm=False):
    # load audio    
    audio_input, sample_rate = sf.read(wav_file)

    # pad input values and return pt tensor
    input_values = processor(audio_input, sampling_rate=16_000, return_tensors="pt").input_values

    # INFERENCE
    # retrieve logits & take argmax
     #.cpu()
    #predicted_ids = torch.argmax(logits, dim=-1)

    # transcribe
    #transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)

    #transcription = processor.decode(logits)
    if use_lm: 
        with torch.no_grad():
            logits = model(input_values).logits.cpu().detach().numpy()[0]            
        transcription = processor_with_lm.decode(logits).text
        return transcription.replace('<s>','')
    else:
        logits = model(input_values).logits
        predicted_ids = torch.argmax(logits, dim=-1)
        transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
        
        return transcription<jupyter_output><empty_output><jupyter_text>Record file using colab<jupyter_code>from record import record_audio

record_audio('test')<jupyter_output><empty_output><jupyter_text>Run Model on recorded file<jupyter_code>output_with_lm = parse_transcription('test.wav', True)
output = parse_transcription('test.wav')
output, output_with_lm<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0997938144329897, 'fraction_numerical': 0.015670103092783504, 'mean_word_length': 5.0049504950495045, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '27147', 'license': 'MIT', 'n_tokens_mistral': 845, 'n_tokens_neox': 815, 'n_words': 136, 'path': 'demos/hf/hindi/hf_hindi_him_4200_with_lm_demo.ipynb', 'repo_name': 'harveenchadha/bol', 'size': '2425'}