mirror of
https://github.com/langgenius/dify.git
synced 2024-11-16 19:59:50 +08:00
d069c668f8
Co-authored-by: StyleZhang <jasonapring2015@outlook.com> Co-authored-by: Garfield Dai <dai.hai@foxmail.com> Co-authored-by: chenhe <guchenhe@gmail.com> Co-authored-by: jyong <jyong@dify.ai> Co-authored-by: Joel <iamjoel007@gmail.com> Co-authored-by: Yeuoly <admin@srmxy.cn>
40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
import io
|
|
from werkzeug.datastructures import FileStorage
|
|
|
|
from core.model_manager import ModelManager
|
|
from core.model_runtime.entities.model_entities import ModelType
|
|
from services.errors.audio import NoAudioUploadedServiceError, AudioTooLargeServiceError, UnsupportedAudioTypeServiceError, ProviderNotSupportSpeechToTextServiceError
|
|
|
|
FILE_SIZE = 15
|
|
FILE_SIZE_LIMIT = FILE_SIZE * 1024 * 1024
|
|
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:
|
|
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
|
|
)
|
|
|
|
buffer = io.BytesIO(file_content)
|
|
buffer.name = 'temp.mp3'
|
|
|
|
return {"text": model_instance.invoke_speech2text(buffer)}
|