-
Notifications
You must be signed in to change notification settings - Fork 13
/
tts.py
260 lines (219 loc) · 10.3 KB
/
tts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Please shoot me a message, open an issue, or comment on Interconnects if you're using this code! I'd love to know.
import argparse
import os
import subprocess
from datetime import timedelta
from math import floor
import numpy as np
import requests
import yaml
from pydub import AudioSegment
from tqdm import tqdm
def strip_title(string):
"""
Config entry, as dicts, keep track of titles as N_title goes here.
Return the text after the _
"""
if "_" in string:
return string.split("_")[1]
else:
return string
def get_cumulative_length(file_list):
cumulative_length = 0
for filename in file_list:
# filepath = os.path.join(directory, filename)
audio = AudioSegment.from_file(filename)
cumulative_length += len(audio)
return cumulative_length / 1000.0 # Convert to seconds
def request_audio(url, payload, headers, querystring, filename):
"""
A function to replace the following code:
response = requests.post(url_nathan, json=payload, headers=headers, params=querystring)
with open(f"{args.output}_part{i}.mp3", 'wb') as part_file:
for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
if chunk:
part_file.write(chunk)
"""
# remove \n and > from payload text
payload["text"] = payload["text"].replace("\n", " ")
payload["text"] = payload["text"].replace(">", "")
if not os.path.exists(filename):
print("-> audio request send to 11labs")
try:
response = requests.post(url, json=payload, headers=headers, params=querystring)
with open(filename, "wb") as part_file:
for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
if chunk:
part_file.write(chunk)
except Exception as e:
print(f"Error: {e}")
else:
print(f"-> audio file {filename} already exists, skipping request")
if __name__ == "__main__":
# import argparse and define txt file path and output path
parser = argparse.ArgumentParser()
parser.add_argument("--input", type=str, required=True, help="input directory to work with")
parser.add_argument("--output", type=str, default="generated_audio", help="output mp3 file path")
parser.add_argument("--elelabs_voice", type=str, default="JOTluSP6086ORVtQED4S", help="11labs voice id")
parser.add_argument("--start_heading", type=str, default="", help="start at section named in generation")
parser.add_argument("--farewell_audio", type=str, default="source/repeat/farewell.mp3", help="farewell audio path")
parser.add_argument("--figure_audio", type=str, default="source/repeat/see-figure.mp3", help="figure audio path")
parser.add_argument("--ignore_title", action="store_true", default=False, help="ignore title and date in config")
args = parser.parse_args()
TOTAL_GEN_AUDIO_FILES = 0
CHUNK_SIZE = 1024 # size of chunks to write to file / download
url_nathan = f"https://api.elevenlabs.io/v1/text-to-speech/{args.elelabs_voice}"
# url_newsread = "https://api.elevenlabs.io/v1/text-to-speech/frqJk20JrduLkgUgHtMR"
API_KEY = os.getenv("ELELABS_API_KEY")
headers = {
"Accept": "audio/mpeg",
"Content-Type": "application/json",
"xi-api-key": API_KEY, # "c23b31aabf009cb93c8feb5f4ddedc85",
}
# Uncomment for higher bitrate (larger files)
# querystring = {"output_format":"mp3_44100_192"}
querystring = {}
# load yml file at args.input + config.yml
with open(args.input + "config.yml", "r") as f:
config = yaml.load(f, Loader=yaml.FullLoader)
# audio_config for 11labs, can change these
payload = {
"model_id": "eleven_multilingual_v2",
"voice_settings": {"similarity_boost": 0.75, "stability": 0.50, "style": 0.05, "use_speaker_boost": True},
}
# create dir audio at args.input + audio
audio_dir = args.input + "audio"
if not os.path.exists(audio_dir):
os.makedirs(audio_dir)
first_gen = True
skip_found = False
section_titles = []
see_figures_idx = []
# iterate over config file that contains headings, text, and figures
for i, (heading, content) in tqdm(enumerate(config.items())):
i = str(i).zfill(2)
if heading in ["md", "date"]:
continue
if not args.ignore_title:
print(f"Generating audio for section {i}, {heading}")
if len(args.start_heading) > 0:
if heading == args.start_heading or skip_found:
skip_found = True
pass
else:
continue
heading = strip_title(heading)
# skip md file path and date
section_titles.append(heading)
if first_gen:
# generate audio file for Title + date
heading = heading + " was published on " + config["date"] + "."
first_gen = False
payload["text"] = heading
# generate audio for heading
fname = f"{audio_dir}/audio_{i}_0.mp3"
request_audio(url_nathan, payload, headers, querystring, fname)
TOTAL_GEN_AUDIO_FILES += 1
# iterate over list of dicts in content and
for para in content:
idx = para["index"]
# convert IDX to 2 digit string
idx = str(idx).zfill(2)
# if para is dict, prepare special audio to indicate figure
if isinstance(para["content"], dict):
# [OPTIONAL] copy file source/repeat/see-figure.mp3 as audio_{idx}.mp3
# only copy if it exists, otherwise it ignores the figures
if os.path.exists(args.figure_audio):
os.system(f"cp {args.figure_audio} {audio_dir}/audio_{i}_{idx}.mp3")
see_figures_idx.append(TOTAL_GEN_AUDIO_FILES)
TOTAL_GEN_AUDIO_FILES += 1
pass
# if para is str, generate audio
elif isinstance(para["content"], str):
payload["text"] = para["content"]
fname = f"{audio_dir}/audio_{i}_{idx}.mp3"
request_audio(url_nathan, payload, headers, querystring, fname)
TOTAL_GEN_AUDIO_FILES += 1
else:
print("Config Error: para is neither dict nor str")
# concatenate all files in audio_dir with ffmpeg into input dir
# if file generated_audio.mp3 already exists, delete it (prevent infinite loop)
if os.path.exists(audio_dir + "/" + args.output + ".mp3"):
os.remove(audio_dir + "/" + args.output + ".mp3")
if os.path.exists(audio_dir + "/" + args.output + "_podcast" + ".mp3"):
os.remove(audio_dir + "/" + args.output + "_podcast" + ".mp3")
# list mp3 files in audio_dir
audio_files_short = [audio_dir + "/" + f for f in os.listdir(audio_dir) if f.endswith((".mp3"))]
audio_files = sorted(audio_files_short)
# output_files = [f"{audio_dir}/audio_{i}.mp3" for i in range(0, TOTAL_GEN_AUDIO_FILES)]
print(f"Sorted audio files {audio_files}")
# for podcast version remove "see figures" from see_figures_idx of files above
podcast_files = [f for i, f in enumerate(audio_files) if i not in see_figures_idx]
# [OPTIONAL] add source/repeat/farewell.mp3 to end of list (here to not mess with later code)
# only do this if the farewell audio exists
if os.path.exists(args.farewell_audio):
audio_files.append(args.farewell_audio)
podcast_files.append(args.farewell_audio)
subprocess.run(
["ffmpeg", "-i", "concat:" + "|".join(audio_files), "-c", "copy", audio_dir + "/" + args.output + ".mp3"]
)
subprocess.run(
[
"ffmpeg",
"-i",
"concat:" + "|".join(podcast_files),
"-c",
"copy",
audio_dir + "/" + args.output + "_podcast" + ".mp3",
]
)
podcast_files = podcast_files[:-1]
# TODO remove all acronyms and other filtering, some that are bad are SOTA and MoE
# TODO add seperate voice for quotes / quote detection
# Get length of the sections and print in podcast format
# get indicies in between first set of _ (e.g. audio_N_Y.mp3, get unique Ns)
sections = np.unique([s.split("_")[1] for s in audio_files_short])
sections = [str(s) for s in sections]
def print_if_hour(seconds, total_len):
if total_len > 3600:
return str(timedelta(seconds=seconds))
elif total_len < 600:
return str(timedelta(seconds=seconds))[3:]
else:
return str(timedelta(seconds=seconds))[2:]
# TODO make below work for multiple figures :/
# iterate over headings for chapters, take first figure per section
files_list_of_lists = []
for s in sections:
files = [f for f in audio_files_short if f.split("_")[1] == s]
files_list_of_lists.append(files)
section_lens = [get_cumulative_length(list_l) for list_l in files_list_of_lists]
total_len = sum(section_lens)
print(f"Cumulative length of all audio files: {section_lens} seconds")
print("----------------------------------")
print("Printing podcast chapter versions (does not include `see figure` audio):")
print("----------------------------------")
print(section_titles[0])
print(next(iter(config.values()))[0]["content"])
print("This is AI generated audio with Python and 11Labs")
print("Source code: https://github.com/natolambert/interconnects-tools")
print("Original post: TODO")
print()
cur_len = 0
for section_title, section_len in zip(section_titles, section_lens):
print(f"{print_if_hour(floor(cur_len), total_len)} {section_title}")
cur_len += section_len
print("----------------------------------")
print("Printing youtube chapter versions:")
print("----------------------------------")
files_list_of_lists = []
for s in sections:
files = [f for f in podcast_files if f.split("_")[1] == s]
files_list_of_lists.append(files)
section_lens = [get_cumulative_length(list_l) for list_l in files_list_of_lists]
total_len = sum(section_lens)
# print first item of the first value content in config, which is the substitle
cur_len = 0
for section_title, section_len in zip(section_titles, section_lens):
print(f"{print_if_hour(floor(cur_len), total_len)} {section_title}")
cur_len += section_len