File size: 1,733 Bytes
c641d5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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"
    )