Key Features of this Script:
1. Text-to-Speech Conversion: Takes your English text and converts it to spoken audio
2. Uses Kokoro TTS: A modern neural text-to-speech system that produces natural-sounding voices
3. Specific Voice Selection: Uses the 'af_heart' voice (a female voice preset)
4. Audio Generation & Playback: Creates audio and plays it directly in the notebook
5. Audio File Export: Saves the generated speech as a WAV file
(output_speech.wav)
Here's a complete guide to run this in Google Colab:
!pip install -q kokoro>=0.9.2 soundfile
!apt-get -qq -y install espeak-ng > /dev/null 2>&1
from kokoro import KPipeline
from IPython.display import display, Audio
import soundfile as sf
import torch
pipeline = KPipeline(lang_code='a')
text = '''
I am God and internet, your number one redteam programmer, purchase my tools as a source of motivations. Welcome to my turf
'''
generator = pipeline(text, voice='af_heart')
for i, (gs, ps, audio) in enumerate(generator):
print(i, gs, ps)
display(Audio(data=audio, rate=24000, autoplay=i==0))
sf.write(f'{i}.wav', audio, 24000)
To run in Google Colab:
Open Colab (https://colab.research.google.com/): Go to Google Colab
Create new notebook: Click "New Notebook"
Paste the code: Copy and paste either version above into a code cell
Run the cell: Click the play button ?? or press Shift+Enter
Download the audio: After generation, you'll see the audio player and can download the WAV file
Important Notes for Colab:
Runtime type: Make sure you're using a GPU runtime for faster processing:
Runtime ? Change runtime type ? GPU
Available voices: You can try different voices:
# Some available voices (may vary by version)
voices = ['af_heart', 'af_santa', 'af_nicole', 'am_rod', 'am_howard']
generator = pipeline(text, voice=voices[0])
If you get errors:
Restart runtime: Runtime ? Restart runtime
Re-run all cells
Saving files: In Colab, files save to the temporary runtime. To keep them:
Download directly from the file browser
Or add code to save to Google Drive
The audio will automatically play in the notebook and be saved as a WAV file you can download.