44import logging
55from queue import Queue
66from threading import Thread
7- from typing import Any , Union
7+ from typing import Any , Generator , Union
88
9- import numpy as np # type: ignore
10- from google .cloud import speech # type: ignore
11- from google .oauth2 import service_account # type: ignore
9+ import numpy as np
10+ from google .cloud import speech
11+ from google .oauth2 import service_account
1212
1313from spokestack .context import SpeechContext
1414
15-
1615_LOG = logging .getLogger (__name__ )
1716
1817
@@ -37,7 +36,7 @@ def __init__(
3736 language : str ,
3837 credentials : Union [None , str , dict ] = None ,
3938 sample_rate : int = 16000 ,
40- ** kwargs ,
39+ ** kwargs : Any ,
4140 ) -> None :
4241 if credentials :
4342 if isinstance (credentials , str ):
@@ -83,14 +82,14 @@ def __call__(self, context: SpeechContext, frame: np.ndarray) -> None:
8382 if context .is_active :
8483 self ._send (frame )
8584
86- def _begin (self , context ) -> None :
85+ def _begin (self , context : SpeechContext ) -> None :
8786 self ._thread = Thread (
8887 target = self ._receive ,
8988 args = (context ,),
9089 )
9190 self ._thread .start ()
9291
93- def _receive (self , context ) :
92+ def _receive (self , context : SpeechContext ) -> None :
9493 for response in self ._client .streaming_recognize (self ._config , self ._drain ()):
9594 for result in response .results [:1 ]:
9695 for alternative in result .alternatives [:1 ]:
@@ -107,7 +106,7 @@ def _receive(self, context):
107106 context .event ("timeout" )
108107 _LOG .debug ("timeout event" )
109108
110- def _drain (self ):
109+ def _drain (self ) -> Generator :
111110 while data := self ._queue .get ():
112111 yield data
113112
@@ -116,7 +115,7 @@ def _commit(self) -> None:
116115 self ._thread .join ()
117116 self ._thread = None
118117
119- def _send (self , frame ) -> None :
118+ def _send (self , frame : np . ndarray ) -> None :
120119 self ._queue .put (speech .StreamingRecognizeRequest (audio_content = frame .tobytes ()))
121120
122121 def reset (self ) -> None :
0 commit comments