Skip to content

Instantly share code, notes, and snippets.

@manmohan24nov
Created April 4, 2021 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manmohan24nov/9de2628a9d3b99ff1243222d06e755ca to your computer and use it in GitHub Desktop.
Save manmohan24nov/9de2628a9d3b99ff1243222d06e755ca to your computer and use it in GitHub Desktop.
# A python package for music and audio analysis.
# https://librosa.org/doc/latest/index.html
import librosa
import torch
from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
# load model and tokenizer
tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h")
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
# The base model pretrained and fine-tuned on 960 hours of Librispeech on 16kHz sampled speech audio.
# When using the model make sure that your speech input is also sampled at 16Khz.
# load any audio file of your choice
collection_of_text = []
for i in range(4):
speech, rate = librosa.load(f"audio_processing//{i+1}_audi_file.wav", sr=16000)
input_values = tokenizer(speech, return_tensors='pt').input_values
# Store logits (non-normalized predictions)
with torch.no_grad():
logits = model(input_values).logits
# Store predicted id's
predicted_ids = torch.argmax(logits, dim=-1)
# decode the audio to generate text
# Passing the prediction to the tokenzer decode to get the transcription
transcription = tokenizer.batch_decode(predicted_ids)[0]
# transcriptions = tokenizer.decode(predicted_ids[0])
print(transcription)
collection_of_text.append(transcription)
print(collection_of_text)
final_complete_speech = ""
# convert batch of text into one complete sentence
for i in collection_of_text:
final_complete_speech += i
print(final_complete_speech)
# Sample output of 1st audio file
ON BEHALF OF THE GREAT STATE OF ILLINOIS TRO TO THE MATON LAND OF LINKON LET ME EXPRESS MY DEEPEST GRATITUDE FOR
THE PRIVILEGE OF ADDRESSING THIS CONVENTION TO NIGHT IS A PARTICULAR HONOR FOR ME BECAUSE LET'S FACEIT MY PRESENCE
ON THIS STAGE IS PRETTY UNLIKLEMY FATHER WAS A FOREIGN STUDE BORN AND RAISED IN A SMALL BILLAGE IN CANION HE GREW
UP HURTING GOS WENT TO SCHOOL IN A TINROOF SHACK HIS FATHER MY GRANDFATHER WAS A COOK A DOMESTIC SERVANT TO THE
BRITISEH BUT MY GRANDFATHER HAD LARGER DREAMS FOR HIS SON THROUGH HARD WORK AND PERSERBERANCE MY FATHER GOT
A SCHOLARSHIP TO STUDY IN A MAGICAL PLACE AMERICA THAT SHOWN IS A BEAKIN A FREEDOM AND OPPORTUNITY TO SO MANY
WHO HAD COME TE CO O TUDYIN HERE MY FATHER ME MY MO SHE WAS BORN IN A TOWN ON THE OTHER SIDE OF THE WORLD IN CANSAS
HER FATHER WORKD ON OIL RIGS AND FARMS THROUGH MOST OF THE DEPRESSION THE DAY AFTER PEARL HARBOUR MY GRANDFATHER
SIGNED UP FOR DUTEE JOING PATEN'S ARMY MARCHED ACROSS EUROPE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment