audio_service.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import io
  2. from werkzeug.datastructures import FileStorage
  3. from core.model_providers.model_factory import ModelFactory
  4. from services.errors.audio import NoAudioUploadedServiceError, AudioTooLargeServiceError, UnsupportedAudioTypeServiceError, ProviderNotSupportSpeechToTextServiceError
  5. FILE_SIZE = 15
  6. FILE_SIZE_LIMIT = FILE_SIZE * 1024 * 1024
  7. ALLOWED_EXTENSIONS = ['mp3', 'mp4', 'mpeg', 'mpga', 'm4a', 'wav', 'webm']
  8. class AudioService:
  9. @classmethod
  10. def transcript(cls, tenant_id: str, file: FileStorage):
  11. if file is None:
  12. raise NoAudioUploadedServiceError()
  13. extension = file.mimetype
  14. if extension not in [f'audio/{ext}' for ext in ALLOWED_EXTENSIONS]:
  15. raise UnsupportedAudioTypeServiceError()
  16. file_content = file.read()
  17. file_size = len(file_content)
  18. if file_size > FILE_SIZE_LIMIT:
  19. message = f"Audio size larger than {FILE_SIZE} mb"
  20. raise AudioTooLargeServiceError(message)
  21. model = ModelFactory.get_speech2text_model(
  22. tenant_id=tenant_id
  23. )
  24. buffer = io.BytesIO(file_content)
  25. buffer.name = 'temp.mp3'
  26. return model.run(buffer)