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