שליחת בקשת תמלול אל Cloud Speech-to-Text On-Prem

דרישות מוקדמות

  1. צריך להשלים את כל השלבים הנדרשים במדריך להתחלה מהירה לפני שמתחילים.
  2. פורסים את ה-API.
  3. שולחים שאילתה ל-API כדי לוודא שהוא פועל.

התקנת יחסי תלות

  1. משכפלים את python-speech ומשנים את הספרייה לספריית הדוגמה.

    $ git clone https://github.com/googleapis/python-speech.git
    $ cd python-speech/samples/snippets
    
  2. אם עוד לא עשיתם את זה, צריך להתקין את pip ואת virtualenv. מידע נוסף זמין במדריך להגדרת סביבת הפיתוח בשפת Python ב-Google Cloud Platform.

  3. יצירת virtualenv. הדוגמאות שבהמשך תואמות ל-Python בגרסאות 2.7 ו-3.4 ואילך.

    $ virtualenv env
    $ source env/bin/activate
    
  4. מתקינים את יחסי התלות שנדרשים להפעלת הדוגמאות.

    $ pip install -r requirements.txt
    

דוגמת קוד

בדוגמת הקוד שבהמשך נעשה שימוש בספריית google-cloud-speech. אתם יכולים להשתמש ב-GitHub כדי לבדוק את המקור ולדווח על בעיות.

תמלול של קובץ אודיו

אפשר להשתמש בדוגמת הקוד שבהמשך כדי לתמלל קובץ אודיו באמצעות כתובת IP ציבורית או כתובת IP ברמת האשכול. מידע נוסף על סוגי כתובות IP מופיע במסמכי העזרה בנושא שאילתות ב-API.

כתובת IP ציבורית:

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

כתובת IP ברמת האשכול:

    # 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

כדי לבצע אימות ב-Speech-to-Text, צריך להגדיר את Application Default Credentials. מידע נוסף זמין במאמר הגדרת אימות לסביבת פיתוח מקומית.

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