-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathepub-to-audiobook-hf.py
304 lines (192 loc) · 9.03 KB
/
epub-to-audiobook-hf.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python3
import re
import sys
from typing import List, Tuple
import os
import time
import argparse
from m4b_util.helpers import Audiobook
from pathlib import Path
from gradio_client import Client
from huggingface_hub import HfApi
import ebooklib
from ebooklib import epub
from bs4 import BeautifulSoup
from natsort import natsorted
########################################################################
# Configure these before running
#######################################################################
spaces_api_url = os.getenv('SPACES_API_URL', 'https://xxxxxxxxxxxxxxxxxxxxxx') #Use form: https://space-name.hf.space/, nothing after .space/
hf_token = os.getenv('HF_TOKEN', 'hf_xxxxxxxxxxxxxxxxxxxxxxxxxxxx')
hf_repo_id = os.getenv('HF_REPO_ID','') #Example: Dupaja/styletts2-public
#########################################################################
# HF Space Control - Spins Up, or Pauses Space, by ID
#########################################################################
def control_hf_space(hf_api):
repo_status = hf_api.get_space_runtime(repo_id=hf_repo_id, token=hf_token)
current_status = repo_status.stage
ready_to_restart = ['PAUSED','STOPPED']
space_error_state = ['NO_APP_FILE','CONFIG_ERROR','BUILD_ERROR','RUNTIME_ERROR','DELETING']
time_slept = 0
if current_status in space_error_state:
print('Your Space is unable to be run at the moment. Status: '+current_status)
print('Your Space must be either RUNNING, PAUSED, BUILDING / RUNNING_BUILDING, or STOPPED for this script to work.')
print('Please check your Space config, and start manually')
sys.exit(0)
elif current_status == 'RUNNING':
return True
elif current_status in ready_to_restart:
repo_status = hf_api.restart_space(repo_id=hf_repo_id, token=hf_token)
current_status = repo_status.stage
print('Restarting your HF Space')
while current_status != 'RUNNING' and time_slept < 600:
time.sleep(15)
time_slept += 15
print('Your space is currently: '+current_status+'. (Time since check started: '+str(time_slept)+' seconds.)')
repo_status = hf_api.get_space_runtime(repo_id=hf_repo_id, token=hf_token)
current_status = repo_status.stage
if current_status != 'RUNNING':
print('Space startup unsuccesfully took more than 10 min, with status: '+current_status)
sys.exit(0)
else:
return True
########################################################################
# StyleTTS2 Code, running on HF Spaces
########################################################################
def convert_chapter(client,chapter_path, chapter_paragraphs, style_voice):
start_time = time.time()
joined_chapter = '\n'.join(chapter_paragraphs)
voicelist = ['f-us-1', 'f-us-2', 'f-us-3', 'f-us-4', 'm-us-1', 'm-us-2', 'm-us-3', 'm-us-4']
if style_voice in voicelist:
result = client.predict(
joined_chapter,
style_voice,
3, # float (numeric value between 3 and 15) in 'Diffusion Steps' Slider component
api_name="/synthesize"
)
else:
result = client.predict(
joined_chapter, # str in 'Text' Textbox component
3, # float (numeric value between 3 and 15) in 'Diffusion Steps' Slider component
api_name="/ljsynthesize"
)
os.rename(result, chapter_path)
time_to_finish = time.time() - start_time
print('Chapter Finished in '+str(time_to_finish)+' seconds, using voice: '+style_voice+' ( '+chapter_path+')')
######################################################################
# Ebook Parsing / Handling Code
# - Modified from : https://github.com/p0n1/epub_to_audiobook
#####################################################################
def get_book(input_file):
return epub.read_epub(input_file)
def get_book_title(book) -> str:
if book.get_metadata('DC', 'title'):
return book.get_metadata("DC", "title")[0][0]
return "Untitled"
def get_book_author(book) -> str:
if book.get_metadata('DC', 'creator'):
return book.get_metadata("DC", "creator")[0][0]
return "Unknown"
def get_chapters(book) -> List[Tuple[str, str]]:
chapters = []
for item in book.get_items_of_type(ebooklib.ITEM_DOCUMENT):
content = item.get_content()
soup = BeautifulSoup(content, features="xml")
title = soup.title.string if soup.title else ""
if title == '':
title = soup.find('h1').text if soup.find('h1') else ''
#print(title)
paragraphs = [p.text+' , ' for p in soup.find_all('p')]
#print(paragraphs)
if not title:
title = 'temp'
title = sanitize_title(title)
chapters.append((title, paragraphs))
soup.decompose()
return chapters
def sanitize_title( title ) -> str:
sanitized_title = re.sub(r"[^\w\s]", "", title, flags=re.UNICODE)
sanitized_title = re.sub(r"\s+", "_", sanitized_title.strip())
return sanitized_title
########################################################################
# Code to combine wav files to M4b, using https://github.com/Tsubashi/m4b-util
########################################################################
def convert_wav_to_m4b(folder, book_title, author):
safe_author = sanitize_title(author)
safe_title = sanitize_title(book_title)+' - '+safe_author+'.m4b'
book = Audiobook(
author=author,
cover="",
output_name=safe_title,
title=book_title,
date="01-04-24",
keep_temp_files=False,
)
sorted_files = natsorted(Path(folder).glob("*.wav"))
book.add_chapters_from_filelist(sorted_files, True, False)
book.bind(os.path.join(folder,safe_title))
if not os.path.exists(os.path.join(folder,'temp_backup')):
os.makedirs(os.path.join(folder,'temp_backup'))
# Remove each .wav file found to a temp_backup folder
for wav_file in sorted_files:
try:
os.rename(wav_file,os.path.join(folder,'temp_backup',wav_file.name))
print(f'Moved file: {wav_file}')
except OSError as e:
print(f'Error: {wav_file} : {e.strerror}')
print(f"Book '{book_title}' by {author} has been created.")
#########################################################
# Controls the generation process, pass in epub filename
########################################################
def generate_audiobook(epub_filename, voice_type, keep_awake):
start_time = time.time()
book = get_book(epub_filename)
title = get_book_title(book)
safe_title = sanitize_title(title)
if not os.path.exists(safe_title):
os.makedirs(safe_title)
print('Epub Title: '+safe_title)
author = get_book_author(book)
print('Epub Author: '+author)
chapters = get_chapters(book)
chapter_num = 0
chapter_folders = []
print('Converting Chapters using StyleTTS 2, via HF Spaces')
if hf_repo_id != '':
hf_api = HfApi()
space_available = control_hf_space(hf_api)
client = Client(spaces_api_url,hf_token=hf_token)
for chapter in chapters:
chapter_title = chapter[0]
if chapter_title != 'temp' and chapter_title != '':
chapter_num += 1
#if chapter_num == 3:
# break
safe_chapter_title = sanitize_title(chapter_title)
chapter_path = safe_title+'/'+str(chapter_num)+' - '+safe_chapter_title+'.wav'
if not os.path.exists(chapter_path):
chapter_paragraphs = chapter[1]
convert_chapter(client,chapter_path,chapter_paragraphs, voice_type)
print('Chapters done generating, now converting to m4b.')
if hf_repo_id != '' and not keep_awake:
pause = hf_api.pause_space(repo_id=hf_repo_id, token=hf_token)
print('Pausing Space: '+hf_repo_id)
convert_wav_to_m4b(safe_title, title, author)
time_to_finish = time.time() -start_time
print('Book Generated in: '+str(time_to_finish)+' seconds')
def main():
parser = argparse.ArgumentParser(description='Process arguments.')
# Add the mandatory arguments
parser.add_argument('filename', help='Epub filename', type=str)
#Adds keepawake
parser.add_argument('--awake', help='Keeps Space unpaused after running, for multiple book runs.', action='store_true')
# Add the optional voice flag that accepts a value
parser.add_argument('--voice', help='Specify voice type. Leave blank to use LJSpeech (best long-form), or set from the following to use multi-voice: f-us-1, f-us-2, f-us-3, f-us-4, m-us-1, m-us-2, m-us-3, m-us-4', type=str, default='LJSpeech - Longform')
# Parse the arguments
args = parser.parse_args()
epub_filename = args.filename
voice_type = args.voice # This will be longform if --voice is empty
keep_awake = args.awake
generate_audiobook(epub_filename, voice_type, keep_awake)
if __name__ == "__main__":
main()