import os
import asyncio
import aiohttp
API_KEY = os.environ["SMALLEST_API_KEY"]
URL = "https://api.smallest.ai/waves/v1/lightning-v3.1/get_speech"
async def synthesize(session, text, filename):
async with session.post(URL, headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
}, json={
"text": text,
"voice_id": "magnus",
"sample_rate": 24000,
"output_format": "wav",
}) as resp:
audio = await resp.read()
with open(filename, "wb") as f:
f.write(audio)
print(f"Saved {filename}")
async def main():
async with aiohttp.ClientSession() as session:
await asyncio.gather(
synthesize(session, "First sentence.", "async_1.wav"),
synthesize(session, "Second sentence.", "async_2.wav"),
synthesize(session, "Third sentence.", "async_3.wav"),
)
asyncio.run(main())