Debian Codegraph @

wyoming/asr.py

raw
1 """Speech to text."""
2
3 from dataclasses import dataclass
4 from enum import Enum
5 from typing import Any, Dict, List, Optional, Union
6
7 from .event import Event, Eventable
8
9 DOMAIN = "asr"
10 _TRANSCRIPT_TYPE = "transcript"
11 _TRANSCRIBE_TYPE = "transcribe"
12 _TRANSCRIPT_START_TYPE = "transcript-start"
13 _TRANSCRIPT_CHUNK_TYPE = "transcript-chunk"
14 _TRANSCRIPT_STOP_TYPE = "transcript-stop"
15
16
17 class VadSensitivity(str, Enum):
18 """How quickly the end of a voice command is detected."""
19
20 DEFAULT = "default"
21 RELAXED = "relaxed"
22 AGGRESSIVE = "aggressive"
23
24
25 @dataclass
26 class Transcript(Eventable):
27 """Transcription response from ASR system"""
28
29 text: str
30 """Text transcription of spoken audio"""
31
32 context: Optional[Dict[str, Any]] = None
33 """Context for next interaction."""
34
35 language: Optional[str] = None
36 """Language of the text."""
37
38 @staticmethod
39 def is_type(event_type: str) -> bool:
40 return event_type == _TRANSCRIPT_TYPE
41
42 def event(self) -> Event:
43 data: Dict[str, Any] = {"text": self.text}
44 if self.language is not None:
45 data["language"] = self.language
46
47 if self.context is not None:
48 data["context"] = self.context
49
50 return Event(type=_TRANSCRIPT_TYPE, data=data)
51
52 @staticmethod
53 def from_event(event: Event) -> "Transcript":
54 return Transcript(
55 text=event.data["text"],
56 language=event.data.get("language"),
57 context=event.data.get("context"),
58 )
59
60
61 @dataclass
62 class Transcribe(Eventable):
63 """Transcription request to ASR system.
64
65 Followed by AudioStart, AudioChunk+, AudioStop
66 """
67
68 name: Optional[str] = None
69 """Name of ASR model to use"""
70
71 language: Optional[str] = None
72 """Language of spoken audio to follow"""
73
74 context: Optional[Dict[str, Any]] = None
75 """Context from previous interactions."""
76
77 vad_sensitivity: Optional[Union[str, VadSensitivity]] = None
78 """How quickly the end of a voice command is detected."""
79
80 transcript_names: Optional[List[str]] = None
81 """Unique names that should bias the ASR transcription.
82
83 These take priority over terms.
84 """
85
86 transcript_terms: Optional[List[str]] = None
87 """Domain-specific terms that should bias the ASR transcription.
88
89 These are lower priority than names.
90 """
91
92 @staticmethod
93 def is_type(event_type: str) -> bool:
94 return event_type == _TRANSCRIBE_TYPE
95
96 def event(self) -> Event:
97 data: Dict[str, Any] = {}
98 if self.name is not None:
99 data["name"] = self.name
100
101 if self.language is not None:
102 data["language"] = self.language
103
104 if self.context is not None:
105 data["context"] = self.context
106
107 if isinstance(self.vad_sensitivity, VadSensitivity):
108 data["vad_sensitivity"] = self.vad_sensitivity.value
109 elif self.vad_sensitivity is not None:
110 data["vad_sensitivity"] = self.vad_sensitivity
111
112 if self.transcript_names is not None:
113 data["transcript_names"] = self.transcript_names
114
115 if self.transcript_terms is not None:
116 data["transcript_terms"] = self.transcript_terms
117
118 return Event(type=_TRANSCRIBE_TYPE, data=data)
119
120 @staticmethod
121 def from_event(event: Event) -> "Transcribe":
122 data = event.data or {}
123
124 vad_sensitivity: Optional[Union[str, VadSensitivity]] = None
125 vad_sensitivity_str = data.get("vad_sensitivity")
126 if vad_sensitivity_str is not None:
127 try:
128 vad_sensitivity = VadSensitivity(vad_sensitivity_str)
129 except ValueError:
130 vad_sensitivity = vad_sensitivity_str
131
132 return Transcribe(
133 name=data.get("name"),
134 language=data.get("language"),
135 context=data.get("context"),
136 vad_sensitivity=vad_sensitivity,
137 transcript_names=data.get("transcript_names"),
138 transcript_terms=data.get("transcript_terms"),
139 )
140
141
142 @dataclass
143 class TranscriptStart(Eventable):
144 """Streaming transcription response from ASR system"""
145
146 context: Optional[Dict[str, Any]] = None
147 """Context for next interaction."""
148
149 language: Optional[str] = None
150 """Language of the text."""
151
152 @staticmethod
153 def is_type(event_type: str) -> bool:
154 return event_type == _TRANSCRIPT_START_TYPE
155
156 def event(self) -> Event:
157 data: Dict[str, Any] = {}
158 if self.language is not None:
159 data["language"] = self.language
160 if self.context is not None:
161 data["context"] = self.context
162
163 return Event(type=_TRANSCRIPT_START_TYPE, data=data)
164
165 @staticmethod
166 def from_event(event: Event) -> "TranscriptStart":
167 if not event.data:
168 return TranscriptStart()
169
170 return TranscriptStart(
171 context=event.data.get("context"), language=event.data.get("language")
172 )
173
174
175 @dataclass
176 class TranscriptChunk(Eventable):
177 """Chunk of streaming transcription response from ASR system"""
178
179 text: str
180 """Chunk of transcript text."""
181
182 @staticmethod
183 def is_type(event_type: str) -> bool:
184 return event_type == _TRANSCRIPT_CHUNK_TYPE
185
186 def event(self) -> Event:
187 return Event(type=_TRANSCRIPT_CHUNK_TYPE, data={"text": self.text})
188
189 @staticmethod
190 def from_event(event: Event) -> "TranscriptChunk":
191 return TranscriptChunk(text=event.data["text"])
192
193
194 @dataclass
195 class TranscriptStop(Eventable):
196 """End of streaming transcription response from ASR system"""
197
198 @staticmethod
199 def is_type(event_type: str) -> bool:
200 return event_type == _TRANSCRIPT_STOP_TYPE
201
202 def event(self) -> Event:
203 return Event(type=_TRANSCRIPT_STOP_TYPE)
204
205 @staticmethod
206 def from_event(event: Event) -> "TranscriptStop":
207 return TranscriptStop()