import asyncio
import json
import requests
import websockets
api_key = <api key>
client_id = <client id>
audio_file_path = <audio file path>
def get_session_id():
url = "https://ai.juvoly.nl/api/authentication/session"
headers = {
"X-Juvoly-Api-Key": api_key,
"X-Juvoly-Client-Id": client_id,
"Content-Type": "application/json"
}
response = requests.post(url, headers=headers, json={})
return response.json().get('sessionId', None)
async def read(websocket):
while True:
response = await websocket.recv()
response_data = json.loads(response)
if response_data.get("type") == "transcript":
print("Received:", response_data.get("transcript"))
async def write(websocket):
with open(audio_file_path, 'rb') as audio_stream:
while chunk := audio_stream.read(10000):
await websocket.send(chunk)
async def connect(session_id):
websocket_url = f"wss://ai.juvoly.nl/ws/speech/{client_id}/{session_id}"
async with websockets.connect(websocket_url) as websocket:
ready_message = await websocket.recv()
if json.loads(ready_message).get("type") == "ready":
write_task = asyncio.create_task(write(websocket))
read_task = asyncio.create_task(read(websocket))
await asyncio.gather(write_task, read_task)
if __name__ == "__main__":
session_id = get_session_id()
asyncio.run(connect(session_id))