Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445#Importing all the necessary packagesimport nltkimport librosaimport torchimport gradio as grfrom transformers import Wav2Vec2Tokenizer, Wav2Vec2ForCTCnltk.download("punkt")#Loading the pre-trained model and the tokenizermodel_name = "facebook/wav2vec2-base-960h"tokenizer = Wav2Vec2Tokenizer.from_pretrained(model_name)model = Wav2Vec2ForCTC.from_pretrained(model_name)def load_data(input_file):#reading the filespeech, sample_rate = librosa.load(input_file)#make it 1-Dif len(speech.shape) > 1:speech = speech[:,0] + speech[:,1]#Resampling the audio at 16KHzif sample_rate !=16000:speech = librosa.resample(speech, sample_rate,16000)return speechdef correct_casing(input_sentence):sentences = nltk.sent_tokenize(input_sentence)return (' '.join([s.replace(s[0],s[0].capitalize(),1) for s in sentences]))def asr_transcript(input_file):speech = load_data(input_file)#Tokenizeinput_values = tokenizer(speech, return_tensors="pt").input_values#Take logitslogits = model(input_values).logits#Take argmaxpredicted_ids = torch.argmax(logits, dim=-1)#Get the words from predicted word idstranscription = tokenizer.decode(predicted_ids[0])#Correcting the letter casingtranscription = correct_casing(transcription.lower())return transcription
app.py+45