File size: 3,029 Bytes
5a69728
a263141
5a69728
a263141
 
5a69728
 
 
 
 
a263141
5a69728
 
a263141
5a69728
 
 
a263141
 
 
 
5a69728
 
 
 
 
 
 
 
 
 
 
 
 
02f5074
 
 
5a69728
a263141
 
 
5a69728
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02f5074
5a69728
 
 
 
 
 
 
 
a263141
 
 
 
 
 
 
 
 
 
 
 
 
 
5a69728
 
 
a263141
5a69728
 
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
from pathlib import Path

import gradio as gr
import librosa
import numpy as np
from espnet2.bin.asr_inference import Speech2Text
from espnet_model_zoo.downloader import ModelDownloader

# sample_data直下に各ファイルが存在するとしている
base_dir = Path("./config")
MODEL_FILE = base_dir / "31epoch.pth"
TRAIN_CONFIG = base_dir / "config.yaml"
NORM_CONFIG = base_dir / "feats_stats.npz"
DEVICE = "cpu"
RESAMPLING_RATE = 16000

# モデル
speech2text = Speech2Text(
    asr_train_config=TRAIN_CONFIG, asr_model_file=MODEL_FILE, device=DEVICE
)


# リサンプリング
def resample(audio: np.ndarray, original_sr: int) -> tuple[np.ndarray, int]:
    """
    入力された音声信号を元のサンプルレートからリサンプリング

    Args:
        audio (np.ndarray): リサンプリングする音声信号。
        original_sr (int): 音声信号の元のサンプルレート。

    Returns:
        tuple[np.ndarray, int]: リサンプリングされた音声信号と目標のサンプルレート
    """
    # int16あのでfloatに変換
    if audio.dtype in [np.int16, np.int32]:
        audio = audio.astype(np.float32) / np.iinfo(audio.dtype).max
    # audioのサンプリングレートをoriginal_srから16kに調整する
    resampled_audio = librosa.resample(
        audio, orig_sr=original_sr, target_sr=RESAMPLING_RATE
    )
    return resampled_audio, RESAMPLING_RATE


# 文字起こし
# def transcribe(input):
def transcribe(input: tuple[int, np.ndarray]) -> str:
    """
    入力された音声信号をテキストに変換

    Args:
        input (tuple[int, np.ndarray]): サンプルレートと音声データを含むタプル。

    Returns:
        str: 音声信号から文字起こしされたテキスト。
    """
    if input is None:
        raise gr.Error("音声ファイルが提出されていません。実行する前に音声ファイルをアップロードしてください。")

    sr = input[0]
    audio = input[1]
    audio, _ = resample(audio, sr)
    # 認識
    nbests = speech2text(audio)
    text, *_ = nbests[0]
    return text


# ウェブアプリを作成 themeなくしている
demo_all = gr.Blocks()
demo_radio = gr.Interface(
    fn=transcribe,
    inputs=gr.Audio(sources="microphone", type="numpy", label="microphoneFile"),
    outputs="text",
    title="録音した音声をテキストに変換",
    description=("録音した音声をテキストに文字起こしします。"),
)
demo_sound = gr.Interface(
    fn=transcribe,
    inputs=gr.Audio(sources="upload", type="numpy", label="Audiofile"),
    outputs="text",
    title="アップロードした音声をテキストに変換",
    description=("アップロードした音声データをテキストに文字起こしします。"),
)

# タブにより統合されたウェブアプリとする
with demo_all:
    gr.TabbedInterface([demo_radio, demo_sound], ["Microphone", "Audio File"])
# ウェブアプリを起動
demo_all.launch(share=True)