importasyncioimportjsonimportwebsocketsasyncdefmain():# Setupurl="wss://s-api.prosa.ai/v2/speech/stt"filename="audio_file.mp3"api_key="..."# Authenticate via HTTP Headerheaders={"x-api-key":api_key}asyncwithwebsockets.connect(url,extra_headers=headers)asws:# Configure the sessionawaitws.send(json.dumps({"label":"Streaming Test","model":"stt-general-online","include_partial":False}))# Concurrently send audio data and receive resultsawaitasyncio.gather(send_audio(filename,ws),receive_message(ws))asyncdefsend_audio(filename:str,ws,chunk_size:int=16000):withopen(filename,"rb")asf:# Read file to sendwhiledata:=f.read(chunk_size):awaitws.send(data)awaitws.send(b"")# Signifies the end of audio streamasyncdefreceive_message(ws):whileTrue:data=json.loads(awaitws.recv())# Receive message# Identify message typemessage_type=data["type"]ifmessage_type=="result":transcript=data["transcript"]# Process final transcriptelifmessage_type=="partial":transcript=data["transcript"]# Process partial transcriptprint(data)if__name__=='__main__':asyncio.run(main())
constWebSocket=require('ws');constfs=require('fs');(async()=>{// Setupconsturl='wss://s-api.prosa.ai/v2/speech/stt';constapiKey='...';constfilename='audio_file.mp3';letclient=newWebSocket(url);// Wait for the client to connect using async/awaitawaitnewPromise(resolve=>client.once('open',resolve));// Authenticate via websocket messageclient.send(JSON.stringify({'token':apiKey}));// Configure the sessionclient.send(JSON.stringify({'model':'stt-general-online','label':'This is example streaming session'}));// Send audio data via websocketletstream=fs.createReadStream(filename);stream.on('readable',async()=>{// Read file to sendletchunk;while((chunk=stream.read(16000))){client.send(chunk,{binary:true})}});stream.on('close',()=>{// Signifies the end of audio streamclient.send(null,{binary:true})});// Receive results via websocketclient.on('message',msg=>{letdata=JSON.parse(msg);constmessage_type=data["type"]if(message_type==="result"){consttranscript=data["transcript"]// Process final transcript}elseif(message_type==="partial"){consttranscript=data["transcript"]// Process partial transcript}});})();
Authentication
The client starts a session by opening a websocket connection and providing the authentication token either via x-api-key header or as the first websocket message.
HTTP Header Authentication
For some websocket client implementation, it is possible to include extra HTTP header on the websocket request.
Using websockets library, it is possible to include extra headers via extra_headers keyword argument.
1 2 3 4 5 6 7 8 91011121314151617181920
importasyncioimportwebsocketsasyncdefmain():# Setupurl="wss://s-api.prosa.ai/v2/speech/stt"api_key="..."# Authenticate via HTTP Headerheaders={"x-api-key":api_key}asyncwithwebsockets.connect(url,extra_headers=headers)asws:passif__name__=='__main__':asyncio.run(main())
Websocket Message Authentication
If it is not feasible for the API Key to be included via HTTP Header, the API Key is expected to be sent as the first websocket message by the client.
1 2 3 4 5 6 7 8 910111213141516171819202122
importasyncioimportjsonimportwebsocketsasyncdefmain():# Setupurl="wss://s-api.prosa.ai/v2/speech/stt"api_key="..."asyncwithwebsockets.connect(url)asws:# Authenticate via websocket messageawaitws.send(json.dumps({"token":api_key}))if__name__=='__main__':asyncio.run(main())
1 2 3 4 5 6 7 8 91011121314151617
constWebSocket=require('ws');(async()=>{// Setupconsturl='wss://s-api.prosa.ai/v2/speech/stt';constapiKey='...';letclient=newWebSocket(url);// Wait for the client to connect using async/awaitawaitnewPromise(resolve=>client.once('open',resolve));// Authenticate via websocket messageclient.send(JSON.stringify({'token':apiKey}));})();
importasyncioimportjsonimportwebsocketsasyncdefmain():# Setupurl="wss://s-api.prosa.ai/v2/speech/stt"api_key="..."# Authenticate via HTTP Headerheaders={"x-api-key":api_key}asyncwithwebsockets.connect(url,extra_headers=headers)asws:# Configure the sessionawaitws.send(json.dumps({"model":"stt-general-online","label":"This is example streaming session"}))if__name__=='__main__':asyncio.run(main())
1 2 3 4 5 6 7 8 91011121314151617181920212223
constWebSocket=require('ws');(async()=>{// Setupconsturl='wss://s-api.prosa.ai/v2/speech/stt';constapiKey='...';letclient=newWebSocket(url);// Wait for the client to connect using async/awaitawaitnewPromise(resolve=>client.once('open',resolve));// Authenticate via websocket messageclient.send(JSON.stringify({'token':apiKey}));// Configure the sessionclient.send(JSON.stringify({'model':'stt-general-online','label':'This is example streaming session'}));})();
Info
See Configuration for more information on the message.
Sending Audio Data
The audio data are sent as bytes. The audio header is expected to be sent as the initial chunk. For reference, the snippet below demonstrate how to send audio data read from a file.
Attention
The server expects an empty bytes to be sent as the last message. This is to signify the end of the audio stream.
importasyncioimportjsonimportwebsocketsasyncdefmain():# Setupurl="wss://s-api.prosa.ai/v2/speech/stt"filename="audio_file.mp3"api_key="..."# Authenticate via HTTP Headerheaders={"x-api-key":api_key}asyncwithwebsockets.connect(url,extra_headers=headers)asws:# Configure the sessionawaitws.send(json.dumps({"model":"stt-general-online","label":"This is example streaming session"}))# Send audio data via websocketawaitsend_audio(filename,ws)asyncdefsend_audio(filename:str,ws,chunk_size:int=16000):withopen(filename,"rb")asf:# Read file to sendwhiledata:=f.read(chunk_size):awaitws.send(data)awaitws.send(b"")# Signifies the end of audio streamif__name__=='__main__':asyncio.run(main())
constWebSocket=require('ws');constfs=require('fs');(async()=>{// Setupconsturl='wss://s-api.prosa.ai/v2/speech/stt';constapiKey='...';constfilename='audio_file.mp3';letclient=newWebSocket(url);// Wait for the client to connect using async/awaitawaitnewPromise(resolve=>client.once('open',resolve));// Authenticate via websocket messageclient.send(JSON.stringify({'token':apiKey}));// Configure the sessionclient.send(JSON.stringify({'model':'stt-general-online','label':'This is example streaming session'}));// Send audio data via websocketletstream=fs.createReadStream(filename);stream.on('readable',async()=>{// Read file to sendletchunk;while((chunk=stream.read(16000))){client.send(chunk,{binary:true})}});stream.on('close',()=>{// Signifies the end of audio streamclient.send(null,{binary:true})});})();
Info
Halting the data transfer allows for pausing of the transcription process.
Receiving Messages
There are several messages sent by server to the client. The client is expected to send and receive messages concurrently.
Info
See Subscribe Operation for more information on the messages that are sent by the server
importasyncioimportjsonimportwebsocketsasyncdefmain():# Setupurl="wss://s-api.prosa.ai/v2/speech/stt"filename="audio_file.mp3"api_key="..."# Authenticate via HTTP Headerheaders={"x-api-key":api_key}asyncwithwebsockets.connect(url,extra_headers=headers)asws:# Configure the sessionawaitws.send(json.dumps({"label":"Streaming Test","model":"stt-general-online","include_partial":False}))# Concurrently send audio data and receive resultsawaitasyncio.gather(send_audio(filename,ws),receive_message(ws))asyncdefsend_audio(filename:str,ws,chunk_size:int=16000):withopen(filename,"rb")asf:# Read file to sendwhiledata:=f.read(chunk_size):awaitws.send(data)awaitws.send(b"")# Signifies the end of audio streamasyncdefreceive_message(ws):whileTrue:data=json.loads(awaitws.recv())# Receive message# Identify message typemessage_type=data["type"]ifmessage_type=="result":transcript=data["transcript"]# Process final transcriptelifmessage_type=="partial":transcript=data["transcript"]# Process partial transcriptprint(data)if__name__=='__main__':asyncio.run(main())
constWebSocket=require('ws');constfs=require('fs');(async()=>{// Setupconsturl='wss://s-api.prosa.ai/v2/speech/stt';constapiKey='...';constfilename='audio_file.mp3';letclient=newWebSocket(url);// Wait for the client to connect using async/awaitawaitnewPromise(resolve=>client.once('open',resolve));// Authenticate via websocket messageclient.send(JSON.stringify({'token':apiKey}));// Configure the sessionclient.send(JSON.stringify({'model':'stt-general-online','label':'This is example streaming session'}));// Send audio data via websocketletstream=fs.createReadStream(filename);stream.on('readable',async()=>{// Read file to sendletchunk;while((chunk=stream.read(16000))){client.send(chunk,{binary:true})}});stream.on('close',()=>{// Signifies the end of audio streamclient.send(null,{binary:true})});// Receive results via websocketclient.on('message',msg=>{letdata=JSON.parse(msg);constmessage_type=data["type"]if(message_type==="result"){consttranscript=data["transcript"]// Process final transcript}elseif(message_type==="partial"){consttranscript=data["transcript"]// Process partial transcript}});})();
Receiving Results
The attribute type in every message identifies the type of the message.
There are 2 types of transcription result:
partial: the result of an ongoing speech segment
result: the result of a completed speech segment
1 2 3 4 5 6 7 8 910111213
asyncdefreceive_message(ws):whileTrue:data=json.loads(awaitws.recv())# Receive message# Identify message typemessage_type=data["type"]ifmessage_type=="result":transcript=data["transcript"]# Process final transcriptelifmessage_type=="partial":transcript=data["transcript"]# Process partial transcript
1 2 3 4 5 6 7 8 91011121314
// Receive results via websocketclient.on('message',msg=>{letdata=JSON.parse(msg);constmessage_type=data["type"]if(message_type==="result"){consttranscript=data["transcript"]// Process final transcript}elseif(message_type==="partial"){consttranscript=data["transcript"]// Process partial transcript}});