piecurus commited on
Commit
dc3ecb8
1 Parent(s): 21907eb

app commit with wav2vec-base-960h

Browse files
Files changed (1) hide show
  1. app.py +41 -4
app.py CHANGED
@@ -1,7 +1,44 @@
 
 
 
 
1
  import gradio as gr
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import soundfile as sf
2
+ import torch
3
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
4
+ from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
5
  import gradio as gr
6
+ import sox
7
 
8
+ def convert(inputfile, outfile):
9
+ sox_tfm = sox.Transformer()
10
+ sox_tfm.set_output_format(
11
+ file_type="wav", channels=1, encoding="signed-integer", rate=16000, bits=16
12
+ )
13
+ sox_tfm.build(inputfile, outfile)
14
+
15
+
16
+
17
+ processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
18
+ model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
19
 
20
+ def parse_transcription(wav_file):
21
+ filename = wav_file.name.split('.')[0]
22
+ convert(wav_file.name, filename + "16k.wav")
23
+ speech, _ = sf.read(filename + "16k.wav")
24
+ input_values = processor(speech, sampling_rate=16_000, return_tensors="pt").input_values
25
+ logits = model(input_values).logits
26
+ predicted_ids = torch.argmax(logits, dim=-1)
27
+ transcription = processor.decode(predicted_ids[0], skip_special_tokens=True)
28
+ return transcription,
29
+
30
+
31
+
32
+ output1 = gr.outputs.Textbox(label="Transcription in English: ")
33
+ output2 = gr.outputs.Textbox(label="Validated Transcription in English")
34
+
35
+ input_ = gr.inputs.Audio(source="microphone", type="file")
36
+ #gr.Interface(parse_transcription, inputs = input_, outputs="text",
37
+ # analytics_enabled=False, show_tips=False, enable_queue=True).launch(inline=False);
38
+
39
+ gr.Interface(parse_transcription, inputs = input_, outputs=[output1, output2], analytics_enabled=False,
40
+ show_tips=False,
41
+ theme='huggingface',
42
+ layout='vertical',
43
+ title="Piecurus Test on Speech Transcription",
44
+ description="This is a live demo for Speech to Text Translation. Models used: facebook/wav2vec2-base-960h", enable_queue=True).launch( inline=False)