essogbe commited on
Commit
760940d
1 Parent(s): 4383c1b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import nltk
2
+ import librosa
3
+ import torch
4
+ import gradio as gr
5
+ from transformers import Wav2Vec2Tokenizer, Wav2Vec2ForCTC
6
+ nltk.download("punkt")
7
+
8
+ model_name = "speechbrain/asr-wav2vec2-dvoice-fongbe"
9
+ tokenizer = Wav2Vec2Tokenizer.from_pretrained(model_name)
10
+ model = Wav2Vec2ForCTC.from_pretrained(model_name)
11
+
12
+ def load_data(input_file):
13
+
14
+ #reading the file
15
+ speech, sample_rate = librosa.load(input_file)
16
+ #make it 1-D
17
+ if len(speech.shape) > 1:
18
+ speech = speech[:,0] + speech[:,1]
19
+ #Resampling the audio at 16KHz
20
+ if sample_rate !=16000:
21
+ speech = librosa.resample(speech, sample_rate,16000)
22
+ return speech
23
+
24
+ def correct_casing(input_sentence):
25
+
26
+ sentences = nltk.sent_tokenize(input_sentence)
27
+ return (' '.join([s.replace(s[0],s[0].capitalize(),1) for s in sentences]))
28
+
29
+
30
+ def asr_transcript(input_file):
31
+
32
+ speech = load_data(input_file)
33
+ #Tokenize
34
+ input_values = tokenizer(speech, return_tensors="pt").input_values
35
+ #Take logits
36
+ logits = model(input_values).logits
37
+ #Take argmax
38
+ predicted_ids = torch.argmax(logits, dim=-1)
39
+ #Get the words from predicted word ids
40
+ transcription = tokenizer.decode(predicted_ids[0])
41
+ #Correcting the letter casing
42
+ transcription = correct_casing(transcription.lower())
43
+ return transcription
44
+
45
+ gr.Interface(asr_transcript,
46
+ inputs = gr.inputs.Audio(source="microphone", type="filepath", optional=True, label="Speaker"),
47
+ outputs = gr.outputs.Textbox(label="Output Text"),
48
+ title="ASR using Wav2Vec 2.0",
49
+ description = "This application displays transcribed text for given audio input",
50
+ examples = [["output2-1.wav"]]], theme="grass").launch()