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
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 |
+
from tqdm import tqdm
|
15 |
+
import gradio as gr
|
16 |
+
from gradio import Progress
|
17 |
+
import urllib.request
|
18 |
+
import zipfile
|
19 |
+
nltk.download('punkt_tab')
|
20 |
+
#device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
21 |
+
#print(f"Device selected is: {device}")
|
22 |
+
|
23 |
+
#nltk.download('punkt') # Make sure to download the necessary models
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
import os
|
28 |
+
import wave
|
29 |
+
import time
|
30 |
+
from piper import PiperVoice
|
31 |
+
|
32 |
+
def load_piper_tts(folder_name):
|
33 |
+
model_file = os.path.join(folder_name, f"{folder_name}.onnx")
|
34 |
+
config_file = os.path.join(folder_name, f"{folder_name}.json")
|
35 |
+
|
36 |
+
# Automatically rename the config file if it's named incorrectly
|
37 |
+
onnx_config_file = os.path.join(folder_name, f"{folder_name}.onnx.json")
|
38 |
+
if os.path.exists(onnx_config_file) and not os.path.exists(config_file):
|
39 |
+
os.rename(onnx_config_file, config_file)
|
40 |
+
print(f"Renamed {onnx_config_file} to {config_file}")
|
41 |
+
|
42 |
+
if not os.path.exists(model_file) or not os.path.exists(config_file):
|
43 |
+
print(f"Model file exists: {os.path.exists(model_file)}")
|
44 |
+
print(f"Config file exists: {os.path.exists(config_file)}")
|
45 |
+
print(f"Contents of {folder_name}:")
|
46 |
+
for item in os.listdir(folder_name):
|
47 |
+
print(item)
|
48 |
+
raise FileNotFoundError(f"Model or config file not found in {folder_name}.")
|
49 |
+
|
50 |
+
global voice
|
51 |
+
voice = PiperVoice.load(model_file, config_path=config_file, use_cuda=False)
|
52 |
+
print("Model loaded successfully.")
|
53 |
+
|
54 |
+
|
55 |
+
def piper_to_tts(text_to_generate, output_audio_name):
|
56 |
+
if 'voice' not in globals():
|
57 |
+
raise RuntimeError("Piper TTS model is not loaded. Please load it using load_piper_tts first.")
|
58 |
+
|
59 |
+
start_time = time.time()
|
60 |
+
|
61 |
+
with wave.open(output_audio_name, 'wb') as wav_file:
|
62 |
+
wav_file.setnchannels(1) # Assuming mono channel
|
63 |
+
wav_file.setsampwidth(2) # Assuming 16-bit samples
|
64 |
+
wav_file.setframerate(voice.config.sample_rate)
|
65 |
+
voice.synthesize(text_to_generate, wav_file)
|
66 |
+
|
67 |
+
end_time = time.time()
|
68 |
+
print(f"Audio generated and saved to {output_audio_name} in {end_time - start_time:.2f} seconds")
|
69 |
+
|
70 |
+
|
71 |
+
def download_and_extract_zip(url, extract_to='.'):
|
72 |
+
try:
|
73 |
+
# Ensure the directory exists
|
74 |
+
os.makedirs(extract_to, exist_ok=True)
|
75 |
+
|
76 |
+
zip_path = os.path.join(extract_to, 'model.zip')
|
77 |
+
|
78 |
+
# Download with progress bar
|
79 |
+
with tqdm(unit='B', unit_scale=True, miniters=1, desc="Downloading Model") as t:
|
80 |
+
def reporthook(blocknum, blocksize, totalsize):
|
81 |
+
t.total = totalsize
|
82 |
+
t.update(blocknum * blocksize - t.n)
|
83 |
+
|
84 |
+
urllib.request.urlretrieve(url, zip_path, reporthook=reporthook)
|
85 |
+
print(f"Downloaded zip file to {zip_path}")
|
86 |
+
|
87 |
+
# Unzipping with progress bar
|
88 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
89 |
+
files = zip_ref.namelist()
|
90 |
+
with tqdm(total=len(files), unit="file", desc="Extracting Files") as t:
|
91 |
+
for file in files:
|
92 |
+
if not file.endswith('/'): # Skip directories
|
93 |
+
# Extract the file to the temporary directory
|
94 |
+
extracted_path = zip_ref.extract(file, extract_to)
|
95 |
+
# Move the file to the base directory
|
96 |
+
base_file_path = os.path.join(extract_to, os.path.basename(file))
|
97 |
+
os.rename(extracted_path, base_file_path)
|
98 |
+
t.update(1)
|
99 |
+
|
100 |
+
# Cleanup: Remove the ZIP file and any empty folders
|
101 |
+
os.remove(zip_path)
|
102 |
+
for root, dirs, files in os.walk(extract_to, topdown=False):
|
103 |
+
for name in dirs:
|
104 |
+
os.rmdir(os.path.join(root, name))
|
105 |
+
print(f"Extracted files to {extract_to}")
|
106 |
+
|
107 |
+
# Check if all required files are present
|
108 |
+
required_files = ['model.pth', 'config.json', 'vocab.json_']
|
109 |
+
missing_files = [file for file in required_files if not os.path.exists(os.path.join(extract_to, file))]
|
110 |
+
|
111 |
+
if not missing_files:
|
112 |
+
print("All required files (model.pth, config.json, vocab.json_) found.")
|
113 |
+
else:
|
114 |
+
print(f"Missing files: {', '.join(missing_files)}")
|
115 |
+
|
116 |
+
except Exception as e:
|
117 |
+
print(f"Failed to download or extract zip file: {e}")
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
def is_folder_empty(folder_path):
|
122 |
+
if os.path.exists(folder_path) and os.path.isdir(folder_path):
|
123 |
+
# List directory contents
|
124 |
+
if not os.listdir(folder_path):
|
125 |
+
return True # The folder is empty
|
126 |
+
else:
|
127 |
+
return False # The folder is not empty
|
128 |
+
else:
|
129 |
+
print(f"The path {folder_path} is not a valid folder.")
|
130 |
+
return None # The path is not a valid folder
|
131 |
+
|
132 |
+
def remove_folder_with_contents(folder_path):
|
133 |
+
try:
|
134 |
+
shutil.rmtree(folder_path)
|
135 |
+
print(f"Successfully removed {folder_path} and all of its contents.")
|
136 |
+
except Exception as e:
|
137 |
+
print(f"Error removing {folder_path}: {e}")
|
138 |
+
|
139 |
+
|
140 |
+
|
141 |
+
|
142 |
+
def wipe_folder(folder_path):
|
143 |
+
# Check if the folder exists
|
144 |
+
if not os.path.exists(folder_path):
|
145 |
+
print(f"The folder {folder_path} does not exist.")
|
146 |
+
return
|
147 |
+
|
148 |
+
# Iterate over all the items in the given folder
|
149 |
+
for item in os.listdir(folder_path):
|
150 |
+
item_path = os.path.join(folder_path, item)
|
151 |
+
# If it's a file, remove it and print a message
|
152 |
+
if os.path.isfile(item_path):
|
153 |
+
os.remove(item_path)
|
154 |
+
print(f"Removed file: {item_path}")
|
155 |
+
# If it's a directory, remove it recursively and print a message
|
156 |
+
elif os.path.isdir(item_path):
|
157 |
+
shutil.rmtree(item_path)
|
158 |
+
print(f"Removed directory and its contents: {item_path}")
|
159 |
+
|
160 |
+
print(f"All contents wiped from {folder_path}.")
|
161 |
+
|
162 |
+
|
163 |
+
# Example usage
|
164 |
+
# folder_to_wipe = 'path_to_your_folder'
|
165 |
+
# wipe_folder(folder_to_wipe)
|
166 |
+
|
167 |
+
|
168 |
+
def create_m4b_from_chapters(input_dir, ebook_file, output_dir):
|
169 |
+
# Function to sort chapters based on their numeric order
|
170 |
+
def sort_key(chapter_file):
|
171 |
+
numbers = re.findall(r'\d+', chapter_file)
|
172 |
+
return int(numbers[0]) if numbers else 0
|
173 |
+
|
174 |
+
# Extract metadata and cover image from the eBook file
|
175 |
+
def extract_metadata_and_cover(ebook_path):
|
176 |
+
try:
|
177 |
+
cover_path = ebook_path.rsplit('.', 1)[0] + '.jpg'
|
178 |
+
subprocess.run(['ebook-meta', ebook_path, '--get-cover', cover_path], check=True)
|
179 |
+
if os.path.exists(cover_path):
|
180 |
+
return cover_path
|
181 |
+
except Exception as e:
|
182 |
+
print(f"Error extracting eBook metadata or cover: {e}")
|
183 |
+
return None
|
184 |
+
# Combine WAV files into a single file
|
185 |
+
def combine_wav_files(chapter_files, output_path):
|
186 |
+
# Initialize an empty audio segment
|
187 |
+
combined_audio = AudioSegment.empty()
|
188 |
+
|
189 |
+
# Sequentially append each file to the combined_audio
|
190 |
+
for chapter_file in chapter_files:
|
191 |
+
audio_segment = AudioSegment.from_wav(chapter_file)
|
192 |
+
combined_audio += audio_segment
|
193 |
+
# Export the combined audio to the output file path
|
194 |
+
combined_audio.export(output_path, format='wav')
|
195 |
+
print(f"Combined audio saved to {output_path}")
|
196 |
+
|
197 |
+
# Function to generate metadata for M4B chapters
|
198 |
+
def generate_ffmpeg_metadata(chapter_files, metadata_file):
|
199 |
+
with open(metadata_file, 'w') as file:
|
200 |
+
file.write(';FFMETADATA1\n')
|
201 |
+
start_time = 0
|
202 |
+
for index, chapter_file in enumerate(chapter_files):
|
203 |
+
duration_ms = len(AudioSegment.from_wav(chapter_file))
|
204 |
+
file.write(f'[CHAPTER]\nTIMEBASE=1/1000\nSTART={start_time}\n')
|
205 |
+
file.write(f'END={start_time + duration_ms}\ntitle=Chapter {index + 1}\n')
|
206 |
+
start_time += duration_ms
|
207 |
+
|
208 |
+
# Generate the final M4B file using ffmpeg
|
209 |
+
def create_m4b(combined_wav, metadata_file, cover_image, output_m4b):
|
210 |
+
# Ensure the output directory exists
|
211 |
+
os.makedirs(os.path.dirname(output_m4b), exist_ok=True)
|
212 |
+
|
213 |
+
ffmpeg_cmd = ['ffmpeg', '-i', combined_wav, '-i', metadata_file]
|
214 |
+
if cover_image:
|
215 |
+
ffmpeg_cmd += ['-i', cover_image, '-map', '0:a', '-map', '2:v']
|
216 |
+
else:
|
217 |
+
ffmpeg_cmd += ['-map', '0:a']
|
218 |
+
|
219 |
+
ffmpeg_cmd += ['-map_metadata', '1', '-c:a', 'aac', '-b:a', '192k']
|
220 |
+
if cover_image:
|
221 |
+
ffmpeg_cmd += ['-c:v', 'png', '-disposition:v', 'attached_pic']
|
222 |
+
ffmpeg_cmd += [output_m4b]
|
223 |
+
|
224 |
+
subprocess.run(ffmpeg_cmd, check=True)
|
225 |
+
|
226 |
+
|
227 |
+
|
228 |
+
# Main logic
|
229 |
+
chapter_files = sorted([os.path.join(input_dir, f) for f in os.listdir(input_dir) if f.endswith('.wav')], key=sort_key)
|
230 |
+
temp_dir = tempfile.gettempdir()
|
231 |
+
temp_combined_wav = os.path.join(temp_dir, 'combined.wav')
|
232 |
+
metadata_file = os.path.join(temp_dir, 'metadata.txt')
|
233 |
+
cover_image = extract_metadata_and_cover(ebook_file)
|
234 |
+
output_m4b = os.path.join(output_dir, os.path.splitext(os.path.basename(ebook_file))[0] + '.m4b')
|
235 |
+
|
236 |
+
combine_wav_files(chapter_files, temp_combined_wav)
|
237 |
+
generate_ffmpeg_metadata(chapter_files, metadata_file)
|
238 |
+
create_m4b(temp_combined_wav, metadata_file, cover_image, output_m4b)
|
239 |
+
|
240 |
+
# Cleanup
|
241 |
+
if os.path.exists(temp_combined_wav):
|
242 |
+
os.remove(temp_combined_wav)
|
243 |
+
if os.path.exists(metadata_file):
|
244 |
+
os.remove(metadata_file)
|
245 |
+
if cover_image and os.path.exists(cover_image):
|
246 |
+
os.remove(cover_image)
|
247 |
+
|
248 |
+
# Example usage
|
249 |
+
# create_m4b_from_chapters('path_to_chapter_wavs', 'path_to_ebook_file', 'path_to_output_dir')
|
250 |
+
|
251 |
+
|
252 |
+
|
253 |
+
|
254 |
+
|
255 |
+
|
256 |
+
#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
|
257 |
+
import os
|
258 |
+
import subprocess
|
259 |
+
import ebooklib
|
260 |
+
from ebooklib import epub
|
261 |
+
from bs4 import BeautifulSoup
|
262 |
+
import re
|
263 |
+
import csv
|
264 |
+
import nltk
|
265 |
+
|
266 |
+
# Only run the main script if Value is True
|
267 |
+
def create_chapter_labeled_book(ebook_file_path):
|
268 |
+
# Function to ensure the existence of a directory
|
269 |
+
def ensure_directory(directory_path):
|
270 |
+
if not os.path.exists(directory_path):
|
271 |
+
os.makedirs(directory_path)
|
272 |
+
print(f"Created directory: {directory_path}")
|
273 |
+
|
274 |
+
ensure_directory(os.path.join(".", 'Working_files', 'Book'))
|
275 |
+
|
276 |
+
def convert_to_epub(input_path, output_path):
|
277 |
+
# Convert the ebook to EPUB format using Calibre's ebook-convert
|
278 |
+
try:
|
279 |
+
subprocess.run(['ebook-convert', input_path, output_path], check=True)
|
280 |
+
except subprocess.CalledProcessError as e:
|
281 |
+
print(f"An error occurred while converting the eBook: {e}")
|
282 |
+
return False
|
283 |
+
return True
|
284 |
+
|
285 |
+
def save_chapters_as_text(epub_path):
|
286 |
+
# Create the directory if it doesn't exist
|
287 |
+
directory = os.path.join(".", "Working_files", "temp_ebook")
|
288 |
+
ensure_directory(directory)
|
289 |
+
|
290 |
+
# Open the EPUB file
|
291 |
+
book = epub.read_epub(epub_path)
|
292 |
+
|
293 |
+
previous_chapter_text = ''
|
294 |
+
previous_filename = ''
|
295 |
+
chapter_counter = 0
|
296 |
+
|
297 |
+
# Iterate through the items in the EPUB file
|
298 |
+
for item in book.get_items():
|
299 |
+
if item.get_type() == ebooklib.ITEM_DOCUMENT:
|
300 |
+
# Use BeautifulSoup to parse HTML content
|
301 |
+
soup = BeautifulSoup(item.get_content(), 'html.parser')
|
302 |
+
text = soup.get_text()
|
303 |
+
|
304 |
+
# Check if the text is not empty
|
305 |
+
if text.strip():
|
306 |
+
if len(text) < 2300 and previous_filename:
|
307 |
+
# Append text to the previous chapter if it's short
|
308 |
+
with open(previous_filename, 'a', encoding='utf-8') as file:
|
309 |
+
file.write('\n' + text)
|
310 |
+
else:
|
311 |
+
# Create a new chapter file and increment the counter
|
312 |
+
previous_filename = os.path.join(directory, f"chapter_{chapter_counter}.txt")
|
313 |
+
chapter_counter += 1
|
314 |
+
with open(previous_filename, 'w', encoding='utf-8') as file:
|
315 |
+
file.write(text)
|
316 |
+
print(f"Saved chapter: {previous_filename}")
|
317 |
+
|
318 |
+
# Example usage
|
319 |
+
input_ebook = ebook_file_path # Replace with your eBook file path
|
320 |
+
output_epub = os.path.join(".", "Working_files", "temp.epub")
|
321 |
+
|
322 |
+
|
323 |
+
if os.path.exists(output_epub):
|
324 |
+
os.remove(output_epub)
|
325 |
+
print(f"File {output_epub} has been removed.")
|
326 |
+
else:
|
327 |
+
print(f"The file {output_epub} does not exist.")
|
328 |
+
|
329 |
+
if convert_to_epub(input_ebook, output_epub):
|
330 |
+
save_chapters_as_text(output_epub)
|
331 |
+
|
332 |
+
# Download the necessary NLTK data (if not already present)
|
333 |
+
#nltk.download('punkt')
|
334 |
+
|
335 |
+
def process_chapter_files(folder_path, output_csv):
|
336 |
+
with open(output_csv, 'w', newline='', encoding='utf-8') as csvfile:
|
337 |
+
writer = csv.writer(csvfile)
|
338 |
+
# Write the header row
|
339 |
+
writer.writerow(['Text', 'Start Location', 'End Location', 'Is Quote', 'Speaker', 'Chapter'])
|
340 |
+
|
341 |
+
# Process each chapter file
|
342 |
+
chapter_files = sorted(os.listdir(folder_path), key=lambda x: int(x.split('_')[1].split('.')[0]))
|
343 |
+
for filename in chapter_files:
|
344 |
+
if filename.startswith('chapter_') and filename.endswith('.txt'):
|
345 |
+
chapter_number = int(filename.split('_')[1].split('.')[0])
|
346 |
+
file_path = os.path.join(folder_path, filename)
|
347 |
+
|
348 |
+
try:
|
349 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
350 |
+
text = file.read()
|
351 |
+
# Insert "NEWCHAPTERABC" at the beginning of each chapter's text
|
352 |
+
if text:
|
353 |
+
text = "NEWCHAPTERABC" + text
|
354 |
+
sentences = nltk.tokenize.sent_tokenize(text)
|
355 |
+
for sentence in sentences:
|
356 |
+
start_location = text.find(sentence)
|
357 |
+
end_location = start_location + len(sentence)
|
358 |
+
writer.writerow([sentence, start_location, end_location, 'True', 'Narrator', chapter_number])
|
359 |
+
except Exception as e:
|
360 |
+
print(f"Error processing file {filename}: {e}")
|
361 |
+
|
362 |
+
# Example usage
|
363 |
+
folder_path = os.path.join(".", "Working_files", "temp_ebook")
|
364 |
+
output_csv = os.path.join(".", "Working_files", "Book", "Other_book.csv")
|
365 |
+
|
366 |
+
process_chapter_files(folder_path, output_csv)
|
367 |
+
|
368 |
+
def sort_key(filename):
|
369 |
+
"""Extract chapter number for sorting."""
|
370 |
+
match = re.search(r'chapter_(\d+)\.txt', filename)
|
371 |
+
return int(match.group(1)) if match else 0
|
372 |
+
|
373 |
+
def combine_chapters(input_folder, output_file):
|
374 |
+
# Create the output folder if it doesn't exist
|
375 |
+
os.makedirs(os.path.dirname(output_file), exist_ok=True)
|
376 |
+
|
377 |
+
# List all txt files and sort them by chapter number
|
378 |
+
files = [f for f in os.listdir(input_folder) if f.endswith('.txt')]
|
379 |
+
sorted_files = sorted(files, key=sort_key)
|
380 |
+
|
381 |
+
with open(output_file, 'w', encoding='utf-8') as outfile: # Specify UTF-8 encoding here
|
382 |
+
for i, filename in enumerate(sorted_files):
|
383 |
+
with open(os.path.join(input_folder, filename), 'r', encoding='utf-8') as infile: # And here
|
384 |
+
outfile.write(infile.read())
|
385 |
+
# Add the marker unless it's the last file
|
386 |
+
if i < len(sorted_files) - 1:
|
387 |
+
outfile.write("\nNEWCHAPTERABC\n")
|
388 |
+
|
389 |
+
# Paths
|
390 |
+
input_folder = os.path.join(".", 'Working_files', 'temp_ebook')
|
391 |
+
output_file = os.path.join(".", 'Working_files', 'Book', 'Chapter_Book.txt')
|
392 |
+
|
393 |
+
|
394 |
+
# Combine the chapters
|
395 |
+
combine_chapters(input_folder, output_file)
|
396 |
+
|
397 |
+
ensure_directory(os.path.join(".", "Working_files", "Book"))
|
398 |
+
|
399 |
+
|
400 |
+
#create_chapter_labeled_book()
|
401 |
+
|
402 |
+
|
403 |
+
|
404 |
+
|
405 |
+
import os
|
406 |
+
import subprocess
|
407 |
+
import sys
|
408 |
+
|
409 |
+
# Check if Calibre's ebook-convert tool is installed
|
410 |
+
def calibre_installed():
|
411 |
+
try:
|
412 |
+
subprocess.run(['ebook-convert', '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
413 |
+
return True
|
414 |
+
except FileNotFoundError:
|
415 |
+
print("Calibre is not installed. Please install Calibre for this functionality.")
|
416 |
+
return False
|
417 |
+
|
418 |
+
|
419 |
+
import os
|
420 |
+
from nltk.tokenize import sent_tokenize
|
421 |
+
from pydub import AudioSegment
|
422 |
+
# Assuming split_long_sentence and wipe_folder are defined elsewhere in your code
|
423 |
+
|
424 |
+
default_target_voice_path = "default_voice.wav" # Ensure this is a valid path
|
425 |
+
default_language_code = "en"
|
426 |
+
def combine_wav_files(input_directory, output_directory, file_name):
|
427 |
+
# Ensure that the output directory exists, create it if necessary
|
428 |
+
os.makedirs(output_directory, exist_ok=True)
|
429 |
+
|
430 |
+
# Specify the output file path
|
431 |
+
output_file_path = os.path.join(output_directory, file_name)
|
432 |
+
|
433 |
+
# Initialize an empty audio segment
|
434 |
+
combined_audio = AudioSegment.empty()
|
435 |
+
|
436 |
+
# Get a list of all .wav files in the specified input directory and sort them
|
437 |
+
input_file_paths = sorted(
|
438 |
+
[os.path.join(input_directory, f) for f in os.listdir(input_directory) if f.endswith(".wav")],
|
439 |
+
key=lambda f: int(''.join(filter(str.isdigit, f)))
|
440 |
+
)
|
441 |
+
|
442 |
+
# Sequentially append each file to the combined_audio
|
443 |
+
for input_file_path in input_file_paths:
|
444 |
+
audio_segment = AudioSegment.from_wav(input_file_path)
|
445 |
+
combined_audio += audio_segment
|
446 |
+
|
447 |
+
# Export the combined audio to the output file path
|
448 |
+
combined_audio.export(output_file_path, format='wav')
|
449 |
+
|
450 |
+
print(f"Combined audio saved to {output_file_path}")
|
451 |
+
|
452 |
+
# Function to split long strings into parts
|
453 |
+
def split_long_sentence(sentence, max_length=249, max_pauses=10):
|
454 |
+
"""
|
455 |
+
Splits a sentence into parts based on length or number of pauses without recursion.
|
456 |
+
|
457 |
+
:param sentence: The sentence to split.
|
458 |
+
:param max_length: Maximum allowed length of a sentence.
|
459 |
+
:param max_pauses: Maximum allowed number of pauses in a sentence.
|
460 |
+
:return: A list of sentence parts that meet the criteria.
|
461 |
+
"""
|
462 |
+
parts = []
|
463 |
+
while len(sentence) > max_length or sentence.count(',') + sentence.count(';') + sentence.count('.') > max_pauses:
|
464 |
+
possible_splits = [i for i, char in enumerate(sentence) if char in ',;.' and i < max_length]
|
465 |
+
if possible_splits:
|
466 |
+
# Find the best place to split the sentence, preferring the last possible split to keep parts longer
|
467 |
+
split_at = possible_splits[-1] + 1
|
468 |
+
else:
|
469 |
+
# If no punctuation to split on within max_length, split at max_length
|
470 |
+
split_at = max_length
|
471 |
+
|
472 |
+
# Split the sentence and add the first part to the list
|
473 |
+
parts.append(sentence[:split_at].strip())
|
474 |
+
sentence = sentence[split_at:].strip()
|
475 |
+
|
476 |
+
# Add the remaining part of the sentence
|
477 |
+
parts.append(sentence)
|
478 |
+
return parts
|
479 |
+
|
480 |
+
#convert chapters into audio with piper-tts
|
481 |
+
|
482 |
+
def convert_chapters_to_audio_standard_model(chapters_dir, output_audio_dir, target_voice_path=None, language=None, piper_model_name=None):
|
483 |
+
load_piper_tts(piper_model_name) # Load the Piper TTS model with the given name
|
484 |
+
if not os.path.exists(output_audio_dir):
|
485 |
+
os.makedirs(output_audio_dir)
|
486 |
+
|
487 |
+
for chapter_file in sorted(os.listdir(chapters_dir)):
|
488 |
+
if chapter_file.endswith('.txt'):
|
489 |
+
match = re.search(r"chapter_(\d+).txt", chapter_file)
|
490 |
+
if match:
|
491 |
+
chapter_num = int(match.group(1))
|
492 |
+
else:
|
493 |
+
print(f"Skipping file {chapter_file} as it does not match the expected format.")
|
494 |
+
continue
|
495 |
+
|
496 |
+
chapter_path = os.path.join(chapters_dir, chapter_file)
|
497 |
+
output_file_name = f"audio_chapter_{chapter_num}.wav"
|
498 |
+
output_file_path = os.path.join(output_audio_dir, output_file_name)
|
499 |
+
temp_audio_directory = os.path.join(".", "Working_files", "temp")
|
500 |
+
os.makedirs(temp_audio_directory, exist_ok=True)
|
501 |
+
temp_count = 0
|
502 |
+
|
503 |
+
with open(chapter_path, 'r', encoding='utf-8') as file:
|
504 |
+
chapter_text = file.read()
|
505 |
+
sentences = sent_tokenize(chapter_text, language='italian' if language == 'it' else 'english')
|
506 |
+
for sentence in tqdm(sentences, desc=f"Chapter {chapter_num}"):
|
507 |
+
fragments = split_long_sentence(sentence, max_length=249 if language == "en" else 213, max_pauses=10)
|
508 |
+
for fragment in fragments:
|
509 |
+
if fragment != "":
|
510 |
+
print(f"Generating fragment: {fragment}...")
|
511 |
+
fragment_file_path = os.path.join(temp_audio_directory, f"{temp_count}.wav")
|
512 |
+
piper_to_tts(fragment, fragment_file_path)
|
513 |
+
temp_count += 1
|
514 |
+
|
515 |
+
combine_wav_files(temp_audio_directory, output_audio_dir, output_file_name)
|
516 |
+
wipe_folder(temp_audio_directory)
|
517 |
+
print(f"Converted chapter {chapter_num} to audio.")
|
518 |
+
|
519 |
+
|
520 |
+
|
521 |
+
|
522 |
+
# Define the functions to be used in the Gradio interface
|
523 |
+
def convert_ebook_to_audio(ebook_file, target_voice_file, voice_key, progress=gr.Progress()):
|
524 |
+
ebook_file_path = ebook_file.name
|
525 |
+
target_voice = target_voice_file.name if target_voice_file else None
|
526 |
+
|
527 |
+
working_files = os.path.join(".", "Working_files", "temp_ebook")
|
528 |
+
full_folder_working_files = os.path.join(".", "Working_files")
|
529 |
+
chapters_directory = os.path.join(".", "Working_files", "temp_ebook")
|
530 |
+
output_audio_directory = os.path.join(".", 'Chapter_wav_files')
|
531 |
+
remove_folder_with_contents(full_folder_working_files)
|
532 |
+
remove_folder_with_contents(output_audio_directory)
|
533 |
+
|
534 |
+
try:
|
535 |
+
progress(0, desc="Starting conversion")
|
536 |
+
except Exception as e:
|
537 |
+
print(f"Error updating progress: {e}")
|
538 |
+
|
539 |
+
if not calibre_installed():
|
540 |
+
return "Calibre is not installed."
|
541 |
+
|
542 |
+
|
543 |
+
try:
|
544 |
+
progress(0.1, desc="Creating chapter-labeled book")
|
545 |
+
except Exception as e:
|
546 |
+
print(f"Error updating progress: {e}")
|
547 |
+
|
548 |
+
create_chapter_labeled_book(ebook_file_path)
|
549 |
+
audiobook_output_path = os.path.join(".", "Audiobooks")
|
550 |
+
|
551 |
+
try:
|
552 |
+
progress(0.3, desc="Converting chapters to audio")
|
553 |
+
except Exception as e:
|
554 |
+
print(f"Error updating progress: {e}")
|
555 |
+
|
556 |
+
convert_chapters_to_audio_standard_model(chapters_directory, output_audio_directory, target_voice, piper_model_name=voice_key)
|
557 |
+
try:
|
558 |
+
progress(0.9, desc="Creating M4B from chapters")
|
559 |
+
except Exception as e:
|
560 |
+
print(f"Error updating progress: {e}")
|
561 |
+
|
562 |
+
create_m4b_from_chapters(output_audio_directory, ebook_file_path, audiobook_output_path)
|
563 |
+
|
564 |
+
# Get the name of the created M4B file
|
565 |
+
m4b_filename = os.path.splitext(os.path.basename(ebook_file_path))[0] + '.m4b'
|
566 |
+
m4b_filepath = os.path.join(audiobook_output_path, m4b_filename)
|
567 |
+
|
568 |
+
try:
|
569 |
+
progress(1.0, desc="Conversion complete")
|
570 |
+
except Exception as e:
|
571 |
+
print(f"Error updating progress: {e}")
|
572 |
+
print(f"Audiobook created at {m4b_filepath}")
|
573 |
+
return f"Audiobook created at {m4b_filepath}", m4b_filepath
|
574 |
+
|
575 |
+
|
576 |
+
|
577 |
+
|
578 |
+
|
579 |
+
def list_audiobook_files(audiobook_folder):
|
580 |
+
# List all files in the audiobook folder
|
581 |
+
files = []
|
582 |
+
for filename in os.listdir(audiobook_folder):
|
583 |
+
if filename.endswith('.m4b'): # Adjust the file extension as needed
|
584 |
+
files.append(os.path.join(audiobook_folder, filename))
|
585 |
+
return files
|
586 |
+
|
587 |
+
def download_audiobooks():
|
588 |
+
audiobook_output_path = os.path.join(".", "Audiobooks")
|
589 |
+
return list_audiobook_files(audiobook_output_path)
|
590 |
+
|
591 |
+
|
592 |
+
|
593 |
+
|
594 |
+
|
595 |
+
|
596 |
+
|
597 |
+
|
598 |
+
|
599 |
+
|
600 |
+
|
601 |
+
|
602 |
+
|
603 |
+
|
604 |
+
|
605 |
+
|
606 |
+
|
607 |
+
|
608 |
+
|
609 |
+
import gradio as gr
|
610 |
+
import json
|
611 |
+
import requests
|
612 |
+
import os
|
613 |
+
from tqdm import tqdm
|
614 |
+
|
615 |
+
# Load the JSON data
|
616 |
+
with open("voices.json", "r") as f:
|
617 |
+
voices_data = json.load(f)
|
618 |
+
|
619 |
+
# Base URL for downloading model files
|
620 |
+
BASE_URL = "https://huggingface.co/rhasspy/piper-voices/resolve/main/"
|
621 |
+
|
622 |
+
# Function to download selected voice files if missing
|
623 |
+
def download_voice(voice_key):
|
624 |
+
voice_info = voices_data[voice_key]
|
625 |
+
files = voice_info["files"]
|
626 |
+
|
627 |
+
download_dir = os.path.join(os.getcwd(), voice_key)
|
628 |
+
os.makedirs(download_dir, exist_ok=True) # Create the folder if it doesn't exist
|
629 |
+
|
630 |
+
downloaded_files = 0
|
631 |
+
|
632 |
+
for file_path, file_info in files.items():
|
633 |
+
local_file_path = os.path.join(download_dir, os.path.basename(file_path))
|
634 |
+
|
635 |
+
# Check if the file already exists
|
636 |
+
if os.path.exists(local_file_path):
|
637 |
+
print(f"File '{os.path.basename(file_path)}' already exists. Skipping download.")
|
638 |
+
else:
|
639 |
+
# Download the file with tqdm progress bar in the terminal
|
640 |
+
url = BASE_URL + file_path
|
641 |
+
response = requests.get(url, stream=True)
|
642 |
+
total_size = int(response.headers.get('content-length', 0))
|
643 |
+
with open(local_file_path, 'wb') as file, tqdm(
|
644 |
+
desc=f"Downloading {os.path.basename(file_path)}",
|
645 |
+
total=total_size,
|
646 |
+
unit='B',
|
647 |
+
unit_scale=True,
|
648 |
+
unit_divisor=1024,
|
649 |
+
) as bar:
|
650 |
+
for data in response.iter_content(1024):
|
651 |
+
file.write(data)
|
652 |
+
bar.update(len(data))
|
653 |
+
downloaded_files += 1
|
654 |
+
|
655 |
+
if downloaded_files == 0:
|
656 |
+
return f"All files for {voice_key} already exist. No files were downloaded."
|
657 |
+
else:
|
658 |
+
return f"Downloaded {downloaded_files} files for {voice_key}."
|
659 |
+
|
660 |
+
|
661 |
+
|
662 |
+
# Define a new function to chain the download and conversion processes
|
663 |
+
def download_and_convert(ebook_file, target_voice_file, voice_key, progress=gr.Progress()):
|
664 |
+
# First, download the voice files
|
665 |
+
download_message = download_voice(voice_key)
|
666 |
+
|
667 |
+
# Then, proceed with the conversion
|
668 |
+
conversion_result, m4b_filepath = convert_ebook_to_audio(ebook_file, target_voice_file, voice_key, progress)
|
669 |
+
|
670 |
+
# Combine the download message with the conversion result
|
671 |
+
return f"{download_message}\n{conversion_result}", m4b_filepath
|
672 |
+
|
673 |
+
# Function to get details of the selected voice
|
674 |
+
def get_voice_details(voice_key):
|
675 |
+
voice_info = voices_data[voice_key]
|
676 |
+
details = f"""
|
677 |
+
**Name:** {voice_info['name']}
|
678 |
+
**Language:** {voice_info['language']['name_english']} ({voice_info['language']['code']})
|
679 |
+
**Country:** {voice_info['language']['country_english']}
|
680 |
+
**Quality:** {voice_info['quality']}
|
681 |
+
**Number of Speakers:** {voice_info.get('num_speakers', 'N/A')}
|
682 |
+
"""
|
683 |
+
return details
|
684 |
+
|
685 |
+
# List of voice keys for dropdown
|
686 |
+
voice_keys = list(voices_data.keys())
|
687 |
+
|
688 |
+
# Integrating with the existing Gradio UI
|
689 |
+
theme = gr.themes.Soft(
|
690 |
+
primary_hue="blue",
|
691 |
+
secondary_hue="blue",
|
692 |
+
neutral_hue="blue",
|
693 |
+
text_size=gr.themes.sizes.text_md,
|
694 |
+
)
|
695 |
+
|
696 |
+
with gr.Blocks(theme=theme) as demo:
|
697 |
+
gr.Markdown(
|
698 |
+
"""
|
699 |
+
# eBook to Audiobook Converter
|
700 |
+
|
701 |
+
Transform your eBooks into immersive audiobooks.
|
702 |
+
"""
|
703 |
+
)
|
704 |
+
|
705 |
+
with gr.Row():
|
706 |
+
with gr.Column(scale=3):
|
707 |
+
ebook_file = gr.File(label="eBook File")
|
708 |
+
target_voice_file = gr.File(label="Target Voice File (Optional)", visible=False)
|
709 |
+
|
710 |
+
voice_selector = gr.Dropdown(label="Select Voice", choices=voice_keys) # Define voice_selector here
|
711 |
+
|
712 |
+
voice_details = gr.Markdown()
|
713 |
+
|
714 |
+
convert_btn = gr.Button("Convert to Audiobook", variant="primary")
|
715 |
+
output = gr.Textbox(label="Conversion Status")
|
716 |
+
audio_player = gr.Audio(label="Audiobook Player", type="filepath")
|
717 |
+
download_btn = gr.Button("Download Audiobook Files")
|
718 |
+
download_files = gr.File(label="Download Files", interactive=False)
|
719 |
+
|
720 |
+
convert_btn.click(
|
721 |
+
download_and_convert,
|
722 |
+
inputs=[ebook_file, target_voice_file, voice_selector], # Now voice_selector is defined
|
723 |
+
outputs=[output, audio_player]
|
724 |
+
)
|
725 |
+
|
726 |
+
download_btn.click(
|
727 |
+
download_audiobooks,
|
728 |
+
outputs=[download_files]
|
729 |
+
)
|
730 |
+
|
731 |
+
# Adding the Download Voices Section
|
732 |
+
download_voice_btn = gr.Button("Download Voice Files", visible=False)
|
733 |
+
|
734 |
+
voice_selector.change(get_voice_details, voice_selector, voice_details)
|
735 |
+
download_voice_btn.click(download_voice, voice_selector, None)
|
736 |
+
|
737 |
+
demo.launch(share=True)
|