-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[web] Update code from huggingface (#2161)
* [web] Update code from huggingface * [fix] fix lint * [web] remove examples --------- Co-authored-by: user01 <[email protected]>
- Loading branch information
Showing
2 changed files
with
67 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,71 @@ | ||
import json | ||
# 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 gradio as gr | ||
import numpy as np | ||
import torch | ||
import wenetruntime as wenet | ||
import wenet | ||
|
||
# TODO: add hotword | ||
chs_model = wenet.load_model('chinese') | ||
en_model = wenet.load_model('english') | ||
|
||
|
||
def recognition(audio, lang='CN'): | ||
if audio is None: | ||
return "Input Error! Please enter one audio!" | ||
# NOTE: model supports 16k sample_rate | ||
if lang == 'CN': | ||
ans = chs_model.transcribe(audio) | ||
elif lang == 'EN': | ||
ans = en_model.transcribe(audio) | ||
else: | ||
return "ERROR! Please select a language!" | ||
|
||
if ans is None: | ||
return "ERROR! No text output! Please try again!" | ||
txt = ans['text'] | ||
return txt | ||
|
||
torch.manual_seed(777) # for lint | ||
|
||
wenet.set_log_level(2) | ||
decoder = wenet.Decoder(lang='chs') | ||
# input | ||
inputs = [ | ||
gr.inputs.Audio(source="microphone", type="filepath", label='Input audio'), | ||
gr.Radio(['EN', 'CN'], label='Language') | ||
] | ||
|
||
def recognition(audio): | ||
sr, y = audio | ||
assert sr in [48000, 16000] | ||
if sr == 48000: # Optional resample to 16000 | ||
y = (y / max(np.max(y), 1) * 32767)[::3].astype("int16") | ||
ans = decoder.decode(y.tobytes(), True) | ||
return json.loads(ans) | ||
output = gr.outputs.Textbox(label="Output Text") | ||
|
||
text = "Speech Recognition in WeNet | 基于 WeNet 的语音识别" | ||
gr.Interface(recognition, inputs="mic", outputs="json", | ||
description=text).launch() | ||
|
||
# description | ||
description = ( | ||
"Wenet Demo ! This is a speech recognition demo that supports Mandarin and English !" # noqa | ||
) | ||
|
||
article = ( | ||
"<p style='text-align: center'>" | ||
"<a href='https://github.com/wenet-e2e/wenet' target='_blank'>Github: Learn more about WeNet</a>" # noqa | ||
"</p>") | ||
|
||
interface = gr.Interface( | ||
fn=recognition, | ||
inputs=inputs, | ||
outputs=output, | ||
title=text, | ||
description=description, | ||
article=article, | ||
theme='huggingface', | ||
) | ||
|
||
interface.launch(enable_queue=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
wenetruntime | ||
gradio | ||
wenet @ git+https://github.com/wenet-e2e/wenet.git | ||
gradio==3.14.0 |