You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey folks, thanks for putting this together as a useful interface to AWS transcribe.
I'm looking for an example that works with an async websocket server calling AWS transcribe.
The problem I'm running into right now is that handler.handle_events() is a blocking call that prevents processing further messages – here's my code thus far with a FastAPI websocket app:
importjsonimporttypingfromfastapiimportFastAPI, WebSocketfromstarlette.endpointsimportWebSocketEndpointfromamazon_transcribe.clientimportTranscribeStreamingClientfromamazon_transcribe.handlersimportTranscriptResultStreamHandlerfromamazon_transcribe.modelimportTranscriptEventimportnumpyasnpfromscipy.io.wavfileimportread, writeapp=FastAPI()
"""Here's an example of a custom event handler you can extend toprocess the returned transcription results as needed. Thishandler will simply print the text out to your interpreter."""classMyEventHandler(TranscriptResultStreamHandler):
asyncdefhandle_transcript_event(self, transcript_event: TranscriptEvent):
# This handler can be implemented to handle transcriptions as needed.# Here's an example to get started.print("LOG: handler called")
results=transcript_event.transcript.resultsforresultinresults:
foraltinresult.alternatives:
print(f"TRANSCRIPT: {alt.transcript}")
@app.websocket_route("/media")classTranscription(WebSocketEndpoint):
asyncdefon_connect(self, websocket: WebSocket) ->None:
awaitwebsocket.accept()
self.transcription_client=TranscribeStreamingClient(region="ca-central-1")
self.stream=Noneself.handler=Noneself.chunks_np=np.array([])
asyncdefon_disconnect(self, websocket: WebSocket, close_code: int) ->None:
print("LOG: disconnect: ")
awaitself.stream.input_stream.end_stream()
asyncdefon_receive(self, websocket: WebSocket, data: typing.Any) ->None:
message=json.loads(data)
ifmessageisnotNone:
# print("LOG: message received: ", message)ifself.streamisNoneandself.handlerisNone:
self.stream=awaitself.transcription_client.start_stream_transcription(
language_code="en-US",
media_sample_rate_hz=44100,
media_encoding="pcm",
)
self.handler=MyEventHandler(self.stream.output_stream)
chunk=message["chunk"]
chunk_np=np.fromiter(chunk.values(), dtype=np.int16)
self.chunks_np=np.append(self.chunks_np, chunk_np)
chunk_bytes=chunk_np.tobytes()
awaitself.stream.input_stream.send_audio_event(audio_chunk=chunk_bytes)
awaitself.handler.handle_events()
Any guidance as to what's a good way to call handler.handle_events() in a non-blocking manner?
Have tried wrapping the handler.handle_events() in a while loop and invoking that via a background task and that runs into a similar blocking behaviour as well.
The text was updated successfully, but these errors were encountered:
Hey folks, thanks for putting this together as a useful interface to AWS transcribe.
I'm looking for an example that works with an async websocket server calling AWS transcribe.
The problem I'm running into right now is that
handler.handle_events()
is a blocking call that prevents processing further messages – here's my code thus far with a FastAPI websocket app:Any guidance as to what's a good way to call
handler.handle_events()
in a non-blocking manner?Have tried wrapping the
handler.handle_events()
in a while loop and invoking that via a background task and that runs into a similar blocking behaviour as well.The text was updated successfully, but these errors were encountered: