使用模型

在實際應用程式或基準測試工作流程中使用已訓練的自訂 Speech-to-Text 模型。只要透過專用端點部署模型,系統就會自動透過辨識器物件取得程式輔助存取權,您可以直接透過 Speech-to-Text V2 API 或 Google Cloud 控制台使用該物件。

事前準備

請確認您已註冊 Google Cloud 帳戶、建立專案、訓練自訂語音模型,並使用端點部署模型。

在 V2 中執行推論

如要使用自訂語音轉文字模型,模型分頁中的模型狀態應為「有效」,且端點分頁中的專屬端點必須已部署

在本範例中, Google Cloud 專案 ID 為 custom-models-walkthrough,對應自訂 Speech-to-Text 模型 quantum-computing-lectures-custom-model 的端點為 quantum-computing-lectures-custom-model-prod-endpoint。可用區域為 us-east1,批次轉錄要求如下:

from google.api_core import client_options
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(
    client_options=client_options.ClientOptions(
      api_endpoint="us-east1-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="projects/custom-models-walkthrough/locations/us-east1/endpoints/quantum-computing-lectures-custom-model-prod-endpoint",
    )
    request = cloud_speech.RecognizeRequest(
        recognizer=f"projects/custom-models-walkthrough/locations/us-east1/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

後續步驟

請參考相關資源,在應用程式中充分運用自訂語音模型。請參閱「評估自訂模型」。