| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 | import iofrom typing import Optionalfrom werkzeug.datastructures import FileStoragefrom core.model_manager import ModelManagerfrom core.model_runtime.entities.model_entities import ModelTypefrom services.errors.audio import (    AudioTooLargeServiceError,    NoAudioUploadedServiceError,    ProviderNotSupportSpeechToTextServiceError,    ProviderNotSupportTextToSpeechServiceError,    UnsupportedAudioTypeServiceError,)FILE_SIZE = 15FILE_SIZE_LIMIT = FILE_SIZE * 1024 * 1024ALLOWED_EXTENSIONS = ['mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'wav', 'webm', 'amr']class AudioService:    @classmethod    def transcript_asr(cls, tenant_id: str, file: FileStorage, end_user: Optional[str] = None):        if file is None:            raise NoAudioUploadedServiceError()        extension = file.mimetype        if extension not in [f'audio/{ext}' for ext in ALLOWED_EXTENSIONS]:            raise UnsupportedAudioTypeServiceError()        file_content = file.read()        file_size = len(file_content)        if file_size > FILE_SIZE_LIMIT:            message = f"Audio size larger than {FILE_SIZE} mb"            raise AudioTooLargeServiceError(message)        model_manager = ModelManager()        model_instance = model_manager.get_default_model_instance(            tenant_id=tenant_id,            model_type=ModelType.SPEECH2TEXT        )        if model_instance is None:            raise ProviderNotSupportSpeechToTextServiceError()        buffer = io.BytesIO(file_content)        buffer.name = 'temp.mp3'        return {"text": model_instance.invoke_speech2text(file=buffer, user=end_user)}    @classmethod    def transcript_tts(cls, tenant_id: str, text: str, streaming: bool, end_user: Optional[str] = None):        model_manager = ModelManager()        model_instance = model_manager.get_default_model_instance(            tenant_id=tenant_id,            model_type=ModelType.TTS        )        if model_instance is None:            raise ProviderNotSupportTextToSpeechServiceError()        try:            return model_instance.invoke_tts(content_text=text.strip(), user=end_user, streaming=streaming)        except Exception as e:            raise e
 |