在實際工作環境應用程式或基準化工作流程中使用訓練好的 Custom Speech-to-Text 模型。透過專用端點部署模型後,您會自動透過辨識器物件取得程式輔助存取權,可直接透過 Speech-to-Text V2 API 或 Google Cloud 控制台使用。
事前準備
請確認您已註冊 Google Cloud 帳戶、建立專案、訓練自訂語音模型,並透過端點部署模型。
在 V2 中執行推論
如要使用自訂語音轉文字模型,「模型」分頁中的模型狀態應為「有效」,且「端點」分頁中的專屬端點必須為「已部署」。
以專案 ID 為 custom-models-walkthrough
的專案為例,對應於自訂 Speech-to-Text 模型 quantum-computing-lectures-custom-model
的端點為 quantum-computing-lectures-custom-model-prod-endpoint
。 Google Cloud 這項功能適用於 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
後續步驟
請參閱相關資源,瞭解如何在應用程式中運用自訂語音模型。請參閱「評估自訂模型」。