Provare Speech-to-Text

Questa guida illustra la procedura per eseguire un test di Speech-to-Text utilizzando il servizio Vertex AI Speech di Google.

Prima di provare questo esempio, segui le istruzioni di configurazione di Python nella guida rapida di Vertex AI per l'utilizzo delle librerie client. Per saperne di più, consulta la documentazione di riferimento dell'API Python di Vertex AI.

  1. Crea un file Python speech-to-text-test.py. Sostituisci il valore image_uri_to_test con l'URI di un'immagine di origine, come mostrato:

    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)
    

    Sostituisci quanto segue:

    • AUDIO_FILE_URI: l'URI di un file audio "gs://your-bucket/your-image.png"
  2. Crea un 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. Crea l'immagine Docker per l'applicazione Speech-to-Text:

    docker build -t speech-to-text-app .
    
  4. Segui le istruzioni riportate in Configura Docker per:

    1. Configura Docker,
    2. Crea un secret e
    3. Carica l'immagine su HaaS.
  5. Accedi al cluster utente e genera il relativo file kubeconfig con un'identità utente. Assicurati di impostare il percorso kubeconfig come variabile di ambiente:

    export KUBECONFIG=${CLUSTER_KUBECONFIG_PATH}
    
  6. Crea un secret Kubernetes eseguendo il seguente comando nel terminale e incollando la chiave API:

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

    Questo comando crea un secret denominato gcp-api-key-secret con una chiave GCP_API_KEY.

  7. Applica il manifest 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
    
    

    Sostituisci quanto segue:

    • HARBOR_INSTANCE_URL: l'URL dell'istanza Harbor.
    • HARBOR_PROJECT: il progetto Harbor.
    • SECRET: il nome del secret creato per archiviare le credenziali Docker.
  8. Controlla lo stato del job:

    kubectl get jobs/speech-to-text-test-job
    # It will show 0/1 completions, then 1/1 after it succeeds
    
  9. Al termine del job, puoi visualizzare l'output nei log del pod:

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