CoolFace
Apppublic

wasertech/French_Wav2Vec2_ASR

sourceHugging Faceupdated 3y agoView on Hugging Face
1likes
app.py68 linesDownload Raw Back to root
1#References: 1. https://www.kdnuggets.com/2021/03/speech-text-wav2vec.html 2            #2. https://www.youtube.com/watch?v=4CoVcsxZphE 3            #3. https://www.analyticsvidhya.com/blog/2021/02/hugging-face-introduces-the-first-automatic-speech-recognition-model-wav2vec2/ 4 5#Importing all the necessary packages6import nltk7import librosa8import torch9import gradio as gr10from transformers import Wav2Vec2Tokenizer, Wav2Vec2ForCTC11nltk.download("punkt")12 13#Loading the model and the tokenizer14model_name = "bofenghuang/asr-wav2vec2-ctc-french" #"wasertech/wav2vec2-cv-fr-9"15tokenizer = Wav2Vec2Tokenizer.from_pretrained(model_name)16model = Wav2Vec2ForCTC.from_pretrained(model_name)17 18 19def load_data(input_file):20  21  """ Function for resampling to ensure that the speech input is sampled at 16KHz.22  """23  #read the file24  speech, sample_rate = librosa.load(input_file)25  #make it 1-D26  if len(speech.shape) > 1: 27      speech = speech[:,0] + speech[:,1]28  #Resampling at 16KHz since wav2vec2-base-960h is pretrained and fine-tuned on speech audio sampled at 16 KHz.29  if sample_rate !=16000:30    speech = librosa.resample(speech, orig_sr=sample_rate, target_sr=16000)31  return speech32  33  34 35def correct_casing(input_sentence):36  """ This function is for correcting the casing of the generated transcribed text37  """38  sentences = nltk.sent_tokenize(input_sentence)39  return (' '.join([s.replace(s[0],s[0].capitalize(),1) for s in sentences]))40  41 42 43def asr_transcript(input_file):44  """This function generates transcripts for the provided audio input45  """46  speech = load_data(input_file)47  48  #Tokenize49  input_values = tokenizer(speech, return_tensors="pt").input_values50  #Take logits51  logits = model(input_values).logits52  #Take argmax53  predicted_ids = torch.argmax(logits, dim=-1)54  #Get the words from predicted word ids55  transcription = tokenizer.decode(predicted_ids[0])56  #Output is all upper case57  transcription = correct_casing(transcription.lower())58  return transcription59  60 61gr.Interface(asr_transcript,62             inputs = gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Démarrer l'enregistrement"),63             outputs = gr.outputs.Textbox(label="Transcription"),64             title="🎙️ Parlez, on vous écoute !",65             description = "Enregistrez un audio ou utilisez les examples pour interagir avec notre dernier modèle.",66             examples = [["wav/1.wav"], ["wav/2.wav"], ["wav/3.wav"], ["wav/4.wav"], ["wav/5.wav"]], theme="grass").launch()67 68