
    F                         S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJr  SSK	J
r
  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SrSr\R4                  S 5       r " S S\5      rg)zSpeech-to-text V2 client.    )absolute_import)division)unicode_literalsN)
list_pager)storage_util)apis)
exceptions)waiter)flag_validations)
properties)	resources)files)urllibspeechv2c              #      #    [        [        R                  R                  [        5      nUR                  5       n UR                  U 5        Sv   UR                  U5        g! UR                  U5        f = f7f)zDContext manager to override an API's endpoint overrides for a while.N)getattrr   VALUESapi_endpoint_overrides	_API_NAMEGetSet)overrideendpoint_propertyold_endpoints      .lib/googlecloudsdk/api_lib/ml/speech/client.py_OverrideEndpointr   (   sf      ..	 #&&(,((#	,','s   9A9A# A9#A66A9c                       \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rSS jrS rS rS rS rS rS rS rSS jr SS jr  SS jr  SS jr SS jr S S jr SS jrSrg)!SpeechV2Client6   zSpeech V2 API client wrappers.c                 B   [         R                  " [        [        5      n[        R
                  R                  UR                  5      R                  U l	        [         R                  " [        [        5      n[        R                  " 5       U l        U R                  R                  [        [        5        UR                  R                   R"                  UR                  R                   R$                  UR                  R                   R&                  S.U l        X l        g )N)LINEAR16MULAWALAW)r   GetClientClassr   _API_VERSIONr   parseurlsplitBASE_URLnetloc_net_locGetMessagesModuler   Registry_resource_parserRegisterApiByNameExplicitDecodingConfigEncodingValueValuesEnumr"   r#   r$   _encoding_to_message	_messages)selfclient_classmessagess      r   __init__SpeechV2Client.__init__9   s    &&y,?LLL)),*?*?@GGDM%%i>H%..0D++I|D ++CCLL00HHNN//GGLL!D N    c                     [        SR                  XR                  5      5         [        R                  " [
        [        5      sS S S 5        $ ! , (       d  f       g = f)Nzhttps://{}-{}/)r   formatr+   r   GetClientInstancer   r&   r4   locations     r   _GetClientForLocation$SpeechV2Client._GetClientForLocationI   s7    	+228]]K	L##I|< 
M	L	Ls   A
Ac                 8    U R                  U5      R                  $ N)r?   projects_locations_recognizersr=   s     r   _RecognizerServiceForLocation,SpeechV2Client._RecognizerServiceForLocationM   s    %%h/NNNr9   c                 8    U R                  U5      R                  $ rB   )r?   projects_locations_operationsr=   s     r   _OperationsServiceForLocation,SpeechV2Client._OperationsServiceForLocationP   s    %%h/MMMr9   c                 8    U R                  U5      R                  $ rB   )r?   projects_locationsr=   s     r   _LocationsServiceForLocation+SpeechV2Client._LocationsServiceForLocationS   s    %%h/BBBr9   c                 T   U R                   R                  US9nX6l        XFl        XVl        U R                   R                  UR                  SS9R                  5       UR                  5       US9nU R                  UR                  5       R                  5       S9R                  U5      $ )z9Call API CreateRecognizer method with provided arguments.)displayNamezspeech.projects.locations)parent_collection)parentrecognizerId
recognizerr>   )r3   
RecognizermodellanguageCodesdefaultRecognitionConfig/SpeechProjectsLocationsRecognizersCreateRequestParentRelativeNameNamerD   Create)r4   resourcedisplay_namerV   language_codesrecognition_configrS   requests           r   CreateRecognizerSpeechV2Client.CreateRecognizerV   s     **|*DJ-*<'nnLL9  

,.]]_ M G --"'') . fWor9   c                     U R                   R                  UR                  5       S9nU R                  UR	                  5       R                  5       S9R                  U5      $ NnamerT   )r3   ,SpeechProjectsLocationsRecognizersGetRequestr[   rD   rZ   r\   r   r4   r^   rb   s      r   GetRecognizerSpeechV2Client.GetRecognizerq   s^    nnII""$ J G --"'') . 	c'lr9   c                     U R                   R                  UR                  5       S9nU R                  UR	                  5       R                  5       S9R                  U5      $ rf   )r3   /SpeechProjectsLocationsRecognizersDeleteRequestr[   rD   rZ   r\   Deleterj   s      r   DeleteRecognizerSpeechV2Client.DeleteRecognizery   s^    nnLL""$ M G --"'') . fWor9   Nc           	          U R                   R                  UR                  5       S9nU(       a  X4l        [        R
                  " U R                  UR                  5       5      UUSUSS9$ )N)rQ   pageSizerecognizerslimitbatch_size_attribute
batch_sizefield)r3   -SpeechProjectsLocationsRecognizersListRequestr[   	page_sizer   YieldFromListrD   r\   )r4   location_resourcerv   r{   rb   s        r   ListRecognizersSpeechV2Client.ListRecognizers   sm    nnJJ --/ K G ###**+<+A+A+CD' r9   c                    U R                   R                  5       nUb  X'l        UR                  S5        Ub  X7l        UR                  S5        Ub  XGl        UR                  S5        XWl        U R                   R                  UR                  5       USR                  U5      S9nU R                  UR                  5       R                  5       S9R                  U5      $ )z9Call API UpdateRecognizer method with provided arguments.r_   rV   r`   ,)rh   rS   
updateMaskrT   )r3   rU   rO   appendrV   rW   rX   .SpeechProjectsLocationsRecognizersPatchRequestr[   joinrD   rZ   r\   Patch)	r4   r^   r_   rV   r`   ra   update_maskrS   rb   s	            r   UpdateRecognizerSpeechV2Client.UpdateRecognizer   s     **,J+(!!!/)**<'nnKK""$88K( L G
 --"'') . eGnr9   c                 
   U R                   R                  5       n[        R                  R	                  U5      (       a  [
        R                  " U5      Ul        O*[        R                  R                  U5      (       a  X%l        U R                  UR                  5       R                  5       S9nX5l        SR!                  U5      Ul        U R                   R%                  UUR'                  5       S9nUR)                  U5      $ )z2Call API Recognize method with provided arguments.rT   r   )recognizeRequestrS   )r3   RecognizeRequestospathisfiler   ReadBinaryFileContentscontentr   ObjectReferenceIsStorageUrlurirD   rZ   r\   configr   
configMask2SpeechProjectsLocationsRecognizersRecognizeRequestr[   	Recognize)r4   r^   audiora   r   recognize_reqrecognizer_servicerb   s           r   RunShortSpeechV2Client.RunShort   s     NN335M	ww~~e#::5Am		%	%	2	25	9	9;;"'') <  ."xx4MnnOO&((* P G ''00r9   c                    U R                   R                  US9nU R                   R                  UR                  5       U/S9nU R	                  UR                  5       R                  5       S9nX6l        U R                   R                  U R                   R                  5       S9Ul
        SR                  U5      Ul        UR                  U5      $ )z@Call API Recognize method with provided arguments in batch mode.)r   )rS   r   rT   )inlineResponseConfigr   )r3   BatchRecognizeFileMetadataBatchRecognizeRequestr[   rD   rZ   r\   r   RecognitionOutputConfigInlineOutputConfigrecognitionOutputConfigr   r   BatchRecognize)r4   r^   r   ra   r   batch_audio_metadatar   r   s           r   RunBatchSpeechV2Client.RunBatch   s      >>DDDONN88((*#$ 9 M
 ;;"'') <  . 	..!%!B!B!D 	/ 	
 )  #xx4M,,];;r9   c                 N    U R                   R                  UR                  S5      $ )z$Converts an Operation to a Resource.z$speech.projects.locations.operations)r.   ParseRelativeNamerh   )r4   	operations     r   GetOperationRefSpeechV2Client.GetOperationRef   s%      22> r9   c                     [         R                  " U R                  U5      U R                  U5      S9n[         R                  " UUUSSS9$ )aw  Waits for a Recognizer operation to complete.

Polls the Speech Operation service until the operation completes, fails, or
  max_wait_ms elapses.

Args:
  location: The location of the resource.
  operation_ref: A Resource created by GetOperationRef describing the
    Operation.
  message: The message to display to the user while they wait.

Returns:
  An Endpoint entity.
)result_serviceoperation_serviced   i N  )polleroperation_refmessagepre_start_sleep_msmax_wait_ms)r
   CloudOperationPollerrD   rH   WaitForr4   r>   r   r   r   s        r   WaitForRecognizerOperation)SpeechV2Client.WaitForRecognizerOperation   sR     ((99(C<<XFF
 >># r9   c                 ~    [         R                  " U R                  U5      S 5      n[         R                  " UUUSS9$ )a|  Waits for a Batch Recognize operation to complete.

Polls the Speech Operation service until the operation completes, fails, or
  max_wait_ms elapses.

Args:
  location: The location of the resource.
  operation_ref: A Resource created by GetOperationRef describing the
    Operation.
  message: The message to display to the user while they wait.

Returns:
  An Endpoint entity.
c                     U $ rB    )xs    r   <lambda>?SpeechV2Client.WaitForBatchRecognizeOperation.<locals>.<lambda>  s    !r9   i \&)r   wait_ceiling_ms)r
   CloudOperationPollerNoResourcesrH   r   r   s        r   WaitForBatchRecognizeOperation-SpeechV2Client.WaitForBatchRecognizeOperation  sD     33**84F
 >> 	 r9   c                     U R                   R                  UR                  5       S9nU R                  UR	                  5       S9R                  U5      $ rf   )r3   !SpeechProjectsLocationsGetRequestr[   rL   r\   r   )r4   r}   rb   s      r   GetLocationSpeechV2Client.GetLocation(  sV    nn>>++- ? G ,,"'') - 	c'lr9   c           	         U R                   R                  [        R                  R                  R
                  R                  5       S9nU(       a  Xl        U(       a  X4l        [        R                  " U R                  S5      UUSUSS9$ )Nrg   globalrs   	locationsru   )r3   "SpeechProjectsLocationsListRequestr   r   coreprojectr   filterr{   r   r|   rL   )r4   
filter_strrv   r{   rb   s        r   ListLocationsSpeechV2Client.ListLocations0  s}    nn??##++//1 @ G !n###))(3' r9   c                     U R                   R                  5       nUb  Xl        Ub  X$l        Uc  XC4$ Ub  UR	                  S5        Ub  UR	                  S5        XC4$ )zUcreates a recognition config object and initializes it with model and language codes.rV   r`   )r3   RecognitionConfigrV   rW   r   )r4   rV   r`   r   ra   s        r   InitializeRecognitionConfig*SpeechV2Client.InitializeRecognitionConfigA  sp     99;!&!)7&,,!!)***r9   c           	      f    U R                  UUR                  UR                  UR                  UUS9$ )N)default_to_auto_decoding_configr   )*_InitializeDecodingConfigRecognizerCommandencodingsample_rateaudio_channel_count)r4   ra   argsr   r   s        r    InitializeDecodingConfigFromArgs/SpeechV2Client.InitializeDecodingConfigFromArgsT  s>     ::  (G ;  r9   c                    Ub  US:X  a   U R                   R                  5       Ul        OU[        R                  ;   af  U R                   R                  5       Ul        U R                  U   UR                  l        Ub  UUR                  l	        Ub  UUR                  l
        O=[        R                  " SS5      eU(       a  U R                   R                  5       Ul        Uc  X4$ US:X  a  UR                  S5        O>U[        R                  ;   a  UR                  S5        OU(       a  UR                  S5        UbM  UR                  c  U R                   R                  5       Ul        X1R                  l	        UR                  S5        UbN  UR                  c  U R                   R                  5       Ul        UUR                  l
        UR                  S5        X4$ )zkInitializes encoding type based on auto (or explicit decoding option), sample rate and audio channel count.AUTOz
--encodingz;[--encoding] must be set to LINEAR16, MULAW, ALAW, or AUTO.auto_decoding_configexplicit_decoding_configz*explicit_decoding_config.sample_rate_hertzz,explicit_decoding_config.audio_channel_count)r3   AutoDetectDecodingConfigautoDecodingConfigr   EXPLICIT_ENCODING_OPTIONSr0   explicitDecodingConfigr2   r   sampleRateHertzaudioChannelCountr	   InvalidArgumentExceptionr   )r4   ra   r   r   r   r   r   s          r   r   9SpeechV2Client._InitializeDecodingConfigRecognizerCommande  s    	V	NN335 	- 'AAANN113 	1
 %%h/ 	11: " 
3
3
C *! 
3
3
E 11I
 	
 
)
..
1
1
3 + ,,6/0	%??	?34	(/0		2	2	:NN113 	1 CN//?EF&		2	2	:NN113 	1  //A GH**r9   c                 f    U R                  UR                  UR                  UR                  U5      $ )z&Initializes PhraseSets based on hints.)+_InitializeAdaptationConfigRecognizeRequesthint_phraseshint_phrase_sets
hint_boost)r4   r   r   s      r   "InitializeAdaptationConfigFromArgs1SpeechV2Client.InitializeAdaptationConfigFromArgs  s0     ;;400$//; r9   c           
         / nU(       am  U R                   R                  U Vs/ s H  nU R                   R                  USS9PM     snUS9nU R                   R                  US9nUR	                  U5        U(       a3  U H-  n	U R                   R                  U	S9n
UR	                  U
5        M/     U R                   R                  US9nUb  UR	                  S5        X4$ s  snf )z8Initializes PhraseSets based on phrases and phrase sets.      @)valueboost)phrasesr   )inlinePhraseSet)	phraseSet)
phraseSets
adaptation)r3   	PhraseSetPhraseAdaptationPhraseSetr   SpeechAdaptation)r4   r   r   r   r   speech_adaptation_phrase_setshint_phraseinline_phrase_setinline_adaptation_phrase_sethint_phrase_setadaptation_phrase_setspeech_adaptation_configs               r   r   :SpeechV2Client._InitializeAdaptationConfigRecognizeRequest  s    %'!..22 ".!-+ nn##+S#A!-  3  &*^^%G%G+ &H &" $**+GH-/ $ B B% !C !
 	&,,-BC	 .  $~~>>0  ?   &#001s   $C!c                    U R                   R                  5       n/ nUR                  b"  UR                  Ul        UR	                  S5        UR
                  b"  UR
                  Ul        UR	                  S5        UR                  b"  UR                  Ul        UR	                  S5        UR                  b"  UR                  Ul
        UR	                  S5        UR                  b"  UR                  Ul        UR	                  S5        UR                  b"  UR                  Ul        UR	                  S5        UR                  bb  UR                   bU  U R                   R#                  UR                  UR                   S9Ul        UR	                  S5        UR	                  S	5        UR&                  (       aA  U R                   R                  R(                  R*                  Ul        UR	                  S
5        OMUR&                  b@  U R                   R                  R(                  R.                  Ul        UR	                  S
5        UR0                  b"  UR0                  Ul        UR	                  S5        Ub  UR5                  U5        X24$ )z.Collects features from the provided arguments.zfeatures.profanityFilterzfeatures.enableWordTimeOffsetszfeatures.enableWordConfidencez#features.enableAutomaticPunctuationz features.enableSpokenPunctuationzfeatures.enableSpokenEmojis)minSpeakerCountmaxSpeakerCountz*features.diarizationConfig.minSpeakerCountz*features.diarizationConfig.maxSpeakerCountzfeatures.multiChannelModezfeatures.maxAlternatives)r3   RecognitionFeaturesprofanity_filterprofanityFilterr   enable_word_time_offsetsenableWordTimeOffsetsenable_word_confidenceenableWordConfidenceenable_automatic_punctuationenableAutomaticPunctuationenable_spoken_punctuationenableSpokenPunctuationenable_spoken_emojisenableSpokenEmojismin_speaker_countmax_speaker_countSpeakerDiarizationConfigdiarizationConfigseparate_channel_recognitionMultiChannelModeValueValuesEnum SEPARATE_RECOGNITION_PER_CHANNELmultiChannelModeMULTI_CHANNEL_MODE_UNSPECIFIEDmax_alternativesmaxAlternativesextend)r4   r   r   features_configinner_update_masks        r   InitializeASRFeaturesFromArgs,SpeechV2Client.InitializeASRFeaturesFromArgs  sB    nn88:O((,(=(=o%9:$$0.2.K.Ko+?@"".-1-H-Ho*>?((4

+
+ 0 DE%%1040N0No-AB  ,+/+D+Do(<=*"". ..
1
1"44"44 2  ' KLKL((
..
,
,
L
L
m
m & :;		*	*	6
..
,
,
L
L
k
k & :;((,(=(=o%9:*+''r9   )r2   r3   r+   r.   )NN)NNN)FNrB   )r   N)__name__
__module____qualname____firstlineno____doc__r7   r?   rD   rH   rL   rc   rk   rp   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r'  __static_attributes__r   r9   r   r   r   6   s    & =ONC6B18<<86$ :>+. ',. ',F+V  IM 1J :(r9   r   )r-  
__future__r   r   r   
contextlibr   apitools.base.pyr   googlecloudsdk.api_lib.storager   googlecloudsdk.api_lib.utilr   r	   r
   $googlecloudsdk.command_lib.ml.speechr   googlecloudsdk.corer   r   googlecloudsdk.core.utilr   	six.movesr   r   r&   contextmanagerr   objectr   r   r9   r   <module>r:     sk      &  '  	 ' 7 , 2 . A * ) *  	 
( 
(](V ](r9   