Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,53 +1,58 @@
|
|
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
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
16 |
|
17 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
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="filepath", source = "microphone"),
|
52 |
-
description=description, article = article, outputs = "text").launch(inline = False)
|
53 |
|
|
|
|
|
|
|
|
1 |
import soundfile as sf
|
2 |
import torch
|
3 |
+
from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor,Wav2Vec2ProcessorWithLM
|
|
|
|
|
|
|
|
|
4 |
import gradio as gr
|
5 |
+
import sox
|
6 |
+
import subprocess
|
7 |
|
|
|
8 |
|
9 |
+
def read_file_and_process(wav_file):
|
10 |
+
filename = wav_file.split('.')[0]
|
11 |
+
filename_16k = filename + "16k.wav"
|
12 |
+
resampler(wav_file, filename_16k)
|
13 |
+
speech, _ = sf.read(filename_16k)
|
14 |
+
inputs = processor(speech, sampling_rate=16_000, return_tensors="pt", padding=True)
|
15 |
|
16 |
+
return inputs
|
|
|
17 |
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
def resampler(input_file_path, output_file_path):
|
20 |
+
command = (
|
21 |
+
f"ffmpeg -hide_banner -loglevel panic -i {input_file_path} -ar 16000 -ac 1 -bits_per_raw_sample 16 -vn "
|
22 |
+
f"{output_file_path}"
|
23 |
+
)
|
24 |
+
subprocess.call(command, shell=True)
|
25 |
|
|
|
|
|
|
|
|
|
26 |
|
|
|
|
|
27 |
|
28 |
+
def parse_transcription(logits):
|
|
|
|
|
29 |
predicted_ids = torch.argmax(logits, dim=-1)
|
|
|
|
|
30 |
transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
|
31 |
return transcription
|
32 |
|
33 |
+
def parse(wav_file):
|
34 |
+
input_values = read_file_and_process(wav_file)
|
35 |
+
with torch.no_grad():
|
36 |
+
logits = model(**input_values).logits
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
model_id = "Harveenchadha/vakyansh-wav2vec2-hindi-him-4200"
|
41 |
+
processor = Wav2Vec2Processor.from_pretrained(model_id)
|
42 |
+
processor_with_LM = Wav2Vec2ProcessorWithLM.from_pretrained(model_id)
|
43 |
+
model = Wav2Vec2ForCTC.from_pretrained(model_id)
|
44 |
+
|
45 |
+
|
46 |
+
input_ = gr.Audio(source="microphone", type="filepath")
|
47 |
+
txtbox = gr.Textbox(
|
48 |
+
label="Hindi text output:",
|
49 |
+
lines=5
|
50 |
+
)
|
51 |
|
52 |
title = "Speech-to-Text (Hindi) using Vakyansh"
|
53 |
description = "Upload a hindi audio clip, and let AI do the hard work of transcribing."
|
54 |
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>"
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
+
gr.Interface(parse, inputs = input_, outputs=txtbox, title=title, description=description, article = article,
|
57 |
+
streaming=True, interactive=True,
|
58 |
+
analytics_enabled=False, show_tips=False, enable_queue=True).launch(inline=False);
|