| from tools.audio import ( | |
| answer_from_transcript, | |
| format_transcript_items, | |
| transcribe_audio, | |
| ) | |
| def test_asr_uses_explicit_credentials_and_model(tmp_path, monkeypatch): | |
| audio = tmp_path / "audio.mp3" | |
| audio.write_bytes(b"audio") | |
| observed = {} | |
| class Client: | |
| def __init__(self, **kwargs): | |
| observed.update(kwargs) | |
| def automatic_speech_recognition(self, audio, model): | |
| observed["audio"] = audio | |
| observed["model"] = model | |
| return type("Result", (), {"text": "spoken words"})() | |
| monkeypatch.setattr("tools.audio.InferenceClient", Client) | |
| assert ( | |
| transcribe_audio(audio, token="env-token", model_id="whisper") == "spoken words" | |
| ) | |
| assert observed["token"] == "env-token" and observed["model"] == "whisper" | |
| assert observed["audio"] == b"audio" | |
| def test_transcript_item_formatting(): | |
| assert ( | |
| format_transcript_items("Alphabetical comma-separated list", ["pear", "apple"]) | |
| == "apple, pear" | |
| ) | |
| def test_deterministic_transcript_extraction(): | |
| pages = "Read page 245, page 197, and pages 132, 133 and 134." | |
| assert answer_from_transcript("page numbers ascending", pages) == ( | |
| "132, 133, 134, 197, 245" | |
| ) | |
| recipe = ( | |
| "In a saucepan, combine ripe strawberries, granulated sugar, freshly squeezed " | |
| "lemon juice and cornstarch. Remove from heat and stir in a dash of pure vanilla " | |
| "extract." | |
| ) | |
| assert answer_from_transcript("alphabetize the ingredients", recipe) == ( | |
| "cornstarch, freshly squeezed lemon juice, granulated sugar, pure vanilla extract, " | |
| "ripe strawberries" | |
| ) | |