From bab6e0658c451dec4673661592fc2102bb51848a Mon Sep 17 00:00:00 2001 From: ftnext Date: Sat, 7 Dec 2024 13:55:43 +0900 Subject: [PATCH 1/3] [test] **kwargs fixed #681 --- tests/recognizers/test_openai.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/recognizers/test_openai.py b/tests/recognizers/test_openai.py index 21c2b04e..8db504ed 100644 --- a/tests/recognizers/test_openai.py +++ b/tests/recognizers/test_openai.py @@ -29,3 +29,26 @@ def test_transcribe_with_openai_whisper(respx_mock, monkeypatch): assert actual == "Transcription by OpenAI Whisper" audio_data.get_wav_data.assert_called_once() + + +@respx.mock(assert_all_called=True, assert_all_mocked=True) +def test_transcribe_with_specified_language(respx_mock, monkeypatch): + # https://github.com/Uberi/speech_recognition/issues/681 + monkeypatch.setenv("OPENAI_API_KEY", "sk_openai_api_key") + + respx_mock.post( + "https://api.openai.com/v1/audio/transcriptions", + data__contains={"language": "en"}, + ).respond( + 200, + json={"text": "English transcription"}, + ) + + audio_data = MagicMock(spec=AudioData) + audio_data.get_wav_data.return_value = b"english_audio" + + actual = openai.recognize( + MagicMock(spec=Recognizer), audio_data, language="en" + ) + + assert actual == "English transcription" From 161385b5aeff5d36a678e05be4a7c95456ba6c6a Mon Sep 17 00:00:00 2001 From: ftnext Date: Sat, 7 Dec 2024 14:24:20 +0900 Subject: [PATCH 2/3] [refactor] Extract setenv --- tests/recognizers/test_openai.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tests/recognizers/test_openai.py b/tests/recognizers/test_openai.py index 8db504ed..724a9967 100644 --- a/tests/recognizers/test_openai.py +++ b/tests/recognizers/test_openai.py @@ -1,16 +1,20 @@ from unittest.mock import MagicMock import httpx +import pytest import respx from speech_recognition import AudioData, Recognizer from speech_recognition.recognizers import openai - -@respx.mock(assert_all_called=True, assert_all_mocked=True) +@pytest.fixture +def setenv_openai_api_key(monkeypatch): def test_transcribe_with_openai_whisper(respx_mock, monkeypatch): monkeypatch.setenv("OPENAI_API_KEY", "sk_openai_api_key") + +@respx.mock(assert_all_called=True, assert_all_mocked=True) +def test_transcribe_with_openai_whisper(respx_mock, setenv_openai_api_key): respx_mock.post( "https://api.openai.com/v1/audio/transcriptions", headers__contains={"Authorization": "Bearer sk_openai_api_key"}, @@ -34,8 +38,6 @@ def test_transcribe_with_openai_whisper(respx_mock, monkeypatch): @respx.mock(assert_all_called=True, assert_all_mocked=True) def test_transcribe_with_specified_language(respx_mock, monkeypatch): # https://github.com/Uberi/speech_recognition/issues/681 - monkeypatch.setenv("OPENAI_API_KEY", "sk_openai_api_key") - respx_mock.post( "https://api.openai.com/v1/audio/transcriptions", data__contains={"language": "en"}, From cc000043dcc1eaf3a964893ca2d359dd8bbfa2e7 Mon Sep 17 00:00:00 2001 From: ftnext Date: Sat, 7 Dec 2024 14:28:04 +0900 Subject: [PATCH 3/3] [test] **kwargs supports #676 --- tests/recognizers/test_openai.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/recognizers/test_openai.py b/tests/recognizers/test_openai.py index 724a9967..412c770b 100644 --- a/tests/recognizers/test_openai.py +++ b/tests/recognizers/test_openai.py @@ -7,9 +7,9 @@ from speech_recognition import AudioData, Recognizer from speech_recognition.recognizers import openai + @pytest.fixture def setenv_openai_api_key(monkeypatch): -def test_transcribe_with_openai_whisper(respx_mock, monkeypatch): monkeypatch.setenv("OPENAI_API_KEY", "sk_openai_api_key") @@ -36,7 +36,7 @@ def test_transcribe_with_openai_whisper(respx_mock, setenv_openai_api_key): @respx.mock(assert_all_called=True, assert_all_mocked=True) -def test_transcribe_with_specified_language(respx_mock, monkeypatch): +def test_transcribe_with_specified_language(respx_mock, setenv_openai_api_key): # https://github.com/Uberi/speech_recognition/issues/681 respx_mock.post( "https://api.openai.com/v1/audio/transcriptions", @@ -54,3 +54,27 @@ def test_transcribe_with_specified_language(respx_mock, monkeypatch): ) assert actual == "English transcription" + + +@respx.mock(assert_all_called=True, assert_all_mocked=True) +def test_transcribe_with_specified_prompt(respx_mock, setenv_openai_api_key): + # https://github.com/Uberi/speech_recognition/pull/676 + respx_mock.post( + "https://api.openai.com/v1/audio/transcriptions", + # ref: https://cookbook.openai.com/examples/whisper_prompting_guide + data__contains={"prompt": "Glossary: Aimee, Shawn, BBQ"}, + ).respond( + 200, + json={"text": "Prompted transcription"}, + ) + + audio_data = MagicMock(spec=AudioData) + audio_data.get_wav_data.return_value = b"audio_data" + + actual = openai.recognize( + MagicMock(spec=Recognizer), + audio_data, + prompt="Glossary: Aimee, Shawn, BBQ", + ) + + assert actual == "Prompted transcription"