[docs]classSpeechToTextInput(BaseModel):query:HttpUrl=Field(description="url of the audio to analyze")
[docs]classEdenAiSpeechToTextTool(EdenaiTool):# type: ignore[override, override, override]"""Tool that queries the Eden AI Speech To Text API. for api reference check edenai documentation: https://app.edenai.run/bricks/speech/asynchronous-speech-to-text. 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_speech_to_text"description:str=("A wrapper around edenai Services speech to text ""Useful for when you have to convert audio to text.""Input should be a url to an audio file.")args_schema:Type[BaseModel]=SpeechToTextInputis_async:bool=Truelanguage:Optional[str]="en"speakers:Optional[int]profanity_filter:bool=Falsecustom_vocabulary:Optional[List[str]]feature:str="audio"subfeature:str="speech_to_text_async"base_url:str="https://api.edenai.run/v2/audio/speech_to_text_async/"
[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
def_wait_processing(self,url:str)->requests.Response:for_inrange(10):time.sleep(1)audio_analysis_result=self._get_edenai(url)temp=audio_analysis_result.json()iftemp["status"]=="finished":iftemp["results"][self.providers[0]]["error"]isnotNone:raiseException(f"""EdenAI returned an unexpected response {temp["results"][self.providers[0]]["error"]}""")else:returnaudio_analysis_resultraiseException("Edenai speech to text job id processing Timed out")def_parse_response(self,response:dict)->str:returnresponse["public_id"]def_run(self,query:str,run_manager:Optional[CallbackManagerForToolRun]=None,)->str:"""Use the tool."""all_params={"file_url":query,"language":self.language,"speakers":self.speakers,"profanity_filter":self.profanity_filter,"custom_vocabulary":self.custom_vocabulary,}# filter so we don't send val to api when val is `Nonequery_params={k:vfork,vinall_params.items()ifvisnotNone}job_id=self._call_eden_ai(query_params)url=self.base_url+job_idaudio_analysis_result=self._wait_processing(url)result=audio_analysis_result.textformatted_text=json.loads(result)returnformatted_text["results"][self.providers[0]]["text"]