o
    Ö­j  ã                   @  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   úh/var/www/html/CropPilot/venv/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 )ÚGoogleCloudSpeechV1Parametersz™Speech-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rŽtƒ ‚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    
ý