aditii09 commited on
Commit
a90f27f
1 Parent(s): d92fc2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -3
app.py CHANGED
@@ -1,4 +1,65 @@
 
 
 
1
  import gradio as gr
2
- name_list = ['models/Harveenchadha/hindi_model_with_lm_vakyansh']
3
- interfaces = [gr.Interface.load(name) for name in name_list]
4
- gr.mix.Parallel(*interfaces, title="Hindi Wav2Vec2 model with LM head", description="Wav2Vec with LM head for Hindi").launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ def parse_transcription_with_lm(logits):
28
+ result = processor_with_LM.batch_decode(logits.cpu().numpy())
29
+ text = result.text
30
+ transcription = text[0].replace('<s>','')
31
+ return transcription
32
+
33
+ def parse_transcription(logits):
34
+ predicted_ids = torch.argmax(logits, dim=-1)
35
+ transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
36
+ return transcription
37
+
38
+ def parse(wav_file, applyLM):
39
+ input_values = read_file_and_process(wav_file)
40
+ with torch.no_grad():
41
+ logits = model(**input_values).logits
42
+
43
+ if applyLM:
44
+ return parse_transcription_with_lm(logits)
45
+ else:
46
+ return parse_transcription(logits)
47
+
48
+ model_id = "Harveenchadha/hindi_model_with_lm_vakyansh"
49
+
50
+ processor = Wav2Vec2Processor.from_pretrained(model_id)
51
+ processor_with_LM = Wav2Vec2ProcessorWithLM.from_pretrained(model_id)
52
+ model = Wav2Vec2ForCTC.from_pretrained(model_id)
53
+
54
+
55
+ input_ = gr.Audio(source="microphone", type="filepath")
56
+ txtbox = gr.Textbox(
57
+ label="Output from model will appear here:",
58
+ lines=5
59
+ )
60
+ chkbox = gr.Checkbox(label="Apply LM", value=False)
61
+
62
+
63
+ gr.Interface(parse, inputs = [input_, chkbox], outputs=txtbox,
64
+ streaming=True, interactive=True,
65
+ analytics_enabled=False, show_tips=False, enable_queue=True).launch(inline=False);