drewThomasson
commited on
Commit
•
235af27
1
Parent(s):
6f192dc
Upload 8 files
Browse files- legacy/custom_model_ebook2audiobookXTTS.py +484 -0
- legacy/custom_model_ebook2audiobookXTTS_gradio.py +609 -0
- legacy/custom_model_ebook2audiobookXTTS_with_link_gradio.py +700 -0
- legacy/ebook2audiobook.py +462 -0
- legacy/gradio_gui_with_email_and_que.py +614 -0
- legacy/install.bat +18 -0
- legacy/install.ps1 +255 -0
- legacy/install.sh +171 -0
legacy/custom_model_ebook2audiobookXTTS.py
ADDED
@@ -0,0 +1,484 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
print("starting...")
|
2 |
+
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
import subprocess
|
6 |
+
import re
|
7 |
+
from pydub import AudioSegment
|
8 |
+
import tempfile
|
9 |
+
from pydub import AudioSegment
|
10 |
+
import os
|
11 |
+
import nltk
|
12 |
+
from nltk.tokenize import sent_tokenize
|
13 |
+
import sys
|
14 |
+
import torch
|
15 |
+
from TTS.api import TTS
|
16 |
+
from TTS.tts.configs.xtts_config import XttsConfig
|
17 |
+
from TTS.tts.models.xtts import Xtts
|
18 |
+
from tqdm import tqdm
|
19 |
+
|
20 |
+
nltk.download('punkt') # Make sure to download the necessary models
|
21 |
+
def is_folder_empty(folder_path):
|
22 |
+
if os.path.exists(folder_path) and os.path.isdir(folder_path):
|
23 |
+
# List directory contents
|
24 |
+
if not os.listdir(folder_path):
|
25 |
+
return True # The folder is empty
|
26 |
+
else:
|
27 |
+
return False # The folder is not empty
|
28 |
+
else:
|
29 |
+
print(f"The path {folder_path} is not a valid folder.")
|
30 |
+
return None # The path is not a valid folder
|
31 |
+
|
32 |
+
def remove_folder_with_contents(folder_path):
|
33 |
+
try:
|
34 |
+
shutil.rmtree(folder_path)
|
35 |
+
print(f"Successfully removed {folder_path} and all of its contents.")
|
36 |
+
except Exception as e:
|
37 |
+
print(f"Error removing {folder_path}: {e}")
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
+
|
42 |
+
def wipe_folder(folder_path):
|
43 |
+
# Check if the folder exists
|
44 |
+
if not os.path.exists(folder_path):
|
45 |
+
print(f"The folder {folder_path} does not exist.")
|
46 |
+
return
|
47 |
+
|
48 |
+
# Iterate over all the items in the given folder
|
49 |
+
for item in os.listdir(folder_path):
|
50 |
+
item_path = os.path.join(folder_path, item)
|
51 |
+
# If it's a file, remove it and print a message
|
52 |
+
if os.path.isfile(item_path):
|
53 |
+
os.remove(item_path)
|
54 |
+
print(f"Removed file: {item_path}")
|
55 |
+
# If it's a directory, remove it recursively and print a message
|
56 |
+
elif os.path.isdir(item_path):
|
57 |
+
shutil.rmtree(item_path)
|
58 |
+
print(f"Removed directory and its contents: {item_path}")
|
59 |
+
|
60 |
+
print(f"All contents wiped from {folder_path}.")
|
61 |
+
|
62 |
+
|
63 |
+
# Example usage
|
64 |
+
# folder_to_wipe = 'path_to_your_folder'
|
65 |
+
# wipe_folder(folder_to_wipe)
|
66 |
+
|
67 |
+
|
68 |
+
def create_m4b_from_chapters(input_dir, ebook_file, output_dir):
|
69 |
+
# Function to sort chapters based on their numeric order
|
70 |
+
def sort_key(chapter_file):
|
71 |
+
numbers = re.findall(r'\d+', chapter_file)
|
72 |
+
return int(numbers[0]) if numbers else 0
|
73 |
+
|
74 |
+
# Extract metadata and cover image from the eBook file
|
75 |
+
def extract_metadata_and_cover(ebook_path):
|
76 |
+
try:
|
77 |
+
cover_path = ebook_path.rsplit('.', 1)[0] + '.jpg'
|
78 |
+
subprocess.run(['ebook-meta', ebook_path, '--get-cover', cover_path], check=True)
|
79 |
+
if os.path.exists(cover_path):
|
80 |
+
return cover_path
|
81 |
+
except Exception as e:
|
82 |
+
print(f"Error extracting eBook metadata or cover: {e}")
|
83 |
+
return None
|
84 |
+
# Combine WAV files into a single file
|
85 |
+
def combine_wav_files(chapter_files, output_path):
|
86 |
+
# Initialize an empty audio segment
|
87 |
+
combined_audio = AudioSegment.empty()
|
88 |
+
|
89 |
+
# Sequentially append each file to the combined_audio
|
90 |
+
for chapter_file in chapter_files:
|
91 |
+
audio_segment = AudioSegment.from_wav(chapter_file)
|
92 |
+
combined_audio += audio_segment
|
93 |
+
# Export the combined audio to the output file path
|
94 |
+
combined_audio.export(output_path, format='wav')
|
95 |
+
print(f"Combined audio saved to {output_path}")
|
96 |
+
|
97 |
+
# Function to generate metadata for M4B chapters
|
98 |
+
def generate_ffmpeg_metadata(chapter_files, metadata_file):
|
99 |
+
with open(metadata_file, 'w') as file:
|
100 |
+
file.write(';FFMETADATA1\n')
|
101 |
+
start_time = 0
|
102 |
+
for index, chapter_file in enumerate(chapter_files):
|
103 |
+
duration_ms = len(AudioSegment.from_wav(chapter_file))
|
104 |
+
file.write(f'[CHAPTER]\nTIMEBASE=1/1000\nSTART={start_time}\n')
|
105 |
+
file.write(f'END={start_time + duration_ms}\ntitle=Chapter {index + 1}\n')
|
106 |
+
start_time += duration_ms
|
107 |
+
|
108 |
+
# Generate the final M4B file using ffmpeg
|
109 |
+
def create_m4b(combined_wav, metadata_file, cover_image, output_m4b):
|
110 |
+
# Ensure the output directory exists
|
111 |
+
os.makedirs(os.path.dirname(output_m4b), exist_ok=True)
|
112 |
+
|
113 |
+
ffmpeg_cmd = ['ffmpeg', '-i', combined_wav, '-i', metadata_file]
|
114 |
+
if cover_image:
|
115 |
+
ffmpeg_cmd += ['-i', cover_image, '-map', '0:a', '-map', '2:v']
|
116 |
+
else:
|
117 |
+
ffmpeg_cmd += ['-map', '0:a']
|
118 |
+
|
119 |
+
ffmpeg_cmd += ['-map_metadata', '1', '-c:a', 'aac', '-b:a', '192k']
|
120 |
+
if cover_image:
|
121 |
+
ffmpeg_cmd += ['-c:v', 'png', '-disposition:v', 'attached_pic']
|
122 |
+
ffmpeg_cmd += [output_m4b]
|
123 |
+
|
124 |
+
subprocess.run(ffmpeg_cmd, check=True)
|
125 |
+
|
126 |
+
|
127 |
+
|
128 |
+
# Main logic
|
129 |
+
chapter_files = sorted([os.path.join(input_dir, f) for f in os.listdir(input_dir) if f.endswith('.wav')], key=sort_key)
|
130 |
+
temp_dir = tempfile.gettempdir()
|
131 |
+
temp_combined_wav = os.path.join(temp_dir, 'combined.wav')
|
132 |
+
metadata_file = os.path.join(temp_dir, 'metadata.txt')
|
133 |
+
cover_image = extract_metadata_and_cover(ebook_file)
|
134 |
+
output_m4b = os.path.join(output_dir, os.path.splitext(os.path.basename(ebook_file))[0] + '.m4b')
|
135 |
+
|
136 |
+
combine_wav_files(chapter_files, temp_combined_wav)
|
137 |
+
generate_ffmpeg_metadata(chapter_files, metadata_file)
|
138 |
+
create_m4b(temp_combined_wav, metadata_file, cover_image, output_m4b)
|
139 |
+
|
140 |
+
# Cleanup
|
141 |
+
if os.path.exists(temp_combined_wav):
|
142 |
+
os.remove(temp_combined_wav)
|
143 |
+
if os.path.exists(metadata_file):
|
144 |
+
os.remove(metadata_file)
|
145 |
+
if cover_image and os.path.exists(cover_image):
|
146 |
+
os.remove(cover_image)
|
147 |
+
|
148 |
+
# Example usage
|
149 |
+
# create_m4b_from_chapters('path_to_chapter_wavs', 'path_to_ebook_file', 'path_to_output_dir')
|
150 |
+
|
151 |
+
|
152 |
+
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
#this code right here isnt the book grabbing thing but its before to refrence in ordero to create the sepecial chapter labeled book thing with calibre idk some systems cant seem to get it so just in case but the next bit of code after this is the book grabbing code with booknlp
|
157 |
+
import os
|
158 |
+
import subprocess
|
159 |
+
import ebooklib
|
160 |
+
from ebooklib import epub
|
161 |
+
from bs4 import BeautifulSoup
|
162 |
+
import re
|
163 |
+
import csv
|
164 |
+
import nltk
|
165 |
+
|
166 |
+
# Only run the main script if Value is True
|
167 |
+
def create_chapter_labeled_book(ebook_file_path):
|
168 |
+
# Function to ensure the existence of a directory
|
169 |
+
def ensure_directory(directory_path):
|
170 |
+
if not os.path.exists(directory_path):
|
171 |
+
os.makedirs(directory_path)
|
172 |
+
print(f"Created directory: {directory_path}")
|
173 |
+
|
174 |
+
ensure_directory(os.path.join(".", 'Working_files', 'Book'))
|
175 |
+
|
176 |
+
def convert_to_epub(input_path, output_path):
|
177 |
+
# Convert the ebook to EPUB format using Calibre's ebook-convert
|
178 |
+
try:
|
179 |
+
subprocess.run(['ebook-convert', input_path, output_path], check=True)
|
180 |
+
except subprocess.CalledProcessError as e:
|
181 |
+
print(f"An error occurred while converting the eBook: {e}")
|
182 |
+
return False
|
183 |
+
return True
|
184 |
+
|
185 |
+
def save_chapters_as_text(epub_path):
|
186 |
+
# Create the directory if it doesn't exist
|
187 |
+
directory = os.path.join(".", "Working_files", "temp_ebook")
|
188 |
+
ensure_directory(directory)
|
189 |
+
|
190 |
+
# Open the EPUB file
|
191 |
+
book = epub.read_epub(epub_path)
|
192 |
+
|
193 |
+
previous_chapter_text = ''
|
194 |
+
previous_filename = ''
|
195 |
+
chapter_counter = 0
|
196 |
+
|
197 |
+
# Iterate through the items in the EPUB file
|
198 |
+
for item in book.get_items():
|
199 |
+
if item.get_type() == ebooklib.ITEM_DOCUMENT:
|
200 |
+
# Use BeautifulSoup to parse HTML content
|
201 |
+
soup = BeautifulSoup(item.get_content(), 'html.parser')
|
202 |
+
text = soup.get_text()
|
203 |
+
|
204 |
+
# Check if the text is not empty
|
205 |
+
if text.strip():
|
206 |
+
if len(text) < 2300 and previous_filename:
|
207 |
+
# Append text to the previous chapter if it's short
|
208 |
+
with open(previous_filename, 'a', encoding='utf-8') as file:
|
209 |
+
file.write('\n' + text)
|
210 |
+
else:
|
211 |
+
# Create a new chapter file and increment the counter
|
212 |
+
previous_filename = os.path.join(directory, f"chapter_{chapter_counter}.txt")
|
213 |
+
chapter_counter += 1
|
214 |
+
with open(previous_filename, 'w', encoding='utf-8') as file:
|
215 |
+
file.write(text)
|
216 |
+
print(f"Saved chapter: {previous_filename}")
|
217 |
+
|
218 |
+
# Example usage
|
219 |
+
input_ebook = ebook_file_path # Replace with your eBook file path
|
220 |
+
output_epub = os.path.join(".", "Working_files", "temp.epub")
|
221 |
+
|
222 |
+
|
223 |
+
if os.path.exists(output_epub):
|
224 |
+
os.remove(output_epub)
|
225 |
+
print(f"File {output_epub} has been removed.")
|
226 |
+
else:
|
227 |
+
print(f"The file {output_epub} does not exist.")
|
228 |
+
|
229 |
+
if convert_to_epub(input_ebook, output_epub):
|
230 |
+
save_chapters_as_text(output_epub)
|
231 |
+
|
232 |
+
# Download the necessary NLTK data (if not already present)
|
233 |
+
nltk.download('punkt')
|
234 |
+
|
235 |
+
def process_chapter_files(folder_path, output_csv):
|
236 |
+
with open(output_csv, 'w', newline='', encoding='utf-8') as csvfile:
|
237 |
+
writer = csv.writer(csvfile)
|
238 |
+
# Write the header row
|
239 |
+
writer.writerow(['Text', 'Start Location', 'End Location', 'Is Quote', 'Speaker', 'Chapter'])
|
240 |
+
|
241 |
+
# Process each chapter file
|
242 |
+
chapter_files = sorted(os.listdir(folder_path), key=lambda x: int(x.split('_')[1].split('.')[0]))
|
243 |
+
for filename in chapter_files:
|
244 |
+
if filename.startswith('chapter_') and filename.endswith('.txt'):
|
245 |
+
chapter_number = int(filename.split('_')[1].split('.')[0])
|
246 |
+
file_path = os.path.join(folder_path, filename)
|
247 |
+
|
248 |
+
try:
|
249 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
250 |
+
text = file.read()
|
251 |
+
# Insert "NEWCHAPTERABC" at the beginning of each chapter's text
|
252 |
+
if text:
|
253 |
+
text = "NEWCHAPTERABC" + text
|
254 |
+
sentences = nltk.tokenize.sent_tokenize(text)
|
255 |
+
for sentence in sentences:
|
256 |
+
start_location = text.find(sentence)
|
257 |
+
end_location = start_location + len(sentence)
|
258 |
+
writer.writerow([sentence, start_location, end_location, 'True', 'Narrator', chapter_number])
|
259 |
+
except Exception as e:
|
260 |
+
print(f"Error processing file {filename}: {e}")
|
261 |
+
|
262 |
+
# Example usage
|
263 |
+
folder_path = os.path.join(".", "Working_files", "temp_ebook")
|
264 |
+
output_csv = os.path.join(".", "Working_files", "Book", "Other_book.csv")
|
265 |
+
|
266 |
+
process_chapter_files(folder_path, output_csv)
|
267 |
+
|
268 |
+
def sort_key(filename):
|
269 |
+
"""Extract chapter number for sorting."""
|
270 |
+
match = re.search(r'chapter_(\d+)\.txt', filename)
|
271 |
+
return int(match.group(1)) if match else 0
|
272 |
+
|
273 |
+
def combine_chapters(input_folder, output_file):
|
274 |
+
# Create the output folder if it doesn't exist
|
275 |
+
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
276 |
+
|
277 |
+
# List all txt files and sort them by chapter number
|
278 |
+
files = [f for f in os.listdir(input_folder) if f.endswith('.txt')]
|
279 |
+
sorted_files = sorted(files, key=sort_key)
|
280 |
+
|
281 |
+
with open(output_file, 'w', encoding='utf-8') as outfile: # Specify UTF-8 encoding here
|
282 |
+
for i, filename in enumerate(sorted_files):
|
283 |
+
with open(os.path.join(input_folder, filename), 'r', encoding='utf-8') as infile: # And here
|
284 |
+
outfile.write(infile.read())
|
285 |
+
# Add the marker unless it's the last file
|
286 |
+
if i < len(sorted_files) - 1:
|
287 |
+
outfile.write("\nNEWCHAPTERABC\n")
|
288 |
+
|
289 |
+
# Paths
|
290 |
+
input_folder = os.path.join(".", 'Working_files', 'temp_ebook')
|
291 |
+
output_file = os.path.join(".", 'Working_files', 'Book', 'Chapter_Book.txt')
|
292 |
+
|
293 |
+
|
294 |
+
# Combine the chapters
|
295 |
+
combine_chapters(input_folder, output_file)
|
296 |
+
|
297 |
+
ensure_directory(os.path.join(".", "Working_files", "Book"))
|
298 |
+
|
299 |
+
|
300 |
+
#create_chapter_labeled_book()
|
301 |
+
|
302 |
+
|
303 |
+
|
304 |
+
|
305 |
+
import os
|
306 |
+
import subprocess
|
307 |
+
import sys
|
308 |
+
import torchaudio
|
309 |
+
|
310 |
+
# Check if Calibre's ebook-convert tool is installed
|
311 |
+
def calibre_installed():
|
312 |
+
try:
|
313 |
+
subprocess.run(['ebook-convert', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
314 |
+
return True
|
315 |
+
except FileNotFoundError:
|
316 |
+
print("Calibre is not installed. Please install Calibre for this functionality.")
|
317 |
+
return False
|
318 |
+
|
319 |
+
|
320 |
+
import os
|
321 |
+
import torch
|
322 |
+
from TTS.api import TTS
|
323 |
+
from nltk.tokenize import sent_tokenize
|
324 |
+
from pydub import AudioSegment
|
325 |
+
# Assuming split_long_sentence and wipe_folder are defined elsewhere in your code
|
326 |
+
|
327 |
+
default_target_voice_path = "default_voice.wav" # Ensure this is a valid path
|
328 |
+
default_language_code = "en"
|
329 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
330 |
+
|
331 |
+
def combine_wav_files(input_directory, output_directory, file_name):
|
332 |
+
# Ensure that the output directory exists, create it if necessary
|
333 |
+
os.makedirs(output_directory, exist_ok=True)
|
334 |
+
|
335 |
+
# Specify the output file path
|
336 |
+
output_file_path = os.path.join(output_directory, file_name)
|
337 |
+
|
338 |
+
# Initialize an empty audio segment
|
339 |
+
combined_audio = AudioSegment.empty()
|
340 |
+
|
341 |
+
# Get a list of all .wav files in the specified input directory and sort them
|
342 |
+
input_file_paths = sorted(
|
343 |
+
[os.path.join(input_directory, f) for f in os.listdir(input_directory) if f.endswith(".wav")],
|
344 |
+
key=lambda f: int(''.join(filter(str.isdigit, f)))
|
345 |
+
)
|
346 |
+
|
347 |
+
# Sequentially append each file to the combined_audio
|
348 |
+
for input_file_path in input_file_paths:
|
349 |
+
audio_segment = AudioSegment.from_wav(input_file_path)
|
350 |
+
combined_audio += audio_segment
|
351 |
+
|
352 |
+
# Export the combined audio to the output file path
|
353 |
+
combined_audio.export(output_file_path, format='wav')
|
354 |
+
|
355 |
+
print(f"Combined audio saved to {output_file_path}")
|
356 |
+
|
357 |
+
# Function to split long strings into parts
|
358 |
+
def split_long_sentence(sentence, max_length=249, max_pauses=10):
|
359 |
+
"""
|
360 |
+
Splits a sentence into parts based on length or number of pauses without recursion.
|
361 |
+
|
362 |
+
:param sentence: The sentence to split.
|
363 |
+
:param max_length: Maximum allowed length of a sentence.
|
364 |
+
:param max_pauses: Maximum allowed number of pauses in a sentence.
|
365 |
+
:return: A list of sentence parts that meet the criteria.
|
366 |
+
"""
|
367 |
+
parts = []
|
368 |
+
while len(sentence) > max_length or sentence.count(',') + sentence.count(';') + sentence.count('.') > max_pauses:
|
369 |
+
possible_splits = [i for i, char in enumerate(sentence) if char in ',;.' and i < max_length]
|
370 |
+
if possible_splits:
|
371 |
+
# Find the best place to split the sentence, preferring the last possible split to keep parts longer
|
372 |
+
split_at = possible_splits[-1] + 1
|
373 |
+
else:
|
374 |
+
# If no punctuation to split on within max_length, split at max_length
|
375 |
+
split_at = max_length
|
376 |
+
|
377 |
+
# Split the sentence and add the first part to the list
|
378 |
+
parts.append(sentence[:split_at].strip())
|
379 |
+
sentence = sentence[split_at:].strip()
|
380 |
+
|
381 |
+
# Add the remaining part of the sentence
|
382 |
+
parts.append(sentence)
|
383 |
+
return parts
|
384 |
+
|
385 |
+
"""
|
386 |
+
if 'tts' not in locals():
|
387 |
+
tts = TTS(selected_tts_model, progress_bar=True).to(device)
|
388 |
+
"""
|
389 |
+
from tqdm import tqdm
|
390 |
+
|
391 |
+
# Convert chapters to audio using XTTS
|
392 |
+
def convert_chapters_to_audio(chapters_dir, output_audio_dir, target_voice_path=None, language=None, custom_model=None):
|
393 |
+
if custom_model:
|
394 |
+
print("Loading custom model...")
|
395 |
+
config = XttsConfig()
|
396 |
+
config.load_json(custom_model['config'])
|
397 |
+
model = Xtts.init_from_config(config)
|
398 |
+
model.load_checkpoint(config, checkpoint_path=custom_model['model'], vocab_path=custom_model['vocab'], use_deepspeed=False)
|
399 |
+
model.to(device)
|
400 |
+
print("Computing speaker latents...")
|
401 |
+
gpt_cond_latent, speaker_embedding = model.get_conditioning_latents(audio_path=[target_voice_path])
|
402 |
+
else:
|
403 |
+
selected_tts_model = "tts_models/multilingual/multi-dataset/xtts_v2"
|
404 |
+
tts = TTS(selected_tts_model, progress_bar=False).to(device)
|
405 |
+
|
406 |
+
if not os.path.exists(output_audio_dir):
|
407 |
+
os.makedirs(output_audio_dir)
|
408 |
+
|
409 |
+
for chapter_file in sorted(os.listdir(chapters_dir)):
|
410 |
+
if chapter_file.endswith('.txt'):
|
411 |
+
match = re.search(r"chapter_(\d+).txt", chapter_file)
|
412 |
+
if match:
|
413 |
+
chapter_num = int(match.group(1))
|
414 |
+
else:
|
415 |
+
print(f"Skipping file {chapter_file} as it does not match the expected format.")
|
416 |
+
continue
|
417 |
+
|
418 |
+
chapter_path = os.path.join(chapters_dir, chapter_file)
|
419 |
+
output_file_name = f"audio_chapter_{chapter_num}.wav"
|
420 |
+
output_file_path = os.path.join(output_audio_dir, output_file_name)
|
421 |
+
temp_audio_directory = os.path.join(".", "Working_files", "temp")
|
422 |
+
os.makedirs(temp_audio_directory, exist_ok=True)
|
423 |
+
temp_count = 0
|
424 |
+
|
425 |
+
with open(chapter_path, 'r', encoding='utf-8') as file:
|
426 |
+
chapter_text = file.read()
|
427 |
+
sentences = sent_tokenize(chapter_text, language='italian' if language == 'it' else 'english')
|
428 |
+
for sentence in tqdm(sentences, desc=f"Chapter {chapter_num}"):
|
429 |
+
fragments = split_long_sentence(sentence, max_length=249 if language == "en" else 213, max_pauses=10)
|
430 |
+
for fragment in fragments:
|
431 |
+
if fragment != "":
|
432 |
+
print(f"Generating fragment: {fragment}...")
|
433 |
+
fragment_file_path = os.path.join(temp_audio_directory, f"{temp_count}.wav")
|
434 |
+
if custom_model:
|
435 |
+
out = model.inference(fragment, language, gpt_cond_latent, speaker_embedding, temperature=0.7)
|
436 |
+
torchaudio.save(fragment_file_path, torch.tensor(out["wav"]).unsqueeze(0), 24000)
|
437 |
+
else:
|
438 |
+
speaker_wav_path = target_voice_path if target_voice_path else default_target_voice_path
|
439 |
+
language_code = language if language else default_language_code
|
440 |
+
tts.tts_to_file(text=fragment, file_path=fragment_file_path, speaker_wav=speaker_wav_path, language=language_code)
|
441 |
+
temp_count += 1
|
442 |
+
|
443 |
+
combine_wav_files(temp_audio_directory, output_audio_dir, output_file_name)
|
444 |
+
wipe_folder(temp_audio_directory)
|
445 |
+
print(f"Converted chapter {chapter_num} to audio.")
|
446 |
+
|
447 |
+
|
448 |
+
# Main execution flow
|
449 |
+
if __name__ == "__main__":
|
450 |
+
if len(sys.argv) < 2:
|
451 |
+
print("Usage: python script.py <ebook_file_path> [target_voice_file_path] [language] [custom_model_path] [custom_config_path] [custom_vocab_path]")
|
452 |
+
sys.exit(1)
|
453 |
+
|
454 |
+
ebook_file_path = sys.argv[1]
|
455 |
+
target_voice = sys.argv[2] if len(sys.argv) > 2 else None
|
456 |
+
language = sys.argv[3] if len(sys.argv) > 3 else None
|
457 |
+
|
458 |
+
custom_model = None
|
459 |
+
if len(sys.argv) > 6:
|
460 |
+
custom_model = {
|
461 |
+
'model': sys.argv[4],
|
462 |
+
'config': sys.argv[5],
|
463 |
+
'vocab': sys.argv[6]
|
464 |
+
}
|
465 |
+
|
466 |
+
if not calibre_installed():
|
467 |
+
sys.exit(1)
|
468 |
+
|
469 |
+
working_files = os.path.join(".", "Working_files", "temp_ebook")
|
470 |
+
full_folder_working_files = os.path.join(".", "Working_files")
|
471 |
+
chapters_directory = os.path.join(".", "Working_files", "temp_ebook")
|
472 |
+
output_audio_directory = os.path.join(".", 'Chapter_wav_files')
|
473 |
+
|
474 |
+
print("Wiping and removing Working_files folder...")
|
475 |
+
remove_folder_with_contents(full_folder_working_files)
|
476 |
+
|
477 |
+
print("Wiping and removing chapter_wav_files folder...")
|
478 |
+
remove_folder_with_contents(output_audio_directory)
|
479 |
+
|
480 |
+
create_chapter_labeled_book(ebook_file_path)
|
481 |
+
audiobook_output_path = os.path.join(".", "Audiobooks")
|
482 |
+
print(f"{chapters_directory}||||{output_audio_directory}|||||{target_voice}")
|
483 |
+
convert_chapters_to_audio(chapters_directory, output_audio_directory, target_voice, language, custom_model)
|
484 |
+
create_m4b_from_chapters(output_audio_directory, ebook_file_path, audiobook_output_path)
|
legacy/custom_model_ebook2audiobookXTTS_gradio.py
ADDED
@@ -0,0 +1,609 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
print("starting...")
|
2 |
+
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
import subprocess
|
6 |
+
import re
|
7 |
+
from pydub import AudioSegment
|
8 |
+
import tempfile
|
9 |
+
from pydub import AudioSegment
|
10 |
+
import os
|
11 |
+
import nltk
|
12 |
+
from nltk.tokenize import sent_tokenize
|
13 |
+
import sys
|
14 |
+
import torch
|
15 |
+
from TTS.api import TTS
|
16 |
+
from TTS.tts.configs.xtts_config import XttsConfig
|
17 |
+
from TTS.tts.models.xtts import Xtts
|
18 |
+
from tqdm import tqdm
|
19 |
+
|
20 |
+
nltk.download('punkt') # Make sure to download the necessary models
|
21 |
+
|
22 |
+
import gradio as gr
|
23 |
+
from gradio import Progress
|
24 |
+
|
25 |
+
|
26 |
+
def is_folder_empty(folder_path):
|
27 |
+
if os.path.exists(folder_path) and os.path.isdir(folder_path):
|
28 |
+
# List directory contents
|
29 |
+
if not os.listdir(folder_path):
|
30 |
+
return True # The folder is empty
|
31 |
+
else:
|
32 |
+
return False # The folder is not empty
|
33 |
+
else:
|
34 |
+
print(f"The path {folder_path} is not a valid folder.")
|
35 |
+
return None # The path is not a valid folder
|
36 |
+
|
37 |
+
def remove_folder_with_contents(folder_path):
|
38 |
+
try:
|
39 |
+
shutil.rmtree(folder_path)
|
40 |
+
print(f"Successfully removed {folder_path} and all of its contents.")
|
41 |
+
except Exception as e:
|
42 |
+
print(f"Error removing {folder_path}: {e}")
|
43 |
+
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
def wipe_folder(folder_path):
|
48 |
+
# Check if the folder exists
|
49 |
+
if not os.path.exists(folder_path):
|
50 |
+
print(f"The folder {folder_path} does not exist.")
|
51 |
+
return
|
52 |
+
|
53 |
+
# Iterate over all the items in the given folder
|
54 |
+
for item in os.listdir(folder_path):
|
55 |
+
item_path = os.path.join(folder_path, item)
|
56 |
+
# If it's a file, remove it and print a message
|
57 |
+
if os.path.isfile(item_path):
|
58 |
+
os.remove(item_path)
|
59 |
+
print(f"Removed file: {item_path}")
|
60 |
+
# If it's a directory, remove it recursively and print a message
|
61 |
+
elif os.path.isdir(item_path):
|
62 |
+
shutil.rmtree(item_path)
|
63 |
+
print(f"Removed directory and its contents: {item_path}")
|
64 |
+
|
65 |
+
print(f"All contents wiped from {folder_path}.")
|
66 |
+
|
67 |
+
|
68 |
+
# Example usage
|
69 |
+
# folder_to_wipe = 'path_to_your_folder'
|
70 |
+
# wipe_folder(folder_to_wipe)
|
71 |
+
|
72 |
+
|
73 |
+
def create_m4b_from_chapters(input_dir, ebook_file, output_dir):
|
74 |
+
# Function to sort chapters based on their numeric order
|
75 |
+
def sort_key(chapter_file):
|
76 |
+
numbers = re.findall(r'\d+', chapter_file)
|
77 |
+
return int(numbers[0]) if numbers else 0
|
78 |
+
|
79 |
+
# Extract metadata and cover image from the eBook file
|
80 |
+
def extract_metadata_and_cover(ebook_path):
|
81 |
+
try:
|
82 |
+
cover_path = ebook_path.rsplit('.', 1)[0] + '.jpg'
|
83 |
+
subprocess.run(['ebook-meta', ebook_path, '--get-cover', cover_path], check=True)
|
84 |
+
if os.path.exists(cover_path):
|
85 |
+
return cover_path
|
86 |
+
except Exception as e:
|
87 |
+
print(f"Error extracting eBook metadata or cover: {e}")
|
88 |
+
return None
|
89 |
+
# Combine WAV files into a single file
|
90 |
+
def combine_wav_files(chapter_files, output_path):
|
91 |
+
# Initialize an empty audio segment
|
92 |
+
combined_audio = AudioSegment.empty()
|
93 |
+
|
94 |
+
# Sequentially append each file to the combined_audio
|
95 |
+
for chapter_file in chapter_files:
|
96 |
+
audio_segment = AudioSegment.from_wav(chapter_file)
|
97 |
+
combined_audio += audio_segment
|
98 |
+
# Export the combined audio to the output file path
|
99 |
+
combined_audio.export(output_path, format='wav')
|
100 |
+
print(f"Combined audio saved to {output_path}")
|
101 |
+
|
102 |
+
# Function to generate metadata for M4B chapters
|
103 |
+
def generate_ffmpeg_metadata(chapter_files, metadata_file):
|
104 |
+
with open(metadata_file, 'w') as file:
|
105 |
+
file.write(';FFMETADATA1\n')
|
106 |
+
start_time = 0
|
107 |
+
for index, chapter_file in enumerate(chapter_files):
|
108 |
+
duration_ms = len(AudioSegment.from_wav(chapter_file))
|
109 |
+
file.write(f'[CHAPTER]\nTIMEBASE=1/1000\nSTART={start_time}\n')
|
110 |
+
file.write(f'END={start_time + duration_ms}\ntitle=Chapter {index + 1}\n')
|
111 |
+
start_time += duration_ms
|
112 |
+
|
113 |
+
# Generate the final M4B file using ffmpeg
|
114 |
+
def create_m4b(combined_wav, metadata_file, cover_image, output_m4b):
|
115 |
+
# Ensure the output directory exists
|
116 |
+
os.makedirs(os.path.dirname(output_m4b), exist_ok=True)
|
117 |
+
|
118 |
+
ffmpeg_cmd = ['ffmpeg', '-i', combined_wav, '-i', metadata_file]
|
119 |
+
if cover_image:
|
120 |
+
ffmpeg_cmd += ['-i', cover_image, '-map', '0:a', '-map', '2:v']
|
121 |
+
else:
|
122 |
+
ffmpeg_cmd += ['-map', '0:a']
|
123 |
+
|
124 |
+
ffmpeg_cmd += ['-map_metadata', '1', '-c:a', 'aac', '-b:a', '192k']
|
125 |
+
if cover_image:
|
126 |
+
ffmpeg_cmd += ['-c:v', 'png', '-disposition:v', 'attached_pic']
|
127 |
+
ffmpeg_cmd += [output_m4b]
|
128 |
+
|
129 |
+
subprocess.run(ffmpeg_cmd, check=True)
|
130 |
+
|
131 |
+
|
132 |
+
|
133 |
+
# Main logic
|
134 |
+
chapter_files = sorted([os.path.join(input_dir, f) for f in os.listdir(input_dir) if f.endswith('.wav')], key=sort_key)
|
135 |
+
temp_dir = tempfile.gettempdir()
|
136 |
+
temp_combined_wav = os.path.join(temp_dir, 'combined.wav')
|
137 |
+
metadata_file = os.path.join(temp_dir, 'metadata.txt')
|
138 |
+
cover_image = extract_metadata_and_cover(ebook_file)
|
139 |
+
output_m4b = os.path.join(output_dir, os.path.splitext(os.path.basename(ebook_file))[0] + '.m4b')
|
140 |
+
|
141 |
+
combine_wav_files(chapter_files, temp_combined_wav)
|
142 |
+
generate_ffmpeg_metadata(chapter_files, metadata_file)
|
143 |
+
create_m4b(temp_combined_wav, metadata_file, cover_image, output_m4b)
|
144 |
+
|
145 |
+
# Cleanup
|
146 |
+
if os.path.exists(temp_combined_wav):
|
147 |
+
os.remove(temp_combined_wav)
|
148 |
+
if os.path.exists(metadata_file):
|
149 |
+
os.remove(metadata_file)
|
150 |
+
if cover_image and os.path.exists(cover_image):
|
151 |
+
os.remove(cover_image)
|
152 |
+
|
153 |
+
# Example usage
|
154 |
+
# create_m4b_from_chapters('path_to_chapter_wavs', 'path_to_ebook_file', 'path_to_output_dir')
|
155 |
+
|
156 |
+
|
157 |
+
|
158 |
+
|
159 |
+
|
160 |
+
|
161 |
+
#this code right here isnt the book grabbing thing but its before to refrence in ordero to create the sepecial chapter labeled book thing with calibre idk some systems cant seem to get it so just in case but the next bit of code after this is the book grabbing code with booknlp
|
162 |
+
import os
|
163 |
+
import subprocess
|
164 |
+
import ebooklib
|
165 |
+
from ebooklib import epub
|
166 |
+
from bs4 import BeautifulSoup
|
167 |
+
import re
|
168 |
+
import csv
|
169 |
+
import nltk
|
170 |
+
|
171 |
+
# Only run the main script if Value is True
|
172 |
+
def create_chapter_labeled_book(ebook_file_path):
|
173 |
+
# Function to ensure the existence of a directory
|
174 |
+
def ensure_directory(directory_path):
|
175 |
+
if not os.path.exists(directory_path):
|
176 |
+
os.makedirs(directory_path)
|
177 |
+
print(f"Created directory: {directory_path}")
|
178 |
+
|
179 |
+
ensure_directory(os.path.join(".", 'Working_files', 'Book'))
|
180 |
+
|
181 |
+
def convert_to_epub(input_path, output_path):
|
182 |
+
# Convert the ebook to EPUB format using Calibre's ebook-convert
|
183 |
+
try:
|
184 |
+
subprocess.run(['ebook-convert', input_path, output_path], check=True)
|
185 |
+
except subprocess.CalledProcessError as e:
|
186 |
+
print(f"An error occurred while converting the eBook: {e}")
|
187 |
+
return False
|
188 |
+
return True
|
189 |
+
|
190 |
+
def save_chapters_as_text(epub_path):
|
191 |
+
# Create the directory if it doesn't exist
|
192 |
+
directory = os.path.join(".", "Working_files", "temp_ebook")
|
193 |
+
ensure_directory(directory)
|
194 |
+
|
195 |
+
# Open the EPUB file
|
196 |
+
book = epub.read_epub(epub_path)
|
197 |
+
|
198 |
+
previous_chapter_text = ''
|
199 |
+
previous_filename = ''
|
200 |
+
chapter_counter = 0
|
201 |
+
|
202 |
+
# Iterate through the items in the EPUB file
|
203 |
+
for item in book.get_items():
|
204 |
+
if item.get_type() == ebooklib.ITEM_DOCUMENT:
|
205 |
+
# Use BeautifulSoup to parse HTML content
|
206 |
+
soup = BeautifulSoup(item.get_content(), 'html.parser')
|
207 |
+
text = soup.get_text()
|
208 |
+
|
209 |
+
# Check if the text is not empty
|
210 |
+
if text.strip():
|
211 |
+
if len(text) < 2300 and previous_filename:
|
212 |
+
# Append text to the previous chapter if it's short
|
213 |
+
with open(previous_filename, 'a', encoding='utf-8') as file:
|
214 |
+
file.write('\n' + text)
|
215 |
+
else:
|
216 |
+
# Create a new chapter file and increment the counter
|
217 |
+
previous_filename = os.path.join(directory, f"chapter_{chapter_counter}.txt")
|
218 |
+
chapter_counter += 1
|
219 |
+
with open(previous_filename, 'w', encoding='utf-8') as file:
|
220 |
+
file.write(text)
|
221 |
+
print(f"Saved chapter: {previous_filename}")
|
222 |
+
|
223 |
+
# Example usage
|
224 |
+
input_ebook = ebook_file_path # Replace with your eBook file path
|
225 |
+
output_epub = os.path.join(".", "Working_files", "temp.epub")
|
226 |
+
|
227 |
+
|
228 |
+
if os.path.exists(output_epub):
|
229 |
+
os.remove(output_epub)
|
230 |
+
print(f"File {output_epub} has been removed.")
|
231 |
+
else:
|
232 |
+
print(f"The file {output_epub} does not exist.")
|
233 |
+
|
234 |
+
if convert_to_epub(input_ebook, output_epub):
|
235 |
+
save_chapters_as_text(output_epub)
|
236 |
+
|
237 |
+
# Download the necessary NLTK data (if not already present)
|
238 |
+
nltk.download('punkt')
|
239 |
+
|
240 |
+
def process_chapter_files(folder_path, output_csv):
|
241 |
+
with open(output_csv, 'w', newline='', encoding='utf-8') as csvfile:
|
242 |
+
writer = csv.writer(csvfile)
|
243 |
+
# Write the header row
|
244 |
+
writer.writerow(['Text', 'Start Location', 'End Location', 'Is Quote', 'Speaker', 'Chapter'])
|
245 |
+
|
246 |
+
# Process each chapter file
|
247 |
+
chapter_files = sorted(os.listdir(folder_path), key=lambda x: int(x.split('_')[1].split('.')[0]))
|
248 |
+
for filename in chapter_files:
|
249 |
+
if filename.startswith('chapter_') and filename.endswith('.txt'):
|
250 |
+
chapter_number = int(filename.split('_')[1].split('.')[0])
|
251 |
+
file_path = os.path.join(folder_path, filename)
|
252 |
+
|
253 |
+
try:
|
254 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
255 |
+
text = file.read()
|
256 |
+
# Insert "NEWCHAPTERABC" at the beginning of each chapter's text
|
257 |
+
if text:
|
258 |
+
text = "NEWCHAPTERABC" + text
|
259 |
+
sentences = nltk.tokenize.sent_tokenize(text)
|
260 |
+
for sentence in sentences:
|
261 |
+
start_location = text.find(sentence)
|
262 |
+
end_location = start_location + len(sentence)
|
263 |
+
writer.writerow([sentence, start_location, end_location, 'True', 'Narrator', chapter_number])
|
264 |
+
except Exception as e:
|
265 |
+
print(f"Error processing file {filename}: {e}")
|
266 |
+
|
267 |
+
# Example usage
|
268 |
+
folder_path = os.path.join(".", "Working_files", "temp_ebook")
|
269 |
+
output_csv = os.path.join(".", "Working_files", "Book", "Other_book.csv")
|
270 |
+
|
271 |
+
process_chapter_files(folder_path, output_csv)
|
272 |
+
|
273 |
+
def sort_key(filename):
|
274 |
+
"""Extract chapter number for sorting."""
|
275 |
+
match = re.search(r'chapter_(\d+)\.txt', filename)
|
276 |
+
return int(match.group(1)) if match else 0
|
277 |
+
|
278 |
+
def combine_chapters(input_folder, output_file):
|
279 |
+
# Create the output folder if it doesn't exist
|
280 |
+
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
281 |
+
|
282 |
+
# List all txt files and sort them by chapter number
|
283 |
+
files = [f for f in os.listdir(input_folder) if f.endswith('.txt')]
|
284 |
+
sorted_files = sorted(files, key=sort_key)
|
285 |
+
|
286 |
+
with open(output_file, 'w', encoding='utf-8') as outfile: # Specify UTF-8 encoding here
|
287 |
+
for i, filename in enumerate(sorted_files):
|
288 |
+
with open(os.path.join(input_folder, filename), 'r', encoding='utf-8') as infile: # And here
|
289 |
+
outfile.write(infile.read())
|
290 |
+
# Add the marker unless it's the last file
|
291 |
+
if i < len(sorted_files) - 1:
|
292 |
+
outfile.write("\nNEWCHAPTERABC\n")
|
293 |
+
|
294 |
+
# Paths
|
295 |
+
input_folder = os.path.join(".", 'Working_files', 'temp_ebook')
|
296 |
+
output_file = os.path.join(".", 'Working_files', 'Book', 'Chapter_Book.txt')
|
297 |
+
|
298 |
+
|
299 |
+
# Combine the chapters
|
300 |
+
combine_chapters(input_folder, output_file)
|
301 |
+
|
302 |
+
ensure_directory(os.path.join(".", "Working_files", "Book"))
|
303 |
+
|
304 |
+
|
305 |
+
#create_chapter_labeled_book()
|
306 |
+
|
307 |
+
|
308 |
+
|
309 |
+
|
310 |
+
import os
|
311 |
+
import subprocess
|
312 |
+
import sys
|
313 |
+
import torchaudio
|
314 |
+
|
315 |
+
# Check if Calibre's ebook-convert tool is installed
|
316 |
+
def calibre_installed():
|
317 |
+
try:
|
318 |
+
subprocess.run(['ebook-convert', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
319 |
+
return True
|
320 |
+
except FileNotFoundError:
|
321 |
+
print("Calibre is not installed. Please install Calibre for this functionality.")
|
322 |
+
return False
|
323 |
+
|
324 |
+
|
325 |
+
import os
|
326 |
+
import torch
|
327 |
+
from TTS.api import TTS
|
328 |
+
from nltk.tokenize import sent_tokenize
|
329 |
+
from pydub import AudioSegment
|
330 |
+
# Assuming split_long_sentence and wipe_folder are defined elsewhere in your code
|
331 |
+
|
332 |
+
default_target_voice_path = "default_voice.wav" # Ensure this is a valid path
|
333 |
+
default_language_code = "en"
|
334 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
335 |
+
|
336 |
+
def combine_wav_files(input_directory, output_directory, file_name):
|
337 |
+
# Ensure that the output directory exists, create it if necessary
|
338 |
+
os.makedirs(output_directory, exist_ok=True)
|
339 |
+
|
340 |
+
# Specify the output file path
|
341 |
+
output_file_path = os.path.join(output_directory, file_name)
|
342 |
+
|
343 |
+
# Initialize an empty audio segment
|
344 |
+
combined_audio = AudioSegment.empty()
|
345 |
+
|
346 |
+
# Get a list of all .wav files in the specified input directory and sort them
|
347 |
+
input_file_paths = sorted(
|
348 |
+
[os.path.join(input_directory, f) for f in os.listdir(input_directory) if f.endswith(".wav")],
|
349 |
+
key=lambda f: int(''.join(filter(str.isdigit, f)))
|
350 |
+
)
|
351 |
+
|
352 |
+
# Sequentially append each file to the combined_audio
|
353 |
+
for input_file_path in input_file_paths:
|
354 |
+
audio_segment = AudioSegment.from_wav(input_file_path)
|
355 |
+
combined_audio += audio_segment
|
356 |
+
|
357 |
+
# Export the combined audio to the output file path
|
358 |
+
combined_audio.export(output_file_path, format='wav')
|
359 |
+
|
360 |
+
print(f"Combined audio saved to {output_file_path}")
|
361 |
+
|
362 |
+
# Function to split long strings into parts
|
363 |
+
def split_long_sentence(sentence, max_length=249, max_pauses=10):
|
364 |
+
"""
|
365 |
+
Splits a sentence into parts based on length or number of pauses without recursion.
|
366 |
+
|
367 |
+
:param sentence: The sentence to split.
|
368 |
+
:param max_length: Maximum allowed length of a sentence.
|
369 |
+
:param max_pauses: Maximum allowed number of pauses in a sentence.
|
370 |
+
:return: A list of sentence parts that meet the criteria.
|
371 |
+
"""
|
372 |
+
parts = []
|
373 |
+
while len(sentence) > max_length or sentence.count(',') + sentence.count(';') + sentence.count('.') > max_pauses:
|
374 |
+
possible_splits = [i for i, char in enumerate(sentence) if char in ',;.' and i < max_length]
|
375 |
+
if possible_splits:
|
376 |
+
# Find the best place to split the sentence, preferring the last possible split to keep parts longer
|
377 |
+
split_at = possible_splits[-1] + 1
|
378 |
+
else:
|
379 |
+
# If no punctuation to split on within max_length, split at max_length
|
380 |
+
split_at = max_length
|
381 |
+
|
382 |
+
# Split the sentence and add the first part to the list
|
383 |
+
parts.append(sentence[:split_at].strip())
|
384 |
+
sentence = sentence[split_at:].strip()
|
385 |
+
|
386 |
+
# Add the remaining part of the sentence
|
387 |
+
parts.append(sentence)
|
388 |
+
return parts
|
389 |
+
|
390 |
+
"""
|
391 |
+
if 'tts' not in locals():
|
392 |
+
tts = TTS(selected_tts_model, progress_bar=True).to(device)
|
393 |
+
"""
|
394 |
+
from tqdm import tqdm
|
395 |
+
|
396 |
+
# Convert chapters to audio using XTTS
|
397 |
+
|
398 |
+
def convert_chapters_to_audio_custom_model(chapters_dir, output_audio_dir, target_voice_path=None, language=None, custom_model=None):
|
399 |
+
if custom_model:
|
400 |
+
print("Loading custom model...")
|
401 |
+
config = XttsConfig()
|
402 |
+
config.load_json(custom_model['config'])
|
403 |
+
model = Xtts.init_from_config(config)
|
404 |
+
model.load_checkpoint(config, checkpoint_path=custom_model['model'], vocab_path=custom_model['vocab'], use_deepspeed=False)
|
405 |
+
model.to(device)
|
406 |
+
print("Computing speaker latents...")
|
407 |
+
gpt_cond_latent, speaker_embedding = model.get_conditioning_latents(audio_path=[target_voice_path])
|
408 |
+
else:
|
409 |
+
selected_tts_model = "tts_models/multilingual/multi-dataset/xtts_v2"
|
410 |
+
tts = TTS(selected_tts_model, progress_bar=False).to(device)
|
411 |
+
|
412 |
+
if not os.path.exists(output_audio_dir):
|
413 |
+
os.makedirs(output_audio_dir)
|
414 |
+
|
415 |
+
for chapter_file in sorted(os.listdir(chapters_dir)):
|
416 |
+
if chapter_file.endswith('.txt'):
|
417 |
+
match = re.search(r"chapter_(\d+).txt", chapter_file)
|
418 |
+
if match:
|
419 |
+
chapter_num = int(match.group(1))
|
420 |
+
else:
|
421 |
+
print(f"Skipping file {chapter_file} as it does not match the expected format.")
|
422 |
+
continue
|
423 |
+
|
424 |
+
chapter_path = os.path.join(chapters_dir, chapter_file)
|
425 |
+
output_file_name = f"audio_chapter_{chapter_num}.wav"
|
426 |
+
output_file_path = os.path.join(output_audio_dir, output_file_name)
|
427 |
+
temp_audio_directory = os.path.join(".", "Working_files", "temp")
|
428 |
+
os.makedirs(temp_audio_directory, exist_ok=True)
|
429 |
+
temp_count = 0
|
430 |
+
|
431 |
+
with open(chapter_path, 'r', encoding='utf-8') as file:
|
432 |
+
chapter_text = file.read()
|
433 |
+
sentences = sent_tokenize(chapter_text, language='italian' if language == 'it' else 'english')
|
434 |
+
for sentence in tqdm(sentences, desc=f"Chapter {chapter_num}"):
|
435 |
+
fragments = split_long_sentence(sentence, max_length=249 if language == "en" else 213, max_pauses=10)
|
436 |
+
for fragment in fragments:
|
437 |
+
if fragment != "":
|
438 |
+
print(f"Generating fragment: {fragment}...")
|
439 |
+
fragment_file_path = os.path.join(temp_audio_directory, f"{temp_count}.wav")
|
440 |
+
if custom_model:
|
441 |
+
out = model.inference(fragment, language, gpt_cond_latent, speaker_embedding, temperature=0.7)
|
442 |
+
torchaudio.save(fragment_file_path, torch.tensor(out["wav"]).unsqueeze(0), 24000)
|
443 |
+
else:
|
444 |
+
speaker_wav_path = target_voice_path if target_voice_path else default_target_voice_path
|
445 |
+
language_code = language if language else default_language_code
|
446 |
+
tts.tts_to_file(text=fragment, file_path=fragment_file_path, speaker_wav=speaker_wav_path, language=language_code)
|
447 |
+
temp_count += 1
|
448 |
+
|
449 |
+
combine_wav_files(temp_audio_directory, output_audio_dir, output_file_name)
|
450 |
+
wipe_folder(temp_audio_directory)
|
451 |
+
print(f"Converted chapter {chapter_num} to audio.")
|
452 |
+
|
453 |
+
|
454 |
+
|
455 |
+
def convert_chapters_to_audio_standard_model(chapters_dir, output_audio_dir, target_voice_path=None, language=None):
|
456 |
+
selected_tts_model = "tts_models/multilingual/multi-dataset/xtts_v2"
|
457 |
+
tts = TTS(selected_tts_model, progress_bar=False).to(device)
|
458 |
+
|
459 |
+
if not os.path.exists(output_audio_dir):
|
460 |
+
os.makedirs(output_audio_dir)
|
461 |
+
|
462 |
+
for chapter_file in sorted(os.listdir(chapters_dir)):
|
463 |
+
if chapter_file.endswith('.txt'):
|
464 |
+
match = re.search(r"chapter_(\d+).txt", chapter_file)
|
465 |
+
if match:
|
466 |
+
chapter_num = int(match.group(1))
|
467 |
+
else:
|
468 |
+
print(f"Skipping file {chapter_file} as it does not match the expected format.")
|
469 |
+
continue
|
470 |
+
|
471 |
+
chapter_path = os.path.join(chapters_dir, chapter_file)
|
472 |
+
output_file_name = f"audio_chapter_{chapter_num}.wav"
|
473 |
+
output_file_path = os.path.join(output_audio_dir, output_file_name)
|
474 |
+
temp_audio_directory = os.path.join(".", "Working_files", "temp")
|
475 |
+
os.makedirs(temp_audio_directory, exist_ok=True)
|
476 |
+
temp_count = 0
|
477 |
+
|
478 |
+
with open(chapter_path, 'r', encoding='utf-8') as file:
|
479 |
+
chapter_text = file.read()
|
480 |
+
sentences = sent_tokenize(chapter_text, language='italian' if language == 'it' else 'english')
|
481 |
+
for sentence in tqdm(sentences, desc=f"Chapter {chapter_num}"):
|
482 |
+
fragments = split_long_sentence(sentence, max_length=249 if language == "en" else 213, max_pauses=10)
|
483 |
+
for fragment in fragments:
|
484 |
+
if fragment != "":
|
485 |
+
print(f"Generating fragment: {fragment}...")
|
486 |
+
fragment_file_path = os.path.join(temp_audio_directory, f"{temp_count}.wav")
|
487 |
+
speaker_wav_path = target_voice_path if target_voice_path else default_target_voice_path
|
488 |
+
language_code = language if language else default_language_code
|
489 |
+
tts.tts_to_file(text=fragment, file_path=fragment_file_path, speaker_wav=speaker_wav_path, language=language_code)
|
490 |
+
temp_count += 1
|
491 |
+
|
492 |
+
combine_wav_files(temp_audio_directory, output_audio_dir, output_file_name)
|
493 |
+
wipe_folder(temp_audio_directory)
|
494 |
+
print(f"Converted chapter {chapter_num} to audio.")
|
495 |
+
|
496 |
+
|
497 |
+
|
498 |
+
# Define the functions to be used in the Gradio interface
|
499 |
+
def convert_ebook_to_audio(ebook_file, target_voice_file, language, use_custom_model, custom_model_file, custom_config_file, custom_vocab_file, progress=gr.Progress()):
|
500 |
+
ebook_file_path = ebook_file.name
|
501 |
+
target_voice = target_voice_file.name if target_voice_file else None
|
502 |
+
custom_model = None
|
503 |
+
if use_custom_model and custom_model_file and custom_config_file and custom_vocab_file:
|
504 |
+
custom_model = {
|
505 |
+
'model': custom_model_file.name,
|
506 |
+
'config': custom_config_file.name,
|
507 |
+
'vocab': custom_vocab_file.name
|
508 |
+
}
|
509 |
+
|
510 |
+
try:
|
511 |
+
progress(0, desc="Starting conversion")
|
512 |
+
except Exception as e:
|
513 |
+
print(f"Error updating progress: {e}")
|
514 |
+
|
515 |
+
if not calibre_installed():
|
516 |
+
return "Calibre is not installed."
|
517 |
+
|
518 |
+
working_files = os.path.join(".", "Working_files", "temp_ebook")
|
519 |
+
full_folder_working_files = os.path.join(".", "Working_files")
|
520 |
+
chapters_directory = os.path.join(".", "Working_files", "temp_ebook")
|
521 |
+
output_audio_directory = os.path.join(".", 'Chapter_wav_files')
|
522 |
+
remove_folder_with_contents(full_folder_working_files)
|
523 |
+
remove_folder_with_contents(output_audio_directory)
|
524 |
+
|
525 |
+
try:
|
526 |
+
progress(0.1, desc="Creating chapter-labeled book")
|
527 |
+
except Exception as e:
|
528 |
+
print(f"Error updating progress: {e}")
|
529 |
+
|
530 |
+
create_chapter_labeled_book(ebook_file_path)
|
531 |
+
audiobook_output_path = os.path.join(".", "Audiobooks")
|
532 |
+
|
533 |
+
try:
|
534 |
+
progress(0.3, desc="Converting chapters to audio")
|
535 |
+
except Exception as e:
|
536 |
+
print(f"Error updating progress: {e}")
|
537 |
+
|
538 |
+
if use_custom_model:
|
539 |
+
convert_chapters_to_audio_custom_model(chapters_directory, output_audio_directory, target_voice, language, custom_model)
|
540 |
+
else:
|
541 |
+
convert_chapters_to_audio_standard_model(chapters_directory, output_audio_directory, target_voice, language)
|
542 |
+
|
543 |
+
try:
|
544 |
+
progress(0.9, desc="Creating M4B from chapters")
|
545 |
+
except Exception as e:
|
546 |
+
print(f"Error updating progress: {e}")
|
547 |
+
|
548 |
+
create_m4b_from_chapters(output_audio_directory, ebook_file_path, audiobook_output_path)
|
549 |
+
|
550 |
+
# Get the name of the created M4B file
|
551 |
+
m4b_filename = os.path.splitext(os.path.basename(ebook_file_path))[0] + '.m4b'
|
552 |
+
m4b_filepath = os.path.join(audiobook_output_path, m4b_filename)
|
553 |
+
|
554 |
+
try:
|
555 |
+
progress(1.0, desc="Conversion complete")
|
556 |
+
except Exception as e:
|
557 |
+
print(f"Error updating progress: {e}")
|
558 |
+
|
559 |
+
return f"Audiobook created at {m4b_filepath}", m4b_filepath
|
560 |
+
|
561 |
+
language_options = [
|
562 |
+
"en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko"
|
563 |
+
]
|
564 |
+
|
565 |
+
theme = gr.themes.Soft(
|
566 |
+
primary_hue="blue",
|
567 |
+
secondary_hue="blue",
|
568 |
+
neutral_hue="blue",
|
569 |
+
text_size=gr.themes.sizes.text_md,
|
570 |
+
)
|
571 |
+
|
572 |
+
with gr.Blocks(theme=theme) as demo:
|
573 |
+
gr.Markdown(
|
574 |
+
"""
|
575 |
+
# eBook to Audiobook Converter
|
576 |
+
|
577 |
+
Transform your eBooks into immersive audiobooks with optional custom TTS models.
|
578 |
+
"""
|
579 |
+
)
|
580 |
+
|
581 |
+
with gr.Row():
|
582 |
+
with gr.Column(scale=3):
|
583 |
+
ebook_file = gr.File(label="eBook File")
|
584 |
+
target_voice_file = gr.File(label="Target Voice File (Optional)")
|
585 |
+
language = gr.Dropdown(label="Language", choices=language_options, value="en")
|
586 |
+
|
587 |
+
with gr.Column(scale=3):
|
588 |
+
use_custom_model = gr.Checkbox(label="Use Custom Model")
|
589 |
+
custom_model_file = gr.File(label="Custom Model File (Optional)", visible=False)
|
590 |
+
custom_config_file = gr.File(label="Custom Config File (Optional)", visible=False)
|
591 |
+
custom_vocab_file = gr.File(label="Custom Vocab File (Optional)", visible=False)
|
592 |
+
|
593 |
+
convert_btn = gr.Button("Convert to Audiobook", variant="primary")
|
594 |
+
output = gr.Textbox(label="Conversion Status")
|
595 |
+
audio_player = gr.Audio(label="Audiobook Player", type="filepath")
|
596 |
+
|
597 |
+
convert_btn.click(
|
598 |
+
convert_ebook_to_audio,
|
599 |
+
inputs=[ebook_file, target_voice_file, language, use_custom_model, custom_model_file, custom_config_file, custom_vocab_file],
|
600 |
+
outputs=[output, audio_player]
|
601 |
+
)
|
602 |
+
|
603 |
+
use_custom_model.change(
|
604 |
+
lambda x: [gr.update(visible=x)] * 3,
|
605 |
+
inputs=[use_custom_model],
|
606 |
+
outputs=[custom_model_file, custom_config_file, custom_vocab_file]
|
607 |
+
)
|
608 |
+
|
609 |
+
demo.launch(share=False)
|
legacy/custom_model_ebook2audiobookXTTS_with_link_gradio.py
ADDED
@@ -0,0 +1,700 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
print("starting...")
|
2 |
+
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
import subprocess
|
6 |
+
import re
|
7 |
+
from pydub import AudioSegment
|
8 |
+
import tempfile
|
9 |
+
from pydub import AudioSegment
|
10 |
+
import os
|
11 |
+
import nltk
|
12 |
+
from nltk.tokenize import sent_tokenize
|
13 |
+
import sys
|
14 |
+
import torch
|
15 |
+
from TTS.api import TTS
|
16 |
+
from TTS.tts.configs.xtts_config import XttsConfig
|
17 |
+
from TTS.tts.models.xtts import Xtts
|
18 |
+
from tqdm import tqdm
|
19 |
+
import gradio as gr
|
20 |
+
from gradio import Progress
|
21 |
+
import urllib.request
|
22 |
+
import zipfile
|
23 |
+
|
24 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
25 |
+
print(f"Device selected is: {device}")
|
26 |
+
|
27 |
+
nltk.download('punkt') # Make sure to download the necessary models
|
28 |
+
|
29 |
+
|
30 |
+
def download_and_extract_zip(url, extract_to='.'):
|
31 |
+
try:
|
32 |
+
# Ensure the directory exists
|
33 |
+
os.makedirs(extract_to, exist_ok=True)
|
34 |
+
|
35 |
+
zip_path = os.path.join(extract_to, 'model.zip')
|
36 |
+
|
37 |
+
# Download with progress bar
|
38 |
+
with tqdm(unit='B', unit_scale=True, miniters=1, desc="Downloading Model") as t:
|
39 |
+
def reporthook(blocknum, blocksize, totalsize):
|
40 |
+
t.total = totalsize
|
41 |
+
t.update(blocknum * blocksize - t.n)
|
42 |
+
|
43 |
+
urllib.request.urlretrieve(url, zip_path, reporthook=reporthook)
|
44 |
+
print(f"Downloaded zip file to {zip_path}")
|
45 |
+
|
46 |
+
# Unzipping with progress bar
|
47 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
48 |
+
files = zip_ref.namelist()
|
49 |
+
with tqdm(total=len(files), unit="file", desc="Extracting Files") as t:
|
50 |
+
for file in files:
|
51 |
+
if not file.endswith('/'): # Skip directories
|
52 |
+
# Extract the file to the temporary directory
|
53 |
+
extracted_path = zip_ref.extract(file, extract_to)
|
54 |
+
# Move the file to the base directory
|
55 |
+
base_file_path = os.path.join(extract_to, os.path.basename(file))
|
56 |
+
os.rename(extracted_path, base_file_path)
|
57 |
+
t.update(1)
|
58 |
+
|
59 |
+
# Cleanup: Remove the ZIP file and any empty folders
|
60 |
+
os.remove(zip_path)
|
61 |
+
for root, dirs, files in os.walk(extract_to, topdown=False):
|
62 |
+
for name in dirs:
|
63 |
+
os.rmdir(os.path.join(root, name))
|
64 |
+
print(f"Extracted files to {extract_to}")
|
65 |
+
|
66 |
+
# Check if all required files are present
|
67 |
+
required_files = ['model.pth', 'config.json', 'vocab.json_']
|
68 |
+
missing_files = [file for file in required_files if not os.path.exists(os.path.join(extract_to, file))]
|
69 |
+
|
70 |
+
if not missing_files:
|
71 |
+
print("All required files (model.pth, config.json, vocab.json_) found.")
|
72 |
+
else:
|
73 |
+
print(f"Missing files: {', '.join(missing_files)}")
|
74 |
+
|
75 |
+
except Exception as e:
|
76 |
+
print(f"Failed to download or extract zip file: {e}")
|
77 |
+
|
78 |
+
|
79 |
+
|
80 |
+
def is_folder_empty(folder_path):
|
81 |
+
if os.path.exists(folder_path) and os.path.isdir(folder_path):
|
82 |
+
# List directory contents
|
83 |
+
if not os.listdir(folder_path):
|
84 |
+
return True # The folder is empty
|
85 |
+
else:
|
86 |
+
return False # The folder is not empty
|
87 |
+
else:
|
88 |
+
print(f"The path {folder_path} is not a valid folder.")
|
89 |
+
return None # The path is not a valid folder
|
90 |
+
|
91 |
+
def remove_folder_with_contents(folder_path):
|
92 |
+
try:
|
93 |
+
shutil.rmtree(folder_path)
|
94 |
+
print(f"Successfully removed {folder_path} and all of its contents.")
|
95 |
+
except Exception as e:
|
96 |
+
print(f"Error removing {folder_path}: {e}")
|
97 |
+
|
98 |
+
|
99 |
+
|
100 |
+
|
101 |
+
def wipe_folder(folder_path):
|
102 |
+
# Check if the folder exists
|
103 |
+
if not os.path.exists(folder_path):
|
104 |
+
print(f"The folder {folder_path} does not exist.")
|
105 |
+
return
|
106 |
+
|
107 |
+
# Iterate over all the items in the given folder
|
108 |
+
for item in os.listdir(folder_path):
|
109 |
+
item_path = os.path.join(folder_path, item)
|
110 |
+
# If it's a file, remove it and print a message
|
111 |
+
if os.path.isfile(item_path):
|
112 |
+
os.remove(item_path)
|
113 |
+
print(f"Removed file: {item_path}")
|
114 |
+
# If it's a directory, remove it recursively and print a message
|
115 |
+
elif os.path.isdir(item_path):
|
116 |
+
shutil.rmtree(item_path)
|
117 |
+
print(f"Removed directory and its contents: {item_path}")
|
118 |
+
|
119 |
+
print(f"All contents wiped from {folder_path}.")
|
120 |
+
|
121 |
+
|
122 |
+
# Example usage
|
123 |
+
# folder_to_wipe = 'path_to_your_folder'
|
124 |
+
# wipe_folder(folder_to_wipe)
|
125 |
+
|
126 |
+
|
127 |
+
def create_m4b_from_chapters(input_dir, ebook_file, output_dir):
|
128 |
+
# Function to sort chapters based on their numeric order
|
129 |
+
def sort_key(chapter_file):
|
130 |
+
numbers = re.findall(r'\d+', chapter_file)
|
131 |
+
return int(numbers[0]) if numbers else 0
|
132 |
+
|
133 |
+
# Extract metadata and cover image from the eBook file
|
134 |
+
def extract_metadata_and_cover(ebook_path):
|
135 |
+
try:
|
136 |
+
cover_path = ebook_path.rsplit('.', 1)[0] + '.jpg'
|
137 |
+
subprocess.run(['ebook-meta', ebook_path, '--get-cover', cover_path], check=True)
|
138 |
+
if os.path.exists(cover_path):
|
139 |
+
return cover_path
|
140 |
+
except Exception as e:
|
141 |
+
print(f"Error extracting eBook metadata or cover: {e}")
|
142 |
+
return None
|
143 |
+
# Combine WAV files into a single file
|
144 |
+
def combine_wav_files(chapter_files, output_path):
|
145 |
+
# Initialize an empty audio segment
|
146 |
+
combined_audio = AudioSegment.empty()
|
147 |
+
|
148 |
+
# Sequentially append each file to the combined_audio
|
149 |
+
for chapter_file in chapter_files:
|
150 |
+
audio_segment = AudioSegment.from_wav(chapter_file)
|
151 |
+
combined_audio += audio_segment
|
152 |
+
# Export the combined audio to the output file path
|
153 |
+
combined_audio.export(output_path, format='wav')
|
154 |
+
print(f"Combined audio saved to {output_path}")
|
155 |
+
|
156 |
+
# Function to generate metadata for M4B chapters
|
157 |
+
def generate_ffmpeg_metadata(chapter_files, metadata_file):
|
158 |
+
with open(metadata_file, 'w') as file:
|
159 |
+
file.write(';FFMETADATA1\n')
|
160 |
+
start_time = 0
|
161 |
+
for index, chapter_file in enumerate(chapter_files):
|
162 |
+
duration_ms = len(AudioSegment.from_wav(chapter_file))
|
163 |
+
file.write(f'[CHAPTER]\nTIMEBASE=1/1000\nSTART={start_time}\n')
|
164 |
+
file.write(f'END={start_time + duration_ms}\ntitle=Chapter {index + 1}\n')
|
165 |
+
start_time += duration_ms
|
166 |
+
|
167 |
+
# Generate the final M4B file using ffmpeg
|
168 |
+
def create_m4b(combined_wav, metadata_file, cover_image, output_m4b):
|
169 |
+
# Ensure the output directory exists
|
170 |
+
os.makedirs(os.path.dirname(output_m4b), exist_ok=True)
|
171 |
+
|
172 |
+
ffmpeg_cmd = ['ffmpeg', '-i', combined_wav, '-i', metadata_file]
|
173 |
+
if cover_image:
|
174 |
+
ffmpeg_cmd += ['-i', cover_image, '-map', '0:a', '-map', '2:v']
|
175 |
+
else:
|
176 |
+
ffmpeg_cmd += ['-map', '0:a']
|
177 |
+
|
178 |
+
ffmpeg_cmd += ['-map_metadata', '1', '-c:a', 'aac', '-b:a', '192k']
|
179 |
+
if cover_image:
|
180 |
+
ffmpeg_cmd += ['-c:v', 'png', '-disposition:v', 'attached_pic']
|
181 |
+
ffmpeg_cmd += [output_m4b]
|
182 |
+
|
183 |
+
subprocess.run(ffmpeg_cmd, check=True)
|
184 |
+
|
185 |
+
|
186 |
+
|
187 |
+
# Main logic
|
188 |
+
chapter_files = sorted([os.path.join(input_dir, f) for f in os.listdir(input_dir) if f.endswith('.wav')], key=sort_key)
|
189 |
+
temp_dir = tempfile.gettempdir()
|
190 |
+
temp_combined_wav = os.path.join(temp_dir, 'combined.wav')
|
191 |
+
metadata_file = os.path.join(temp_dir, 'metadata.txt')
|
192 |
+
cover_image = extract_metadata_and_cover(ebook_file)
|
193 |
+
output_m4b = os.path.join(output_dir, os.path.splitext(os.path.basename(ebook_file))[0] + '.m4b')
|
194 |
+
|
195 |
+
combine_wav_files(chapter_files, temp_combined_wav)
|
196 |
+
generate_ffmpeg_metadata(chapter_files, metadata_file)
|
197 |
+
create_m4b(temp_combined_wav, metadata_file, cover_image, output_m4b)
|
198 |
+
|
199 |
+
# Cleanup
|
200 |
+
if os.path.exists(temp_combined_wav):
|
201 |
+
os.remove(temp_combined_wav)
|
202 |
+
if os.path.exists(metadata_file):
|
203 |
+
os.remove(metadata_file)
|
204 |
+
if cover_image and os.path.exists(cover_image):
|
205 |
+
os.remove(cover_image)
|
206 |
+
|
207 |
+
# Example usage
|
208 |
+
# create_m4b_from_chapters('path_to_chapter_wavs', 'path_to_ebook_file', 'path_to_output_dir')
|
209 |
+
|
210 |
+
|
211 |
+
|
212 |
+
|
213 |
+
|
214 |
+
|
215 |
+
#this code right here isnt the book grabbing thing but its before to refrence in ordero to create the sepecial chapter labeled book thing with calibre idk some systems cant seem to get it so just in case but the next bit of code after this is the book grabbing code with booknlp
|
216 |
+
import os
|
217 |
+
import subprocess
|
218 |
+
import ebooklib
|
219 |
+
from ebooklib import epub
|
220 |
+
from bs4 import BeautifulSoup
|
221 |
+
import re
|
222 |
+
import csv
|
223 |
+
import nltk
|
224 |
+
|
225 |
+
# Only run the main script if Value is True
|
226 |
+
def create_chapter_labeled_book(ebook_file_path):
|
227 |
+
# Function to ensure the existence of a directory
|
228 |
+
def ensure_directory(directory_path):
|
229 |
+
if not os.path.exists(directory_path):
|
230 |
+
os.makedirs(directory_path)
|
231 |
+
print(f"Created directory: {directory_path}")
|
232 |
+
|
233 |
+
ensure_directory(os.path.join(".", 'Working_files', 'Book'))
|
234 |
+
|
235 |
+
def convert_to_epub(input_path, output_path):
|
236 |
+
# Convert the ebook to EPUB format using Calibre's ebook-convert
|
237 |
+
try:
|
238 |
+
subprocess.run(['ebook-convert', input_path, output_path], check=True)
|
239 |
+
except subprocess.CalledProcessError as e:
|
240 |
+
print(f"An error occurred while converting the eBook: {e}")
|
241 |
+
return False
|
242 |
+
return True
|
243 |
+
|
244 |
+
def save_chapters_as_text(epub_path):
|
245 |
+
# Create the directory if it doesn't exist
|
246 |
+
directory = os.path.join(".", "Working_files", "temp_ebook")
|
247 |
+
ensure_directory(directory)
|
248 |
+
|
249 |
+
# Open the EPUB file
|
250 |
+
book = epub.read_epub(epub_path)
|
251 |
+
|
252 |
+
previous_chapter_text = ''
|
253 |
+
previous_filename = ''
|
254 |
+
chapter_counter = 0
|
255 |
+
|
256 |
+
# Iterate through the items in the EPUB file
|
257 |
+
for item in book.get_items():
|
258 |
+
if item.get_type() == ebooklib.ITEM_DOCUMENT:
|
259 |
+
# Use BeautifulSoup to parse HTML content
|
260 |
+
soup = BeautifulSoup(item.get_content(), 'html.parser')
|
261 |
+
text = soup.get_text()
|
262 |
+
|
263 |
+
# Check if the text is not empty
|
264 |
+
if text.strip():
|
265 |
+
if len(text) < 2300 and previous_filename:
|
266 |
+
# Append text to the previous chapter if it's short
|
267 |
+
with open(previous_filename, 'a', encoding='utf-8') as file:
|
268 |
+
file.write('\n' + text)
|
269 |
+
else:
|
270 |
+
# Create a new chapter file and increment the counter
|
271 |
+
previous_filename = os.path.join(directory, f"chapter_{chapter_counter}.txt")
|
272 |
+
chapter_counter += 1
|
273 |
+
with open(previous_filename, 'w', encoding='utf-8') as file:
|
274 |
+
file.write(text)
|
275 |
+
print(f"Saved chapter: {previous_filename}")
|
276 |
+
|
277 |
+
# Example usage
|
278 |
+
input_ebook = ebook_file_path # Replace with your eBook file path
|
279 |
+
output_epub = os.path.join(".", "Working_files", "temp.epub")
|
280 |
+
|
281 |
+
|
282 |
+
if os.path.exists(output_epub):
|
283 |
+
os.remove(output_epub)
|
284 |
+
print(f"File {output_epub} has been removed.")
|
285 |
+
else:
|
286 |
+
print(f"The file {output_epub} does not exist.")
|
287 |
+
|
288 |
+
if convert_to_epub(input_ebook, output_epub):
|
289 |
+
save_chapters_as_text(output_epub)
|
290 |
+
|
291 |
+
# Download the necessary NLTK data (if not already present)
|
292 |
+
nltk.download('punkt')
|
293 |
+
|
294 |
+
def process_chapter_files(folder_path, output_csv):
|
295 |
+
with open(output_csv, 'w', newline='', encoding='utf-8') as csvfile:
|
296 |
+
writer = csv.writer(csvfile)
|
297 |
+
# Write the header row
|
298 |
+
writer.writerow(['Text', 'Start Location', 'End Location', 'Is Quote', 'Speaker', 'Chapter'])
|
299 |
+
|
300 |
+
# Process each chapter file
|
301 |
+
chapter_files = sorted(os.listdir(folder_path), key=lambda x: int(x.split('_')[1].split('.')[0]))
|
302 |
+
for filename in chapter_files:
|
303 |
+
if filename.startswith('chapter_') and filename.endswith('.txt'):
|
304 |
+
chapter_number = int(filename.split('_')[1].split('.')[0])
|
305 |
+
file_path = os.path.join(folder_path, filename)
|
306 |
+
|
307 |
+
try:
|
308 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
309 |
+
text = file.read()
|
310 |
+
# Insert "NEWCHAPTERABC" at the beginning of each chapter's text
|
311 |
+
if text:
|
312 |
+
text = "NEWCHAPTERABC" + text
|
313 |
+
sentences = nltk.tokenize.sent_tokenize(text)
|
314 |
+
for sentence in sentences:
|
315 |
+
start_location = text.find(sentence)
|
316 |
+
end_location = start_location + len(sentence)
|
317 |
+
writer.writerow([sentence, start_location, end_location, 'True', 'Narrator', chapter_number])
|
318 |
+
except Exception as e:
|
319 |
+
print(f"Error processing file {filename}: {e}")
|
320 |
+
|
321 |
+
# Example usage
|
322 |
+
folder_path = os.path.join(".", "Working_files", "temp_ebook")
|
323 |
+
output_csv = os.path.join(".", "Working_files", "Book", "Other_book.csv")
|
324 |
+
|
325 |
+
process_chapter_files(folder_path, output_csv)
|
326 |
+
|
327 |
+
def sort_key(filename):
|
328 |
+
"""Extract chapter number for sorting."""
|
329 |
+
match = re.search(r'chapter_(\d+)\.txt', filename)
|
330 |
+
return int(match.group(1)) if match else 0
|
331 |
+
|
332 |
+
def combine_chapters(input_folder, output_file):
|
333 |
+
# Create the output folder if it doesn't exist
|
334 |
+
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
335 |
+
|
336 |
+
# List all txt files and sort them by chapter number
|
337 |
+
files = [f for f in os.listdir(input_folder) if f.endswith('.txt')]
|
338 |
+
sorted_files = sorted(files, key=sort_key)
|
339 |
+
|
340 |
+
with open(output_file, 'w', encoding='utf-8') as outfile: # Specify UTF-8 encoding here
|
341 |
+
for i, filename in enumerate(sorted_files):
|
342 |
+
with open(os.path.join(input_folder, filename), 'r', encoding='utf-8') as infile: # And here
|
343 |
+
outfile.write(infile.read())
|
344 |
+
# Add the marker unless it's the last file
|
345 |
+
if i < len(sorted_files) - 1:
|
346 |
+
outfile.write("\nNEWCHAPTERABC\n")
|
347 |
+
|
348 |
+
# Paths
|
349 |
+
input_folder = os.path.join(".", 'Working_files', 'temp_ebook')
|
350 |
+
output_file = os.path.join(".", 'Working_files', 'Book', 'Chapter_Book.txt')
|
351 |
+
|
352 |
+
|
353 |
+
# Combine the chapters
|
354 |
+
combine_chapters(input_folder, output_file)
|
355 |
+
|
356 |
+
ensure_directory(os.path.join(".", "Working_files", "Book"))
|
357 |
+
|
358 |
+
|
359 |
+
#create_chapter_labeled_book()
|
360 |
+
|
361 |
+
|
362 |
+
|
363 |
+
|
364 |
+
import os
|
365 |
+
import subprocess
|
366 |
+
import sys
|
367 |
+
import torchaudio
|
368 |
+
|
369 |
+
# Check if Calibre's ebook-convert tool is installed
|
370 |
+
def calibre_installed():
|
371 |
+
try:
|
372 |
+
subprocess.run(['ebook-convert', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
373 |
+
return True
|
374 |
+
except FileNotFoundError:
|
375 |
+
print("Calibre is not installed. Please install Calibre for this functionality.")
|
376 |
+
return False
|
377 |
+
|
378 |
+
|
379 |
+
import os
|
380 |
+
import torch
|
381 |
+
from TTS.api import TTS
|
382 |
+
from nltk.tokenize import sent_tokenize
|
383 |
+
from pydub import AudioSegment
|
384 |
+
# Assuming split_long_sentence and wipe_folder are defined elsewhere in your code
|
385 |
+
|
386 |
+
default_target_voice_path = "default_voice.wav" # Ensure this is a valid path
|
387 |
+
default_language_code = "en"
|
388 |
+
def combine_wav_files(input_directory, output_directory, file_name):
|
389 |
+
# Ensure that the output directory exists, create it if necessary
|
390 |
+
os.makedirs(output_directory, exist_ok=True)
|
391 |
+
|
392 |
+
# Specify the output file path
|
393 |
+
output_file_path = os.path.join(output_directory, file_name)
|
394 |
+
|
395 |
+
# Initialize an empty audio segment
|
396 |
+
combined_audio = AudioSegment.empty()
|
397 |
+
|
398 |
+
# Get a list of all .wav files in the specified input directory and sort them
|
399 |
+
input_file_paths = sorted(
|
400 |
+
[os.path.join(input_directory, f) for f in os.listdir(input_directory) if f.endswith(".wav")],
|
401 |
+
key=lambda f: int(''.join(filter(str.isdigit, f)))
|
402 |
+
)
|
403 |
+
|
404 |
+
# Sequentially append each file to the combined_audio
|
405 |
+
for input_file_path in input_file_paths:
|
406 |
+
audio_segment = AudioSegment.from_wav(input_file_path)
|
407 |
+
combined_audio += audio_segment
|
408 |
+
|
409 |
+
# Export the combined audio to the output file path
|
410 |
+
combined_audio.export(output_file_path, format='wav')
|
411 |
+
|
412 |
+
print(f"Combined audio saved to {output_file_path}")
|
413 |
+
|
414 |
+
# Function to split long strings into parts
|
415 |
+
def split_long_sentence(sentence, max_length=249, max_pauses=10):
|
416 |
+
"""
|
417 |
+
Splits a sentence into parts based on length or number of pauses without recursion.
|
418 |
+
|
419 |
+
:param sentence: The sentence to split.
|
420 |
+
:param max_length: Maximum allowed length of a sentence.
|
421 |
+
:param max_pauses: Maximum allowed number of pauses in a sentence.
|
422 |
+
:return: A list of sentence parts that meet the criteria.
|
423 |
+
"""
|
424 |
+
parts = []
|
425 |
+
while len(sentence) > max_length or sentence.count(',') + sentence.count(';') + sentence.count('.') > max_pauses:
|
426 |
+
possible_splits = [i for i, char in enumerate(sentence) if char in ',;.' and i < max_length]
|
427 |
+
if possible_splits:
|
428 |
+
# Find the best place to split the sentence, preferring the last possible split to keep parts longer
|
429 |
+
split_at = possible_splits[-1] + 1
|
430 |
+
else:
|
431 |
+
# If no punctuation to split on within max_length, split at max_length
|
432 |
+
split_at = max_length
|
433 |
+
|
434 |
+
# Split the sentence and add the first part to the list
|
435 |
+
parts.append(sentence[:split_at].strip())
|
436 |
+
sentence = sentence[split_at:].strip()
|
437 |
+
|
438 |
+
# Add the remaining part of the sentence
|
439 |
+
parts.append(sentence)
|
440 |
+
return parts
|
441 |
+
|
442 |
+
"""
|
443 |
+
if 'tts' not in locals():
|
444 |
+
tts = TTS(selected_tts_model, progress_bar=True).to(device)
|
445 |
+
"""
|
446 |
+
from tqdm import tqdm
|
447 |
+
|
448 |
+
# Convert chapters to audio using XTTS
|
449 |
+
|
450 |
+
def convert_chapters_to_audio_custom_model(chapters_dir, output_audio_dir, target_voice_path=None, language=None, custom_model=None):
|
451 |
+
|
452 |
+
if target_voice_path==None:
|
453 |
+
target_voice_path = default_target_voice_path
|
454 |
+
|
455 |
+
if custom_model:
|
456 |
+
print("Loading custom model...")
|
457 |
+
config = XttsConfig()
|
458 |
+
config.load_json(custom_model['config'])
|
459 |
+
model = Xtts.init_from_config(config)
|
460 |
+
model.load_checkpoint(config, checkpoint_path=custom_model['model'], vocab_path=custom_model['vocab'], use_deepspeed=False)
|
461 |
+
model.to(device)
|
462 |
+
print("Computing speaker latents...")
|
463 |
+
gpt_cond_latent, speaker_embedding = model.get_conditioning_latents(audio_path=[target_voice_path])
|
464 |
+
else:
|
465 |
+
selected_tts_model = "tts_models/multilingual/multi-dataset/xtts_v2"
|
466 |
+
tts = TTS(selected_tts_model, progress_bar=False).to(device)
|
467 |
+
|
468 |
+
if not os.path.exists(output_audio_dir):
|
469 |
+
os.makedirs(output_audio_dir)
|
470 |
+
|
471 |
+
for chapter_file in sorted(os.listdir(chapters_dir)):
|
472 |
+
if chapter_file.endswith('.txt'):
|
473 |
+
match = re.search(r"chapter_(\d+).txt", chapter_file)
|
474 |
+
if match:
|
475 |
+
chapter_num = int(match.group(1))
|
476 |
+
else:
|
477 |
+
print(f"Skipping file {chapter_file} as it does not match the expected format.")
|
478 |
+
continue
|
479 |
+
|
480 |
+
chapter_path = os.path.join(chapters_dir, chapter_file)
|
481 |
+
output_file_name = f"audio_chapter_{chapter_num}.wav"
|
482 |
+
output_file_path = os.path.join(output_audio_dir, output_file_name)
|
483 |
+
temp_audio_directory = os.path.join(".", "Working_files", "temp")
|
484 |
+
os.makedirs(temp_audio_directory, exist_ok=True)
|
485 |
+
temp_count = 0
|
486 |
+
|
487 |
+
with open(chapter_path, 'r', encoding='utf-8') as file:
|
488 |
+
chapter_text = file.read()
|
489 |
+
sentences = sent_tokenize(chapter_text, language='italian' if language == 'it' else 'english')
|
490 |
+
for sentence in tqdm(sentences, desc=f"Chapter {chapter_num}"):
|
491 |
+
fragments = split_long_sentence(sentence, max_length=249 if language == "en" else 213, max_pauses=10)
|
492 |
+
for fragment in fragments:
|
493 |
+
if fragment != "":
|
494 |
+
print(f"Generating fragment: {fragment}...")
|
495 |
+
fragment_file_path = os.path.join(temp_audio_directory, f"{temp_count}.wav")
|
496 |
+
if custom_model:
|
497 |
+
out = model.inference(fragment, language, gpt_cond_latent, speaker_embedding, temperature=0.7)
|
498 |
+
torchaudio.save(fragment_file_path, torch.tensor(out["wav"]).unsqueeze(0), 24000)
|
499 |
+
else:
|
500 |
+
speaker_wav_path = target_voice_path if target_voice_path else default_target_voice_path
|
501 |
+
language_code = language if language else default_language_code
|
502 |
+
tts.tts_to_file(text=fragment, file_path=fragment_file_path, speaker_wav=speaker_wav_path, language=language_code)
|
503 |
+
temp_count += 1
|
504 |
+
|
505 |
+
combine_wav_files(temp_audio_directory, output_audio_dir, output_file_name)
|
506 |
+
wipe_folder(temp_audio_directory)
|
507 |
+
print(f"Converted chapter {chapter_num} to audio.")
|
508 |
+
|
509 |
+
|
510 |
+
|
511 |
+
def convert_chapters_to_audio_standard_model(chapters_dir, output_audio_dir, target_voice_path=None, language=None):
|
512 |
+
selected_tts_model = "tts_models/multilingual/multi-dataset/xtts_v2"
|
513 |
+
tts = TTS(selected_tts_model, progress_bar=False).to(device)
|
514 |
+
|
515 |
+
if not os.path.exists(output_audio_dir):
|
516 |
+
os.makedirs(output_audio_dir)
|
517 |
+
|
518 |
+
for chapter_file in sorted(os.listdir(chapters_dir)):
|
519 |
+
if chapter_file.endswith('.txt'):
|
520 |
+
match = re.search(r"chapter_(\d+).txt", chapter_file)
|
521 |
+
if match:
|
522 |
+
chapter_num = int(match.group(1))
|
523 |
+
else:
|
524 |
+
print(f"Skipping file {chapter_file} as it does not match the expected format.")
|
525 |
+
continue
|
526 |
+
|
527 |
+
chapter_path = os.path.join(chapters_dir, chapter_file)
|
528 |
+
output_file_name = f"audio_chapter_{chapter_num}.wav"
|
529 |
+
output_file_path = os.path.join(output_audio_dir, output_file_name)
|
530 |
+
temp_audio_directory = os.path.join(".", "Working_files", "temp")
|
531 |
+
os.makedirs(temp_audio_directory, exist_ok=True)
|
532 |
+
temp_count = 0
|
533 |
+
|
534 |
+
with open(chapter_path, 'r', encoding='utf-8') as file:
|
535 |
+
chapter_text = file.read()
|
536 |
+
sentences = sent_tokenize(chapter_text, language='italian' if language == 'it' else 'english')
|
537 |
+
for sentence in tqdm(sentences, desc=f"Chapter {chapter_num}"):
|
538 |
+
fragments = split_long_sentence(sentence, max_length=249 if language == "en" else 213, max_pauses=10)
|
539 |
+
for fragment in fragments:
|
540 |
+
if fragment != "":
|
541 |
+
print(f"Generating fragment: {fragment}...")
|
542 |
+
fragment_file_path = os.path.join(temp_audio_directory, f"{temp_count}.wav")
|
543 |
+
speaker_wav_path = target_voice_path if target_voice_path else default_target_voice_path
|
544 |
+
language_code = language if language else default_language_code
|
545 |
+
tts.tts_to_file(text=fragment, file_path=fragment_file_path, speaker_wav=speaker_wav_path, language=language_code)
|
546 |
+
temp_count += 1
|
547 |
+
|
548 |
+
combine_wav_files(temp_audio_directory, output_audio_dir, output_file_name)
|
549 |
+
wipe_folder(temp_audio_directory)
|
550 |
+
print(f"Converted chapter {chapter_num} to audio.")
|
551 |
+
|
552 |
+
|
553 |
+
|
554 |
+
# Define the functions to be used in the Gradio interface
|
555 |
+
def convert_ebook_to_audio(ebook_file, target_voice_file, language, use_custom_model, custom_model_file, custom_config_file, custom_vocab_file, custom_model_url=None, progress=gr.Progress()):
|
556 |
+
ebook_file_path = ebook_file.name
|
557 |
+
target_voice = target_voice_file.name if target_voice_file else None
|
558 |
+
custom_model = None
|
559 |
+
|
560 |
+
|
561 |
+
working_files = os.path.join(".", "Working_files", "temp_ebook")
|
562 |
+
full_folder_working_files = os.path.join(".", "Working_files")
|
563 |
+
chapters_directory = os.path.join(".", "Working_files", "temp_ebook")
|
564 |
+
output_audio_directory = os.path.join(".", 'Chapter_wav_files')
|
565 |
+
remove_folder_with_contents(full_folder_working_files)
|
566 |
+
remove_folder_with_contents(output_audio_directory)
|
567 |
+
|
568 |
+
if use_custom_model and custom_model_file and custom_config_file and custom_vocab_file:
|
569 |
+
custom_model = {
|
570 |
+
'model': custom_model_file.name,
|
571 |
+
'config': custom_config_file.name,
|
572 |
+
'vocab': custom_vocab_file.name
|
573 |
+
}
|
574 |
+
if use_custom_model and custom_model_url:
|
575 |
+
print(f"Received custom model URL: {custom_model_url}")
|
576 |
+
download_dir = os.path.join(".", "Working_files", "custom_model")
|
577 |
+
download_and_extract_zip(custom_model_url, download_dir)
|
578 |
+
custom_model = {
|
579 |
+
'model': os.path.join(download_dir, 'model.pth'),
|
580 |
+
'config': os.path.join(download_dir, 'config.json'),
|
581 |
+
'vocab': os.path.join(download_dir, 'vocab.json_')
|
582 |
+
}
|
583 |
+
|
584 |
+
try:
|
585 |
+
progress(0, desc="Starting conversion")
|
586 |
+
except Exception as e:
|
587 |
+
print(f"Error updating progress: {e}")
|
588 |
+
|
589 |
+
if not calibre_installed():
|
590 |
+
return "Calibre is not installed."
|
591 |
+
|
592 |
+
|
593 |
+
try:
|
594 |
+
progress(0.1, desc="Creating chapter-labeled book")
|
595 |
+
except Exception as e:
|
596 |
+
print(f"Error updating progress: {e}")
|
597 |
+
|
598 |
+
create_chapter_labeled_book(ebook_file_path)
|
599 |
+
audiobook_output_path = os.path.join(".", "Audiobooks")
|
600 |
+
|
601 |
+
try:
|
602 |
+
progress(0.3, desc="Converting chapters to audio")
|
603 |
+
except Exception as e:
|
604 |
+
print(f"Error updating progress: {e}")
|
605 |
+
|
606 |
+
if use_custom_model:
|
607 |
+
convert_chapters_to_audio_custom_model(chapters_directory, output_audio_directory, target_voice, language, custom_model)
|
608 |
+
else:
|
609 |
+
convert_chapters_to_audio_standard_model(chapters_directory, output_audio_directory, target_voice, language)
|
610 |
+
|
611 |
+
try:
|
612 |
+
progress(0.9, desc="Creating M4B from chapters")
|
613 |
+
except Exception as e:
|
614 |
+
print(f"Error updating progress: {e}")
|
615 |
+
|
616 |
+
create_m4b_from_chapters(output_audio_directory, ebook_file_path, audiobook_output_path)
|
617 |
+
|
618 |
+
# Get the name of the created M4B file
|
619 |
+
m4b_filename = os.path.splitext(os.path.basename(ebook_file_path))[0] + '.m4b'
|
620 |
+
m4b_filepath = os.path.join(audiobook_output_path, m4b_filename)
|
621 |
+
|
622 |
+
try:
|
623 |
+
progress(1.0, desc="Conversion complete")
|
624 |
+
except Exception as e:
|
625 |
+
print(f"Error updating progress: {e}")
|
626 |
+
print(f"Audiobook created at {m4b_filepath}")
|
627 |
+
return f"Audiobook created at {m4b_filepath}", m4b_filepath
|
628 |
+
|
629 |
+
|
630 |
+
def list_audiobook_files(audiobook_folder):
|
631 |
+
# List all files in the audiobook folder
|
632 |
+
files = []
|
633 |
+
for filename in os.listdir(audiobook_folder):
|
634 |
+
if filename.endswith('.m4b'): # Adjust the file extension as needed
|
635 |
+
files.append(os.path.join(audiobook_folder, filename))
|
636 |
+
return files
|
637 |
+
|
638 |
+
def download_audiobooks():
|
639 |
+
audiobook_output_path = os.path.join(".", "Audiobooks")
|
640 |
+
return list_audiobook_files(audiobook_output_path)
|
641 |
+
|
642 |
+
|
643 |
+
language_options = [
|
644 |
+
"en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko"
|
645 |
+
]
|
646 |
+
|
647 |
+
theme = gr.themes.Soft(
|
648 |
+
primary_hue="blue",
|
649 |
+
secondary_hue="blue",
|
650 |
+
neutral_hue="blue",
|
651 |
+
text_size=gr.themes.sizes.text_md,
|
652 |
+
)
|
653 |
+
|
654 |
+
# Gradio UI setup
|
655 |
+
with gr.Blocks(theme=theme) as demo:
|
656 |
+
gr.Markdown(
|
657 |
+
"""
|
658 |
+
# eBook to Audiobook Converter
|
659 |
+
|
660 |
+
Transform your eBooks into immersive audiobooks with optional custom TTS models.
|
661 |
+
"""
|
662 |
+
)
|
663 |
+
|
664 |
+
with gr.Row():
|
665 |
+
with gr.Column(scale=3):
|
666 |
+
ebook_file = gr.File(label="eBook File")
|
667 |
+
target_voice_file = gr.File(label="Target Voice File (Optional)")
|
668 |
+
language = gr.Dropdown(label="Language", choices=language_options, value="en")
|
669 |
+
|
670 |
+
with gr.Column(scale=3):
|
671 |
+
use_custom_model = gr.Checkbox(label="Use Custom Model")
|
672 |
+
custom_model_file = gr.File(label="Custom Model File (Optional)", visible=False)
|
673 |
+
custom_config_file = gr.File(label="Custom Config File (Optional)", visible=False)
|
674 |
+
custom_vocab_file = gr.File(label="Custom Vocab File (Optional)", visible=False)
|
675 |
+
custom_model_url = gr.Textbox(label="Custom Model Zip URL (Optional)", visible=False)
|
676 |
+
|
677 |
+
convert_btn = gr.Button("Convert to Audiobook", variant="primary")
|
678 |
+
output = gr.Textbox(label="Conversion Status")
|
679 |
+
audio_player = gr.Audio(label="Audiobook Player", type="filepath")
|
680 |
+
download_btn = gr.Button("Download Audiobook Files")
|
681 |
+
download_files = gr.File(label="Download Files", interactive=False)
|
682 |
+
|
683 |
+
convert_btn.click(
|
684 |
+
convert_ebook_to_audio,
|
685 |
+
inputs=[ebook_file, target_voice_file, language, use_custom_model, custom_model_file, custom_config_file, custom_vocab_file, custom_model_url],
|
686 |
+
outputs=[output, audio_player]
|
687 |
+
)
|
688 |
+
|
689 |
+
use_custom_model.change(
|
690 |
+
lambda x: [gr.update(visible=x)] * 4,
|
691 |
+
inputs=[use_custom_model],
|
692 |
+
outputs=[custom_model_file, custom_config_file, custom_vocab_file, custom_model_url]
|
693 |
+
)
|
694 |
+
|
695 |
+
download_btn.click(
|
696 |
+
download_audiobooks,
|
697 |
+
outputs=[download_files]
|
698 |
+
)
|
699 |
+
|
700 |
+
demo.launch(share=True)
|
legacy/ebook2audiobook.py
ADDED
@@ -0,0 +1,462 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
print("starting...")
|
2 |
+
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
import subprocess
|
6 |
+
import re
|
7 |
+
from pydub import AudioSegment
|
8 |
+
import tempfile
|
9 |
+
from pydub import AudioSegment
|
10 |
+
import os
|
11 |
+
import nltk
|
12 |
+
from nltk.tokenize import sent_tokenize
|
13 |
+
nltk.download('punkt') # Make sure to download the necessary models
|
14 |
+
def is_folder_empty(folder_path):
|
15 |
+
if os.path.exists(folder_path) and os.path.isdir(folder_path):
|
16 |
+
# List directory contents
|
17 |
+
if not os.listdir(folder_path):
|
18 |
+
return True # The folder is empty
|
19 |
+
else:
|
20 |
+
return False # The folder is not empty
|
21 |
+
else:
|
22 |
+
print(f"The path {folder_path} is not a valid folder.")
|
23 |
+
return None # The path is not a valid folder
|
24 |
+
|
25 |
+
def remove_folder_with_contents(folder_path):
|
26 |
+
try:
|
27 |
+
shutil.rmtree(folder_path)
|
28 |
+
print(f"Successfully removed {folder_path} and all of its contents.")
|
29 |
+
except Exception as e:
|
30 |
+
print(f"Error removing {folder_path}: {e}")
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
def wipe_folder(folder_path):
|
36 |
+
# Check if the folder exists
|
37 |
+
if not os.path.exists(folder_path):
|
38 |
+
print(f"The folder {folder_path} does not exist.")
|
39 |
+
return
|
40 |
+
|
41 |
+
# Iterate over all the items in the given folder
|
42 |
+
for item in os.listdir(folder_path):
|
43 |
+
item_path = os.path.join(folder_path, item)
|
44 |
+
# If it's a file, remove it and print a message
|
45 |
+
if os.path.isfile(item_path):
|
46 |
+
os.remove(item_path)
|
47 |
+
print(f"Removed file: {item_path}")
|
48 |
+
# If it's a directory, remove it recursively and print a message
|
49 |
+
elif os.path.isdir(item_path):
|
50 |
+
shutil.rmtree(item_path)
|
51 |
+
print(f"Removed directory and its contents: {item_path}")
|
52 |
+
|
53 |
+
print(f"All contents wiped from {folder_path}.")
|
54 |
+
|
55 |
+
|
56 |
+
# Example usage
|
57 |
+
# folder_to_wipe = 'path_to_your_folder'
|
58 |
+
# wipe_folder(folder_to_wipe)
|
59 |
+
|
60 |
+
|
61 |
+
def create_m4b_from_chapters(input_dir, ebook_file, output_dir):
|
62 |
+
# Function to sort chapters based on their numeric order
|
63 |
+
def sort_key(chapter_file):
|
64 |
+
numbers = re.findall(r'\d+', chapter_file)
|
65 |
+
return int(numbers[0]) if numbers else 0
|
66 |
+
|
67 |
+
# Extract metadata and cover image from the eBook file
|
68 |
+
def extract_metadata_and_cover(ebook_path):
|
69 |
+
try:
|
70 |
+
cover_path = ebook_path.rsplit('.', 1)[0] + '.jpg'
|
71 |
+
subprocess.run(['ebook-meta', ebook_path, '--get-cover', cover_path], check=True)
|
72 |
+
if os.path.exists(cover_path):
|
73 |
+
return cover_path
|
74 |
+
except Exception as e:
|
75 |
+
print(f"Error extracting eBook metadata or cover: {e}")
|
76 |
+
return None
|
77 |
+
# Combine WAV files into a single file
|
78 |
+
def combine_wav_files(chapter_files, output_path):
|
79 |
+
# Initialize an empty audio segment
|
80 |
+
combined_audio = AudioSegment.empty()
|
81 |
+
|
82 |
+
# Sequentially append each file to the combined_audio
|
83 |
+
for chapter_file in chapter_files:
|
84 |
+
audio_segment = AudioSegment.from_wav(chapter_file)
|
85 |
+
combined_audio += audio_segment
|
86 |
+
# Export the combined audio to the output file path
|
87 |
+
combined_audio.export(output_path, format='wav')
|
88 |
+
print(f"Combined audio saved to {output_path}")
|
89 |
+
|
90 |
+
# Function to generate metadata for M4B chapters
|
91 |
+
def generate_ffmpeg_metadata(chapter_files, metadata_file):
|
92 |
+
with open(metadata_file, 'w') as file:
|
93 |
+
file.write(';FFMETADATA1\n')
|
94 |
+
start_time = 0
|
95 |
+
for index, chapter_file in enumerate(chapter_files):
|
96 |
+
duration_ms = len(AudioSegment.from_wav(chapter_file))
|
97 |
+
file.write(f'[CHAPTER]\nTIMEBASE=1/1000\nSTART={start_time}\n')
|
98 |
+
file.write(f'END={start_time + duration_ms}\ntitle=Chapter {index + 1}\n')
|
99 |
+
start_time += duration_ms
|
100 |
+
|
101 |
+
# Generate the final M4B file using ffmpeg
|
102 |
+
def create_m4b(combined_wav, metadata_file, cover_image, output_m4b):
|
103 |
+
# Ensure the output directory exists
|
104 |
+
os.makedirs(os.path.dirname(output_m4b), exist_ok=True)
|
105 |
+
|
106 |
+
ffmpeg_cmd = ['ffmpeg', '-i', combined_wav, '-i', metadata_file]
|
107 |
+
if cover_image:
|
108 |
+
ffmpeg_cmd += ['-i', cover_image, '-map', '0:a', '-map', '2:v']
|
109 |
+
else:
|
110 |
+
ffmpeg_cmd += ['-map', '0:a']
|
111 |
+
|
112 |
+
ffmpeg_cmd += ['-map_metadata', '1', '-c:a', 'aac', '-b:a', '192k']
|
113 |
+
if cover_image:
|
114 |
+
ffmpeg_cmd += ['-c:v', 'png', '-disposition:v', 'attached_pic']
|
115 |
+
ffmpeg_cmd += [output_m4b]
|
116 |
+
|
117 |
+
subprocess.run(ffmpeg_cmd, check=True)
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
# Main logic
|
122 |
+
chapter_files = sorted([os.path.join(input_dir, f) for f in os.listdir(input_dir) if f.endswith('.wav')], key=sort_key)
|
123 |
+
temp_dir = tempfile.gettempdir()
|
124 |
+
temp_combined_wav = os.path.join(temp_dir, 'combined.wav')
|
125 |
+
metadata_file = os.path.join(temp_dir, 'metadata.txt')
|
126 |
+
cover_image = extract_metadata_and_cover(ebook_file)
|
127 |
+
output_m4b = os.path.join(output_dir, os.path.splitext(os.path.basename(ebook_file))[0] + '.m4b')
|
128 |
+
|
129 |
+
combine_wav_files(chapter_files, temp_combined_wav)
|
130 |
+
generate_ffmpeg_metadata(chapter_files, metadata_file)
|
131 |
+
create_m4b(temp_combined_wav, metadata_file, cover_image, output_m4b)
|
132 |
+
|
133 |
+
# Cleanup
|
134 |
+
if os.path.exists(temp_combined_wav):
|
135 |
+
os.remove(temp_combined_wav)
|
136 |
+
if os.path.exists(metadata_file):
|
137 |
+
os.remove(metadata_file)
|
138 |
+
if cover_image and os.path.exists(cover_image):
|
139 |
+
os.remove(cover_image)
|
140 |
+
|
141 |
+
# Example usage
|
142 |
+
# create_m4b_from_chapters('path_to_chapter_wavs', 'path_to_ebook_file', 'path_to_output_dir')
|
143 |
+
|
144 |
+
|
145 |
+
|
146 |
+
|
147 |
+
|
148 |
+
|
149 |
+
#this code right here isnt the book grabbing thing but its before to refrence in ordero to create the sepecial chapter labeled book thing with calibre idk some systems cant seem to get it so just in case but the next bit of code after this is the book grabbing code with booknlp
|
150 |
+
import os
|
151 |
+
import subprocess
|
152 |
+
import ebooklib
|
153 |
+
from ebooklib import epub
|
154 |
+
from bs4 import BeautifulSoup
|
155 |
+
import re
|
156 |
+
import csv
|
157 |
+
import nltk
|
158 |
+
|
159 |
+
# Only run the main script if Value is True
|
160 |
+
def create_chapter_labeled_book(ebook_file_path):
|
161 |
+
# Function to ensure the existence of a directory
|
162 |
+
def ensure_directory(directory_path):
|
163 |
+
if not os.path.exists(directory_path):
|
164 |
+
os.makedirs(directory_path)
|
165 |
+
print(f"Created directory: {directory_path}")
|
166 |
+
|
167 |
+
ensure_directory(os.path.join(".", 'Working_files', 'Book'))
|
168 |
+
|
169 |
+
def convert_to_epub(input_path, output_path):
|
170 |
+
# Convert the ebook to EPUB format using Calibre's ebook-convert
|
171 |
+
try:
|
172 |
+
subprocess.run(['ebook-convert', input_path, output_path], check=True)
|
173 |
+
except subprocess.CalledProcessError as e:
|
174 |
+
print(f"An error occurred while converting the eBook: {e}")
|
175 |
+
return False
|
176 |
+
return True
|
177 |
+
|
178 |
+
def save_chapters_as_text(epub_path):
|
179 |
+
# Create the directory if it doesn't exist
|
180 |
+
directory = os.path.join(".", "Working_files", "temp_ebook")
|
181 |
+
ensure_directory(directory)
|
182 |
+
|
183 |
+
# Open the EPUB file
|
184 |
+
book = epub.read_epub(epub_path)
|
185 |
+
|
186 |
+
previous_chapter_text = ''
|
187 |
+
previous_filename = ''
|
188 |
+
chapter_counter = 0
|
189 |
+
|
190 |
+
# Iterate through the items in the EPUB file
|
191 |
+
for item in book.get_items():
|
192 |
+
if item.get_type() == ebooklib.ITEM_DOCUMENT:
|
193 |
+
# Use BeautifulSoup to parse HTML content
|
194 |
+
soup = BeautifulSoup(item.get_content(), 'html.parser')
|
195 |
+
text = soup.get_text()
|
196 |
+
|
197 |
+
# Check if the text is not empty
|
198 |
+
if text.strip():
|
199 |
+
if len(text) < 2300 and previous_filename:
|
200 |
+
# Append text to the previous chapter if it's short
|
201 |
+
with open(previous_filename, 'a', encoding='utf-8') as file:
|
202 |
+
file.write('\n' + text)
|
203 |
+
else:
|
204 |
+
# Create a new chapter file and increment the counter
|
205 |
+
previous_filename = os.path.join(directory, f"chapter_{chapter_counter}.txt")
|
206 |
+
chapter_counter += 1
|
207 |
+
with open(previous_filename, 'w', encoding='utf-8') as file:
|
208 |
+
file.write(text)
|
209 |
+
print(f"Saved chapter: {previous_filename}")
|
210 |
+
|
211 |
+
# Example usage
|
212 |
+
input_ebook = ebook_file_path # Replace with your eBook file path
|
213 |
+
output_epub = os.path.join(".", "Working_files", "temp.epub")
|
214 |
+
|
215 |
+
|
216 |
+
if os.path.exists(output_epub):
|
217 |
+
os.remove(output_epub)
|
218 |
+
print(f"File {output_epub} has been removed.")
|
219 |
+
else:
|
220 |
+
print(f"The file {output_epub} does not exist.")
|
221 |
+
|
222 |
+
if convert_to_epub(input_ebook, output_epub):
|
223 |
+
save_chapters_as_text(output_epub)
|
224 |
+
|
225 |
+
# Download the necessary NLTK data (if not already present)
|
226 |
+
nltk.download('punkt')
|
227 |
+
|
228 |
+
def process_chapter_files(folder_path, output_csv):
|
229 |
+
with open(output_csv, 'w', newline='', encoding='utf-8') as csvfile:
|
230 |
+
writer = csv.writer(csvfile)
|
231 |
+
# Write the header row
|
232 |
+
writer.writerow(['Text', 'Start Location', 'End Location', 'Is Quote', 'Speaker', 'Chapter'])
|
233 |
+
|
234 |
+
# Process each chapter file
|
235 |
+
chapter_files = sorted(os.listdir(folder_path), key=lambda x: int(x.split('_')[1].split('.')[0]))
|
236 |
+
for filename in chapter_files:
|
237 |
+
if filename.startswith('chapter_') and filename.endswith('.txt'):
|
238 |
+
chapter_number = int(filename.split('_')[1].split('.')[0])
|
239 |
+
file_path = os.path.join(folder_path, filename)
|
240 |
+
|
241 |
+
try:
|
242 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
243 |
+
text = file.read()
|
244 |
+
# Insert "NEWCHAPTERABC" at the beginning of each chapter's text
|
245 |
+
if text:
|
246 |
+
text = "NEWCHAPTERABC" + text
|
247 |
+
sentences = nltk.tokenize.sent_tokenize(text)
|
248 |
+
for sentence in sentences:
|
249 |
+
start_location = text.find(sentence)
|
250 |
+
end_location = start_location + len(sentence)
|
251 |
+
writer.writerow([sentence, start_location, end_location, 'True', 'Narrator', chapter_number])
|
252 |
+
except Exception as e:
|
253 |
+
print(f"Error processing file {filename}: {e}")
|
254 |
+
|
255 |
+
# Example usage
|
256 |
+
folder_path = os.path.join(".", "Working_files", "temp_ebook")
|
257 |
+
output_csv = os.path.join(".", "Working_files", "Book", "Other_book.csv")
|
258 |
+
|
259 |
+
process_chapter_files(folder_path, output_csv)
|
260 |
+
|
261 |
+
def sort_key(filename):
|
262 |
+
"""Extract chapter number for sorting."""
|
263 |
+
match = re.search(r'chapter_(\d+)\.txt', filename)
|
264 |
+
return int(match.group(1)) if match else 0
|
265 |
+
|
266 |
+
def combine_chapters(input_folder, output_file):
|
267 |
+
# Create the output folder if it doesn't exist
|
268 |
+
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
269 |
+
|
270 |
+
# List all txt files and sort them by chapter number
|
271 |
+
files = [f for f in os.listdir(input_folder) if f.endswith('.txt')]
|
272 |
+
sorted_files = sorted(files, key=sort_key)
|
273 |
+
|
274 |
+
with open(output_file, 'w', encoding='utf-8') as outfile: # Specify UTF-8 encoding here
|
275 |
+
for i, filename in enumerate(sorted_files):
|
276 |
+
with open(os.path.join(input_folder, filename), 'r', encoding='utf-8') as infile: # And here
|
277 |
+
outfile.write(infile.read())
|
278 |
+
# Add the marker unless it's the last file
|
279 |
+
if i < len(sorted_files) - 1:
|
280 |
+
outfile.write("\nNEWCHAPTERABC\n")
|
281 |
+
|
282 |
+
# Paths
|
283 |
+
input_folder = os.path.join(".", 'Working_files', 'temp_ebook')
|
284 |
+
output_file = os.path.join(".", 'Working_files', 'Book', 'Chapter_Book.txt')
|
285 |
+
|
286 |
+
|
287 |
+
# Combine the chapters
|
288 |
+
combine_chapters(input_folder, output_file)
|
289 |
+
|
290 |
+
ensure_directory(os.path.join(".", "Working_files", "Book"))
|
291 |
+
|
292 |
+
|
293 |
+
#create_chapter_labeled_book()
|
294 |
+
|
295 |
+
|
296 |
+
|
297 |
+
|
298 |
+
import os
|
299 |
+
import subprocess
|
300 |
+
import sys
|
301 |
+
import torchaudio
|
302 |
+
|
303 |
+
# Check if Calibre's ebook-convert tool is installed
|
304 |
+
def calibre_installed():
|
305 |
+
try:
|
306 |
+
subprocess.run(['ebook-convert', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
307 |
+
return True
|
308 |
+
except FileNotFoundError:
|
309 |
+
print("Calibre is not installed. Please install Calibre for this functionality.")
|
310 |
+
return False
|
311 |
+
|
312 |
+
|
313 |
+
import os
|
314 |
+
import torch
|
315 |
+
from TTS.api import TTS
|
316 |
+
from nltk.tokenize import sent_tokenize
|
317 |
+
from pydub import AudioSegment
|
318 |
+
# Assuming split_long_sentence and wipe_folder are defined elsewhere in your code
|
319 |
+
|
320 |
+
default_target_voice_path = "default_voice.wav" # Ensure this is a valid path
|
321 |
+
default_language_code = "en"
|
322 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
323 |
+
|
324 |
+
def combine_wav_files(input_directory, output_directory, file_name):
|
325 |
+
# Ensure that the output directory exists, create it if necessary
|
326 |
+
os.makedirs(output_directory, exist_ok=True)
|
327 |
+
|
328 |
+
# Specify the output file path
|
329 |
+
output_file_path = os.path.join(output_directory, file_name)
|
330 |
+
|
331 |
+
# Initialize an empty audio segment
|
332 |
+
combined_audio = AudioSegment.empty()
|
333 |
+
|
334 |
+
# Get a list of all .wav files in the specified input directory and sort them
|
335 |
+
input_file_paths = sorted(
|
336 |
+
[os.path.join(input_directory, f) for f in os.listdir(input_directory) if f.endswith(".wav")],
|
337 |
+
key=lambda f: int(''.join(filter(str.isdigit, f)))
|
338 |
+
)
|
339 |
+
|
340 |
+
# Sequentially append each file to the combined_audio
|
341 |
+
for input_file_path in input_file_paths:
|
342 |
+
audio_segment = AudioSegment.from_wav(input_file_path)
|
343 |
+
combined_audio += audio_segment
|
344 |
+
|
345 |
+
# Export the combined audio to the output file path
|
346 |
+
combined_audio.export(output_file_path, format='wav')
|
347 |
+
|
348 |
+
print(f"Combined audio saved to {output_file_path}")
|
349 |
+
|
350 |
+
# Function to split long strings into parts
|
351 |
+
def split_long_sentence(sentence, max_length=249, max_pauses=10):
|
352 |
+
"""
|
353 |
+
Splits a sentence into parts based on length or number of pauses without recursion.
|
354 |
+
|
355 |
+
:param sentence: The sentence to split.
|
356 |
+
:param max_length: Maximum allowed length of a sentence.
|
357 |
+
:param max_pauses: Maximum allowed number of pauses in a sentence.
|
358 |
+
:return: A list of sentence parts that meet the criteria.
|
359 |
+
"""
|
360 |
+
parts = []
|
361 |
+
while len(sentence) > max_length or sentence.count(',') + sentence.count(';') + sentence.count('.') > max_pauses:
|
362 |
+
possible_splits = [i for i, char in enumerate(sentence) if char in ',;.' and i < max_length]
|
363 |
+
if possible_splits:
|
364 |
+
# Find the best place to split the sentence, preferring the last possible split to keep parts longer
|
365 |
+
split_at = possible_splits[-1] + 1
|
366 |
+
else:
|
367 |
+
# If no punctuation to split on within max_length, split at max_length
|
368 |
+
split_at = max_length
|
369 |
+
|
370 |
+
# Split the sentence and add the first part to the list
|
371 |
+
parts.append(sentence[:split_at].strip())
|
372 |
+
sentence = sentence[split_at:].strip()
|
373 |
+
|
374 |
+
# Add the remaining part of the sentence
|
375 |
+
parts.append(sentence)
|
376 |
+
return parts
|
377 |
+
|
378 |
+
"""
|
379 |
+
if 'tts' not in locals():
|
380 |
+
tts = TTS(selected_tts_model, progress_bar=True).to(device)
|
381 |
+
"""
|
382 |
+
from tqdm import tqdm
|
383 |
+
|
384 |
+
# Convert chapters to audio using XTTS
|
385 |
+
def convert_chapters_to_audio(chapters_dir, output_audio_dir, target_voice_path=None, language=None):
|
386 |
+
selected_tts_model = "tts_models/multilingual/multi-dataset/xtts_v2"
|
387 |
+
tts = TTS(selected_tts_model, progress_bar=False).to(device) # Set progress_bar to False to avoid nested progress bars
|
388 |
+
|
389 |
+
if not os.path.exists(output_audio_dir):
|
390 |
+
os.makedirs(output_audio_dir)
|
391 |
+
|
392 |
+
for chapter_file in sorted(os.listdir(chapters_dir)):
|
393 |
+
if chapter_file.endswith('.txt'):
|
394 |
+
# Extract chapter number from the filename
|
395 |
+
match = re.search(r"chapter_(\d+).txt", chapter_file)
|
396 |
+
if match:
|
397 |
+
chapter_num = int(match.group(1))
|
398 |
+
else:
|
399 |
+
print(f"Skipping file {chapter_file} as it does not match the expected format.")
|
400 |
+
continue
|
401 |
+
|
402 |
+
chapter_path = os.path.join(chapters_dir, chapter_file)
|
403 |
+
output_file_name = f"audio_chapter_{chapter_num}.wav"
|
404 |
+
output_file_path = os.path.join(output_audio_dir, output_file_name)
|
405 |
+
temp_audio_directory = os.path.join(".", "Working_files", "temp")
|
406 |
+
os.makedirs(temp_audio_directory, exist_ok=True)
|
407 |
+
temp_count = 0
|
408 |
+
|
409 |
+
with open(chapter_path, 'r', encoding='utf-8') as file:
|
410 |
+
chapter_text = file.read()
|
411 |
+
# Use the specified language model for sentence tokenization
|
412 |
+
sentences = sent_tokenize(chapter_text, language='italian' if language == 'it' else 'english')
|
413 |
+
for sentence in tqdm(sentences, desc=f"Chapter {chapter_num}"):
|
414 |
+
fragments = []
|
415 |
+
if language == "en":
|
416 |
+
fragments = split_long_sentence(sentence, max_length=249, max_pauses=10)
|
417 |
+
if language == "it":
|
418 |
+
fragments = split_long_sentence(sentence, max_length=213, max_pauses=10)
|
419 |
+
for fragment in fragments:
|
420 |
+
if fragment != "": #a hot fix to avoid blank fragments
|
421 |
+
print(f"Generating fragment: {fragment}...")
|
422 |
+
fragment_file_path = os.path.join(temp_audio_directory, f"{temp_count}.wav")
|
423 |
+
speaker_wav_path = target_voice_path if target_voice_path else default_target_voice_path
|
424 |
+
language_code = language if language else default_language_code
|
425 |
+
tts.tts_to_file(text=fragment, file_path=fragment_file_path, speaker_wav=speaker_wav_path, language=language_code)
|
426 |
+
temp_count += 1
|
427 |
+
|
428 |
+
combine_wav_files(temp_audio_directory, output_audio_dir, output_file_name)
|
429 |
+
wipe_folder(temp_audio_directory)
|
430 |
+
print(f"Converted chapter {chapter_num} to audio.")
|
431 |
+
|
432 |
+
|
433 |
+
|
434 |
+
# Main execution flow
|
435 |
+
if __name__ == "__main__":
|
436 |
+
if len(sys.argv) < 2:
|
437 |
+
print("Usage: python script.py <ebook_file_path> [target_voice_file_path]")
|
438 |
+
sys.exit(1)
|
439 |
+
|
440 |
+
ebook_file_path = sys.argv[1]
|
441 |
+
target_voice = sys.argv[2] if len(sys.argv) > 2 else None
|
442 |
+
language = sys.argv[3] if len(sys.argv) > 3 else None
|
443 |
+
|
444 |
+
if not calibre_installed():
|
445 |
+
sys.exit(1)
|
446 |
+
|
447 |
+
working_files = os.path.join(".","Working_files", "temp_ebook")
|
448 |
+
full_folder_working_files =os.path.join(".","Working_files")
|
449 |
+
chapters_directory = os.path.join(".","Working_files", "temp_ebook")
|
450 |
+
output_audio_directory = os.path.join(".", 'Chapter_wav_files')
|
451 |
+
|
452 |
+
print("Wiping and removeing Working_files folder...")
|
453 |
+
remove_folder_with_contents(full_folder_working_files)
|
454 |
+
|
455 |
+
print("Wiping and and removeing chapter_wav_files folder...")
|
456 |
+
remove_folder_with_contents(output_audio_directory)
|
457 |
+
|
458 |
+
create_chapter_labeled_book(ebook_file_path)
|
459 |
+
audiobook_output_path = os.path.join(".", "Audiobooks")
|
460 |
+
print(f"{chapters_directory}||||{output_audio_directory}|||||{target_voice}")
|
461 |
+
convert_chapters_to_audio(chapters_directory, output_audio_directory, target_voice, language)
|
462 |
+
create_m4b_from_chapters(output_audio_directory, ebook_file_path, audiobook_output_path)
|
legacy/gradio_gui_with_email_and_que.py
ADDED
@@ -0,0 +1,614 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
print("starting...")
|
2 |
+
import ebooklib
|
3 |
+
from ebooklib import epub
|
4 |
+
|
5 |
+
import os
|
6 |
+
import subprocess
|
7 |
+
import ebooklib
|
8 |
+
from ebooklib import epub
|
9 |
+
from bs4 import BeautifulSoup
|
10 |
+
import re
|
11 |
+
import csv
|
12 |
+
import nltk
|
13 |
+
|
14 |
+
import os
|
15 |
+
import subprocess
|
16 |
+
import sys
|
17 |
+
import torchaudio
|
18 |
+
|
19 |
+
import os
|
20 |
+
import torch
|
21 |
+
from TTS.api import TTS
|
22 |
+
from nltk.tokenize import sent_tokenize
|
23 |
+
from pydub import AudioSegment
|
24 |
+
|
25 |
+
from tqdm import tqdm
|
26 |
+
|
27 |
+
|
28 |
+
|
29 |
+
import os
|
30 |
+
import subprocess
|
31 |
+
import ebooklib
|
32 |
+
from ebooklib import epub
|
33 |
+
from bs4 import BeautifulSoup
|
34 |
+
import re
|
35 |
+
import csv
|
36 |
+
import nltk
|
37 |
+
|
38 |
+
from bs4 import BeautifulSoup
|
39 |
+
import os
|
40 |
+
import shutil
|
41 |
+
import subprocess
|
42 |
+
import re
|
43 |
+
from pydub import AudioSegment
|
44 |
+
import tempfile
|
45 |
+
import urllib.request
|
46 |
+
import zipfile
|
47 |
+
import requests
|
48 |
+
from tqdm import tqdm
|
49 |
+
import nltk
|
50 |
+
from nltk.tokenize import sent_tokenize
|
51 |
+
import torch
|
52 |
+
import torchaudio
|
53 |
+
import gradio as gr
|
54 |
+
from threading import Lock, Thread
|
55 |
+
from queue import Queue
|
56 |
+
import smtplib
|
57 |
+
from email.mime.text import MIMEText
|
58 |
+
|
59 |
+
|
60 |
+
import os
|
61 |
+
import shutil
|
62 |
+
import subprocess
|
63 |
+
import re
|
64 |
+
from pydub import AudioSegment
|
65 |
+
import tempfile
|
66 |
+
from pydub import AudioSegment
|
67 |
+
import os
|
68 |
+
import nltk
|
69 |
+
from nltk.tokenize import sent_tokenize
|
70 |
+
import sys
|
71 |
+
import torch
|
72 |
+
from TTS.api import TTS
|
73 |
+
from TTS.tts.configs.xtts_config import XttsConfig
|
74 |
+
from TTS.tts.models.xtts import Xtts
|
75 |
+
from tqdm import tqdm
|
76 |
+
import gradio as gr
|
77 |
+
from gradio import Progress
|
78 |
+
import urllib.request
|
79 |
+
import zipfile
|
80 |
+
|
81 |
+
|
82 |
+
default_target_voice_path = "default_voice.wav" # Ensure this is a valid path
|
83 |
+
default_language_code = "en"
|
84 |
+
|
85 |
+
|
86 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
87 |
+
print(f"Device selected is: {device}")
|
88 |
+
|
89 |
+
nltk.download('punkt') # Ensure necessary models are downloaded
|
90 |
+
|
91 |
+
# Global variables for queue management
|
92 |
+
queue = Queue()
|
93 |
+
queue_lock = Lock()
|
94 |
+
|
95 |
+
# Function to send an email with the download link
|
96 |
+
def send_email(to_address, download_link):
|
97 |
+
from_address = "your_email@example.com" # Replace with your email
|
98 |
+
subject = "Your Audiobook is Ready"
|
99 |
+
body = f"Your audiobook has been processed. You can download it from the following link: {download_link}"
|
100 |
+
|
101 |
+
msg = MIMEText(body)
|
102 |
+
msg['Subject'] = subject
|
103 |
+
msg['From'] = from_address
|
104 |
+
msg['To'] = to_address
|
105 |
+
|
106 |
+
try:
|
107 |
+
with smtplib.SMTP('smtp.example.com', 587) as server: # Replace with your SMTP server details
|
108 |
+
server.starttls()
|
109 |
+
server.login(from_address, "your_password") # Replace with your email password
|
110 |
+
server.sendmail(from_address, [to_address], msg.as_string())
|
111 |
+
print(f"Email sent to {to_address}")
|
112 |
+
except Exception as e:
|
113 |
+
print(f"Failed to send email: {e}")
|
114 |
+
|
115 |
+
# Function to download and extract the custom model
|
116 |
+
def download_and_extract_zip(url, extract_to='.'):
|
117 |
+
try:
|
118 |
+
os.makedirs(extract_to, exist_ok=True)
|
119 |
+
zip_path = os.path.join(extract_to, 'model.zip')
|
120 |
+
|
121 |
+
with tqdm(unit='B', unit_scale=True, miniters=1, desc="Downloading Model") as t:
|
122 |
+
def reporthook(blocknum, blocksize, totalsize):
|
123 |
+
t.total = totalsize
|
124 |
+
t.update(blocknum * blocksize - t.n)
|
125 |
+
urllib.request.urlretrieve(url, zip_path, reporthook=reporthook)
|
126 |
+
print(f"Downloaded zip file to {zip_path}")
|
127 |
+
|
128 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
129 |
+
files = zip_ref.namelist()
|
130 |
+
with tqdm(total=len(files), unit="file", desc="Extracting Files") as t:
|
131 |
+
for file in files:
|
132 |
+
if not file.endswith('/'):
|
133 |
+
extracted_path = zip_ref.extract(file, extract_to)
|
134 |
+
base_file_path = os.path.join(extract_to, os.path.basename(file))
|
135 |
+
os.rename(extracted_path, base_file_path)
|
136 |
+
t.update(1)
|
137 |
+
|
138 |
+
os.remove(zip_path)
|
139 |
+
for root, dirs, files in os.walk(extract_to, topdown=False):
|
140 |
+
for name in dirs:
|
141 |
+
os.rmdir(os.path.join(root, name))
|
142 |
+
print(f"Extracted files to {extract_to}")
|
143 |
+
|
144 |
+
required_files = ['model.pth', 'config.json', 'vocab.json_']
|
145 |
+
missing_files = [file for file in required_files if not os.path.exists(os.path.join(extract_to, file))]
|
146 |
+
|
147 |
+
if not missing_files:
|
148 |
+
print("All required files (model.pth, config.json, vocab.json_) found.")
|
149 |
+
else:
|
150 |
+
print(f"Missing files: {', '.join(missing_files)}")
|
151 |
+
|
152 |
+
except Exception as e:
|
153 |
+
print(f"Failed to download or extract zip file: {e}")
|
154 |
+
|
155 |
+
# Function to check if a folder is empty
|
156 |
+
def is_folder_empty(folder_path):
|
157 |
+
if os.path.exists(folder_path) and os.path.isdir(folder_path):
|
158 |
+
return not os.listdir(folder_path)
|
159 |
+
else:
|
160 |
+
print(f"The path {folder_path} is not a valid folder.")
|
161 |
+
return None
|
162 |
+
|
163 |
+
# Function to remove a folder and its contents
|
164 |
+
def remove_folder_with_contents(folder_path):
|
165 |
+
try:
|
166 |
+
shutil.rmtree(folder_path)
|
167 |
+
print(f"Successfully removed {folder_path} and all of its contents.")
|
168 |
+
except Exception as e:
|
169 |
+
print(f"Error removing {folder_path}: {e}")
|
170 |
+
|
171 |
+
# Function to wipe the contents of a folder
|
172 |
+
def wipe_folder(folder_path):
|
173 |
+
if not os.path.exists(folder_path):
|
174 |
+
print(f"The folder {folder_path} does not exist.")
|
175 |
+
return
|
176 |
+
|
177 |
+
for item in os.listdir(folder_path):
|
178 |
+
item_path = os.path.join(folder_path, item)
|
179 |
+
if os.path.isfile(item_path):
|
180 |
+
os.remove(item_path)
|
181 |
+
print(f"Removed file: {item_path}")
|
182 |
+
elif os.path.isdir(item_path):
|
183 |
+
shutil.rmtree(item_path)
|
184 |
+
print(f"Removed directory and its contents: {item_path}")
|
185 |
+
|
186 |
+
print(f"All contents wiped from {folder_path}.")
|
187 |
+
|
188 |
+
# Function to create M4B from chapters
|
189 |
+
def create_m4b_from_chapters(input_dir, ebook_file, output_dir):
|
190 |
+
def sort_key(chapter_file):
|
191 |
+
numbers = re.findall(r'\d+', chapter_file)
|
192 |
+
return int(numbers[0]) if numbers else 0
|
193 |
+
|
194 |
+
def extract_metadata_and_cover(ebook_path):
|
195 |
+
try:
|
196 |
+
cover_path = ebook_path.rsplit('.', 1)[0] + '.jpg'
|
197 |
+
subprocess.run(['ebook-meta', ebook_path, '--get-cover', cover_path], check=True)
|
198 |
+
if os.path.exists(cover_path):
|
199 |
+
return cover_path
|
200 |
+
except Exception as e:
|
201 |
+
print(f"Error extracting eBook metadata or cover: {e}")
|
202 |
+
return None
|
203 |
+
|
204 |
+
def combine_wav_files(chapter_files, output_path):
|
205 |
+
combined_audio = AudioSegment.empty()
|
206 |
+
for chapter_file in chapter_files:
|
207 |
+
audio_segment = AudioSegment.from_wav(chapter_file)
|
208 |
+
combined_audio += audio_segment
|
209 |
+
combined_audio.export(output_path, format='wav')
|
210 |
+
print(f"Combined audio saved to {output_path}")
|
211 |
+
|
212 |
+
def generate_ffmpeg_metadata(chapter_files, metadata_file):
|
213 |
+
with open(metadata_file, 'w') as file:
|
214 |
+
file.write(';FFMETADATA1\n')
|
215 |
+
start_time = 0
|
216 |
+
for index, chapter_file in enumerate(chapter_files):
|
217 |
+
duration_ms = len(AudioSegment.from_wav(chapter_file))
|
218 |
+
file.write(f'[CHAPTER]\nTIMEBASE=1/1000\nSTART={start_time}\n')
|
219 |
+
file.write(f'END={start_time + duration_ms}\ntitle=Chapter {index + 1}\n')
|
220 |
+
start_time += duration_ms
|
221 |
+
|
222 |
+
def create_m4b(combined_wav, metadata_file, cover_image, output_m4b):
|
223 |
+
os.makedirs(os.path.dirname(output_m4b), exist_ok=True)
|
224 |
+
|
225 |
+
ffmpeg_cmd = ['ffmpeg', '-i', combined_wav, '-i', metadata_file]
|
226 |
+
if cover_image:
|
227 |
+
ffmpeg_cmd += ['-i', cover_image, '-map', '0:a', '-map', '2:v']
|
228 |
+
else:
|
229 |
+
ffmpeg_cmd += ['-map', '0:a']
|
230 |
+
|
231 |
+
ffmpeg_cmd += ['-map_metadata', '1', '-c:a', 'aac', '-b:a', '192k']
|
232 |
+
if cover_image:
|
233 |
+
ffmpeg_cmd += ['-c:v', 'png', '-disposition:v', 'attached_pic']
|
234 |
+
ffmpeg_cmd += [output_m4b]
|
235 |
+
|
236 |
+
subprocess.run(ffmpeg_cmd, check=True)
|
237 |
+
|
238 |
+
chapter_files = sorted([os.path.join(input_dir, f) for f in os.listdir(input_dir) if f.endswith('.wav')], key=sort_key)
|
239 |
+
temp_dir = tempfile.gettempdir()
|
240 |
+
temp_combined_wav = os.path.join(temp_dir, 'combined.wav')
|
241 |
+
metadata_file = os.path.join(temp_dir, 'metadata.txt')
|
242 |
+
cover_image = extract_metadata_and_cover(ebook_file)
|
243 |
+
output_m4b = os.path.join(output_dir, os.path.splitext(os.path.basename(ebook_file))[0] + '.m4b')
|
244 |
+
|
245 |
+
combine_wav_files(chapter_files, temp_combined_wav)
|
246 |
+
generate_ffmpeg_metadata(chapter_files, metadata_file)
|
247 |
+
create_m4b(temp_combined_wav, metadata_file, cover_image, output_m4b)
|
248 |
+
|
249 |
+
if os.path.exists(temp_combined_wav):
|
250 |
+
os.remove(temp_combined_wav)
|
251 |
+
if os.path.exists(metadata_file):
|
252 |
+
os.remove(metadata_file)
|
253 |
+
if cover_image and os.path.exists(cover_image):
|
254 |
+
os.remove(cover_image)
|
255 |
+
|
256 |
+
# Function to create chapter-labeled book
|
257 |
+
def create_chapter_labeled_book(ebook_file_path):
|
258 |
+
def ensure_directory(directory_path):
|
259 |
+
if not os.path.exists(directory_path):
|
260 |
+
os.makedirs(directory_path)
|
261 |
+
print(f"Created directory: {directory_path}")
|
262 |
+
|
263 |
+
ensure_directory(os.path.join(".", 'Working_files', 'Book'))
|
264 |
+
|
265 |
+
def convert_to_epub(input_path, output_path):
|
266 |
+
try:
|
267 |
+
subprocess.run(['ebook-convert', input_path, output_path], check=True)
|
268 |
+
except subprocess.CalledProcessError as e:
|
269 |
+
print(f"An error occurred while converting the eBook: {e}")
|
270 |
+
return False
|
271 |
+
return True
|
272 |
+
|
273 |
+
def save_chapters_as_text(epub_path):
|
274 |
+
directory = os.path.join(".", "Working_files", "temp_ebook")
|
275 |
+
ensure_directory(directory)
|
276 |
+
|
277 |
+
book = epub.read_epub(epub_path)
|
278 |
+
|
279 |
+
previous_chapter_text = ''
|
280 |
+
previous_filename = ''
|
281 |
+
chapter_counter = 0
|
282 |
+
|
283 |
+
for item in book.get_items():
|
284 |
+
if item.get_type() == ebooklib.ITEM_DOCUMENT:
|
285 |
+
soup = BeautifulSoup(item.get_content(), 'html.parser')
|
286 |
+
text = soup.get_text()
|
287 |
+
|
288 |
+
if text.strip():
|
289 |
+
if len(text) < 2300 and previous_filename:
|
290 |
+
with open(previous_filename, 'a', encoding='utf-8') as file:
|
291 |
+
file.write('\n' + text)
|
292 |
+
else:
|
293 |
+
previous_filename = os.path.join(directory, f"chapter_{chapter_counter}.txt")
|
294 |
+
chapter_counter += 1
|
295 |
+
with open(previous_filename, 'w', encoding='utf-8') as file:
|
296 |
+
file.write(text)
|
297 |
+
print(f"Saved chapter: {previous_filename}")
|
298 |
+
|
299 |
+
input_ebook = ebook_file_path
|
300 |
+
output_epub = os.path.join(".", "Working_files", "temp.epub")
|
301 |
+
|
302 |
+
if os.path.exists(output_epub):
|
303 |
+
os.remove(output_epub)
|
304 |
+
print(f"File {output_epub} has been removed.")
|
305 |
+
else:
|
306 |
+
print(f"The file {output_epub} does not exist.")
|
307 |
+
|
308 |
+
if convert_to_epub(input_ebook, output_epub):
|
309 |
+
save_chapters_as_text(output_epub)
|
310 |
+
|
311 |
+
nltk.download('punkt')
|
312 |
+
|
313 |
+
def process_chapter_files(folder_path, output_csv):
|
314 |
+
with open(output_csv, 'w', newline='', encoding='utf-8') as csvfile:
|
315 |
+
writer = csv.writer(csvfile)
|
316 |
+
writer.writerow(['Text', 'Start Location', 'End Location', 'Is Quote', 'Speaker', 'Chapter'])
|
317 |
+
|
318 |
+
chapter_files = sorted(os.listdir(folder_path), key=lambda x: int(x.split('_')[1].split('.')[0]))
|
319 |
+
for filename in chapter_files:
|
320 |
+
if filename.startswith('chapter_') and filename.endswith('.txt'):
|
321 |
+
chapter_number = int(filename.split('_')[1].split('.')[0])
|
322 |
+
file_path = os.path.join(folder_path, filename)
|
323 |
+
|
324 |
+
try:
|
325 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
326 |
+
text = file.read()
|
327 |
+
if text:
|
328 |
+
text = "NEWCHAPTERABC" + text
|
329 |
+
sentences = nltk.tokenize.sent_tokenize(text)
|
330 |
+
for sentence in sentences:
|
331 |
+
start_location = text.find(sentence)
|
332 |
+
end_location = start_location + len(sentence)
|
333 |
+
writer.writerow([sentence, start_location, end_location, 'True', 'Narrator', chapter_number])
|
334 |
+
except Exception as e:
|
335 |
+
print(f"Error processing file {filename}: {e}")
|
336 |
+
|
337 |
+
folder_path = os.path.join(".", "Working_files", "temp_ebook")
|
338 |
+
output_csv = os.path.join(".", "Working_files", "Book", "Other_book.csv")
|
339 |
+
|
340 |
+
process_chapter_files(folder_path, output_csv)
|
341 |
+
|
342 |
+
def sort_key(filename):
|
343 |
+
match = re.search(r'chapter_(\d+)\.txt', filename)
|
344 |
+
return int(match.group(1)) if match else 0
|
345 |
+
|
346 |
+
def combine_chapters(input_folder, output_file):
|
347 |
+
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
348 |
+
|
349 |
+
files = [f for f in os.listdir(input_folder) if f.endswith('.txt')]
|
350 |
+
sorted_files = sorted(files, key=sort_key)
|
351 |
+
|
352 |
+
with open(output_file, 'w', encoding='utf-8') as outfile:
|
353 |
+
for i, filename in enumerate(sorted_files):
|
354 |
+
with open(os.path.join(input_folder, filename), 'r', encoding='utf-8') as infile:
|
355 |
+
outfile.write(infile.read())
|
356 |
+
if i < len(sorted_files) - 1:
|
357 |
+
outfile.write("\nNEWCHAPTERABC\n")
|
358 |
+
|
359 |
+
input_folder = os.path.join(".", 'Working_files', 'temp_ebook')
|
360 |
+
output_file = os.path.join(".", 'Working_files', 'Book', 'Chapter_Book.txt')
|
361 |
+
|
362 |
+
combine_chapters(input_folder, output_file)
|
363 |
+
ensure_directory(os.path.join(".", "Working_files", "Book"))
|
364 |
+
|
365 |
+
# Function to combine WAV files
|
366 |
+
def combine_wav_files(input_directory, output_directory, file_name):
|
367 |
+
os.makedirs(output_directory, exist_ok=True)
|
368 |
+
output_file_path = os.path.join(output_directory, file_name)
|
369 |
+
combined_audio = AudioSegment.empty()
|
370 |
+
input_file_paths = sorted(
|
371 |
+
[os.path.join(input_directory, f) for f in os.listdir(input_directory) if f.endswith(".wav")],
|
372 |
+
key=lambda f: int(''.join(filter(str.isdigit, f)))
|
373 |
+
)
|
374 |
+
for input_file_path in input_file_paths:
|
375 |
+
audio_segment = AudioSegment.from_wav(input_file_path)
|
376 |
+
combined_audio += audio_segment
|
377 |
+
combined_audio.export(output_file_path, format='wav')
|
378 |
+
print(f"Combined audio saved to {output_file_path}")
|
379 |
+
|
380 |
+
# Function to split long sentences
|
381 |
+
def split_long_sentence(sentence, max_length=249, max_pauses=10):
|
382 |
+
parts = []
|
383 |
+
while len(sentence) > max_length or sentence.count(',') + sentence.count(';') + sentence.count('.') > max_pauses:
|
384 |
+
possible_splits = [i for i, char in enumerate(sentence) if char in ',;.' and i < max_length]
|
385 |
+
if possible_splits:
|
386 |
+
split_at = possible_splits[-1] + 1
|
387 |
+
else:
|
388 |
+
split_at = max_length
|
389 |
+
parts.append(sentence[:split_at].strip())
|
390 |
+
sentence = sentence[split_at:].strip()
|
391 |
+
parts.append(sentence)
|
392 |
+
return parts
|
393 |
+
|
394 |
+
# Function to convert chapters to audio using custom model
|
395 |
+
def convert_chapters_to_audio_custom_model(chapters_dir, output_audio_dir, target_voice_path=None, language=None, custom_model=None):
|
396 |
+
if target_voice_path is None:
|
397 |
+
target_voice_path = default_target_voice_path
|
398 |
+
if custom_model:
|
399 |
+
print("Loading custom model...")
|
400 |
+
config = XttsConfig()
|
401 |
+
config.load_json(custom_model['config'])
|
402 |
+
model = Xtts.init_from_config(config)
|
403 |
+
model.load_checkpoint(config, checkpoint_path=custom_model['model'], vocab_path=custom_model['vocab'], use_deepspeed=False)
|
404 |
+
model.device
|
405 |
+
print("Computing speaker latents...")
|
406 |
+
gpt_cond_latent, speaker_embedding = model.get_conditioning_latents(audio_path=[target_voice_path])
|
407 |
+
else:
|
408 |
+
selected_tts_model = "tts_models/multilingual/multi-dataset/xtts_v2"
|
409 |
+
tts = TTS(selected_tts_model, progress_bar=False).to(device)
|
410 |
+
|
411 |
+
if not os.path.exists(output_audio_dir):
|
412 |
+
os.makedirs(output_audio_dir)
|
413 |
+
|
414 |
+
for chapter_file in sorted(os.listdir(chapters_dir)):
|
415 |
+
if chapter_file.endswith('.txt'):
|
416 |
+
match = re.search(r"chapter_(\d+).txt", chapter_file)
|
417 |
+
if match:
|
418 |
+
chapter_num = int(match.group(1))
|
419 |
+
else:
|
420 |
+
print(f"Skipping file {chapter_file} as it does not match the expected format.")
|
421 |
+
continue
|
422 |
+
|
423 |
+
chapter_path = os.path.join(chapters_dir, chapter_file)
|
424 |
+
output_file_name = f"audio_chapter_{chapter_num}.wav"
|
425 |
+
output_file_path = os.path.join(output_audio_dir, output_file_name)
|
426 |
+
temp_audio_directory = os.path.join(".", "Working_files", "temp")
|
427 |
+
os.makedirs(temp_audio_directory, exist_ok=True)
|
428 |
+
temp_count = 0
|
429 |
+
|
430 |
+
with open(chapter_path, 'r', encoding='utf-8') as file:
|
431 |
+
chapter_text = file.read()
|
432 |
+
sentences = sent_tokenize(chapter_text, language='italian' if language == 'it' else 'english')
|
433 |
+
for sentence in tqdm(sentences, desc=f"Chapter {chapter_num}"):
|
434 |
+
fragments = split_long_sentence(sentence, max_length=249 if language == "en" else 213, max_pauses=10)
|
435 |
+
for fragment in fragments:
|
436 |
+
if fragment != "":
|
437 |
+
print(f"Generating fragment: {fragment}...")
|
438 |
+
fragment_file_path = os.path.join(temp_audio_directory, f"{temp_count}.wav")
|
439 |
+
if custom_model:
|
440 |
+
out = model.inference(fragment, language, gpt_cond_latent, speaker_embedding, temperature=0.7)
|
441 |
+
torchaudio.save(fragment_file_path, torch.tensor(out["wav"]).unsqueeze(0), 24000)
|
442 |
+
else:
|
443 |
+
speaker_wav_path = target_voice_path if target_voice_path else default_target_voice_path
|
444 |
+
language_code = language if language else default_language_code
|
445 |
+
tts.tts_to_file(text=fragment, file_path=fragment_file_path, speaker_wav=speaker_wav_path, language=language_code)
|
446 |
+
temp_count += 1
|
447 |
+
|
448 |
+
combine_wav_files(temp_audio_directory, output_audio_dir, output_file_name)
|
449 |
+
wipe_folder(temp_audio_directory)
|
450 |
+
print(f"Converted chapter {chapter_num} to audio.")
|
451 |
+
|
452 |
+
# Function to convert chapters to audio using standard model
|
453 |
+
def convert_chapters_to_audio_standard_model(chapters_dir, output_audio_dir, target_voice_path=None, language=None):
|
454 |
+
selected_tts_model = "tts_models/multilingual/multi-dataset/xtts_v2"
|
455 |
+
tts = TTS(selected_tts_model, progress_bar=False).to(device)
|
456 |
+
|
457 |
+
if not os.path.exists(output_audio_dir):
|
458 |
+
os.makedirs(output_audio_dir)
|
459 |
+
|
460 |
+
for chapter_file in sorted(os.listdir(chapters_dir)):
|
461 |
+
if chapter_file.endswith('.txt'):
|
462 |
+
match = re.search(r"chapter_(\d+).txt", chapter_file)
|
463 |
+
if match:
|
464 |
+
chapter_num = int(match.group(1))
|
465 |
+
else:
|
466 |
+
print(f"Skipping file {chapter_file} as it does not match the expected format.")
|
467 |
+
continue
|
468 |
+
|
469 |
+
chapter_path = os.path.join(chapters_dir, chapter_file)
|
470 |
+
output_file_name = f"audio_chapter_{chapter_num}.wav"
|
471 |
+
output_file_path = os.path.join(output_audio_dir, output_file_name)
|
472 |
+
temp_audio_directory = os.path.join(".", "Working_files", "temp")
|
473 |
+
os.makedirs(temp_audio_directory, exist_ok=True)
|
474 |
+
temp_count = 0
|
475 |
+
|
476 |
+
with open(chapter_path, 'r', encoding='utf-8') as file:
|
477 |
+
chapter_text = file.read()
|
478 |
+
sentences = sent_tokenize(chapter_text, language='italian' if language == 'it' else 'english')
|
479 |
+
for sentence in tqdm(sentences, desc=f"Chapter {chapter_num}"):
|
480 |
+
fragments = split_long_sentence(sentence, max_length=249 if language == "en" else 213, max_pauses=10)
|
481 |
+
for fragment in fragments:
|
482 |
+
if fragment != "":
|
483 |
+
print(f"Generating fragment: {fragment}...")
|
484 |
+
fragment_file_path = os.path.join(temp_audio_directory, f"{temp_count}.wav")
|
485 |
+
speaker_wav_path = target_voice_path if target_voice_path else default_target_voice_path
|
486 |
+
language_code = language if language else default_language_code
|
487 |
+
tts.tts_to_file(text=fragment, file_path=fragment_file_path, speaker_wav=speaker_wav_path, language=language_code)
|
488 |
+
temp_count += 1
|
489 |
+
|
490 |
+
combine_wav_files(temp_audio_directory, output_audio_dir, output_file_name)
|
491 |
+
wipe_folder(temp_audio_directory)
|
492 |
+
print(f"Converted chapter {chapter_num} to audio.")
|
493 |
+
|
494 |
+
# Function to handle the processing of an eBook to an audiobook
|
495 |
+
def process_request(ebook_file, target_voice, language, email, use_custom_model, custom_model):
|
496 |
+
working_files = os.path.join(".", "Working_files", "temp_ebook")
|
497 |
+
full_folder_working_files = os.path.join(".", "Working_files")
|
498 |
+
chapters_directory = os.path.join(".", "Working_files", "temp_ebook")
|
499 |
+
output_audio_directory = os.path.join(".", 'Chapter_wav_files')
|
500 |
+
remove_folder_with_contents(full_folder_working_files)
|
501 |
+
remove_folder_with_contents(output_audio_directory)
|
502 |
+
|
503 |
+
create_chapter_labeled_book(ebook_file.name)
|
504 |
+
audiobook_output_path = os.path.join(".", "Audiobooks")
|
505 |
+
|
506 |
+
if use_custom_model:
|
507 |
+
convert_chapters_to_audio_custom_model(chapters_directory, output_audio_directory, target_voice, language, custom_model)
|
508 |
+
else:
|
509 |
+
convert_chapters_to_audio_standard_model(chapters_directory, output_audio_directory, target_voice, language)
|
510 |
+
|
511 |
+
create_m4b_from_chapters(output_audio_directory, ebook_file.name, audiobook_output_path)
|
512 |
+
|
513 |
+
m4b_filepath = os.path.join(audiobook_output_path, os.path.splitext(os.path.basename(ebook_file.name))[0] + '.m4b')
|
514 |
+
|
515 |
+
# Upload the final audiobook to file.io
|
516 |
+
with open(m4b_filepath, 'rb') as f:
|
517 |
+
response = requests.post('https://file.io', files={'file': f})
|
518 |
+
download_link = response.json().get('link', '')
|
519 |
+
|
520 |
+
# Send the download link to the user's email
|
521 |
+
if email and download_link:
|
522 |
+
send_email(email, download_link)
|
523 |
+
|
524 |
+
return download_link
|
525 |
+
|
526 |
+
# Function to manage the queue and process each request sequentially
|
527 |
+
def handle_queue():
|
528 |
+
while True:
|
529 |
+
ebook_file, target_voice, language, email, use_custom_model, custom_model = queue.get()
|
530 |
+
process_request(ebook_file, target_voice, language, email, use_custom_model, custom_model)
|
531 |
+
queue.task_done()
|
532 |
+
|
533 |
+
# Start the queue handler thread
|
534 |
+
thread = Thread(target=handle_queue, daemon=True)
|
535 |
+
thread.start()
|
536 |
+
|
537 |
+
# Gradio function to add a request to the queue
|
538 |
+
def enqueue_request(ebook_file, target_voice_file, language, email, use_custom_model, custom_model_file, custom_config_file, custom_vocab_file, custom_model_url=None):
|
539 |
+
target_voice = target_voice_file.name if target_voice_file else None
|
540 |
+
custom_model = None
|
541 |
+
|
542 |
+
if use_custom_model and custom_model_file and custom_config_file and custom_vocab_file:
|
543 |
+
custom_model = {
|
544 |
+
'model': custom_model_file.name,
|
545 |
+
'config': custom_config_file.name,
|
546 |
+
'vocab': custom_vocab_file.name
|
547 |
+
}
|
548 |
+
if use_custom_model and custom_model_url:
|
549 |
+
download_dir = os.path.join(".", "Working_files", "custom_model")
|
550 |
+
download_and_extract_zip(custom_model_url, download_dir)
|
551 |
+
custom_model = {
|
552 |
+
'model': os.path.join(download_dir, 'model.pth'),
|
553 |
+
'config': os.path.join(download_dir, 'config.json'),
|
554 |
+
'vocab': os.path.join(download_dir, 'vocab.json_')
|
555 |
+
}
|
556 |
+
|
557 |
+
# Add request to the queue
|
558 |
+
queue_lock.acquire()
|
559 |
+
queue.put((ebook_file, target_voice, language, email, use_custom_model, custom_model))
|
560 |
+
position = queue.qsize()
|
561 |
+
queue_lock.release()
|
562 |
+
return f"Your request has been added to the queue. You are number {position} in line."
|
563 |
+
|
564 |
+
# Gradio UI setup
|
565 |
+
language_options = [
|
566 |
+
"en", "es", "fr", "de", "it", "pt", "pl", "tr", "ru", "nl", "cs", "ar", "zh-cn", "ja", "hu", "ko"
|
567 |
+
]
|
568 |
+
|
569 |
+
theme = gr.themes.Soft(
|
570 |
+
primary_hue="blue",
|
571 |
+
secondary_hue="blue",
|
572 |
+
neutral_hue="blue",
|
573 |
+
text_size=gr.themes.sizes.text_md,
|
574 |
+
)
|
575 |
+
|
576 |
+
with gr.Blocks(theme=theme) as demo:
|
577 |
+
gr.Markdown(
|
578 |
+
"""
|
579 |
+
# eBook to Audiobook Converter
|
580 |
+
|
581 |
+
Transform your eBooks into immersive audiobooks with optional custom TTS models.
|
582 |
+
"""
|
583 |
+
)
|
584 |
+
|
585 |
+
with gr.Row():
|
586 |
+
with gr.Column(scale=3):
|
587 |
+
ebook_file = gr.File(label="eBook File")
|
588 |
+
target_voice_file = gr.File(label="Target Voice File (Optional)")
|
589 |
+
language = gr.Dropdown(label="Language", choices=language_options, value="en")
|
590 |
+
email = gr.Textbox(label="Email Address")
|
591 |
+
|
592 |
+
with gr.Column(scale=3):
|
593 |
+
use_custom_model = gr.Checkbox(label="Use Custom Model")
|
594 |
+
custom_model_file = gr.File(label="Custom Model File (Optional)", visible=False)
|
595 |
+
custom_config_file = gr.File(label="Custom Config File (Optional)", visible=False)
|
596 |
+
custom_vocab_file = gr.File(label="Custom Vocab File (Optional)", visible=False)
|
597 |
+
custom_model_url = gr.Textbox(label="Custom Model Zip URL (Optional)", visible=False)
|
598 |
+
|
599 |
+
convert_btn = gr.Button("Convert to Audiobook", variant="primary")
|
600 |
+
queue_status = gr.Textbox(label="Queue Status")
|
601 |
+
|
602 |
+
convert_btn.click(
|
603 |
+
enqueue_request,
|
604 |
+
inputs=[ebook_file, target_voice_file, language, email, use_custom_model, custom_model_file, custom_config_file, custom_vocab_file, custom_model_url],
|
605 |
+
outputs=[queue_status]
|
606 |
+
)
|
607 |
+
|
608 |
+
use_custom_model.change(
|
609 |
+
lambda x: [gr.update(visible=x)] * 4,
|
610 |
+
inputs=[use_custom_model],
|
611 |
+
outputs=[custom_model_file, custom_config_file, custom_vocab_file, custom_model_url]
|
612 |
+
)
|
613 |
+
|
614 |
+
demo.launch(share=True)
|
legacy/install.bat
ADDED
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
@echo off
|
2 |
+
:: Check for administrative privileges
|
3 |
+
net session >nul 2>&1
|
4 |
+
if %errorLevel% neq 0 (
|
5 |
+
echo This script requires administrator privileges.
|
6 |
+
echo Switching to administrator...
|
7 |
+
|
8 |
+
powershell -Command "Start-Process cmd -ArgumentList '/c', '%~dpnx0' -Verb runAs"
|
9 |
+
exit /b
|
10 |
+
)
|
11 |
+
|
12 |
+
:: If already elevated, continue the script
|
13 |
+
echo Running with administrator privileges...
|
14 |
+
|
15 |
+
:: Run the PowerShell script in the same directory as this batch file
|
16 |
+
powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0install.ps1"
|
17 |
+
|
18 |
+
pause
|
legacy/install.ps1
ADDED
@@ -0,0 +1,255 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Function to check if the script is running as Administrator
|
2 |
+
function Test-IsAdmin {
|
3 |
+
$currentUser = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
|
4 |
+
return $currentUser.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
|
5 |
+
}
|
6 |
+
|
7 |
+
# If the script is not running as Administrator, restart it with elevated privileges
|
8 |
+
if (-not (Test-IsAdmin)) {
|
9 |
+
Write-Host "This script requires administrative privileges. Restarting as Administrator..." -ForegroundColor Yellow
|
10 |
+
Start-Process powershell.exe -ArgumentList "-NoProfile", "-ExecutionPolicy RemoteSigned", "-File", "`"$PSCommandPath`" $Params" -Verb RunAs
|
11 |
+
exit
|
12 |
+
}
|
13 |
+
|
14 |
+
################# Main script starts here with admin privileges #################
|
15 |
+
|
16 |
+
# Function to check if Conda is installed
|
17 |
+
function Check-CondaInstalled {
|
18 |
+
Write-Host "Checking if Conda is installed..."
|
19 |
+
$condaPath = (Get-Command conda -ErrorAction SilentlyContinue).Source
|
20 |
+
if ($condaPath) {
|
21 |
+
Write-Host "Conda is already installed at: $condaPath"
|
22 |
+
return $true
|
23 |
+
} else {
|
24 |
+
Write-Host "Conda is not installed."
|
25 |
+
return $false
|
26 |
+
}
|
27 |
+
}
|
28 |
+
|
29 |
+
function Check-ProgramsInstalled {
|
30 |
+
param (
|
31 |
+
[string[]]$Programs
|
32 |
+
)
|
33 |
+
|
34 |
+
$programsMissing = @()
|
35 |
+
|
36 |
+
if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
|
37 |
+
Write-Host "Chocolatey is not installed. Installing Chocolatey..."
|
38 |
+
Set-ExecutionPolicy Bypass -Scope Process -Force
|
39 |
+
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
|
40 |
+
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
|
41 |
+
|
42 |
+
if (-not (Get-Command choco -ErrorAction SilentlyContinue)) {
|
43 |
+
return $true
|
44 |
+
} else {
|
45 |
+
Write-Host "Chocolatey installed successfully."
|
46 |
+
}
|
47 |
+
}
|
48 |
+
|
49 |
+
foreach ($program in $Programs) {
|
50 |
+
if (Get-Command $program -ErrorAction SilentlyContinue) {
|
51 |
+
Write-Host "$program is installed."
|
52 |
+
} else {
|
53 |
+
$programsMissing += $program
|
54 |
+
}
|
55 |
+
}
|
56 |
+
|
57 |
+
$missingCount = $programsMissing.Count
|
58 |
+
|
59 |
+
if ($missingCount -eq 0) {
|
60 |
+
return $true
|
61 |
+
} else {
|
62 |
+
$installedCount = 0
|
63 |
+
foreach ($program in $programsMissing) {
|
64 |
+
if ($program -eq "ffmpeg") {
|
65 |
+
Write-Host "Installing ffmpeg..."
|
66 |
+
choco install ffmpeg -y
|
67 |
+
|
68 |
+
if (Get-Command ffmpeg -ErrorAction SilentlyContinue) {
|
69 |
+
Write-Host "ffmpeg installed successfully!"
|
70 |
+
$installedCount += 1
|
71 |
+
}
|
72 |
+
} elseif ($program -eq "calibre") {
|
73 |
+
# Avoid conflict with calibre built-in lxml
|
74 |
+
pip uninstall lxml -y
|
75 |
+
|
76 |
+
# Install Calibre using Chocolatey
|
77 |
+
Write-Host "Installing Calibre..."
|
78 |
+
choco install calibre -y
|
79 |
+
|
80 |
+
# Verify Calibre installation
|
81 |
+
if (Get-Command calibre -ErrorAction SilentlyContinue) {
|
82 |
+
Write-Host "Calibre installed successfully!"
|
83 |
+
$installedCount += 1
|
84 |
+
}
|
85 |
+
}
|
86 |
+
}
|
87 |
+
}
|
88 |
+
if ($installedCount -eq $countMissing) {
|
89 |
+
return $false
|
90 |
+
}
|
91 |
+
return $true
|
92 |
+
}
|
93 |
+
|
94 |
+
# Function to check if Docker is installed and running
|
95 |
+
function Check-Docker {
|
96 |
+
Write-Host "Checking if Docker is installed..."
|
97 |
+
$dockerPath = (Get-Command docker -ErrorAction SilentlyContinue).Source
|
98 |
+
if ($dockerPath) {
|
99 |
+
Write-Host "Docker is installed at: $dockerPath"
|
100 |
+
# Check if Docker service is running
|
101 |
+
$dockerStatus = (Get-Service -Name com.docker.service -ErrorAction SilentlyContinue).Status
|
102 |
+
if ($dockerStatus -eq 'Running') {
|
103 |
+
Write-Host "Docker service is running."
|
104 |
+
return $true
|
105 |
+
} else {
|
106 |
+
Write-Host "Docker service is installed but not running. Attempting to start Docker service..."
|
107 |
+
Start-Service -Name "com.docker.service" -ErrorAction SilentlyContinue
|
108 |
+
|
109 |
+
# Wait for Docker service to start
|
110 |
+
while ((Get-Service -Name "com.docker.service").Status -ne 'Running') {
|
111 |
+
Write-Host "Waiting for Docker service to start..."
|
112 |
+
Start-Sleep -Seconds 5
|
113 |
+
}
|
114 |
+
Write-Host "Docker service is now running."
|
115 |
+
return $true
|
116 |
+
}
|
117 |
+
} else {
|
118 |
+
Write-Host "Docker is not installed."
|
119 |
+
return $false
|
120 |
+
}
|
121 |
+
}
|
122 |
+
|
123 |
+
######### Miniconda installation
|
124 |
+
|
125 |
+
$minicondaUrl = "https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe"
|
126 |
+
$installerPath = "$env:TEMP\Miniconda3-latest-Windows-x86_64.exe"
|
127 |
+
|
128 |
+
if (-not (Check-CondaInstalled)) {
|
129 |
+
# Check if the Miniconda installer already exists
|
130 |
+
if (-not (Test-Path $installerPath)) {
|
131 |
+
Write-Host "Downloading Miniconda installer..."
|
132 |
+
Invoke-WebRequest -Uri $minicondaUrl -OutFile $installerPath
|
133 |
+
} else {
|
134 |
+
Write-Host "Miniconda installer already exists at $installerPath. Skipping download."
|
135 |
+
}
|
136 |
+
|
137 |
+
# Set the installation path for Miniconda
|
138 |
+
$installPath = "C:\Miniconda3"
|
139 |
+
|
140 |
+
Write-Host "Installing Miniconda..."
|
141 |
+
Start-Process -FilePath $installerPath -ArgumentList "/InstallationType=JustMe", "/RegisterPython=0", "/AddToPath=1", "/S", "/D=$installPath" -NoNewWindow -Wait
|
142 |
+
|
143 |
+
Write-Host "Verifying Miniconda installation..."
|
144 |
+
& "$installPath\Scripts\conda.exe" --version
|
145 |
+
Write-Host "Miniconda installation complete."
|
146 |
+
} else {
|
147 |
+
Write-Host "Skipping Miniconda installation."
|
148 |
+
}
|
149 |
+
|
150 |
+
######### Docker installation
|
151 |
+
|
152 |
+
$dockerMsiUrl = "https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe"
|
153 |
+
$dockerInstallerPath = "$env:TEMP\DockerInstaller.exe"
|
154 |
+
|
155 |
+
$dockerUtilsNeeded = Check-ProgramsInstalled -Programs @("ffmpeg", "calibre")
|
156 |
+
|
157 |
+
if ($dockerUtilsNeeded) {
|
158 |
+
if (-not (Check-Docker)) {
|
159 |
+
# Verify the installer file or re-download if corrupted or missing
|
160 |
+
if (-not (Test-Path $dockerInstallerPath)) {
|
161 |
+
Write-Host "Downloading Docker installer for Windows..."
|
162 |
+
Invoke-WebRequest -Uri $dockerMsiUrl -OutFile $dockerInstallerPath
|
163 |
+
}
|
164 |
+
|
165 |
+
# Launch the Docker installer
|
166 |
+
Write-Host "Launching Docker installer..."
|
167 |
+
Start-Process -FilePath $dockerInstallerPath
|
168 |
+
Write-Host "Please complete the Docker installation manually."
|
169 |
+
pause
|
170 |
+
|
171 |
+
# Ensure Docker service is running after installation
|
172 |
+
Write-Host "Ensuring Docker service is running..."
|
173 |
+
Start-Service -Name "com.docker.service" -ErrorAction SilentlyContinue
|
174 |
+
|
175 |
+
# Wait for Docker service to start
|
176 |
+
while ((Get-Service -Name "com.docker.service").Status -ne 'Running') {
|
177 |
+
Write-Host "Waiting for Docker service to start..."
|
178 |
+
Start-Sleep -Seconds 5
|
179 |
+
}
|
180 |
+
|
181 |
+
Write-Host "Docker service is now running."
|
182 |
+
}
|
183 |
+
}
|
184 |
+
|
185 |
+
######### Install ebook2audiobook
|
186 |
+
|
187 |
+
if (Check-CondaInstalled) {
|
188 |
+
|
189 |
+
Write-Host "Installing ebook2audiobook..." -ForegroundColor Yellow
|
190 |
+
|
191 |
+
# Set the working directory to the script's directory
|
192 |
+
$scriptDir = $PSScriptRoot
|
193 |
+
Set-Location -Path $scriptDir
|
194 |
+
|
195 |
+
# Create new Conda environment with Python 3.11 in the script directory, showing progress
|
196 |
+
Write-Host "Creating Conda environment with Python 3.11 in $scriptDir..."
|
197 |
+
& conda create --prefix "$scriptDir\python_env" python=3.11 -y -v
|
198 |
+
|
199 |
+
# Ensure the correct Python environment is active
|
200 |
+
Write-Host "Checking Python version in Conda environment..."
|
201 |
+
|
202 |
+
# Get python.exe version from python_env
|
203 |
+
$pythonEnvVersion = & "$scriptDir\python_env\python.exe" --version
|
204 |
+
|
205 |
+
# Get the Conda-managed Python version using conda run
|
206 |
+
$pythonVersion = & conda run --prefix "$scriptDir\python_env" python --version
|
207 |
+
|
208 |
+
if ($pythonVersion.Trim() -eq $pythonEnvVersion.Trim()) {
|
209 |
+
Write-Host "Python versions match, proceeding with installation..."
|
210 |
+
|
211 |
+
if ($dockerUtilsNeeded) {
|
212 |
+
# Build Docker image for utils
|
213 |
+
Write-Host "Building Docker image for utils..."
|
214 |
+
& conda run --prefix "$scriptDir\python_env" docker build -f DockerfileUtils -t utils .
|
215 |
+
}
|
216 |
+
|
217 |
+
# Install required Python packages with pip, showing progress
|
218 |
+
Write-Host "Installing required Python packages..."
|
219 |
+
& conda run --prefix "$scriptDir\python_env" python.exe -m pip install --upgrade pip --progress-bar on -v
|
220 |
+
& conda run --prefix "$scriptDir\python_env" pip install pydub nltk beautifulsoup4 ebooklib translate coqui-tts tqdm mecab mecab-python3 unidic gradio>=4.44.0 docker --progress-bar on -v
|
221 |
+
|
222 |
+
# Download unidic language model for MeCab with progress
|
223 |
+
Write-Host "Downloading unidic language model for MeCab..."
|
224 |
+
& conda run --prefix "$scriptDir\python_env" python.exe -m unidic download
|
225 |
+
|
226 |
+
# Download spacy NLP model with progress
|
227 |
+
Write-Host "Downloading spaCy language model..."
|
228 |
+
& conda run --prefix "$scriptDir\python_env" python.exe -m spacy download en_core_web_sm
|
229 |
+
|
230 |
+
# Install ebook2audiobook
|
231 |
+
Write-Host "Installing ebook2audiobook..."
|
232 |
+
& conda run --prefix "$scriptDir\python_env" pip install -e .
|
233 |
+
|
234 |
+
# Delete Docker and Miniconda installers if both are installed and running
|
235 |
+
if ((Check-CondaInstalled) -and (Check-Docker)) {
|
236 |
+
Write-Host "Both Conda and Docker are installed and running. Deleting installer files..."
|
237 |
+
Remove-Item -Path $installerPath -Force -ErrorAction SilentlyContinue
|
238 |
+
Remove-Item -Path $dockerInstallerPath -Force -ErrorAction SilentlyContinue
|
239 |
+
Write-Host "Installer files deleted."
|
240 |
+
}
|
241 |
+
|
242 |
+
Write-Host "******************* ebook2audiobook installation successful! *******************" -ForegroundColor Green
|
243 |
+
Write-Host "To launch ebook2audiobook:" -ForegroundColor Yellow
|
244 |
+
Write-Host "- in command line mode: ./ebook2audiobook.cmd --headless [other options]"
|
245 |
+
Write-Host "- in graphic web mode: ./ebook2audiobook.cmd [--share]"
|
246 |
+
} else {
|
247 |
+
Write-Host "The python terminal is still using the OS python version $pythonVersion, but it should be $pythonEnvVersion from the python_env virtual environment"
|
248 |
+
}
|
249 |
+
|
250 |
+
# Deactivate Conda environment
|
251 |
+
Write-Host "Deactivating Conda environment..."
|
252 |
+
& conda deactivate
|
253 |
+
} else {
|
254 |
+
Write-Host "Installation cannot proceed. Either Conda is not installed or Docker is not running." -ForegroundColor Red
|
255 |
+
}
|
legacy/install.sh
ADDED
@@ -0,0 +1,171 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env bash
|
2 |
+
|
3 |
+
WGET=$(which wget 2>/dev/null)
|
4 |
+
CONDA_VERSION=$(conda --version 2>/dev/null)
|
5 |
+
DOCKER_UTILS=$(which docker 2>/dev/null)
|
6 |
+
DOCKER_UTILS_NEEDED=false
|
7 |
+
PACK_MGR=""
|
8 |
+
PACK_MGR_OPTIONS=""
|
9 |
+
|
10 |
+
if [[ "$OSTYPE" == "darwin"* ]]; then
|
11 |
+
PACK_MGR="brew install"
|
12 |
+
elif command -v emerge &> /dev/null; then
|
13 |
+
PACK_MGR="sudo emerge"
|
14 |
+
elif command -v dnf &> /dev/null; then
|
15 |
+
PACK_MGR="sudo dnf install"
|
16 |
+
PACK_MGR_OPTIONS="-y"
|
17 |
+
elif command -v yum &> /dev/null; then
|
18 |
+
PACK_MGR="sudo yum install"
|
19 |
+
PACK_MGR_OPTIONS="-y"
|
20 |
+
elif command -v zypper &> /dev/null; then
|
21 |
+
PACK_MGR="sudo zypper install"
|
22 |
+
PACK_MGR_OPTIONS="-y"
|
23 |
+
elif command -v pacman &> /dev/null; then
|
24 |
+
PACK_MGR="sudo pacman -Sy"
|
25 |
+
elif command -v apt-get &> /dev/null; then
|
26 |
+
sudo apt-get update
|
27 |
+
PACK_MGR="sudo apt-get install"
|
28 |
+
PACK_MGR_OPTIONS="-y"
|
29 |
+
elif command -v apk &> /dev/null; then
|
30 |
+
PACK_MGR="sudo apk add"
|
31 |
+
fi
|
32 |
+
|
33 |
+
check_programs_installed() {
|
34 |
+
local programs=("$@")
|
35 |
+
declare -a programs_missing
|
36 |
+
|
37 |
+
for program in "${programs[@]}"; do
|
38 |
+
if command -v "$program" >/dev/null 2>&1; then
|
39 |
+
echo "$program is installed."
|
40 |
+
else
|
41 |
+
echo "$program is not installed."
|
42 |
+
programs_missing+=($program)
|
43 |
+
fi
|
44 |
+
done
|
45 |
+
|
46 |
+
local count=${#programs_missing[@]}
|
47 |
+
|
48 |
+
if [[ $count -eq 0 || "$PKG_MGR" = "" ]]; then
|
49 |
+
DOCKER_UTILS_NEEDED=true
|
50 |
+
else
|
51 |
+
for program in "${programs_missing[@]}"; do
|
52 |
+
if [ "$program" = "ffmpeg" ];then
|
53 |
+
eval "$PKG_MGR ffmpeg $PKG_MGR_OPTIONS"
|
54 |
+
if command -v ffmpeg >/dev/null 2>&1; then
|
55 |
+
echo "FFmpeg installed successfully!"
|
56 |
+
else
|
57 |
+
echo "FFmpeg installation failed."
|
58 |
+
DOCKER_UTILS_NEEDED=true
|
59 |
+
break
|
60 |
+
fi
|
61 |
+
elif [ "$program" = "calibre" ];then
|
62 |
+
# avoid conflict with calibre builtin lxml
|
63 |
+
pip uninstall lxml -y 2>/dev/null
|
64 |
+
|
65 |
+
if [[ "$OSTYPE" == "Linux" ]]; then
|
66 |
+
echo "Installing Calibre for Linux..."
|
67 |
+
$WGET -nv -O- https://download.calibre-ebook.com/linux-installer.sh | sh /dev/stdin
|
68 |
+
elif [[ "$OSTYPE" == "Darwin"* ]]; then
|
69 |
+
echo "Installing Calibre for macOS using Homebrew..."
|
70 |
+
eval "$PACK_MGR --cask calibre"
|
71 |
+
fi
|
72 |
+
|
73 |
+
if command -v calibre >/dev/null 2>&1; then
|
74 |
+
echo "Calibre installed successfully!"
|
75 |
+
else
|
76 |
+
echo "Calibre installation failed."
|
77 |
+
fi
|
78 |
+
fi
|
79 |
+
done
|
80 |
+
fi
|
81 |
+
}
|
82 |
+
|
83 |
+
# Check for Homebrew on macOS
|
84 |
+
if [[ "$OSTYPE" == "darwin"* ]]; then
|
85 |
+
echo "Detected macOS."
|
86 |
+
if ! command -v brew &> /dev/null; then
|
87 |
+
echo "Homebrew is not installed. Installing Homebrew..."
|
88 |
+
/usr/bin/env bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
|
89 |
+
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zprofile
|
90 |
+
eval "$(/opt/homebrew/bin/brew shellenv)"
|
91 |
+
fi
|
92 |
+
fi
|
93 |
+
|
94 |
+
if [ -z "$WGET" ]; then
|
95 |
+
echo -e "\e[33m wget is missing! trying to install it... \e[0m"
|
96 |
+
if [ "$PACK_MGR" != "" ]; then
|
97 |
+
eval "$PACK_MGR wget $PACK_MGR_OPTIONS"
|
98 |
+
else
|
99 |
+
echo "Cannot recognize your package manager. Please install wget manually."
|
100 |
+
fi
|
101 |
+
WGET=$(which wget 2>/dev/null)
|
102 |
+
fi
|
103 |
+
|
104 |
+
if [[ -n "$WGET" && -z "$CONDA_VERSION" ]]; then
|
105 |
+
echo -e "\e[33m conda is missing! trying to install it... \e[0m"
|
106 |
+
|
107 |
+
if [[ "$OSTYPE" == "darwin"* ]]; then
|
108 |
+
$WGET https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh -O Miniconda3-latest.sh
|
109 |
+
else
|
110 |
+
$WGET https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O Miniconda3-latest.sh
|
111 |
+
fi
|
112 |
+
|
113 |
+
chmod +x Miniconda3-latest.sh
|
114 |
+
./Miniconda3-latest.sh -b -u && \
|
115 |
+
~/miniconda3/bin/conda init && \
|
116 |
+
rm -f Miniconda3-latest.sh
|
117 |
+
|
118 |
+
# Source the appropriate shell configuration file
|
119 |
+
SHELL_RC=~/miniconda3/etc/profile.d/conda.sh
|
120 |
+
source $SHELL_RC
|
121 |
+
|
122 |
+
CONDA_VERSION=$(conda --version 2>/dev/null)
|
123 |
+
echo -e "\e[32m===============>>> conda is installed! <<===============\e[0m"
|
124 |
+
fi
|
125 |
+
|
126 |
+
check_programs_installed()
|
127 |
+
|
128 |
+
if [ $DOCKER_UTILS_NEEDED = true ]; then
|
129 |
+
if [[ -n "$WGET" && -z "$DOCKER_UTILS" ]]; then
|
130 |
+
echo -e "\e[33m docker is missing! trying to install it... \e[0m"
|
131 |
+
if [[ "$OSTYPE" == "darwin"* ]]; then
|
132 |
+
echo "Installing Docker using Homebrew..."
|
133 |
+
brew install --cask docker
|
134 |
+
else
|
135 |
+
$WGET -qO get-docker.sh https://get.docker.com && \
|
136 |
+
sudo sh get-docker.sh && \
|
137 |
+
sudo systemctl start docker && \
|
138 |
+
sudo systemctl enable docker && \
|
139 |
+
docker run hello-world && \
|
140 |
+
DOCKER_UTILS=$(which docker 2>/dev/null)
|
141 |
+
rm -f get-docker.sh
|
142 |
+
fi
|
143 |
+
echo -e "\e[32m===============>>> docker is installed! <<===============\e[0m"
|
144 |
+
fi
|
145 |
+
fi
|
146 |
+
|
147 |
+
if [[ -n "$WGET" && -n "$CONDA_VERSION" ]]; then
|
148 |
+
SHELL_RC=~/miniconda3/etc/profile.d/conda.sh
|
149 |
+
echo -e "\e[33m Installing ebook2audiobook... \e[0m"
|
150 |
+
if [ $DOCKER_UTILS_NEEDED = true ]; then
|
151 |
+
conda create --prefix $(pwd)/python_env python=3.11 -y
|
152 |
+
source $SHELL_RC
|
153 |
+
conda activate $(pwd)/python_env
|
154 |
+
$DOCKER_UTILS build -f DockerfileUtils -t utils .
|
155 |
+
fi
|
156 |
+
pip install --upgrade pip && \
|
157 |
+
pip install pydub nltk beautifulsoup4 ebooklib translate coqui-tts tqdm mecab mecab-python3 unidic gradio>=4.44.0 docker && \
|
158 |
+
python -m unidic download && \
|
159 |
+
python -m spacy download en_core_web_sm && \
|
160 |
+
pip install -e .
|
161 |
+
if [ $DOCKER_UTILS_NEEDED = true ]; then
|
162 |
+
conda deactivate
|
163 |
+
conda deactivate
|
164 |
+
fi
|
165 |
+
echo -e "\e[32m******************* ebook2audiobook installation successful! *******************\e[0m"
|
166 |
+
echo -e "\e[33mTo launch ebook2audiobook:\e[0m"
|
167 |
+
echo -e "- in command line mode: ./ebook2audiobook.sh --headless [other options]"
|
168 |
+
echo -e "- in graphic web mode: ./ebook2audiobook.sh [--share]"
|
169 |
+
fi
|
170 |
+
|
171 |
+
exit 0
|