[docs]classTextToSpeechInput(BaseModel):query:str=Field(description="text to generate audio from")
[docs]classEdenAiTextToSpeechTool(EdenaiTool):# type: ignore[override, override, override]"""Tool that queries the Eden AI Text to speech API. for api reference check edenai documentation: https://docs.edenai.co/reference/audio_text_to_speech_create. To use, you should have the environment variable ``EDENAI_API_KEY`` set with your API token. You can find your token here: https://app.edenai.run/admin/account/settings """name:str="edenai_text_to_speech"description:str=("A wrapper around edenai Services text to speech.""Useful for when you need to convert text to speech.""""the output is a string representing the URL of the audio file, or the path to the downloaded wav file """)args_schema:Type[BaseModel]=TextToSpeechInputlanguage:Optional[str]="en"""" language of the text passed to the model. """# optional params see api documentation for more inforeturn_type:Literal["url","wav"]="url"rate:Optional[int]=Nonepitch:Optional[int]=Nonevolume:Optional[int]=Noneaudio_format:Optional[str]=Nonesampling_rate:Optional[int]=Nonevoice_models:Dict[str,str]=Field(default_factory=dict)voice:Literal["MALE","FEMALE"]"""voice option : 'MALE' or 'FEMALE' """feature:str="audio"subfeature:str="text_to_speech"
[docs]@validator("providers")defcheck_only_one_provider_selected(cls,v:List[str])->List[str]:""" This tool has no feature to combine providers results. Therefore we only allow one provider """iflen(v)>1:raiseValueError("Please select only one provider. ""The feature to combine providers results is not available ""for this tool.")returnv
@model_validator(mode="before")@classmethoddefcheck_voice_models_key_is_provider_name(cls,values:dict)->Any:forkeyinvalues.get("voice_models",{}).keys():ifkeynotinvalues.get("providers",[]):raiseValueError("voice_model should be formatted like this ""{<provider_name>: <its_voice_model>}")returnvaluesdef_download_wav(self,url:str,save_path:str)->None:response=requests.get(url)ifresponse.status_code==200:withopen(save_path,"wb")asf:f.write(response.content)else:raiseValueError("Error while downloading wav file")def_parse_response(self,response:list)->str:result=response[0]ifself.return_type=="url":returnresult["audio_resource_url"]else:self._download_wav(result["audio_resource_url"],"audio.wav")return"audio.wav"def_run(self,query:str,run_manager:Optional[CallbackManagerForToolRun]=None,)->str:"""Use the tool."""all_params={"text":query,"language":self.language,"option":self.voice,"return_type":self.return_type,"rate":self.rate,"pitch":self.pitch,"volume":self.volume,"audio_format":self.audio_format,"sampling_rate":self.sampling_rate,"settings":self.voice_models,}# filter so we don't send val to api when val is `Nonequery_params={k:vfork,vinall_params.items()ifvisnotNone}returnself._call_eden_ai(query_params)