modify app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,25 @@
|
|
| 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 =
|
| 11 |
TRAIN_CONFIG = base_dir / "config.yaml"
|
| 12 |
NORM_CONFIG = base_dir / "feats_stats.npz"
|
| 13 |
-
DEVICE
|
| 14 |
RESAMPLING_RATE = 16000
|
| 15 |
THRESHOLD = 5000000
|
| 16 |
|
| 17 |
# モデル
|
| 18 |
-
speech2text = Speech2Text(
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
# リサンプリング
|
| 21 |
def resample(audio: np.ndarray, original_sr: int) -> tuple[np.ndarray, int]:
|
|
@@ -30,7 +34,9 @@ def resample(audio: np.ndarray, original_sr: int) -> tuple[np.ndarray, int]:
|
|
| 30 |
tuple[np.ndarray, int]: リサンプリングされた音声信号と目標のサンプルレート
|
| 31 |
"""
|
| 32 |
# audioのサンプリングレートをoriginal_srから16kに調整する
|
| 33 |
-
resampled_audio = librosa.resample(
|
|
|
|
|
|
|
| 34 |
return resampled_audio, RESAMPLING_RATE
|
| 35 |
|
| 36 |
|
|
@@ -62,11 +68,23 @@ def transcribe(input: tuple[int, np.ndarray]) -> str:
|
|
| 62 |
|
| 63 |
# ウェブアプリを作成 themeなくしている
|
| 64 |
demo_all = gr.Blocks()
|
| 65 |
-
demo_radio = gr.Interface(
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# タブにより統合されたウェブアプリとする
|
| 69 |
with demo_all:
|
| 70 |
-
gr.TabbedInterface([demo_radio,demo_sound],["Microphone","Audio File"])
|
| 71 |
# ウェブアプリを起動
|
| 72 |
demo_all.launch(share=True)
|
|
|
|
| 1 |
from pathlib import Path
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
+
import librosa
|
| 5 |
+
import numpy as np
|
| 6 |
from espnet2.bin.asr_inference import Speech2Text
|
| 7 |
from espnet_model_zoo.downloader import ModelDownloader
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# sample_data直下に各ファイルが存在するとしている
|
| 10 |
base_dir = Path("./config")
|
| 11 |
+
MODEL_FILE = base_dir / "31epoch.pth"
|
| 12 |
TRAIN_CONFIG = base_dir / "config.yaml"
|
| 13 |
NORM_CONFIG = base_dir / "feats_stats.npz"
|
| 14 |
+
DEVICE = "cpu"
|
| 15 |
RESAMPLING_RATE = 16000
|
| 16 |
THRESHOLD = 5000000
|
| 17 |
|
| 18 |
# モデル
|
| 19 |
+
speech2text = Speech2Text(
|
| 20 |
+
asr_train_config=TRAIN_CONFIG, asr_model_file=MODEL_FILE, device=DEVICE
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
|
| 24 |
# リサンプリング
|
| 25 |
def resample(audio: np.ndarray, original_sr: int) -> tuple[np.ndarray, int]:
|
|
|
|
| 34 |
tuple[np.ndarray, int]: リサンプリングされた音声信号と目標のサンプルレート
|
| 35 |
"""
|
| 36 |
# audioのサンプリングレートをoriginal_srから16kに調整する
|
| 37 |
+
resampled_audio = librosa.resample(
|
| 38 |
+
audio, orig_sr=original_sr, target_sr=RESAMPLING_RATE
|
| 39 |
+
)
|
| 40 |
return resampled_audio, RESAMPLING_RATE
|
| 41 |
|
| 42 |
|
|
|
|
| 68 |
|
| 69 |
# ウェブアプリを作成 themeなくしている
|
| 70 |
demo_all = gr.Blocks()
|
| 71 |
+
demo_radio = gr.Interface(
|
| 72 |
+
fn=transcribe,
|
| 73 |
+
inputs=gr.Audio(sources="microphone", type="numpy", label="microphoneFile"),
|
| 74 |
+
outputs="text",
|
| 75 |
+
title="録音した音声をテキストに変換",
|
| 76 |
+
description=("録音した音声をテキストに文字起こしします。"),
|
| 77 |
+
)
|
| 78 |
+
demo_sound = gr.Interface(
|
| 79 |
+
fn=transcribe,
|
| 80 |
+
inputs=gr.Audio(sources="upload", type="numpy", label="Audiofile"),
|
| 81 |
+
outputs="text",
|
| 82 |
+
title="アップロードした音声をテキストに変換",
|
| 83 |
+
description=("アップロードした音声データをテキストに文字起こしします。"),
|
| 84 |
+
)
|
| 85 |
|
| 86 |
# タブにより統合されたウェブアプリとする
|
| 87 |
with demo_all:
|
| 88 |
+
gr.TabbedInterface([demo_radio, demo_sound], ["Microphone", "Audio File"])
|
| 89 |
# ウェブアプリを起動
|
| 90 |
demo_all.launch(share=True)
|