Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assemblyai/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.10.12"
__version__ = "1.10.13"
14 changes: 7 additions & 7 deletions assemblyai/dictation/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class DictationLiveSession:
"""
A live dictation that audio is pushed into.

Returned by `DictationTranscriber.open_live()`. The request starts on one
of the transcriber's worker threads the moment the session opens;
Returned by `DictationTranscriber.open_live()`. The request starts as soon
as one of the transcriber's worker threads is available;
`write()` hands it audio, `close()` ends the audio, and `result()` waits
for the transcript. Built for callback-driven sources — a microphone
library, a WebRTC track, a telephony media stream — where the audio
Expand Down Expand Up @@ -222,7 +222,8 @@ def __init__(
config: Default dictation options. Per-call `config` overrides it.
max_workers: Thread pool size for `open_live()` sessions, each of
which runs its request on a worker thread. Defaults to the CPU
count minus one.
count minus one, with a minimum of two workers so a second live
upload can start while the first is still recording.
api_key: The API key to authenticate with. Builds a `Client` for this
transcriber. Given alongside `client`, it takes precedence: the
transcriber builds its own client from a copy of that client's
Expand All @@ -242,8 +243,7 @@ def __init__(
)

if not max_workers:
cpu_count = os.cpu_count()
max_workers = max(1, cpu_count - 1) if cpu_count else 1
max_workers = max(2, (os.cpu_count() or 1) - 1)

self._executor = concurrent.futures.ThreadPoolExecutor(
max_workers=max_workers,
Expand Down Expand Up @@ -320,8 +320,8 @@ def open_live(

The push-style counterpart of `transcribe_live()`, for sources that
deliver audio through a callback rather than an iterator. The request
starts on a worker thread immediately; call `session.write(chunk)` from
the callback, then `session.result()` for the transcript once the
starts as soon as a worker thread is available. Call `session.write(chunk)`
from the callback, then `session.result()` for the transcript once the
speaker stops. See `DictationLiveSession`.

Args:
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/test_dictation.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,43 @@ def test_open_live_starts_the_request_before_any_audio(monkeypatch):
session.result()


@pytest.mark.parametrize(
("cpu_count", "max_workers"),
[(1, None), (2, None), (None, None), (1, 2)],
ids=["one-cpu", "two-cpus", "unknown-cpu-count", "explicit-workers"],
)
def test_open_live_uploads_two_recordings_concurrently(
monkeypatch, cpu_count, max_workers
):
monkeypatch.setattr(dictation_client.os, "cpu_count", lambda: cpu_count)
first_audio = b"RIFFfirst"
second_audio = b"RIFFsecond"
uploaded = {first_audio: threading.Event(), second_audio: threading.Event()}

def fake(client, *, chunks, **kwargs):
uploaded[next(chunks)].set()
assert list(chunks) == []
return aai.DictationResponse(**_OK_RESPONSE)

monkeypatch.setattr(api, "transcribe_live", fake)

with aai.DictationTranscriber(max_workers=max_workers) as transcriber:
first = transcriber.open_live()
second = transcriber.open_live()
try:
first.write(first_audio)
second.write(second_audio)
assert uploaded[first_audio].wait(timeout=2.0)
assert uploaded[second_audio].wait(
timeout=2.0
), "the second upload must start before the first recording ends"
finally:
first.close()
second.close()

assert first.result().text == second.result().text == _OK_RESPONSE["text"]


def test_open_live_uses_the_transcribers_default_config(monkeypatch):
# Given a transcriber with a default config and a session opened without one
seen = _fake_live(monkeypatch)
Expand Down