cahya commited on
Commit
8c3d56b
1 Parent(s): 25a6dbb

replace sox with librosa

Browse files
Files changed (2) hide show
  1. app.py +14 -16
  2. requirements.txt +6 -4
app.py CHANGED
@@ -1,23 +1,24 @@
1
  import soundfile as sf
 
2
  import torch
3
  from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
4
  import gradio as gr
5
- import sox
6
  import os
7
 
8
 
9
- def convert(inputfile, outfile):
10
- sox_tfm = sox.Transformer()
11
- sox_tfm.set_output_format(
12
- file_type="wav", channels=1, encoding="signed-integer", rate=16000, bits=16
13
- )
14
- sox_tfm.build(inputfile, outfile)
15
-
16
-
17
  api_token = os.getenv("API_TOKEN")
18
  model_name = "indonesian-nlp/wav2vec2-indonesian-javanese-sundanese"
19
  processor = Wav2Vec2Processor.from_pretrained(model_name, use_auth_token=api_token)
20
  model = Wav2Vec2ForCTC.from_pretrained(model_name, use_auth_token=api_token)
 
 
 
 
 
 
 
 
 
21
 
22
 
23
  def parse_transcription(wav_file):
@@ -25,6 +26,7 @@ def parse_transcription(wav_file):
25
  convert(wav_file.name, filename + "16k.wav")
26
  speech, _ = sf.read(filename + "16k.wav")
27
  input_values = processor(speech, sampling_rate=16_000, return_tensors="pt").input_values
 
28
  logits = model(input_values).logits
29
  predicted_ids = torch.argmax(logits, dim=-1)
30
  transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
@@ -37,14 +39,10 @@ input_ = gr.inputs.Audio(source="microphone", type="file")
37
 
38
  gr.Interface(parse_transcription, inputs=input_, outputs=[output],
39
  analytics_enabled=False,
40
- show_tips=False,
41
- theme='huggingface',
42
- layout='vertical',
43
  title="Multilingual Speech Recognition for Indonesian Languages",
44
  description="Automatic Speech Recognition Live Demo for Indonesian, Javanese and Sundanese Language",
45
- article="Due to the issue with the current Gradio version, please use <a href='https://asr.ai-research.id/' target='_blank'>Multilingual Indonesian ASR</a> instead this one.<br>"
46
- "This demo was built for the project "
47
  "<a href='https://github.com/indonesian-nlp/multilingual-asr' target='_blank'>Multilingual Speech Recognition for Indonesian Languages</a>. "
48
  "It uses the <a href='https://huggingface.co/indonesian-nlp/wav2vec2-indonesian-javanese-sundanese' target='_blank'>indonesian-nlp/wav2vec2-indonesian-javanese-sundanese</a> model "
49
- "which was fine-tuned on Indonesian Common Voice, Javanese and Sundanese OpenSLR speech datasets.",
50
- enable_queue=True).launch( inline=False)
 
1
  import soundfile as sf
2
+ import librosa
3
  import torch
4
  from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
5
  import gradio as gr
 
6
  import os
7
 
8
 
 
 
 
 
 
 
 
 
9
  api_token = os.getenv("API_TOKEN")
10
  model_name = "indonesian-nlp/wav2vec2-indonesian-javanese-sundanese"
11
  processor = Wav2Vec2Processor.from_pretrained(model_name, use_auth_token=api_token)
12
  model = Wav2Vec2ForCTC.from_pretrained(model_name, use_auth_token=api_token)
13
+ device = "cuda" if torch.cuda.is_available() else "cpu"
14
+ model = model.to(device)
15
+
16
+
17
+ def convert(inputfile, outfile):
18
+ target_sr = 16000
19
+ data, sample_rate = librosa.load(inputfile)
20
+ data = librosa.resample(data, orig_sr=sample_rate, target_sr=target_sr)
21
+ sf.write(outfile, data, target_sr)
22
 
23
 
24
  def parse_transcription(wav_file):
 
26
  convert(wav_file.name, filename + "16k.wav")
27
  speech, _ = sf.read(filename + "16k.wav")
28
  input_values = processor(speech, sampling_rate=16_000, return_tensors="pt").input_values
29
+ input_values = input_values.to(device)
30
  logits = model(input_values).logits
31
  predicted_ids = torch.argmax(logits, dim=-1)
32
  transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
 
39
 
40
  gr.Interface(parse_transcription, inputs=input_, outputs=[output],
41
  analytics_enabled=False,
 
 
 
42
  title="Multilingual Speech Recognition for Indonesian Languages",
43
  description="Automatic Speech Recognition Live Demo for Indonesian, Javanese and Sundanese Language",
44
+ article="This demo was built for the project "
 
45
  "<a href='https://github.com/indonesian-nlp/multilingual-asr' target='_blank'>Multilingual Speech Recognition for Indonesian Languages</a>. "
46
  "It uses the <a href='https://huggingface.co/indonesian-nlp/wav2vec2-indonesian-javanese-sundanese' target='_blank'>indonesian-nlp/wav2vec2-indonesian-javanese-sundanese</a> model "
47
+ "which was fine-tuned on Indonesian Common Voice, Javanese and Sundanese OpenSLR speech datasets."
48
+ ).launch( inline=False, server_name="0.0.0.0", show_tips=False, enable_queue=True)
requirements.txt CHANGED
@@ -1,6 +1,8 @@
1
- gradio==2.4.1
2
- soundfile
3
  torch
 
4
  transformers
5
- sox
6
- sentencepiece
 
 
 
 
 
 
1
  torch
2
+ tokenizers
3
  transformers
4
+ datasets
5
+ gradio>=3.0.0
6
+ soundfile
7
+ sentencepiece
8
+ librosa