| 1 |
"""Information about available services, models, etc..""" |
| 2 |
|
| 3 |
from dataclasses import dataclass, field |
| 4 |
from typing import Any, Dict, List, Optional |
| 5 |
|
| 6 |
from .audio import AudioFormat |
| 7 |
from .event import Event, Eventable |
| 8 |
from .util.dataclasses_json import DataClassJsonMixin |
| 9 |
|
| 10 |
DOMAIN = "info" |
| 11 |
_DESCRIBE_TYPE = "describe" |
| 12 |
_INFO_TYPE = "info" |
| 13 |
_SELECT_PROGRAM_TYPE = "select-program" |
| 14 |
|
| 15 |
|
| 16 |
@dataclass |
| 17 |
class Describe(Eventable): |
| 18 |
"""Request info message.""" |
| 19 |
|
| 20 |
@staticmethod |
| 21 |
def is_type(event_type: str) -> bool: |
| 22 |
return event_type == _DESCRIBE_TYPE |
| 23 |
|
| 24 |
def event(self) -> Event: |
| 25 |
return Event(type=_DESCRIBE_TYPE) |
| 26 |
|
| 27 |
@staticmethod |
| 28 |
def from_event(event: Event) -> "Describe": |
| 29 |
return Describe() |
| 30 |
|
| 31 |
|
| 32 |
@dataclass |
| 33 |
class SelectProgram(Eventable): |
| 34 |
"""Select which program handles this connection. |
| 35 |
|
| 36 |
Sent by the client after connecting, before the first request event (e.g., |
| 37 |
describe/transcribe/synthesize/detect/recognize). Selects one of the |
| 38 |
programs advertised in the info message by name for the lifetime of the |
| 39 |
connection. The domain (asr, tts, ...) is implied by the request events |
| 40 |
that follow, so ``name`` only needs to be unique within a domain. |
| 41 |
|
| 42 |
If this event is not sent, the first program of each type in the info |
| 43 |
message is used. Servers are expected to drop unrecognized events, so |
| 44 |
sending this to a server that predates it is a no-op (the default program |
| 45 |
is used). |
| 46 |
""" |
| 47 |
|
| 48 |
name: str |
| 49 |
"""Name of the program to use (matches a program name in the info message).""" |
| 50 |
|
| 51 |
@staticmethod |
| 52 |
def is_type(event_type: str) -> bool: |
| 53 |
return event_type == _SELECT_PROGRAM_TYPE |
| 54 |
|
| 55 |
def event(self) -> Event: |
| 56 |
return Event(type=_SELECT_PROGRAM_TYPE, data={"name": self.name}) |
| 57 |
|
| 58 |
@staticmethod |
| 59 |
def from_event(event: Event) -> "SelectProgram": |
| 60 |
return SelectProgram(name=event.data["name"]) |
| 61 |
|
| 62 |
|
| 63 |
@dataclass |
| 64 |
class Attribution(DataClassJsonMixin): |
| 65 |
"""Attribution for an artifact.""" |
| 66 |
|
| 67 |
name: str |
| 68 |
"""Who made it.""" |
| 69 |
|
| 70 |
url: str |
| 71 |
"""Where it's from.""" |
| 72 |
|
| 73 |
|
| 74 |
@dataclass |
| 75 |
class Artifact(DataClassJsonMixin): |
| 76 |
"""Information about a service, model, etc..""" |
| 77 |
|
| 78 |
name: str |
| 79 |
"""Name/id of artifact.""" |
| 80 |
|
| 81 |
attribution: Attribution |
| 82 |
"""Who made the artifact and where it's from.""" |
| 83 |
|
| 84 |
installed: bool |
| 85 |
"""True if the artifact is currently installed.""" |
| 86 |
|
| 87 |
description: Optional[str] |
| 88 |
"""Human-readable description of the artifact.""" |
| 89 |
|
| 90 |
version: Optional[str] |
| 91 |
"""Version of the artifact.""" |
| 92 |
|
| 93 |
|
| 94 |
# ----------------------------------------------------------------------------- |
| 95 |
|
| 96 |
|
| 97 |
@dataclass |
| 98 |
class AsrModel(Artifact): |
| 99 |
"""Speech-to-text model.""" |
| 100 |
|
| 101 |
languages: List[str] |
| 102 |
"""List of supported model languages.""" |
| 103 |
|
| 104 |
|
| 105 |
@dataclass |
| 106 |
class AsrProgram(Artifact): |
| 107 |
"""Speech-to-text service.""" |
| 108 |
|
| 109 |
models: List[AsrModel] |
| 110 |
"""List of available models.""" |
| 111 |
|
| 112 |
supports_transcript_streaming: bool = False |
| 113 |
"""True if transcript streaming events are supported.""" |
| 114 |
|
| 115 |
requires_external_vad: bool = True |
| 116 |
"""True if ASR program needs an external VAD to detect the end of voice commands.""" |
| 117 |
|
| 118 |
prefers_auto_gain_enabled: bool = True |
| 119 |
"""True if input audio should adjust gain automatically for best results.""" |
| 120 |
|
| 121 |
prefers_noise_reduction_enabled: bool = True |
| 122 |
"""True if input audio should apply noise reduction for best results.""" |
| 123 |
|
| 124 |
|
| 125 |
# ----------------------------------------------------------------------------- |
| 126 |
|
| 127 |
|
| 128 |
@dataclass |
| 129 |
class TtsVoiceSpeaker(DataClassJsonMixin): |
| 130 |
"""Individual speaker in a multi-speaker voice.""" |
| 131 |
|
| 132 |
name: str |
| 133 |
"""Name/id of speaker.""" |
| 134 |
|
| 135 |
|
| 136 |
@dataclass |
| 137 |
class TtsVoice(Artifact): |
| 138 |
"""Text-to-speech voice.""" |
| 139 |
|
| 140 |
languages: List[str] |
| 141 |
"""List of languages available in the voice.""" |
| 142 |
|
| 143 |
speakers: Optional[List[TtsVoiceSpeaker]] = None |
| 144 |
"""List of individual speakers in the voice.""" |
| 145 |
|
| 146 |
|
| 147 |
@dataclass |
| 148 |
class TtsProgram(Artifact): |
| 149 |
"""Text-to-speech service.""" |
| 150 |
|
| 151 |
voices: List[TtsVoice] |
| 152 |
"""List of available voices.""" |
| 153 |
|
| 154 |
supports_synthesize_streaming: bool = False |
| 155 |
"""True if synthesize text streaming events are supported.""" |
| 156 |
|
| 157 |
|
| 158 |
# ----------------------------------------------------------------------------- |
| 159 |
|
| 160 |
|
| 161 |
@dataclass |
| 162 |
class HandleModel(Artifact): |
| 163 |
"""Intent handling model.""" |
| 164 |
|
| 165 |
languages: List[str] |
| 166 |
"""List of supported languages in the model.""" |
| 167 |
|
| 168 |
|
| 169 |
@dataclass |
| 170 |
class HandleProgram(Artifact): |
| 171 |
"""Intent handling service.""" |
| 172 |
|
| 173 |
models: List[HandleModel] |
| 174 |
"""List of available models.""" |
| 175 |
|
| 176 |
supports_handled_streaming: bool = False |
| 177 |
"""True if handled response streaming events are supported.""" |
| 178 |
|
| 179 |
supports_home_control: bool = False |
| 180 |
"""True if the service may execute home control actions during handling.""" |
| 181 |
|
| 182 |
|
| 183 |
# ----------------------------------------------------------------------------- |
| 184 |
|
| 185 |
|
| 186 |
@dataclass |
| 187 |
class WakeModel(Artifact): |
| 188 |
"""Wake word detection model.""" |
| 189 |
|
| 190 |
languages: List[str] |
| 191 |
"""List of languages supported by the model.""" |
| 192 |
|
| 193 |
phrase: Optional[str] |
| 194 |
"""Wake up phrase used by the model.""" |
| 195 |
|
| 196 |
|
| 197 |
@dataclass |
| 198 |
class WakeProgram(Artifact): |
| 199 |
"""Wake word detection service.""" |
| 200 |
|
| 201 |
models: List[WakeModel] |
| 202 |
"""List of available models.""" |
| 203 |
|
| 204 |
|
| 205 |
# ----------------------------------------------------------------------------- |
| 206 |
|
| 207 |
|
| 208 |
@dataclass |
| 209 |
class IntentModel(Artifact): |
| 210 |
"""Intent recognition model.""" |
| 211 |
|
| 212 |
languages: List[str] |
| 213 |
"""List of languages supported by the model.""" |
| 214 |
|
| 215 |
|
| 216 |
@dataclass |
| 217 |
class IntentProgram(Artifact): |
| 218 |
"""Intent recognition service.""" |
| 219 |
|
| 220 |
models: List[IntentModel] |
| 221 |
"""List of available models.""" |
| 222 |
|
| 223 |
|
| 224 |
# ----------------------------------------------------------------------------- |
| 225 |
|
| 226 |
|
| 227 |
@dataclass |
| 228 |
class Satellite(Artifact): |
| 229 |
"""Satellite information.""" |
| 230 |
|
| 231 |
area: Optional[str] = None |
| 232 |
"""Name of the area the satellite is in.""" |
| 233 |
|
| 234 |
has_vad: Optional[bool] = None |
| 235 |
"""True if a local VAD will be used to detect the end of voice commands.""" |
| 236 |
|
| 237 |
active_wake_words: Optional[List[str]] = None |
| 238 |
"""Wake words that are currently being listened for.""" |
| 239 |
|
| 240 |
max_active_wake_words: Optional[int] = None |
| 241 |
"""Maximum number of local wake words that can be run simultaneously.""" |
| 242 |
|
| 243 |
supports_trigger: Optional[bool] = None |
| 244 |
"""Satellite supports remotely triggering pipeline runs.""" |
| 245 |
|
| 246 |
|
| 247 |
# ----------------------------------------------------------------------------- |
| 248 |
|
| 249 |
|
| 250 |
@dataclass |
| 251 |
class MicProgram(Artifact): |
| 252 |
"""Microphone information.""" |
| 253 |
|
| 254 |
mic_format: AudioFormat |
| 255 |
"""Input audio format.""" |
| 256 |
|
| 257 |
|
| 258 |
# ----------------------------------------------------------------------------- |
| 259 |
|
| 260 |
|
| 261 |
@dataclass |
| 262 |
class SndProgram(Artifact): |
| 263 |
"""Sound output information.""" |
| 264 |
|
| 265 |
snd_format: AudioFormat |
| 266 |
"""Output audio format.""" |
| 267 |
|
| 268 |
|
| 269 |
# ----------------------------------------------------------------------------- |
| 270 |
|
| 271 |
|
| 272 |
@dataclass |
| 273 |
class Info(Eventable): |
| 274 |
"""Response to describe message with information about available services, models, etc.""" |
| 275 |
|
| 276 |
asr: List[AsrProgram] = field(default_factory=list) |
| 277 |
"""Speech-to-text services.""" |
| 278 |
|
| 279 |
tts: List[TtsProgram] = field(default_factory=list) |
| 280 |
"""Text-to-speech services.""" |
| 281 |
|
| 282 |
handle: List[HandleProgram] = field(default_factory=list) |
| 283 |
"""Intent handling services.""" |
| 284 |
|
| 285 |
intent: List[IntentProgram] = field(default_factory=list) |
| 286 |
"""Intent recognition services.""" |
| 287 |
|
| 288 |
wake: List[WakeProgram] = field(default_factory=list) |
| 289 |
"""Wake word detection services.""" |
| 290 |
|
| 291 |
mic: List[MicProgram] = field(default_factory=list) |
| 292 |
"""Audio input services.""" |
| 293 |
|
| 294 |
snd: List[SndProgram] = field(default_factory=list) |
| 295 |
"""Audio output services.""" |
| 296 |
|
| 297 |
satellite: Optional[Satellite] = None |
| 298 |
"""Satellite information.""" |
| 299 |
|
| 300 |
@staticmethod |
| 301 |
def is_type(event_type: str) -> bool: |
| 302 |
return event_type == _INFO_TYPE |
| 303 |
|
| 304 |
def event(self) -> Event: |
| 305 |
data: Dict[str, Any] = { |
| 306 |
"asr": [p.to_dict() for p in self.asr], |
| 307 |
"tts": [p.to_dict() for p in self.tts], |
| 308 |
"handle": [p.to_dict() for p in self.handle], |
| 309 |
"intent": [p.to_dict() for p in self.intent], |
| 310 |
"wake": [p.to_dict() for p in self.wake], |
| 311 |
"mic": [p.to_dict() for p in self.mic], |
| 312 |
"snd": [p.to_dict() for p in self.snd], |
| 313 |
} |
| 314 |
|
| 315 |
if self.satellite is not None: |
| 316 |
data["satellite"] = self.satellite.to_dict() |
| 317 |
|
| 318 |
return Event(type=_INFO_TYPE, data=data) |
| 319 |
|
| 320 |
@staticmethod |
| 321 |
def from_event(event: Event) -> "Info": |
| 322 |
satellite: Optional[Satellite] = None |
| 323 |
satellite_data = event.data.get("satellite") |
| 324 |
if satellite_data is not None: |
| 325 |
satellite = Satellite.from_dict(satellite_data) |
| 326 |
|
| 327 |
return Info( |
| 328 |
asr=[AsrProgram.from_dict(d) for d in event.data.get("asr", [])], |
| 329 |
tts=[TtsProgram.from_dict(d) for d in event.data.get("tts", [])], |
| 330 |
handle=[HandleProgram.from_dict(d) for d in event.data.get("handle", [])], |
| 331 |
intent=[IntentProgram.from_dict(d) for d in event.data.get("intent", [])], |
| 332 |
wake=[WakeProgram.from_dict(d) for d in event.data.get("wake", [])], |
| 333 |
mic=[MicProgram.from_dict(d) for d in event.data.get("mic", [])], |
| 334 |
snd=[SndProgram.from_dict(d) for d in event.data.get("snd", [])], |
| 335 |
satellite=satellite, |
| 336 |
) |