add app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from espnet2.bin.asr_inference import Speech2Text
|
| 4 |
+
from espnet_model_zoo.downloader import ModelDownloader
|
| 5 |
+
import numpy as np
|
| 6 |
+
import librosa
|
| 7 |
+
|
| 8 |
+
# sample_data直下に各ファイルが存在するとしている
|
| 9 |
+
base_dir = Path("./config")
|
| 10 |
+
MODEL_FILE = base_dir / "31epoch.pth"
|
| 11 |
+
TRAIN_CONFIG = base_dir / "config.yaml"
|
| 12 |
+
NORM_CONFIG = base_dir / "feats_stats.npz"
|
| 13 |
+
DEVICE = "cpu"
|
| 14 |
+
RESAMPLING_RATE = 16000
|
| 15 |
+
THRESHOLD = 5000000
|
| 16 |
+
|
| 17 |
+
# モデル
|
| 18 |
+
speech2text = Speech2Text(asr_train_config = TRAIN_CONFIG, asr_model_file = MODEL_FILE, device = DEVICE)
|
| 19 |
+
|
| 20 |
+
# リサンプリング
|
| 21 |
+
def resample(audio: np.ndarray, original_sr: int) -> tuple[np.ndarray, int]:
|
| 22 |
+
"""
|
| 23 |
+
入力された音声信号を元のサンプルレートからリサンプリング
|
| 24 |
+
|
| 25 |
+
Args:
|
| 26 |
+
audio (np.ndarray): リサンプリングする音声信号。
|
| 27 |
+
original_sr (int): 音声信号の元のサンプルレート。
|
| 28 |
+
|
| 29 |
+
Returns:
|
| 30 |
+
tuple[np.ndarray, int]: リサンプリングされた音声信号と目標のサンプルレート
|
| 31 |
+
"""
|
| 32 |
+
# audioのサンプリングレートをoriginal_srから16kに調整する
|
| 33 |
+
resampled_audio = librosa.resample(audio,orig_sr=original_sr, target_sr=RESAMPLING_RATE)
|
| 34 |
+
return resampled_audio, RESAMPLING_RATE
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# 文字起こし
|
| 38 |
+
# def transcribe(input):
|
| 39 |
+
def transcribe(input: tuple[int, np.ndarray]) -> str:
|
| 40 |
+
"""
|
| 41 |
+
入力された音声信号をテキストに変換
|
| 42 |
+
|
| 43 |
+
Args:
|
| 44 |
+
input (tuple[int, np.ndarray]): サンプルレートと音声データを含むタプル。
|
| 45 |
+
|
| 46 |
+
Returns:
|
| 47 |
+
str: 音声信号から文字起こしされたテキスト。
|
| 48 |
+
"""
|
| 49 |
+
if input is None:
|
| 50 |
+
raise gr.Error("音声ファイルが提出されていません。実行する前に音声ファイルをアップロードしてください。")
|
| 51 |
+
|
| 52 |
+
sr = input[0]
|
| 53 |
+
audio = input[1]
|
| 54 |
+
# リサンプリング(短すぎると、リサンプリングできないため、あまりに短いファイルはリサンプリングしない)
|
| 55 |
+
if len(audio) > THRESHOLD:
|
| 56 |
+
audio, _ = resample(audio, sr)
|
| 57 |
+
# 認識
|
| 58 |
+
nbests = speech2text(audio)
|
| 59 |
+
text, *_ = nbests[0]
|
| 60 |
+
return text
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# ウェブアプリを作成 themeなくしている
|
| 64 |
+
demo_all = gr.Blocks()
|
| 65 |
+
demo_radio = gr.Interface(fn=transcribe,inputs=gr.Audio(sources="microphone", type="numpy",label="microphoneFile"),outputs="text", title = "録音した音声をテキストに変換",description=("録音した音声をテキストに文字起こしします。"))
|
| 66 |
+
demo_sound = gr.Interface(fn=transcribe,inputs=gr.Audio(sources="upload", type = "numpy", label="Audiofile"),outputs= "text",title="アップロードした音声をテキストに変換",description=("アップロードした音声データをテキストに文字起こしします。"))
|
| 67 |
+
|
| 68 |
+
# タブにより統合されたウェブアプリとする
|
| 69 |
+
with demo_all:
|
| 70 |
+
gr.TabbedInterface([demo_radio,demo_sound],["Microphone","Audio File"])
|
| 71 |
+
# ウェブアプリを起動
|
| 72 |
+
demo_all.launch(share=True)
|