o
    dZh                     @  s   d dl mZ d dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZ er4d dlmZmZmZ d dlmZ G dd	 d	ed
dZG dd ded
dZdddZ	ddddZdS )    )annotations)TYPE_CHECKING	TypedDict)URLError)	AudioData)RequestErrorUnknownValueError)RecognitionConfigRecognizeResponseSpeechContext)Requiredc                   @  s:   e Zd ZU dZded< ded< ded< ded< ded	< d
S )GoogleCloudRecognizerParametersaC  Optional parameters.

    The recognition language is determined by ``language_code``, which is a BCP-47 language tag like ``"en-US"`` (US English). Default: ``"en-US"``.
    A list of supported language tags can be found in the `Speech-to-Text supported languages <https://cloud.google.com/speech/docs/languages>`__.

    If ``preferred_phrases`` is an iterable of phrase strings, those given phrases will be more likely to be recognized over similar-sounding alternatives.
    This is useful for things like keyword/command recognition or adding new phrases that aren't in Google's vocabulary.
    Note that the API imposes certain `restrictions on the list of phrase strings <https://cloud.google.com/speech/limits#content>`__.

    ``show_all``: See :py:func:`recognize`.

    ``model``: You can select the model to get best results. (See `RecognitionConfig's documentation <https://cloud.google.com/python/docs/reference/speech/latest/google.cloud.speech_v1.types.RecognitionConfig>`__ for detail)

    ``use_enhanced``: Set to true to use an enhanced model for speech recognition.
    z	list[str]preferred_phrasesboolshow_allstrlanguage_codemodeluse_enhancedN__name__
__module____qualname____doc____annotations__ r   r   Z/var/www/auris/lib/python3.10/site-packages/speech_recognition/recognizers/google_cloud.pyr      s   
 r   F)totalc                   @  sJ   e Zd ZU dZded< ded< ded< ded	< d
ed< ded< d
ed< dS )GoogleCloudSpeechV1ParameterszSpeech-to-Text V1 API's parameters.

    https://cloud.google.com/python/docs/reference/speech/latest/google.cloud.speech_v1.types.RecognitionConfig
    z)Required[RecognitionConfig.AudioEncoding]encodingzRequired[int]sample_rate_hertzzRequired[str]r   zlist[SpeechContext]speech_contextsr   enable_word_time_offsetsr   r   r   Nr   r   r   r   r   r   .   s   
 r   
audio_datar   recognizer_paramsreturnr	   c                 C  sr   ddl m} |jjj| j|ddd}|dd  }r%|j|dg|d< |d	d
r/d|d< |jdi ||B S )Nr   speechr   zen-US)r   r    r   r   )Zphrasesr!   r   FTr"   r   )google.cloudr'   r	   ZAudioEncodingZFLACsample_ratepopr   )r#   r$   r'   
parametersr   r   r   r   _build_config=   s   

r,   Ncredentials_json_path
str | Nonekwargsstr | RecognizeResponsec              
   K  s6  zddl m} ddlm} W n ty   tdw |r"|j|n| }|jd|j	  kr3dkr7n ndnt
dt|j	ddd	}|j|d
}t|| }	z	|j|	|d}
W n# |yi } zt|d}~w ty| } ztd|jd}~ww |dr|
S t|
jdkrt ddd |
jD }|S )a  Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the Google Cloud Speech-to-Text V1 API.

    This function requires a Google Cloud Platform account; see the `Set up Speech-to-Text <https://cloud.google.com/speech-to-text/docs/before-you-begin>`__ for details and instructions. Basically, create a project, enable billing for the project, enable the Google Cloud Speech API for the project.
    And create local authentication credentials for your user account. The result is a JSON file containing the API credentials. You can specify the JSON file by ``credentials_json_path``. If not specified, the library will try to automatically `find the default API credentials JSON file <https://developers.google.com/identity/protocols/application-default-credentials>`__.

    Returns the most likely transcription if ``show_all`` is False (the default). Otherwise, returns the raw API response as a JSON dictionary.
    For other parameters, see :py:class:`GoogleCloudRecognizerParameters`.

    Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if the speech recognition operation failed, if the credentials aren't valid, or if there is no Internet connection.
    r   )GoogleAPICallErrorr&   zXmissing google-cloud-speech module: ensure that google-cloud-speech is set up correctly.i@  i  N   )Zconvert_rateZconvert_width)content)configaudioz"recognition connection failed: {0}r    c                 s  s     | ]}|j d  j V  qdS )r   N)Zalternatives
transcriptstrip).0resultr   r   r   	<genexpr>   s
    
zrecognize.<locals>.<genexpr>)Zgoogle.api_core.exceptionsr1   r(   r'   ImportErrorr   ZSpeechClientZfrom_service_account_jsonZget_flac_datar)   maxminZRecognitionAudior,   copy	recognizer   formatreasongetlenresultsr   join)Z
recognizerr#   r-   r/   r1   r'   clientZ	flac_datar5   r4   responseer7   r   r   r   r@   Q   sN   	


r@   )r#   r   r$   r   r%   r	   )N)r#   r   r-   r.   r/   r   r%   r0   )
__future__r   typingr   r   urllib.errorr   Zspeech_recognition.audior   Zspeech_recognition.exceptionsr   r   Zgoogle.cloud.speechr	   r
   r   Ztyping_extensionsr   r   r   r,   r@   r   r   r   r   <module>   s    
