Liangcd commited on
Commit
2a2c14b
1 Parent(s): a487abc

[demo] Update app.py and add examples

Browse files
app.py CHANGED
@@ -1,3 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
  import json
3
  import gradio as gr
@@ -5,48 +19,47 @@ import numpy as np
5
  import wenetruntime as wenet
6
 
7
  wenet.set_log_level(2)
8
- decoder = wenet.Decoder(lang='chs')
9
 
10
 
11
  def recognition(audio):
12
- print(audio)
13
  if audio is None:
14
  return "Input Error! Please enter one audio!"
15
  sr, y = audio
16
  assert sr in [48000, 16000]
17
  if sr == 48000: # Optional resample to 16000
18
  y = (y / max(np.max(y), 1) * 32767)[::3].astype("int16")
19
- ans = decoder.decode(y.tobytes(), True)
20
- if ans == None:
21
  return "ERROR! No text output! Please try again!"
22
- # ans (json)
23
  # {
24
  # 'nbest' : [{"sentence" : ""}], 'type' : 'final_result
25
  # }
26
  ans = json.loads(ans)
27
- print(ans)
28
  txt = ans['nbest'][0]['sentence']
29
  return txt
30
 
31
 
32
  # input
33
- inputs = [
34
- gr.inputs.Audio(source="microphone",
35
- type="numpy",
36
- label='Speaker#1')
37
- ]
38
 
39
  output = gr.outputs.Textbox(label="Output Text")
40
 
41
- # examples = ['examples/BAC009S0764W0121.wav']
 
 
 
 
 
42
  text = "Speech Recognition in WeNet | 基于 WeNet 的语音识别"
43
 
44
  # description
45
- description = ("WeSpeaker Demo ! Try it with your own voice !")
46
 
47
  article = (
48
  "<p style='text-align: center'>"
49
- "<a href='https://github.com/wenet-e2e/wespeaker' target='_blank'>Github: Learn more about WeSpeaker</a>"
50
  "</p>")
51
 
52
  interface = gr.Interface(
@@ -56,6 +69,7 @@ interface = gr.Interface(
56
  title=text,
57
  description=description,
58
  article=article,
 
59
  theme='huggingface',
60
  )
61
 
 
1
+ # Copyright (c) 2022 Horizon Robotics. (authors: Binbin Zhang)
2
+ # 2022 Chengdong Liang ([email protected])
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
 
16
  import json
17
  import gradio as gr
 
19
  import wenetruntime as wenet
20
 
21
  wenet.set_log_level(2)
22
+ decoder_cn = wenet.Decoder(lang='chs')
23
 
24
 
25
  def recognition(audio):
 
26
  if audio is None:
27
  return "Input Error! Please enter one audio!"
28
  sr, y = audio
29
  assert sr in [48000, 16000]
30
  if sr == 48000: # Optional resample to 16000
31
  y = (y / max(np.max(y), 1) * 32767)[::3].astype("int16")
32
+ ans = decoder_cn.decode(y.tobytes(), True)
33
+ if ans is None:
34
  return "ERROR! No text output! Please try again!"
35
+ # NOTE: ans (json)
36
  # {
37
  # 'nbest' : [{"sentence" : ""}], 'type' : 'final_result
38
  # }
39
  ans = json.loads(ans)
 
40
  txt = ans['nbest'][0]['sentence']
41
  return txt
42
 
43
 
44
  # input
45
+ inputs = gr.inputs.Audio(source="microphone", type="numpy", label='Input audio')
 
 
 
 
46
 
47
  output = gr.outputs.Textbox(label="Output Text")
48
 
49
+ examples = [
50
+ ['examples/BAC009S0767W0127.wav'],
51
+ ['examples/BAC009S0767W0424.wav'],
52
+ ['examples/BAC009S0767W0488.wav'],
53
+ ]
54
+
55
  text = "Speech Recognition in WeNet | 基于 WeNet 的语音识别"
56
 
57
  # description
58
+ description = ("Wenet Demo ! This is a Mandarin streaming speech recognition !")
59
 
60
  article = (
61
  "<p style='text-align: center'>"
62
+ "<a href='https://github.com/wenet-e2e/wenet' target='_blank'>Github: Learn more about WeNet</a>"
63
  "</p>")
64
 
65
  interface = gr.Interface(
 
69
  title=text,
70
  description=description,
71
  article=article,
72
+ examples=examples,
73
  theme='huggingface',
74
  )
75
 
examples/BAC009S0767W0127.wav ADDED
Binary file (130 kB). View file
 
examples/BAC009S0767W0424.wav ADDED
Binary file (81 kB). View file
 
examples/BAC009S0767W0488.wav ADDED
Binary file (95.8 kB). View file