| import logging | import logging | ||||
| import re | import re | ||||
| from abc import abstractmethod | from abc import abstractmethod | ||||
| from typing import Optional | |||||
| from typing import Any, Optional | |||||
| from pydantic import ConfigDict | from pydantic import ConfigDict | ||||
| else: | else: | ||||
| return [{"name": d["name"], "value": d["mode"]} for d in voices] | return [{"name": d["name"], "value": d["mode"]} for d in voices] | ||||
| def _get_model_default_voice(self, model: str, credentials: dict) -> any: | |||||
| def _get_model_default_voice(self, model: str, credentials: dict) -> Any: | |||||
| """ | """ | ||||
| Get voice for given tts model | Get voice for given tts model | ||||
| import concurrent.futures | import concurrent.futures | ||||
| import copy | import copy | ||||
| from typing import Optional | |||||
| from typing import Any, Optional | |||||
| from openai import AzureOpenAI | from openai import AzureOpenAI | ||||
| def _invoke( | def _invoke( | ||||
| self, model: str, tenant_id: str, credentials: dict, content_text: str, voice: str, user: Optional[str] = None | self, model: str, tenant_id: str, credentials: dict, content_text: str, voice: str, user: Optional[str] = None | ||||
| ) -> any: | |||||
| ) -> Any: | |||||
| """ | """ | ||||
| _invoke text2speech model | _invoke text2speech model | ||||
| except Exception as ex: | except Exception as ex: | ||||
| raise CredentialsValidateFailedError(str(ex)) | raise CredentialsValidateFailedError(str(ex)) | ||||
| def _tts_invoke_streaming(self, model: str, credentials: dict, content_text: str, voice: str) -> any: | |||||
| def _tts_invoke_streaming(self, model: str, credentials: dict, content_text: str, voice: str) -> Any: | |||||
| """ | """ | ||||
| _tts_invoke_streaming text2speech model | _tts_invoke_streaming text2speech model | ||||
| :param model: model name | :param model: model name | 
| from typing import Optional | |||||
| from typing import Any, Optional | |||||
| import httpx | import httpx | ||||
| content_text: str, | content_text: str, | ||||
| voice: str, | voice: str, | ||||
| user: Optional[str] = None, | user: Optional[str] = None, | ||||
| ) -> any: | |||||
| ) -> Any: | |||||
| """ | """ | ||||
| Invoke text2speech model | Invoke text2speech model | ||||
| except Exception as ex: | except Exception as ex: | ||||
| raise CredentialsValidateFailedError(str(ex)) | raise CredentialsValidateFailedError(str(ex)) | ||||
| def _tts_invoke_streaming(self, model: str, credentials: dict, content_text: str, voice: str) -> any: | |||||
| def _tts_invoke_streaming(self, model: str, credentials: dict, content_text: str, voice: str) -> Any: | |||||
| """ | """ | ||||
| Invoke streaming text2speech model | Invoke streaming text2speech model | ||||
| :param model: model name | :param model: model name | ||||
| except Exception as ex: | except Exception as ex: | ||||
| raise InvokeBadRequestError(str(ex)) | raise InvokeBadRequestError(str(ex)) | ||||
| def _tts_invoke_streaming_sentence(self, credentials: dict, content_text: str, voice: Optional[str] = None) -> any: | |||||
| def _tts_invoke_streaming_sentence(self, credentials: dict, content_text: str, voice: Optional[str] = None) -> Any: | |||||
| """ | """ | ||||
| Invoke streaming text2speech model | Invoke streaming text2speech model | ||||
| import concurrent.futures | import concurrent.futures | ||||
| from typing import Optional | |||||
| from typing import Any, Optional | |||||
| from openai import OpenAI | from openai import OpenAI | ||||
| def _invoke( | def _invoke( | ||||
| self, model: str, tenant_id: str, credentials: dict, content_text: str, voice: str, user: Optional[str] = None | self, model: str, tenant_id: str, credentials: dict, content_text: str, voice: str, user: Optional[str] = None | ||||
| ) -> any: | |||||
| ) -> Any: | |||||
| """ | """ | ||||
| _invoke text2speech model | _invoke text2speech model | ||||
| except Exception as ex: | except Exception as ex: | ||||
| raise CredentialsValidateFailedError(str(ex)) | raise CredentialsValidateFailedError(str(ex)) | ||||
| def _tts_invoke_streaming(self, model: str, credentials: dict, content_text: str, voice: str) -> any: | |||||
| def _tts_invoke_streaming(self, model: str, credentials: dict, content_text: str, voice: str) -> Any: | |||||
| """ | """ | ||||
| _tts_invoke_streaming text2speech model | _tts_invoke_streaming text2speech model | ||||
| InvokeBadRequestError: [InvokeBadRequestError, KeyError, ValueError], | InvokeBadRequestError: [InvokeBadRequestError, KeyError, ValueError], | ||||
| } | } | ||||
| def _get_model_default_voice(self, model: str, credentials: dict) -> any: | |||||
| def _get_model_default_voice(self, model: str, credentials: dict) -> Any: | |||||
| return "" | return "" | ||||
| def _get_model_word_limit(self, model: str, credentials: dict) -> int: | def _get_model_word_limit(self, model: str, credentials: dict) -> int: | ||||
| json_obj = json.loads(json_str) | json_obj = json.loads(json_str) | ||||
| return json_obj | return json_obj | ||||
| def _tts_invoke_streaming(self, model_type: str, payload: dict, sagemaker_endpoint: str) -> any: | |||||
| def _tts_invoke_streaming(self, model_type: str, payload: dict, sagemaker_endpoint: str) -> Any: | |||||
| """ | """ | ||||
| _tts_invoke_streaming text2speech model | _tts_invoke_streaming text2speech model | ||||
| import threading | import threading | ||||
| from queue import Queue | from queue import Queue | ||||
| from typing import Optional | |||||
| from typing import Any, Optional | |||||
| import dashscope | import dashscope | ||||
| from dashscope import SpeechSynthesizer | from dashscope import SpeechSynthesizer | ||||
| def _invoke( | def _invoke( | ||||
| self, model: str, tenant_id: str, credentials: dict, content_text: str, voice: str, user: Optional[str] = None | self, model: str, tenant_id: str, credentials: dict, content_text: str, voice: str, user: Optional[str] = None | ||||
| ) -> any: | |||||
| ) -> Any: | |||||
| """ | """ | ||||
| _invoke text2speech model | _invoke text2speech model | ||||
| except Exception as ex: | except Exception as ex: | ||||
| raise CredentialsValidateFailedError(str(ex)) | raise CredentialsValidateFailedError(str(ex)) | ||||
| def _tts_invoke_streaming(self, model: str, credentials: dict, content_text: str, voice: str) -> any: | |||||
| def _tts_invoke_streaming(self, model: str, credentials: dict, content_text: str, voice: str) -> Any: | |||||
| """ | """ | ||||
| _tts_invoke_streaming text2speech model | _tts_invoke_streaming text2speech model | ||||
| import concurrent.futures | import concurrent.futures | ||||
| from typing import Optional | |||||
| from typing import Any, Optional | |||||
| from xinference_client.client.restful.restful_client import RESTfulAudioModelHandle | from xinference_client.client.restful.restful_client import RESTfulAudioModelHandle | ||||
| return self.model_voices["__default"]["all"] | return self.model_voices["__default"]["all"] | ||||
| def _get_model_default_voice(self, model: str, credentials: dict) -> any: | |||||
| def _get_model_default_voice(self, model: str, credentials: dict) -> Any: | |||||
| return "" | return "" | ||||
| def _get_model_word_limit(self, model: str, credentials: dict) -> int: | def _get_model_word_limit(self, model: str, credentials: dict) -> int: | ||||
| def _get_model_workers_limit(self, model: str, credentials: dict) -> int: | def _get_model_workers_limit(self, model: str, credentials: dict) -> int: | ||||
| return 5 | return 5 | ||||
| def _tts_invoke_streaming(self, model: str, credentials: dict, content_text: str, voice: str) -> any: | |||||
| def _tts_invoke_streaming(self, model: str, credentials: dict, content_text: str, voice: str) -> Any: | |||||
| """ | """ | ||||
| _tts_invoke_streaming text2speech model | _tts_invoke_streaming text2speech model | ||||