o
    i                     @  s   d dl mZ d dlmZmZmZ d dlmZ d dlm	Z	 d dl
mZmZ er8d dlmZmZmZ d dlmZ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cast)URLError)	AudioData)RequestErrorUnknownValueError)RecognitionConfigRecognizeResponseSpeechContext)RequiredUnpackc                   @  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   ^/home/kuhnn/.local/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  s|   ddl m} t|jj|jjj| j|ddd}|dd  }r*|j|dg|d< |d	d
r4d|d< |jdi ||B S )Nr   speechr   zen-US)r!   r"   r   r   )phrasesr#   r   FTr$   r   )	google.cloudr)   r   r
   AudioEncodingFLACsample_ratepopr   )r%   r&   r)   
parametersr   r   r   r   _build_config=   s   

r1   Ncredentials_json_path
str | Nonekwargs'Unpack[GoogleCloudRecognizerParameters]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   )convert_rateconvert_width)content)configaudioz"recognition connection failed: {0}r    c                 s  s     | ]}|j d  j V  qdS )r   N)alternatives
transcriptstrip).0resultr   r   r   	<genexpr>   s
    
zrecognize.<locals>.<genexpr>)google.api_core.exceptionsr7   r+   r)   ImportErrorr   SpeechClientfrom_service_account_jsonget_flac_datar.   maxminRecognitionAudior1   copy	recognizer   formatreasongetlenresultsr	   join)
recognizerr%   r2   r4   r7   r)   client	flac_datar=   r<   responseer@   r   r   r   rN   Q   sN   	


rN   )r%   r   r&   r   r'   r
   )N)r%   r   r2   r3   r4   r5   r'   r6   )
__future__r   typingr   r   r   urllib.errorr   speech_recognition.audior   speech_recognition.exceptionsr   r	   google.cloud.speechr
   r   r   typing_extensionsr   r   r   r    r1   rN   r   r   r   r   <module>   s    
