Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
title = "Speech-to-Text (Hindi) using Vakyansh"
|
46 |
+
description = "Upload a hindi audio clip, and let AI do the hard work of transcribing."
|
47 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2104.06678'>Large-Scale Self- and Semi-Supervised Learning for Speech Translation</a></p>"
|
48 |
+
gr.Interface(
|
49 |
+
parse_transcription,
|
50 |
+
title=title,
|
51 |
+
inputs=gr.inputs.Audio(label="Record Audio File", type="file", source = "microphone"),
|
52 |
+
description=description, article = article, outputs = "text").launch(inline = False)
|
53 |
+
|