Hi. 🙂 ✋
In this post, I will show you how to make simple text-to-speech. Generator using an amazing API called ElevenLabs.
Let's start step by step
step 1
Create an account in ElevenLabs from here
step 2
Get Your API Key from here
Step 3
Install required libraries.
elevenlabs==2.35.0
python-dotenv==1.2.1
Step 4
Create .env to store your API key safely
Step 5
Writing code 😎
from os import getenv
from dotenv import load_dotenv
import webbrowser
from elevenlabs.client import ElevenLabs
from elevenlabs.play import play
load_dotenv()
api_key = getenv('api_key')
if not api_key:
print('You need api key')
print('Create account from : https://try.elevenlabs.io/2rb2e0emy5xg')
print('Go to :https://elevenlabs.io/app/developers/api-keys')
print('Or write any thing to open the link on webbrowser')
text = input('write any thing:')
webbrowser.open_new_tab(url='https://try.elevenlabs.io/2rb2e0emy5xg')
elevenlabs = ElevenLabs(api_key=api_key)
text = """
"Okay… [calm] breathe. Just one step at a time. [hesitates] I thought I was ready for this, but my hands are shaking.
[softly] Listen… I’m not angry. I’m just… disappointed. [frustrated sigh] Because you promised me you would show up.
[trying to sound fine] It’s okay! Really. I can handle it. [voice cracks] I can handle it… I just didn’t want to handle it alone.
[whispers] Do you hear that? The room gets so loud when nobody answers. [pauses] And then your brain starts filling in the silence with worst-case stories.
[sudden realization] Wait. No— I’m doing it again. [grounded] I’m here. I’m safe. I can choose what happens next.
[hopeful] Maybe this isn’t the end of the story. [small laugh] Maybe it’s the part where it finally turns.
[excited] Okay—okay. New plan. [confident] I’m going to call you once, then I’m going to move forward. [smiling] Because I’m done begging for a place in my own life."
"""
audio = elevenlabs.text_to_speech.convert(
voice_id='hpp4J3VqNfWAUOO0d1Us',
model_id='eleven_v3',
text = text
)
try:
play(audio)
except Exception as e:
# If you are on linux, you need to install ffmpeg
if 'ffmpeg' in str(e):
print('Error playing audio, make sure you have ffmpeg installed')
t = input('open ffmpeg installation guide on webbrowser? (y/n)')
if t.lower() == 'y':
webbrowser.open_new_tab(url='https://www.youtube.com/watch?v=5kjka11cKEY')
else:
print('You can install ffmpeg from here: https://ffmpeg.org/download.html')
Top comments (0)