本指南将引导您完成使用 Google 的 Vertex AI Speech 服务运行 Speech-to-Text 测试的过程。
在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Python 设置说明执行操作。如需了解详情,请参阅 Vertex AI Python API 参考文档。
创建 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”
创建 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"]为 Speech-to-Text 应用构建 Docker 映像:
docker build -t speech-to-text-app .按照配置 Docker 中的说明执行以下操作:
- 配置 Docker,
- 创建 Secret,并
- 将映像上传到 HaaS。
登录用户集群,并使用用户身份生成其 kubeconfig 文件。确保您已将 kubeconfig 路径设置为环境变量:
export KUBECONFIG=${CLUSTER_KUBECONFIG_PATH}在终端中运行以下命令,粘贴您的 API 密钥,以创建 Kubernetes Secret:
kubectl create secret generic gcp-api-key-secret \ --from-literal=GCP_API_KEY='PASTE_YOUR_API_KEY_HERE'此命令会创建一个名为
gcp-api-key-secret的 Secret,其中包含一个键GCP_API_KEY。应用 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 实例网址。HARBOR_PROJECT:Harbor 项目。SECRET:为存储 Docker 凭据而创建的 Secret 的名称。
检查作业状态:
kubectl get jobs/speech-to-text-test-job # It will show 0/1 completions, then 1/1 after it succeeds作业完成后,您可以在 pod 日志中查看输出:
kubectl logs -l job-name=speech-to-text-test-job