aditii09 commited on
Commit
41ccc45
1 Parent(s): a5f4690
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ git clone https://huggingface.co/spaces/aditii09/hindi-asr
2
+
3
+ import soundfile as sf
4
+ import torch
5
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor,Wav2Vec2ProcessorWithLM
6
+ import gradio as gr
7
+ import sox
8
+ import subprocess
9
+
10
+
11
+ def read_file_and_process(wav_file):
12
+ filename = wav_file.split('.')[0]
13
+ filename_16k = filename + "16k.wav"
14
+ resampler(wav_file, filename_16k)
15
+ speech, _ = sf.read(filename_16k)
16
+ inputs = processor(speech, sampling_rate=16_000, return_tensors="pt", padding=True)
17
+
18
+ return inputs
19
+
20
+
21
+ def resampler(input_file_path, output_file_path):
22
+ command = (
23
+ f"ffmpeg -hide_banner -loglevel panic -i {input_file_path} -ar 16000 -ac 1 -bits_per_raw_sample 16 -vn "
24
+ f"{output_file_path}"
25
+ )
26
+ subprocess.call(command, shell=True)
27
+
28
+
29
+ def parse_transcription_with_lm(logits):
30
+ result = processor_with_LM.batch_decode(logits.cpu().numpy())
31
+ text = result.text
32
+ transcription = text[0].replace('<s>','')
33
+ return transcription
34
+
35
+ def parse_transcription(logits):
36
+ predicted_ids = torch.argmax(logits, dim=-1)
37
+ transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
38
+ return transcription
39
+
40
+ def parse(wav_file, applyLM):
41
+ input_values = read_file_and_process(wav_file)
42
+ with torch.no_grad():
43
+ logits = model(**input_values).logits
44
+ if applyLM:
45
+ return parse_transcription_with_lm(wav_file)
46
+ else:
47
+ return parse_transcription(wav_file)
48
+
49
+
50
+ if applyLM:
51
+ return parse_transcription_with_lm(logits)
52
+ else:
53
+ return parse_transcription(logits)
54
+
55
+
56
+ model_id = "aditii09/hindi-asr"
57
+ processor = Wav2Vec2Processor.from_pretrained(model_id)
58
+ processor_with_LM = Wav2Vec2ProcessorWithLM.from_pretrained(model_id)
59
+ model = Wav2Vec2ForCTC.from_pretrained(model_id)
60
+
61
+
62
+ input_ = gr.Audio(source="microphone", type="filepath")
63
+ txtbox = gr.Textbox(
64
+ label="Output from model will appear here:",
65
+ lines=5
66
+ )
67
+ chkbox = gr.Checkbox(label="Apply LM", value=False)
68
+
69
+
70
+ gr.Interface(parse, inputs = [input_, chkbox], outputs=txtbox,
71
+ streaming=True, interactive=True,
72
+ analytics_enabled=False, show_tips=False, enable_queue=True).launch(inline=False);