Anika
Anika

Reputation: 1

Creating websocket server from AWS Transcribe example code

I have the following code from AWS for using AWS transcribe streaming API. I want to extract the backend server code and make a socket out of it.

Link for code - https://github.com/awslabs/amazon-transcribe-streaming-sdk/blob/develop/examples/simple_file.py

So far I have written this but it neither giving me any error nor it is printing anything.

async def write_chunks(stream,data):
    # This connects the raw audio chunks generator coming from the microphone
    # and passes them along to the transcription stream.
    async for chunk in data:
        await stream.input_stream.send_audio_event(audio_chunk=chunk)
    await stream.input_stream.end_stream()
 
 
class MyEventHandler(TranscriptResultStreamHandler):
    def handle_transcript_event(self, transcript_event: TranscriptEvent):
        # This handler can be implemented to handle transcriptions as needed.
        # Here's an example to get started.
        results = transcript_event.transcript.results
        for result in results:
            for alt in result.alternatives:
                print(alt.transcript)
 
async def handle_client(data):
    print("Received audio")
 
    stream = await client.start_stream_transcription(
        language_code="en-US",
        media_sample_rate_hz=16000,
        media_encoding="pcm"
    )
    handler = MyEventHandler(stream.output_stream)
    # This connects the raw audio chunks generator coming from the microphone
    # and passes them along to the transcription stream.
    await asyncio.gather(write_chunks(stream,data), handler.handle_events())
 
    
async def main():
    server = await websockets.serve(handle_client, "localhost", 8282)
    await server.wait_closed()
    
asyncio.run(main())    

Upvotes: 0

Views: 113

Answers (0)

Related Questions