Harveenchadha
commited on
Commit
•
c106aba
1
Parent(s):
7c840cd
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import soundfile as sf
|
2 |
+
import torch
|
3 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
|
4 |
+
import argparse
|
5 |
+
from glob import glob
|
6 |
+
import torchaudio
|
7 |
+
import subprocess
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
resampler = torchaudio.transforms.Resample(48_000, 16_000)
|
11 |
+
|
12 |
+
def get_filename(wav_file):
|
13 |
+
filename_local = wav_file.split('/')[-1][:-4]
|
14 |
+
filename_new = '/tmp/'+filename_local+'_16.wav'
|
15 |
+
|
16 |
+
|
17 |
+
subprocess.call(["sox {} -r {} -b 16 -c 1 {}".format(wav_file, str(16000), filename_new)], shell=True)
|
18 |
+
return filename_new
|
19 |
+
|
20 |
+
def parse_transcription(wav_file):
|
21 |
+
# load pretrained model
|
22 |
+
processor = Wav2Vec2Processor.from_pretrained("Harveenchadha/vakyansh-wav2vec2-hindi-him-4200")
|
23 |
+
model = Wav2Vec2ForCTC.from_pretrained("Harveenchadha/vakyansh-wav2vec2-hindi-him-4200")
|
24 |
+
|
25 |
+
# load audio
|
26 |
+
|
27 |
+
|
28 |
+
wav_file = get_filename(wav_file.name)
|
29 |
+
audio_input, sample_rate = sf.read(wav_file)
|
30 |
+
#test_file = resampler(test_file[0])
|
31 |
+
|
32 |
+
# pad input values and return pt tensor
|
33 |
+
input_values = processor(audio_input, sampling_rate=16_000, return_tensors="pt").input_values
|
34 |
+
|
35 |
+
# INFERENCE
|
36 |
+
# retrieve logits & take argmax
|
37 |
+
logits = model(input_values).logits
|
38 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
39 |
+
|
40 |
+
# transcribe
|
41 |
+
transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
|
42 |
+
return transcription
|
43 |
+
|
44 |
+
|
45 |
+
input = gr.inputs.Audio(source="microphone", type="file")
|
46 |
+
gr.Interface(parse_transcription, inputs = input, outputs="text",
|
47 |
+
analytics_enabled=False, show_tips=False).launch(inline=False);
|