Speech-to-Text を試す

このガイドでは、Google の Vertex AI Speech サービスを使用して Speech-to-Text テストを実行するプロセスについて説明します。

このサンプルを試す前に、Vertex AI クイックスタート: クライアント ライブラリの使用にある Python の設定手順を行ってください。詳細については、Vertex AI Python API のリファレンス ドキュメントをご覧ください。

  1. Python ファイル speech-to-text-test.py を作成します。次のように、image_uri_to_test の値をソースイメージの URI に置き換えます。

    from google.cloud import speech
    
    def transcribe_gcs_audio(gcs_uri: str) -> speech.RecognizeResponse:
        client = speech.SpeechClient()
    
        audio = speech.RecognitionAudio(uri=gcs_uri)
        config = speech.RecognitionConfig(
            encoding=speech.RecognitionConfig.AudioEncoding.FLAC,
            sample_rate_hertz=16000,
            language_code="en-US", # Specify the language code (e.g., "en-US" for US English)
            # You can add more features here, e.g.:
            # enable_automatic_punctuation=True,
            # model="default" # or "latest_long", "phone_call", "video", "chirp" (v2 API)
        )
    
        # Performs synchronous speech recognition on the audio file
        response = client.recognize(config=config, audio=audio)
    
        # Print the transcription
        for result in response.results:
            print(f"Transcript: {result.alternatives[0].transcript}")
            if result.alternatives[0].confidence:
                print(f"Confidence: {result.alternatives[0].confidence:.2f}")
    
        return response
    
    if __name__ == "__main__":
        # Replace with the URI of your audio file in Google Cloud Storage
        audio_file_uri = "AUDIO_FILE_URI"
    
        print(f"Transcribing audio from: {audio_file_uri}")
        transcribe_gcs_audio(audio_file_uri)
    

    次のように置き換えます。

    • AUDIO_FILE_URI: 音声ファイルの URI「gs://your-bucket/your-image.png
  2. Dockerfile を作成します。

    ROM python:3.9-slim
    
    WORKDIR /app
    
    COPY speech-to-text-test.py /app/
    
    # Install 'requests' for HTTP calls
    RUN pip install --no-cache-dir requests
    
    CMD ["python", "speech-to-text-test.py"]
    
  3. Speech-to-Text アプリケーションの Docker イメージをビルドします。

    docker build -t speech-to-text-app .
    
  4. Docker の構成の手順に沿って、次の操作を行います。

    1. Docker を構成する。
    2. シークレットを作成する。
    3. イメージを HaaS にアップロードします。
  5. ユーザー クラスタにログインし、ユーザー ID を使用して kubeconfig ファイルを生成します。kubeconfig パスを環境変数として設定していることを確認します。

    export KUBECONFIG=${CLUSTER_KUBECONFIG_PATH}
    
  6. ターミナルで次のコマンドを実行し、API キーを貼り付けて、Kubernetes Secret を作成します。

    kubectl create secret generic gcp-api-key-secret \
      --from-literal=GCP_API_KEY='PASTE_YOUR_API_KEY_HERE'
    

    このコマンドは、鍵 GCP_API_KEY を含む gcp-api-key-secret という名前のシークレットを作成します。

  7. Kubernetes マニフェストを適用します。

    apiVersion: batch/v1
    kind: Job
    metadata:
      name: speech-to-text-test-job
    spec:
      template:
        spec:
          containers:
          - name: speech-to-text-test-container
            image: HARBOR_INSTANCE_URL/HARBOR_PROJECT/speech-to-text-app:latest # Your image path
            # Mount the API key from the secret into the container
            # as an environment variable named GCP_API_KEY.
            imagePullSecrets:
            - name: SECRET
            envFrom:
            - secretRef:
                name: gcp-api-key-secret
          restartPolicy: Never
      backoffLimit: 4
    
    

    次のように置き換えます。

    • HARBOR_INSTANCE_URL: Harbor インスタンスの URL。
    • HARBOR_PROJECT: Harbor プロジェクト。
    • SECRET: Docker 認証情報を保存するために作成されたシークレットの名前。
  8. ジョブのステータスを確認します。

    kubectl get jobs/speech-to-text-test-job
    # It will show 0/1 completions, then 1/1 after it succeeds
    
  9. ジョブが完了したら、Pod のログで出力を確認できます。

    kubectl logs -l job-name=speech-to-text-test-job