o
    ìcj	                     @  sR   d Z ddlmZ ddlZddlZddlmZmZmZ e	e
ZG dd deZdS )u;   STT vía Google Cloud Speech-to-Text (batch por utterance).    )annotationsN)AudioFormatSpeechToTextTranscriptResultc                   @  s<   e Zd ZdZddddd
dZdd ZddddddZdS )GoogleSpeechToTextgooglezes-ARdefault)language_codemodelr	   strr
   returnNonec                C  s   || _ || _d | _d S )N)r	   r
   _client)selfr	   r
    r   >/var/www/html/bot-asterisk-ari/backend/voice/stt/google_stt.py__init__   s   
zGoogleSpeechToText.__init__c                 C  s,   | j d u rddlm} | | _ || _| j S )Nr   	speech_v1)r   google.cloudr   SpeechClient_speech)r   speechr   r   r   _get_client   s
   

zGoogleSpeechToText._get_clientN)audio_formatr	   audiobytesr   AudioFormat | None
str | Noner   c                  sh   |pt  |p
j stdddS d	 fdd}z	t|I d H W S  ty3   td  w )
N Ttextis_finalr   r   c                    s   ddl m}   }jdkr| jjjn| jjj}| j|jj	dj
d}|j|| j dd}|js;tddd	S |jd jd }t|jpHd d|jrUt|jd
S d d
S )Nr   r   mulawT)encodingsample_rate_hertzr	   r
   enable_automatic_punctuationaudio_channel_count)content)configr   r   r    )r!   r"   
confidence)r   r   r   r$   RecognitionConfigAudioEncodingMULAWLINEAR16sample_rate_hzr
   channels	recognizeRecognitionAudioresultsr   alternatives
transcriptstripr*   float)r   clientr$   r)   responsealtr   fmtlangr   r   r   _sync.   s8   


z+GoogleSpeechToText.recognize.<locals>._syncu   Google STT falló)r   r   )r   r	   r   asyncio	to_thread	Exceptionlogger	exception)r   r   r   r	   r>   r   r;   r   r1   "   s   


zGoogleSpeechToText.recognize)r	   r   r
   r   r   r   )r   r   r   r   r	   r   r   r   )__name__
__module____qualname__provider_namer   r   r1   r   r   r   r   r      s    
r   )__doc__
__future__r   r?   loggingvoice.protocolsr   r   r   	getLoggerrD   rB   r   r   r   r   r   <module>   s    
