able-leather-48732
08/20/2025, 3:10 PMtall-airline-16756
08/20/2025, 3:10 PMtall-airline-16756
08/20/2025, 3:11 PMable-leather-48732
08/20/2025, 3:13 PMtall-airline-16756
08/20/2025, 3:14 PMaiohttp
library to make asynchronous HTTP requests. This way, you can send text and get audio back smoothly without blocking your app.
3. Don’t forget to include your Hugging Face API key in the request headers to authenticate your calls.
4. Once you get the audio response, you can either save it to a file or stream it directly—whichever fits your app best.
Here’s a simple example to illustrate:
import aiohttp
import asyncio
async def synthesize_text(api_endpoint, api_key, text):
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
payload = {
"inputs": text
}
async with aiohttp.ClientSession() as session:
async with <http://session.post|session.post>(api_endpoint, headers=headers, json=payload) as response:
if response.status == 200:
audio_data = await response.read()
with open("output.wav", "wb") as audio_file:
audio_file.write(audio_data)
print("Audio saved as output.wav")
else:
print(f"Error: {response.status}, {await response.text()}")
# Example usage
api_endpoint = "<HUGGING_FACE_API_ENDPOINT>" # Replace with your endpoint
api_key = "<HUGGING_FACE_API_KEY>" # Replace with your API key
text = "Hello, this is a test."
asyncio.run(synthesize_text(api_endpoint, api_key, text))
This little snippet sends your text to the Hugging Face TTS endpoint and saves the audio as a WAV file. Feel free to tweak it to match your specific model and what your app needs.
If you want me to help with anything else or customize this further, just let me know—I’m here to help!
Sources: livekit-plugins/livekit-plugins-speechify/livekit/plugins/speechify/tts.py | Baseten TTS integration guide | LiveKit Docs