2023-07-07 17:50:42 +08:00
|
|
|
import io
|
|
|
|
from werkzeug.datastructures import FileStorage
|
|
|
|
from core.llm.llm_builder import LLMBuilder
|
|
|
|
from core.llm.provider.llm_provider_service import LLMProviderService
|
|
|
|
from services.errors.audio import NoAudioUploadedServiceError, AudioTooLargeServiceError, UnsupportedAudioTypeServiceError, ProviderNotSupportSpeechToTextServiceError
|
|
|
|
from core.llm.whisper import Whisper
|
|
|
|
from models.provider import ProviderName
|
|
|
|
|
2023-07-12 17:18:56 +08:00
|
|
|
FILE_SIZE = 15
|
|
|
|
FILE_SIZE_LIMIT = FILE_SIZE * 1024 * 1024
|
2023-07-07 17:50:42 +08:00
|
|
|
ALLOWED_EXTENSIONS = ['mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'wav', 'webm']
|
|
|
|
|
|
|
|
class AudioService:
|
|
|
|
@classmethod
|
|
|
|
def transcript(cls, tenant_id: str, file: FileStorage):
|
|
|
|
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:
|
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)
|
|
|
|
|
2023-07-17 00:14:19 +08:00
|
|
|
provider_name = LLMBuilder.get_default_provider(tenant_id, 'whisper-1')
|
2023-07-07 17:50:42 +08:00
|
|
|
if provider_name != ProviderName.OPENAI.value:
|
2023-07-12 17:18:56 +08:00
|
|
|
raise ProviderNotSupportSpeechToTextServiceError()
|
2023-07-07 17:50:42 +08:00
|
|
|
|
|
|
|
provider_service = LLMProviderService(tenant_id, provider_name)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return Whisper(provider_service.provider).transcribe(buffer)
|