Spaces:
Runtime error
Runtime error
Add paddlespeech asr.
Browse files- app.py +42 -0
- packages.txt +2 -0
- requirements.txt +8 -0
app.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import librosa
|
| 3 |
+
import numpy as np
|
| 4 |
+
import paddlehub as hub
|
| 5 |
+
from paddlenlp import Taskflow
|
| 6 |
+
from paddlespeech.cli import ASRExecutor
|
| 7 |
+
import soundfile as sf
|
| 8 |
+
|
| 9 |
+
# asr_model = hub.Module(name='u2_conformer_aishell')
|
| 10 |
+
asr_executor = ASRExecutor()
|
| 11 |
+
text_correct_model = Taskflow("text_correction")
|
| 12 |
+
punc_model = hub.Module(name='ernie_punc')
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def speech_recognize(file):
|
| 16 |
+
data, sr = librosa.load(file)
|
| 17 |
+
if sr != 16000:
|
| 18 |
+
data = librosa.resample(data, sr, 16000)
|
| 19 |
+
sf.write(file, data, samplerate=16000)
|
| 20 |
+
|
| 21 |
+
print(f'[Audio Input] shape: {data.shape}, dtype: {data.dtype}, file: {file}')
|
| 22 |
+
# text = asr_model.speech_recognize(file, device='cpu')
|
| 23 |
+
text = asr_executor('wenetspeech', 'zh', 16000, None, None, file, 'cpu')
|
| 24 |
+
text_correction = text_correct_model(text)[0]
|
| 25 |
+
cor_text, errors = text_correction['target'], text_correction['errors']
|
| 26 |
+
print(f'[Text Correction] errors: {errors}')
|
| 27 |
+
punc_text = punc_model.add_puncs(cor_text, device='cpu')[0]
|
| 28 |
+
|
| 29 |
+
ret = ''
|
| 30 |
+
ret += f'[ASR] {text}\n'
|
| 31 |
+
ret += f'[COR] {cor_text}\n'
|
| 32 |
+
ret += f'[PUN] {punc_text}'
|
| 33 |
+
return ret
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
iface = gr.Interface(
|
| 37 |
+
fn=speech_recognize,
|
| 38 |
+
inputs=gr.inputs.Audio(source="microphone", type='filepath'),
|
| 39 |
+
outputs="text",
|
| 40 |
+
)
|
| 41 |
+
iface.launch()
|
| 42 |
+
|
packages.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
libsndfile1
|
| 2 |
+
sox
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
paddlepaddle
|
| 2 |
+
paddlespeech
|
| 3 |
+
paddlehub
|
| 4 |
+
paddlenlp
|
| 5 |
+
pypinyin
|
| 6 |
+
SoundFile==0.9.0.post1
|
| 7 |
+
librosa
|
| 8 |
+
opencv-python-headless
|