test_speech2text.py 1.7 KB

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