File size: 2,975 Bytes
2a2c14b
 
 
 
 
 
 
 
 
 
 
 
 
 
a487abc
 
 
 
15307f9
 
a487abc
45ed0df
 
f6f3627
a487abc
 
8775cab
a487abc
 
b72dbd0
15307f9
b72dbd0
76ddd3a
8775cab
45ed0df
 
 
 
8775cab
45ed0df
 
 
 
f6f3627
 
8775cab
 
45ed0df
2a2c14b
a487abc
2a2c14b
a487abc
 
 
 
 
 
 
 
 
8775cab
 
 
 
a487abc
 
 
2a2c14b
8775cab
 
 
 
 
 
2a2c14b
 
a487abc
 
 
8775cab
 
 
a487abc
 
 
2a2c14b
a487abc
 
 
 
 
 
 
 
 
2a2c14b
a487abc
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright (c) 2022 Horizon Robotics. (authors: Binbin Zhang)
#               2022 Chengdong Liang ([email protected])
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import gradio as gr
import wenetruntime as wenet
import librosa

wenet.set_log_level(2)
decoder = wenet.Decoder(lang='chs')
cur_lang = 'CN'
# decoder_en = wenet.Decoder(lang='en')


def recognition(audio, lang='CN'):
    if audio is None:
        return "Input Error! Please enter one audio!"
    y, _ = librosa.load(audio, sr=16000)
    # NOTE: model supports 16k sample_rate
    y = (y * (1 << 15)).astype("int16")
    global cur_lang
    if lang == 'CN':
        if cur_lang != lang:
            del decoder
            decoder = wenet.Decoder(lang='chs')
            cur_lang = 'CN'
    elif lang == 'EN':
        if cur_lang != lang:
            del decoder
            decoder = wenet.Decoder(lang='en')
            cur_lang = 'EN'
        # ans = decoder_en.decode(y.tobytes(), True)
        return "ERROR! English is not supported yet!"
    else:
        return "ERROR! Please select a language!"
    ans = decoder.decode(y.tobytes(), True)
    if ans is None:
        return "ERROR! No text output! Please try again!"
    # NOTE: ans (json)
    # {
    #    'nbest' : [{"sentence" : ""}], 'type' : 'final_result
    # }
    ans = json.loads(ans)
    txt = ans['nbest'][0]['sentence']
    return txt


# input
inputs = [
    gr.inputs.Audio(source="microphone", type="filepath", label='Input audio'),
    gr.Radio(['EN', 'CN'], label='Language')
]

output = gr.outputs.Textbox(label="Output Text")

examples = [
    ['examples/BAC009S0767W0127.wav', 'CN'],
    ['examples/BAC009S0767W0424.wav', 'CN'],
    ['examples/BAC009S0767W0488.wav', 'CN'],
    ['examples/1995-1836-0002.flac', 'EN'],
    ['examples/61-70968-0000.flac', 'EN'],
    ['examples/672-122797-0000.flac', 'EN'],
]

text = "Speech Recognition in WeNet | 基于 WeNet 的语音识别"

# description
description = (
    "Wenet Demo ! This is a speech recognition demo that supports Mandarin and English !"
)

article = (
    "<p style='text-align: center'>"
    "<a href='https://github.com/wenet-e2e/wenet' target='_blank'>Github: Learn more about WeNet</a>"
    "</p>")

interface = gr.Interface(
    fn=recognition,
    inputs=inputs,
    outputs=output,
    title=text,
    description=description,
    article=article,
    examples=examples,
    theme='huggingface',
)

interface.launch(enable_queue=True)