Spaces:
Running
Running
File size: 1,926 Bytes
a357a65 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# -*- coding: utf-8 -*-
"""Untitled29.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Lv3LjRH9bHwMhKsWvFcELMzKqmXd9UIb
"""
!pip install -q transformers
!pip install -q gradio
import nltk
import librosa
import torch
import soundfile as sf
import gradio as gr
from transformers import Wav2Vec2Tokenizer, Wav2Vec2ForCTC
nltk.download("punkt")
input_file = "/content/drive/MyDrive/AAAAUDIO/My Audio.wav"
tokenizer = Wav2Vec2Tokenizer.from_pretrained("facebook/wav2vec2-base-960h")
model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
def load_data(input_file):
""" Function for resampling to ensure that the speech input is sampled at 16KHz.
"""
#read the file
speech, sample_rate = sf.read(input_file)
#make it 1-D
if len(speech.shape) > 1:
speech = speech[:,0] + speech[:,1]
#Resampling at 16KHz since wav2vec2-base-960h is pretrained and fine-tuned on speech audio sampled at 16 KHz.
if sample_rate !=16000:
speech = librosa.resample(speech, sample_rate,16000)
return speech
def asr_transcript(input_file):
speech = load_data(input_file)
#Tokenize
input_values = tokenizer(speech, return_tensors="pt").input_values
#Take logits
logits = model(input_values).logits
#Take argmax
predicted_ids = torch.argmax(logits, dim=-1)
#Get the words from predicted word ids
transcription = tokenizer.decode(predicted_ids[0])
#Output is all upper case
transcription = correct_casing(transcription.lower())
return transcription
gr.Interface(asr_transcript,
inputs = gr.inputs.Audio(label = "Input Audio", type= "file"),
outputs = gr.outputs.Textbox(label="Output Text"),
title="Real-time ASR using Wav2Vec 2.0",
description = "asdfghnjmk",
examples = [["/content/drive/MyDrive/AAAAUDIO/My Audio.wav"]]).launch()
|