-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathfake_models.py
115 lines (86 loc) · 3.27 KB
/
fake_models.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from __future__ import annotations
from collections.abc import AsyncIterator
from typing import Literal
import numpy as np
import numpy.typing as npt
try:
from agents.voice import (
AudioInput,
StreamedAudioInput,
StreamedTranscriptionSession,
STTModel,
STTModelSettings,
TTSModel,
TTSModelSettings,
VoiceWorkflowBase,
)
except ImportError:
pass
class FakeTTS(TTSModel):
"""Fakes TTS by just returning string bytes."""
def __init__(self, strategy: Literal["default", "split_words"] = "default"):
self.strategy = strategy
@property
def model_name(self) -> str:
return "fake_tts"
async def run(self, text: str, settings: TTSModelSettings) -> AsyncIterator[bytes]:
if self.strategy == "default":
yield np.zeros(2, dtype=np.int16).tobytes()
elif self.strategy == "split_words":
for _ in text.split():
yield np.zeros(2, dtype=np.int16).tobytes()
async def verify_audio(self, text: str, audio: bytes, dtype: npt.DTypeLike = np.int16) -> None:
assert audio == np.zeros(2, dtype=dtype).tobytes()
async def verify_audio_chunks(
self, text: str, audio_chunks: list[bytes], dtype: npt.DTypeLike = np.int16
) -> None:
assert audio_chunks == [np.zeros(2, dtype=dtype).tobytes() for _word in text.split()]
class FakeSession(StreamedTranscriptionSession):
"""A fake streamed transcription session that yields preconfigured transcripts."""
def __init__(self):
self.outputs: list[str] = []
async def transcribe_turns(self) -> AsyncIterator[str]:
for t in self.outputs:
yield t
async def close(self) -> None:
return None
class FakeSTT(STTModel):
"""A fake STT model that either returns a single transcript or yields multiple."""
def __init__(self, outputs: list[str] | None = None):
self.outputs = outputs or []
@property
def model_name(self) -> str:
return "fake_stt"
async def transcribe(self, _: AudioInput, __: STTModelSettings, ___: bool, ____: bool) -> str:
return self.outputs.pop(0)
async def create_session(
self,
_: StreamedAudioInput,
__: STTModelSettings,
___: bool,
____: bool,
) -> StreamedTranscriptionSession:
session = FakeSession()
session.outputs = self.outputs
return session
class FakeWorkflow(VoiceWorkflowBase):
"""A fake workflow that yields preconfigured outputs."""
def __init__(self, outputs: list[list[str]] | None = None):
self.outputs = outputs or []
def add_output(self, output: list[str]) -> None:
self.outputs.append(output)
def add_multiple_outputs(self, outputs: list[list[str]]) -> None:
self.outputs.extend(outputs)
async def run(self, _: str) -> AsyncIterator[str]:
if not self.outputs:
raise ValueError("No output configured")
output = self.outputs.pop(0)
for t in output:
yield t
class FakeStreamedAudioInput:
@classmethod
async def get(cls, count: int) -> StreamedAudioInput:
input = StreamedAudioInput()
for _ in range(count):
await input.add_audio(np.zeros(2, dtype=np.int16))
return input