BishanSingh246 commited on
Commit
b0be44a
1 Parent(s): 4d543b3

Add application file

Browse files
Files changed (3) hide show
  1. app.py +65 -0
  2. packages.txt +4 -0
  3. requirements.txt +8 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
49
+ model_id = "Harveenchadha/vakyansh-wav2vec2-hindi-him-4200"
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);
packages.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ libsndfile1
2
+ sox
3
+ ffmpeg
4
+
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ https://github.com/kpu/kenlm/archive/master.zip
3
+ pyctcdecode
4
+ soundfile
5
+ torch
6
+ transformers
7
+ sox
8
+ scipy