· last year · Nov 25, 2023, 07:35 AM
1from elevenlabs import generate, set_api_key, Voice, VoiceSettings
2from pydub import AudioSegment
3import io
4import os
5import hashlib
6from utils.os_stuff import get_env_var_or_fail
7import logging
8
9set_api_key(get_env_var_or_fail('ELEVEN_LABS_API_KEY'))
10
11HOST_VOICE = Voice(
12 voice_id="21m00Tcm4TlvDq8ikWAM",
13 name="Rachel",
14 category="premade",
15 settings=VoiceSettings(stability=0.35, similarity_boost=0.9),
16)
17
18ADS_VOICE = Voice(
19 voice_id="TxGEqnHWrfWFTfGW9XjX",
20 name="Josh",
21 category="premade",
22 settings=VoiceSettings(stability=0.35, similarity_boost=0.9),
23)
24
25
26def load_audio_bytes(audio_bytes):
27 audio_file = io.BytesIO(audio_bytes)
28 audio_segment = AudioSegment.from_file(audio_file, format='mp3')
29 return audio_segment
30
31
32def convert_text_to_mp3(text, voice):
33 # Generate the cache key by MD5 hashing the text
34 cache_key = hashlib.md5(text.encode()).hexdigest()
35
36 # Check if the file already exists in cache
37 cache_dir = ".eleven_labs_cache"
38 cache_file = os.path.join(cache_dir, f"{cache_key}.mp3")
39
40 if not os.path.exists(cache_dir):
41 os.makedirs(cache_dir)
42
43 if os.path.exists(cache_file):
44 # If it does exist, load and return as an AudioSegment
45 audio_segment = AudioSegment.from_mp3(cache_file)
46 else:
47 # If it does not exist, call the API, create, save, and return as an AudioSegment
48 char_count = len(text)
49 logging.info(f'Calling eleven labs for {char_count} chars...')
50 section_1_voice_over = load_audio_bytes(generate(
51 text=text,
52 voice=voice
53 ))
54 section_1_voice_over.export(cache_file, format='mp3')
55 audio_segment = section_1_voice_over
56
57 return audio_segment