o
    z­j!  ã                   @   sj   d dl mZmZmZ d dlmZ d dlZd dlZeƒ ZG dd„ deƒZ	e 
d¡edƒfde	fd	d
„ƒZdS )é    )Ú	APIRouterÚBodyÚHTTPException)Ú	BaseModelNc                   @   s"   e Zd ZU eed< dZeed< dS )ÚTTSInputÚtextÚenÚlanguageN)Ú__name__Ú
__module__Ú__qualname__ÚstrÚ__annotations__r	   © r   r   ú8/var/www/html/CropPilot/backend/app/api/endpoints/tts.pyr      s   
 r   z/tts.Ú	tts_inputc              
   Ã   s.  �zddl m} W n ty } ztddd�|‚d }~ww ze| ¡ }|j| jd�}|j| j|j	j
d�}|j|jjd�}|j|||d	�}d
t ¡ › d�}tj dd|¡}	tjtj |	¡dd� t|	dƒ�}
|
 |j¡ W d   ƒ n1 stw   Y  d|› �}d|iW S  ty– } ztdt|ƒd�‚d }~ww )Nr   )Útexttospeechi÷  zXGoogle Cloud Text-to-Speech is not installed. Run: pip install google-cloud-texttospeech)Ústatus_codeÚdetail)r   )Úlanguage_codeÚssml_gender)Úaudio_encoding)ÚinputÚvoiceÚaudio_configÚtts_z.mp3ÚstaticÚ	tts_audioT)Úexist_okÚwbz/static/tts_audio/Ú	audio_urliô  )Úgoogle.cloudr   ÚImportErrorr   ÚTextToSpeechClientÚSynthesisInputr   ÚVoiceSelectionParamsr	   ÚSsmlVoiceGenderÚNEUTRALÚAudioConfigÚAudioEncodingÚMP3Úsynthesize_speechÚuuidÚuuid4ÚosÚpathÚjoinÚmakedirsÚdirnameÚopenÚwriteÚaudio_contentÚ	Exceptionr   )r   r   ÚeÚclientÚsynthesis_inputr   r   ÚresponseÚfilenameÚ
audio_pathÚoutr    r   r   r   Útts   sJ   €þý€ÿþÿÿÿ

€ÿr>   )Úfastapir   r   r   Úpydanticr   r,   r.   Úrouterr   Úpostr>   r   r   r   r   Ú<module>   s    