Skip to content

Instantly share code, notes, and snippets.

@petitchamp
Forked from ecovictoriano/python-gcp-stt.md
Created February 2, 2023 11:03
Show Gist options
  • Save petitchamp/afe5f7143aa5cfbaf68742bf14f1651f to your computer and use it in GitHub Desktop.
Save petitchamp/afe5f7143aa5cfbaf68742bf14f1651f to your computer and use it in GitHub Desktop.
Transcribe audio file to text (speech-to-text) using Google Cloud Platform's Speech API

Transcribe audio file to text (speech-to-text) using Google Cloud Platform's Speech API

Convert audio to text using GCP Speech API

Requirements

pip install --upgrade google-cloud-speech

Export the GCP credential env and execute the request

#!/bin/bash

export GOOGLE_APPLICATION_CREDENTIALS=$(pwd)/credentials-file.json

python gcp.py
# gcp.py
import io, os, subprocess, json
from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types

def transcribe_file(speech_file):
  client = speech.SpeechClient()

  with io.open(speech_file, 'rb') as audio_file:
    content = audio_file.read()

  audio = types.RecognitionAudio(content=content)
  config = types.RecognitionConfig(
    encoding          = enums.RecognitionConfig.AudioEncoding.LINEAR16,
    sample_rate_hertz = 16000,
    language_code     = 'en-GB'
    # language_code     = 'en-US'
  )

  response = client.recognize(config, audio)
  return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment