モデル適応により音声文字変換の結果を改善する

モデル適応を使用して、Cloud Speech-to-Text から取得する音声文字変換の結果の精度を改善できます。モデル適応を使用すると、音声データで他に提案される候補よりも高い頻度で Cloud STT が認識する必要がある単語やフレーズを指定できます。モデル適応は、特に次のようなユースケースで音声文字変換の精度を改善するうえで有用です。

  • 頻繁に出現する可能性が高い単語やフレーズが音声に含まれている。
  • まれにしか使用されない単語(固有名詞など)や一般的には使用されない単語が音声に含まれている可能性がある。
  • 音声に雑音が入っていたり、はっきりと聞こえない。

このドキュメントを読む前に、モデル適応の概要で、この機能の仕組みの概要を確認してください。モデル適応リクエストごとのフレーズと文字の制限については、割り当てと上限をご覧ください。

コードサンプル

モデル適応は Cloud STT のオプション構成で、必要に応じて音声文字変換の結果をカスタマイズするために使用できます。認識リクエストの本文の構成については、RecognitionConfig のドキュメントをご覧ください。

次のコードサンプルは、SpeechAdaptation リソース(PhraseSetCustomClassモデル適応ブースト)を使用して音声文字変換の精度を改善する方法を示しています。将来のリクエストで PhraseSet または CustomClass を使用するには、リソース作成時にレスポンスで返されたリソース name をメモします。

ご使用の言語で利用可能なビルド済みクラスの一覧については、サポートされているクラストークンをご覧ください。

Python

Cloud STT 用のクライアント ライブラリをインストールして使用する方法については、Cloud STT クライアント ライブラリをご覧ください。詳細については、Cloud STT Python API のリファレンス ドキュメントをご覧ください。

Cloud STT に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

import os

from google.cloud import speech_v1p1beta1 as speech

PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT")


def transcribe_with_model_adaptation(
    audio_uri: str,
    custom_class_id: str,
    phrase_set_id: str,
) -> str:
    """Create `PhraseSet` and `CustomClasses` for custom item lists in input data.
    Args:
        audio_uri (str): The Cloud Storage URI of the input audio. e.g. gs://[BUCKET]/[FILE]
        custom_class_id (str): The unique ID of the custom class to create
        phrase_set_id (str): The unique ID of the PhraseSet to create.
    Returns:
        The transcript of the input audio.
    """
    # Specifies the location where the Speech API will be accessed.
    location = "global"

    # Audio object
    audio = speech.RecognitionAudio(uri=audio_uri)

    # Create the adaptation client
    adaptation_client = speech.AdaptationClient()

    # The parent resource where the custom class and phrase set will be created.
    parent = f"projects/{PROJECT_ID}/locations/{location}"

    # Create the custom class resource
    adaptation_client.create_custom_class(
        {
            "parent": parent,
            "custom_class_id": custom_class_id,
            "custom_class": {
                "items": [
                    {"value": "sushido"},
                    {"value": "altura"},
                    {"value": "taneda"},
                ]
            },
        }
    )
    custom_class_name = (
        f"projects/{PROJECT_ID}/locations/{location}/customClasses/{custom_class_id}"
    )
    # Create the phrase set resource
    phrase_set_response = adaptation_client.create_phrase_set(
        {
            "parent": parent,
            "phrase_set_id": phrase_set_id,
            "phrase_set": {
                "boost": 10,
                "phrases": [
                    {"value": f"Visit restaurants like ${{{custom_class_name}}}"}
                ],
            },
        }
    )
    phrase_set_name = phrase_set_response.name
    # The next section shows how to use the newly created custom
    # class and phrase set to send a transcription request with speech adaptation

    # Speech adaptation configuration
    speech_adaptation = speech.SpeechAdaptation(phrase_set_references=[phrase_set_name])

    # speech configuration object
    config = speech.RecognitionConfig(
        encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=24000,
        language_code="en-US",
        adaptation=speech_adaptation,
    )

    # Create the speech client
    speech_client = speech.SpeechClient()

    response = speech_client.recognize(config=config, audio=audio)

    for result in response.results:
        print(f"Transcript: {result.alternatives[0].transcript}")