This project demonstrates how to convert text to speech using the TTS APIs from OpenAI and Azure. It has been reworked to provide a streamlined development experience in GitHub Codespaces with a modern file structure and improved diagnostics.
- tts_demo.py: Main Python script that reads text from
text_to_convert.txt
and generates speech audio.- For OpenAI TTS, the script outputs
output.mp3
. - For Azure TTS, the script outputs
output_azure.mp3
.
- For OpenAI TTS, the script outputs
- text_to_convert.txt: Contains the text to convert into speech.
- .env: Environment file (should contain your API keys and region) that is loaded via Codespaces secrets.
- requirements.txt: Lists the Python dependencies.
- .devcontainer/: Contains the
devcontainer.json
for setting up your Codespace. - diagnostics/: Contains scripts for verifying environment variable injection and Azure configuration (
validate_env.py
andvalidate_azure.py
). - .gitignore: Ensures that generated audio files, environment files, and cache directories are not committed.
- Dual TTS Endpoints: Easily switch between OpenAI and Azure TTS using command-line options.
- Customizable Voices: Select voices using the command-line arguments (
--voice
for Azure,--voice-oai
for OpenAI). - Diagnostics Tools: Validate your environment setup with the scripts in the
diagnostics
folder. - GitHub Codespaces Ready: Preconfigured with a
devcontainer
for a seamless VSCode experience in Codespaces.
- nova
- shimmer
- echo
- onyx
- fable
- alloy
- ash
- sage
- coral
- en-US-AriaNeural
- en-US-GuyNeural
- en-US-JennyNeural
- en-GB-LibbyNeural
- en-AU-CatherineNeural
- (See Azure Voice Fonts Documentation for a full list)
- Python 3.8 or higher.
- An OpenAI API key.
- An Azure Speech Service API key and region if using Azure TTS.
- GitHub Codespaces or local Docker environment supporting the devcontainer configuration.
-
Clone the Repository in GitHub Codespaces
- Open your repository in GitHub, click the Code button, and select the Codespaces tab to create a new Codespace.
-
Configure API Keys
- In the root directory, create a file named
.env
(or use Codespaces secrets) with the following content:OPENAI_API_KEY=your-openai-api-key AZURE_API_KEY=your-azure-api-key AZURE_REGION=your-azure-region
- In the root directory, create a file named
-
Install Dependencies
- Open a terminal in the project root and run:
pip install -r requirements.txt
- Open a terminal in the project root and run:
-
Run Diagnostics (Optional)
- Validate that your environment variables are correctly injected:
python diagnostics/validate_env.py python diagnostics/validate_azure.py
- Validate that your environment variables are correctly injected:
-
Running the TTS Demo
- For OpenAI TTS:
python tts_demo.py --endpoint openai --voice-oai nova
- For Azure TTS:
python tts_demo.py --endpoint azure --voice en-US-AriaNeural
- For OpenAI TTS:
After running the script, check the output audio file (either output.mp3
or output_azure.mp3
) using your preferred media player.
- OpenAI TTS API Documentation
- Azure Speech Service Documentation
- GitHub Codespaces Documentation
- Python-dotenv Documentation
This project is provided for educational purposes only. Please adhere to the usage policies of OpenAI and Azure when using their services.
- Thanks to OpenAI and Azure for their TTS services.
- Special thanks to the GitHub Codespaces team for providing an excellent development environment.