test_speech2text.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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(model="whisper-1", credentials={"server_url": "invalid_url"})
  9. model.validate_credentials(model="whisper-1", credentials={"server_url": os.environ.get("LOCALAI_SERVER_URL")})
  10. def test_invoke_model():
  11. model = LocalAISpeech2text()
  12. # Get the directory of the current file
  13. current_dir = os.path.dirname(os.path.abspath(__file__))
  14. # Get assets directory
  15. assets_dir = os.path.join(os.path.dirname(current_dir), "assets")
  16. # Construct the path to the audio file
  17. audio_file_path = os.path.join(assets_dir, "audio.mp3")
  18. # Open the file and get the file object
  19. with open(audio_file_path, "rb") as audio_file:
  20. file = audio_file
  21. result = model.invoke(
  22. model="whisper-1",
  23. credentials={"server_url": os.environ.get("LOCALAI_SERVER_URL")},
  24. file=file,
  25. user="abc-123",
  26. )
  27. assert isinstance(result, str)
  28. assert result == "1, 2, 3, 4, 5, 6, 7, 8, 9, 10"