remo009 commited on
Commit
801f1c4
โ€ข
1 Parent(s): d84c1d3

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +27 -7
  2. app.py +67 -0
  3. packages.txt +3 -0
  4. requirements.txt +8 -0
README.md CHANGED
@@ -1,13 +1,33 @@
1
  ---
2
- title: Hindi Audio Text
3
- emoji: ๐ŸŒ
4
- colorFrom: yellow
5
- colorTo: pink
6
  sdk: gradio
7
- sdk_version: 3.39.0
8
  app_file: app.py
9
  pinned: false
10
- license: openrail
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Vakyansh Hindi Speech Recognition Demo (Wav2vec2)
3
+ emoji: ๐Ÿ 
4
+ colorFrom: indigo
5
+ colorTo: gray
6
  sdk: gradio
 
7
  app_file: app.py
8
  pinned: false
 
9
  ---
10
 
11
+ # Configuration
12
+
13
+ `title`: _string_
14
+ Display title for the Space
15
+
16
+ `emoji`: _string_
17
+ Space emoji (emoji-only character allowed)
18
+
19
+ `colorFrom`: _string_
20
+ Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
21
+
22
+ `colorTo`: _string_
23
+ Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
24
+
25
+ `sdk`: _string_
26
+ Can be either `gradio` or `streamlit`
27
+
28
+ `app_file`: _string_
29
+ Path to your main application file (which contains either `gradio` or `streamlit` Python code).
30
+ Path is relative to the root of the repository.
31
+
32
+ `pinned`: _boolean_
33
+ Whether the Space stays on top of your list.
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ #https://github.com/kpu/kenlm/archive/master.zip
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
+
45
+ if applyLM:
46
+ return parse_transcription_with_lm(logits)
47
+ else:
48
+ return parse_transcription(logits)
49
+
50
+
51
+ model_id = "Harveenchadha/vakyansh-wav2vec2-hindi-him-4200"
52
+ processor = Wav2Vec2Processor.from_pretrained(model_id)
53
+ processor_with_LM = Wav2Vec2ProcessorWithLM.from_pretrained(model_id)
54
+ model = Wav2Vec2ForCTC.from_pretrained(model_id)
55
+
56
+
57
+ input_ = gr.Audio(source="microphone", type="filepath")
58
+ txtbox = gr.Textbox(
59
+ label="Output from model will appear here:",
60
+ lines=5
61
+ )
62
+ chkbox = gr.Checkbox(label="Apply LM", value=False)
63
+
64
+
65
+ gr.Interface(parse, inputs = [input_, chkbox], outputs=txtbox,
66
+ streaming=True, interactive=True,
67
+ analytics_enabled=False, show_tips=False, enable_queue=True).launch(inline=False);
packages.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ libsndfile1
2
+ sox
3
+ ffmpeg
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ gradio
2
+ pyctcdecode
3
+ soundfile
4
+ torch
5
+ transformers
6
+ sox
7
+ scipy
8
+