test_speech2text.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import os
  2. import pytest
  3. from core.model_runtime.errors.validate import CredentialsValidateFailedError
  4. from core.model_runtime.model_providers.localai.speech2text.speech2text import LocalAISpeech2text
  5. def test_validate_credentials():
  6. model = LocalAISpeech2text()
  7. with pytest.raises(CredentialsValidateFailedError):
  8. model.validate_credentials(
  9. model='whisper-1',
  10. credentials={
  11. 'server_url': 'invalid_url'
  12. }
  13. )
  14. model.validate_credentials(
  15. model='whisper-1',
  16. credentials={
  17. 'server_url': os.environ.get('LOCALAI_SERVER_URL')
  18. }
  19. )
  20. def test_invoke_model():
  21. model = LocalAISpeech2text()
  22. # Get the directory of the current file
  23. current_dir = os.path.dirname(os.path.abspath(__file__))
  24. # Get assets directory
  25. assets_dir = os.path.join(os.path.dirname(current_dir), 'assets')
  26. # Construct the path to the audio file
  27. audio_file_path = os.path.join(assets_dir, 'audio.mp3')
  28. # Open the file and get the file object
  29. with open(audio_file_path, 'rb') as audio_file:
  30. file = audio_file
  31. result = model.invoke(
  32. model='whisper-1',
  33. credentials={
  34. 'server_url': os.environ.get('LOCALAI_SERVER_URL')
  35. },
  36. file=file,
  37. user="abc-123"
  38. )
  39. assert isinstance(result, str)
  40. assert result == '1, 2, 3, 4, 5, 6, 7, 8, 9, 10'