Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import os
|
8 |
+
|
9 |
+
|
10 |
+
def read_file_and_process(wav_file):
|
11 |
+
filename = wav_file.split('.')[0]
|
12 |
+
filename_16k = filename + "16k.wav"
|
13 |
+
resampler(wav_file, filename_16k)
|
14 |
+
speech, _ = sf.read(filename_16k)
|
15 |
+
inputs = processor(speech, sampling_rate=16_000, return_tensors="pt", padding=True)
|
16 |
+
|
17 |
+
return inputs
|
18 |
+
|
19 |
+
|
20 |
+
def resampler(input_file_path, output_file_path):
|
21 |
+
command = (
|
22 |
+
f"ffmpeg -hide_banner -loglevel panic -i {input_file_path} -ar 16000 -ac 1 -bits_per_raw_sample 16 -vn "
|
23 |
+
f"{output_file_path}"
|
24 |
+
)
|
25 |
+
subprocess.call(command, shell=True)
|
26 |
+
|
27 |
+
|
28 |
+
def parse_transcription_with_lm(logits):
|
29 |
+
result = processor_with_LM.batch_decode(logits.cpu().numpy())
|
30 |
+
text = result.text
|
31 |
+
transcription = text[0].replace('<s>','')
|
32 |
+
return transcription
|
33 |
+
|
34 |
+
def parse_transcription(logits):
|
35 |
+
predicted_ids = torch.argmax(logits, dim=-1)
|
36 |
+
transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
|
37 |
+
return transcription
|
38 |
+
|
39 |
+
def parse(wav_file, applyLM):
|
40 |
+
input_values = read_file_and_process(wav_file)
|
41 |
+
with torch.no_grad():
|
42 |
+
logits = model(**input_values).logits
|
43 |
+
|
44 |
+
if applyLM:
|
45 |
+
return parse_transcription_with_lm(logits)
|
46 |
+
else:
|
47 |
+
return parse_transcription(logits)
|
48 |
+
|
49 |
+
access_token = os.getenv("ACCESS_TOKEN")
|
50 |
+
model_id = "Anujgr8/wav2vec2-indic-hindi-codeswitch-anuj"
|
51 |
+
processor = Wav2Vec2Processor.from_pretrained(model_id,token=access_token)
|
52 |
+
processor_with_LM = Wav2Vec2ProcessorWithLM.from_pretrained(model_id,token=access_token)
|
53 |
+
model = Wav2Vec2ForCTC.from_pretrained(model_id,token=access_token)
|
54 |
+
|
55 |
+
|
56 |
+
input_ = gr.Audio(source="microphone", type="filepath")
|
57 |
+
txtbox = gr.Textbox(
|
58 |
+
label="Output from model will appear here:",
|
59 |
+
lines=5
|
60 |
+
)
|
61 |
+
chkbox = gr.Checkbox(label="Apply LM", value=False)
|
62 |
+
|
63 |
+
|
64 |
+
gr.Interface(parse, inputs = [input_, chkbox], outputs=txtbox,
|
65 |
+
streaming=True, interactive=True,
|
66 |
+
analytics_enabled=False, show_tips=False, enable_queue=True).launch(inline=False);
|