2023-07-07 17:50:42 +08:00
|
|
|
import io
|
2024-01-24 01:05:37 +08:00
|
|
|
from typing import Optional
|
2024-01-02 23:42:00 +08:00
|
|
|
|
2024-02-06 13:21:13 +08:00
|
|
|
from werkzeug.datastructures import FileStorage
|
|
|
|
|
2024-01-02 23:42:00 +08:00
|
|
|
from core.model_manager import ModelManager
|
|
|
|
from core.model_runtime.entities.model_entities import ModelType
|
2024-02-06 13:21:13 +08:00
|
|
|
from services.errors.audio import (
|
|
|
|
AudioTooLargeServiceError,
|
|
|
|
NoAudioUploadedServiceError,
|
|
|
|
ProviderNotSupportSpeechToTextServiceError,
|
|
|
|
ProviderNotSupportTextToSpeechServiceError,
|
|
|
|
UnsupportedAudioTypeServiceError,
|
|
|
|
)
|
2023-07-07 17:50:42 +08:00
|
|
|
|
2024-02-15 22:41:18 +08:00
|
|
|
FILE_SIZE = 30
|
2023-07-12 17:18:56 +08:00
|
|
|
FILE_SIZE_LIMIT = FILE_SIZE * 1024 * 1024
|
2024-01-24 01:05:37 +08:00
|
|
|
ALLOWED_EXTENSIONS = ['mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'wav', 'webm', 'amr']
|
2023-07-07 17:50:42 +08:00
|
|
|
|
2023-08-12 00:57:00 +08:00
|
|
|
|
2023-07-07 17:50:42 +08:00
|
|
|
class AudioService:
|
|
|
|
@classmethod
|
2024-02-20 15:16:46 +08:00
|
|
|
def transcript_asr(cls, tenant_id: str, file: FileStorage, end_user: Optional[str] = None):
|
2023-07-07 17:50:42 +08:00
|
|
|
if file is None:
|
|
|
|
raise NoAudioUploadedServiceError()
|
2024-01-24 01:05:37 +08:00
|
|
|
|
2023-07-07 17:50:42 +08:00
|
|
|
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:
|
2023-07-12 17:18:56 +08:00
|
|
|
message = f"Audio size larger than {FILE_SIZE} mb"
|
2023-07-07 17:50:42 +08:00
|
|
|
raise AudioTooLargeServiceError(message)
|
|
|
|
|
2024-01-02 23:42:00 +08:00
|
|
|
model_manager = ModelManager()
|
|
|
|
model_instance = model_manager.get_default_model_instance(
|
|
|
|
tenant_id=tenant_id,
|
|
|
|
model_type=ModelType.SPEECH2TEXT
|
2023-08-12 00:57:00 +08:00
|
|
|
)
|
2024-01-24 01:05:37 +08:00
|
|
|
if model_instance is None:
|
|
|
|
raise ProviderNotSupportSpeechToTextServiceError()
|
2023-07-07 17:50:42 +08:00
|
|
|
|
|
|
|
buffer = io.BytesIO(file_content)
|
2023-07-12 17:18:56 +08:00
|
|
|
buffer.name = 'temp.mp3'
|
2023-07-07 17:50:42 +08:00
|
|
|
|
2024-01-24 01:05:37 +08:00
|
|
|
return {"text": model_instance.invoke_speech2text(file=buffer, user=end_user)}
|
|
|
|
|
|
|
|
@classmethod
|
2024-02-15 22:41:18 +08:00
|
|
|
def transcript_tts(cls, tenant_id: str, text: str, voice: str, streaming: bool, end_user: Optional[str] = None):
|
2024-01-24 01:05:37 +08:00
|
|
|
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:
|
2024-02-15 22:41:18 +08:00
|
|
|
return model_instance.invoke_tts(content_text=text.strip(), user=end_user, streaming=streaming, tenant_id=tenant_id, voice=voice)
|
|
|
|
except Exception as e:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def transcript_tts_voices(cls, tenant_id: str, language: str):
|
|
|
|
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.get_tts_voices(language)
|
2024-01-24 01:05:37 +08:00
|
|
|
except Exception as e:
|
|
|
|
raise e
|