Speech-to-Text v1 から v2 への移行

Speech-to-Text API v2 では、最新の Google Cloud API 設計を活用して、エンタープライズ セキュリティと規制に関する要件をすぐに満たすことができます。

これらの要件は次のようにして満たされます。

  • データ所在地: Speech-to-Text v2 は、ベルギーやシンガポールなどの Google Cloud リージョンで既存の音声文字変換モデルを幅広く提供しています。そのため、完全にリージョン化されたサービスを通じて音声文字変換モデルを呼び出すことができます。

  • 認識機能のリソースフルネス: 認識機能は再利用可能な認識構成で、モデル、言語、機能の組み合わせを含めることができます。

  • ロギング: リソースの作成と音声文字変換は、Google Cloud コンソールで利用可能なログを生成します。そのため、テレメトリーとデバッグが容易になります。

  • 暗号化: Speech-to-Text v2 は、すべてのリソースとバッチ音声文字変換の顧客管理の暗号鍵をサポートしています。

  • 音声自動検出: Speech-to-Text v2 では、音声ファイルのサンプルレート、チャンネル数、形式を自動的に検出できます。リクエスト構成でその情報を指定する必要はありません。

v1 から v2 への移行

v1 API から v2 API への移行は自動的には行われません。機能セットを利用するには、最小限の実装変更が必要です。

API での移行

Speech-to-Text v1 と同様、音声文字変換を行うには、音声の言語と使用する認識モデルを選択して、RecognitionConfig を作成する必要があります。

Python

from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech

def quickstart_v2(
    project_id: str,
    audio_file: str,
) -> cloud_speech.RecognizeResponse:
    """Transcribe an audio file."""
    # Instantiates a client
    client = SpeechClient()

    # Reads a file as bytes
    with open(audio_file, "rb") as f:
        content = f.read()

    config = cloud_speech.RecognitionConfig(
        auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
        language_codes=["en-US"],
        model="long",
    )

    request = cloud_speech.RecognizeRequest(
        recognizer=f"projects/{project_id}/locations/global/recognizers/_",
        config=config,
        content=content,
    )

    # Transcribes the audio into text
    response = client.recognize(request=request)

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

    return response

必要に応じて、Speech-to-Text API を使用するリージョンを選択し、そのリージョンの言語とモデルの可用性を確認します。

Python

from google.api_core.client_options import ClientOptions
from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech

def change_speech_v2_location(
    project_id: str,
    location: str,
    audio_file: str,
) -> cloud_speech.RecognizeResponse:
    """Transcribe an audio file in a specific region."""
    # Instantiates a client to a regionalized Speech endpoint.
    client = SpeechClient(
        client_options=ClientOptions(
            api_endpoint=f"{location}-speech.googleapis.com",
        )
    )

    # Reads a file as bytes
    with open(audio_file, "rb") as f:
        content = f.read()

    config = cloud_speech.RecognitionConfig(
        auto_decoding_config=cloud_speech.AutoDetectDecodingConfig(),
        language_codes=["en-US"],
        model="long",
    )

    request = cloud_speech.RecognizeRequest(
        recognizer=f"projects/{project_id}/locations/{location}/recognizers/_",
        config=config,
        content=content,
    )

    # Transcribes the audio into text
    response = client.recognize(request=request)

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

    return response

多数の音声文字変換リクエストで特定の認識構成を再利用する必要がある場合は、認識機能リソースを作成することもできます。

Python

from google.cloud.speech_v2 import SpeechClient
from google.cloud.speech_v2.types import cloud_speech

def create_recognizer(project_id: str, recognizer_id: str) -> cloud_speech.Recognizer:
    # Instantiates a client
    client = SpeechClient()

    request = cloud_speech.CreateRecognizerRequest(
        parent=f"projects/{project_id}/locations/global",
        recognizer_id=recognizer_id,
        recognizer=cloud_speech.Recognizer(
            default_recognition_config=cloud_speech.RecognitionConfig(
                language_codes=["en-US"], model="long"
            ),
        ),
    )

    operation = client.create_recognizer(request=request)
    recognizer = operation.result()

    print("Created Recognizer:", recognizer.name)
    return recognizer

新しい v2 API のリクエストとレスポンスには、他にも違いがあります。詳細については、リファレンス ドキュメントをご覧ください。

UI での移行

Speech Google Cloud コンソールを使用して移行するには、次の手順に従います。

  1. Speech Google Cloud コンソールに移動します。

  2. [音声文字変換] ページに移動します。

  3. [新しい音声文字変換] をクリックし、[音声設定] タブで音声を選択します。

  4. [音声文字変換のオプション] タブで [V2] を選択します。

次のステップ