# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


# [START speech_transcribe_with_model_adaptation]

from google.cloud import speech_v1p1beta1 as speech


def transcribe_with_model_adaptation(
    project_id, location, storage_uri, custom_class_id, phrase_set_id
):

    """
    Create`PhraseSet` and `CustomClasses` to create custom lists of similar
    items that are likely to occur in your input data.
    """

    # Create the adaptation client
    adaptation_client = speech.AdaptationClient()

    # The parent resource where the custom class and phrase set will be created.
    parent = f"projects/{project_id}/locations/{location}"

    # Create the custom class
    custom_class_response = adaptation_client.create_custom_class(
        {
            "parent": parent,
            "custom_class_id": custom_class_id,
            "custom_class": {
                "items": [
                    {"value": "sushido"},
                    {"value": "altura"},
                    {"value": "taneda"},
                ]
            },
        }
    )

    # Create the phrase set
    phrase_set_response = adaptation_client.create_phrase_set(
        {
            "parent": parent,
            "phrase_set_id": phrase_set_id,
            "phrase_set": {
                "boost": 10,
                "phrases": [{"value": f"Visit restaurants like ${custom_class_id}"}],
            },
        }
    )

    # The next section shows how to use the newly created custom
    # class and phrase set to send a transcription request with speech adaptation

    # Speech adaptation configuration
    speech_adaptation = speech.SpeechAdaptation(
        phrase_sets=[phrase_set_response], custom_classes=[custom_class_response]
    )

    # speech configuration object
    config = speech.RecognitionConfig(
        encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=24000,
        language_code="en-US",
        adaptation=speech_adaptation,
    )

    # The name of the audio file to transcribe
    # storage_uri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE]

    audio = speech.RecognitionAudio(uri=storage_uri)

    # Create the speech client
    speech_client = speech.SpeechClient()

    response = speech_client.recognize(config=config, audio=audio)

    for result in response.results:
        print("Transcript: {}".format(result.alternatives[0].transcript))

    # [END speech_transcribe_with_model_adaptation]
    return response.results[0].alternatives[0].transcript

@@ -1191,16 +1191,25 @@
 om class
+ resource
 %0A    cus
@@ -1582,24 +1582,74 @@
     %7D%0A    )%0A
+    custom_class_name = custom_class_response.name
 %0A    # Creat
@@ -1664,16 +1664,25 @@
 rase set
+ resource
 %0A    phr
@@ -1956,18 +1956,20 @@
 m_class_
-id
+name
 %7D%22%7D%5D,%0A  
@@ -1993,24 +1993,70 @@
     %7D%0A    )%0A
+    phrase_set_name = phrase_set_response.name
 %0A    # The n
@@ -2297,16 +2297,26 @@
 rase_set
+_reference
 s=%5Bphras
@@ -2325,55 +2325,11 @@
 set_
-response%5D, custom_classes=%5Bcustom_class_respons
+nam
 e%5D%0A 

Update speech_model_adaptation_beta.py