Mengirim permintaan transkripsi ke Cloud Speech-to-Text On-Prem

Prasyarat

  1. Selesaikan semua langkah yang diperlukan di panduan memulai sebelum memulai.
  2. Deploy API.
  3. Buat kueri API untuk memastikan API tersebut berfungsi.

Menginstal dependensi

  1. Clone python-speech dan ubah direktori ke direktori contoh.

    $ git clone https://github.com/googleapis/python-speech.git
    $ cd python-speech/samples/snippets
    
  2. Instal pip dan virtualenv jika Anda belum melakukannya. Lihat Panduan Penyiapan Lingkungan Pengembangan Python Google Cloud Platform untuk informasi selengkapnya.

  3. Buat virtualenv. Contoh di bawah ini kompatibel dengan Python 2.7 dan 3.4+.

    $ virtualenv env
    $ source env/bin/activate
    
  4. Instal dependensi yang diperlukan untuk menjalankan contoh.

    $ pip install -r requirements.txt
    

Contoh kode

Contoh kode di bawah menggunakan library google-cloud-speech. Anda dapat menggunakan GitHub untuk menjelajahi sumber dan melaporkan masalah.

Mentranskripsi file audio

Anda dapat menggunakan contoh kode di bawah untuk mentranskripsi file audio menggunakan IP publik atau IP tingkat cluster. Untuk informasi selengkapnya tentang jenis IP, lihat dokumentasi tentang membuat kueri API.

IP publik:

    # Using a Public IP
    $ python transcribe_onprem.py --file_path="../resources/two_channel_16k.wav" --api_endpoint=${PUBLIC_IP}:443

IP tingkat cluster:

    # Using a cluster level IP
    $ kubectl port-forward -n $NAMESPACE $POD 10000:10000
    $ python transcribe_onprem.py --file_path="../resources/two_channel_16k.wav" --api_endpoint="0.0.0.0:10000"

Python

Untuk mengautentikasi ke Speech-to-Text, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

def transcribe_onprem(
    local_file_path: str,
    api_endpoint: str,
) -> speech_v1p1beta1.RecognizeResponse:
    """
    Transcribe a short audio file using synchronous speech recognition on-prem

    Args:
      local_file_path: The path to local audio file, e.g. /path/audio.wav
      api_endpoint: Endpoint to call for speech recognition, e.g. 0.0.0.0:10000

    Returns:
      The speech recognition response
          {
    """
    # api_endpoint = '0.0.0.0:10000'
    # local_file_path = '../resources/two_channel_16k.raw'

    # Create a gRPC channel to your server
    channel = grpc.insecure_channel(target=api_endpoint)
    transport = speech_v1p1beta1.services.speech.transports.SpeechGrpcTransport(
        channel=channel
    )

    client = speech_v1p1beta1.SpeechClient(transport=transport)

    # The language of the supplied audio
    language_code = "en-US"

    # Sample rate in Hertz of the audio data sent
    sample_rate_hertz = 16000

    # Encoding of audio data sent. This sample sets this explicitly.
    # This field is optional for FLAC and WAV audio formats.
    encoding = speech_v1p1beta1.RecognitionConfig.AudioEncoding.LINEAR16
    config = {
        "encoding": encoding,
        "language_code": language_code,
        "sample_rate_hertz": sample_rate_hertz,
    }
    with io.open(local_file_path, "rb") as f:
        content = f.read()
    audio = {"content": content}

    response = client.recognize(request={"config": config, "audio": audio})
    for result in response.results:
        # First alternative is the most probable result
        alternative = result.alternatives[0]
        print(f"Transcript: {alternative.transcript}")

    return response