Text-to-Speech Conversion using Sarvam AI API
This notebook demonstrates how to convert text into speech using the Sarvam AI Text-to-Speech API.The resulting audio files are saved as .wav
files.
Prerequisites
Before running this notebook, ensure you have the following installed:
- Python 3.7 or higher
- Required Python packages:
requests
,base64
,wave
You can install the required packages using pip:
Import Required Libraries
First, let’s import all the necessary libraries.
2. Set Up the API Endpoint and Payload
To use the Saaras API, you need an API subscription key. Follow these steps to set up your API key:
- Obtain your API key: If you don’t have an API key, sign up on the Sarvam AI Dashboard to get one.
- Replace the placeholder key: In the code below, replace “YOUR_SARVAM_AI_API_KEY” with your actual API key.
Setting Up the API Endpoint and Payload
This section defines the API endpoint and the payload for the translation request. Replace the placeholder values with your actual API key and desired parameters.
Text to be converted into speech
Split Text into Chunks
The Sarvam AI API may have a limit on the number of characters per request. To handle this, we split the text into chunks of 500 characters or less.
Process Each Chunk
Iterate over each chunk, send it to the Sarvam AI API, and save the resulting audio as a .wav
file.
Output
After running the notebook, you will have multiple .wav
files (e.g., output1.wav
, output2.wav
, etc.) containing the speech for each chunk of text.
Conclusion
This notebook provides a step-by-step guide to converting text into speech using the Sarvam AI API. You can modify the text, language, and other parameters to suit your specific needs.
Additional Resources
For more details, refer to the our official documentation and we are always there to support and help you on our Discord Server:
- Documentation: docs.sarvam.ai
- Community: Join the Discord Community
9. Final Notes
- Keep your API key secure.
- Use clear audio for best results.
Keep Building! 🚀