drewThomasson commited on
Commit
306e52a
1 Parent(s): d069ecb

Upload 11 files

Browse files
ebook2audiobookXTTS/Dockerfile ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official NVIDIA CUDA image with cudnn8 and Ubuntu 20.04 as the base
2
+ FROM nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu20.04
3
+
4
+ # Set non-interactive installation to avoid timezone and other prompts
5
+ ENV DEBIAN_FRONTEND=noninteractive
6
+
7
+ # Install necessary packages including Miniconda
8
+ RUN apt-get update && apt-get install -y --no-install-recommends \
9
+ wget \
10
+ git \
11
+ espeak \
12
+ espeak-ng \
13
+ ffmpeg \
14
+ tk \
15
+ mecab \
16
+ libmecab-dev \
17
+ mecab-ipadic-utf8 \
18
+ build-essential \
19
+ calibre \
20
+ && rm -rf /var/lib/apt/lists/*
21
+
22
+ RUN ebook-convert --version
23
+
24
+ # Install Miniconda
25
+ RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh && \
26
+ bash ~/miniconda.sh -b -p /opt/conda && \
27
+ rm ~/miniconda.sh
28
+
29
+
30
+
31
+ # Set PATH to include conda
32
+ ENV PATH=/opt/conda/bin:$PATH
33
+
34
+ # Create a conda environment with Python 3.10
35
+ RUN conda create -n ebookenv python=3.10 -y
36
+
37
+ # Activate the conda environment
38
+ SHELL ["conda", "run", "-n", "ebookenv", "/bin/bash", "-c"]
39
+
40
+ # Install Python dependencies using conda and pip
41
+ RUN conda install -n ebookenv -c conda-forge \
42
+ pydub \
43
+ nltk \
44
+ mecab-python3 \
45
+ && pip install --no-cache-dir \
46
+ bs4 \
47
+ beautifulsoup4 \
48
+ ebooklib \
49
+ tqdm \
50
+ tts==0.21.3 \
51
+ unidic \
52
+ gradio
53
+
54
+ # Download unidic
55
+ RUN python -m unidic download
56
+
57
+ # Set the working directory in the container
58
+ WORKDIR /ebook2audiobookXTTS
59
+
60
+ # Clone the ebook2audiobookXTTS repository
61
+ RUN git clone https://github.com/DrewThomasson/ebook2audiobookXTTS.git .
62
+
63
+ # Copy test audio file
64
+ COPY default_voice.wav /ebook2audiobookXTTS/
65
+
66
+ # Run a test to set up XTTS
67
+ RUN echo "import torch" > /tmp/script1.py && \
68
+ echo "from TTS.api import TTS" >> /tmp/script1.py && \
69
+ echo "device = 'cuda' if torch.cuda.is_available() else 'cpu'" >> /tmp/script1.py && \
70
+ echo "print(TTS().list_models())" >> /tmp/script1.py && \
71
+ echo "tts = TTS('tts_models/multilingual/multi-dataset/xtts_v2').to(device)" >> /tmp/script1.py && \
72
+ echo "wav = tts.tts(text='Hello world!', speaker_wav='default_voice.wav', language='en')" >> /tmp/script1.py && \
73
+ echo "tts.tts_to_file(text='Hello world!', speaker_wav='default_voice.wav', language='en', file_path='output.wav')" >> /tmp/script1.py && \
74
+ yes | python /tmp/script1.py
75
+
76
+ # Remove the test audio file
77
+ RUN rm -f /ebook2audiobookXTTS/output.wav
78
+
79
+ # Verify that the script exists and has the correct permissions
80
+ RUN ls -la /ebook2audiobookXTTS/
81
+
82
+ # Check if the script exists and log its presence
83
+ RUN if [ -f /ebook2audiobookXTTS/custom_model_ebook2audiobookXTTS_with_link_gradio.py ]; then echo "Script found."; else echo "Script not found."; exit 1; fi
84
+
85
+ # Modify the Python script to set share=True
86
+ RUN sed -i 's/demo.launch(share=False)/demo.launch(share=True)/' /ebook2audiobookXTTS/custom_model_ebook2audiobookXTTS_with_link_gradio.py
87
+
88
+ # Download the punkt package for nltk
89
+ RUN python -m nltk.downloader punkt
90
+
91
+ # Set the command to run your GUI application using the conda environment
92
+ CMD ["conda", "run", "--no-capture-output", "-n", "ebookenv", "python", "/ebook2audiobookXTTS/custom_model_ebook2audiobookXTTS_with_link_gradio.py"]
93
+
ebook2audiobookXTTS/LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Drew Thomasson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
ebook2audiobookXTTS/README.md ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # 📚 ebook2audiobook
2
+
3
+ Convert eBooks to audiobooks with chapters and metadata using Calibre and Coqui XTTS. Supports optional voice cloning and multiple languages!
4
+
5
+ ## 🌟 Features
6
+
7
+ - 📖 Converts eBooks to text format with Calibre.
8
+ - 📚 Splits eBook into chapters for organized audio.
9
+ - 🎙️ High-quality text-to-speech with Coqui XTTS.
10
+ - 🗣️ Optional voice cloning with your own voice file.
11
+ - 🌍 Supports multiple languages (English by default).
12
+ - 🖥️ Designed to run on 4GB RAM.
13
+
14
+ ## 🛠️ Requirements
15
+
16
+ - Python 3.x
17
+ - `coqui-tts` Python package
18
+ - Calibre (for eBook conversion)
19
+ - FFmpeg (for audiobook creation)
20
+ - Optional: Custom voice file for voice cloning
21
+
22
+ ### 🔧 Installation Instructions
23
+
24
+ 1. **Install Python 3.x** from [Python.org](https://www.python.org/downloads/).
25
+
26
+ 2. **Install Calibre**:
27
+ - **Ubuntu**: `sudo apt-get install -y calibre`
28
+ - **macOS**: `brew install calibre`
29
+ - **Windows** (Admin Powershell): `choco install calibre`
30
+
31
+ 3. **Install FFmpeg**:
32
+ - **Ubuntu**: `sudo apt-get install -y ffmpeg`
33
+ - **macOS**: `brew install ffmpeg`
34
+ - **Windows** (Admin Powershell): `choco install ffmpeg`
35
+
36
+ 4. **Optional: Install Mecab** (for non-Latin languages):
37
+ - **Ubuntu**: `sudo apt-get install -y mecab libmecab-dev mecab-ipadic-utf8`
38
+ - **macOS**: `brew install mecab`, `brew install mecab-ipadic`
39
+ - **Windows**: [mecab-website-to-install-manually](https://taku910.github.io/mecab/#download) (Note: Japanese support is limited)
40
+
41
+ 5. **Install Python packages**:
42
+ ```bash
43
+ pip install tts==0.21.3 pydub nltk beautifulsoup4 ebooklib tqdm
44
+
45
+ python -m nltk.downloader punkt
46
+ ```
47
+
48
+ **For non-Latin languages**:
49
+ ```bash
50
+ pip install mecab mecab-python3 unidic
51
+
52
+ python -m unidic download
53
+ ```
54
+
55
+ ## 🌐 Supported Languages
56
+
57
+ - **English (en)**
58
+ - **Spanish (es)**
59
+ - **French (fr)**
60
+ - **German (de)**
61
+ - **Italian (it)**
62
+ - **Portuguese (pt)**
63
+ - **Polish (pl)**
64
+ - **Turkish (tr)**
65
+ - **Russian (ru)**
66
+ - **Dutch (nl)**
67
+ - **Czech (cs)**
68
+ - **Arabic (ar)**
69
+ - **Chinese (zh-cn)**
70
+ - **Japanese (ja)**
71
+ - **Hungarian (hu)**
72
+ - **Korean (ko)**
73
+
74
+ Specify the language code when running the script.
75
+
76
+ ## 🚀 Usage
77
+
78
+ ### 🖥️ Gradio Web Interface
79
+
80
+ 1. **Run the Script**:
81
+ ```bash
82
+ python custom_model_ebook2audiobookXTTS_gradio.py
83
+ ```
84
+
85
+ 2. **Open the Web App**: Click the URL provided in the terminal to access the web app and convert eBooks.
86
+
87
+ ### 📝 Basic Usage
88
+
89
+ ```bash
90
+ python ebook2audiobook.py <path_to_ebook_file> [path_to_voice_file] [language_code]
91
+ ```
92
+
93
+ - **<path_to_ebook_file>**: Path to your eBook file.
94
+ - **[path_to_voice_file]**: Optional for voice cloning.
95
+ - **[language_code]**: Optional to specify language.
96
+
97
+ ### 🧩 Custom XTTS Model
98
+
99
+ ```bash
100
+ python custom_model_ebook2audiobookXTTS.py <ebook_file_path> <target_voice_file_path> <language> <custom_model_path> <custom_config_path> <custom_vocab_path>
101
+ ```
102
+
103
+ - **<ebook_file_path>**: Path to your eBook file.
104
+ - **<target_voice_file_path>**: Optional for voice cloning.
105
+ - **<language>**: Optional to specify language.
106
+ - **<custom_model_path>**: Path to `model.pth`.
107
+ - **<custom_config_path>**: Path to `config.json`.
108
+ - **<custom_vocab_path>**: Path to `vocab.json`.
109
+
110
+ ### 🐳 Using Docker
111
+
112
+ You can also use Docker to run the eBook to Audiobook converter. This method ensures consistency across different environments and simplifies setup.
113
+
114
+ #### 🚀 Running the Docker Container
115
+
116
+ To run the Docker container and start the Gradio interface, use the following command:
117
+
118
+ -Run with CPU only
119
+ ```powershell
120
+ docker run -it --rm -p 7860:7860 --platform=linux/amd64 athomasson2/ebook2audiobookxtts:huggingface python app.py
121
+ ```
122
+ -Run with GPU Speedup (Nvida graphics cards only)
123
+ ```powershell
124
+ docker run -it --rm --gpus all -p 7860:7860 --platform=linux/amd64 athomasson2/ebook2audiobookxtts:huggingface python app.py
125
+ ```
126
+
127
+ This command will start the Gradio interface on port 7860.(localhost:7860)
128
+
129
+ #### 🖥️ Docker GUI
130
+
131
+ <img width="1401" alt="Screenshot 2024-08-25 at 10 08 40 AM" src="https://github.com/user-attachments/assets/78cfd33e-cd46-41cc-8128-3820160a5e40">
132
+ <img width="1406" alt="Screenshot 2024-08-25 at 10 08 51 AM" src="https://github.com/user-attachments/assets/dbfad9f6-e6e5-4cad-b248-adb76c5434f3">
133
+
134
+ ### 🛠️ For Custom Xtts Models
135
+
136
+ Models built to be better at a specific voice. Check out my Hugging Face page [here](https://huggingface.co/drewThomasson).
137
+
138
+ To use a custom model, paste the link of the `Finished_model_files.zip` file like this:
139
+
140
+ [David Attenborough fine tuned Finished_model_files.zip](https://huggingface.co/drewThomasson/xtts_David_Attenborough_fine_tune/resolve/main/Finished_model_files.zip?download=true)
141
+
142
+
143
+
144
+
145
+ More details can be found at the [Dockerfile Hub Page]([https://github.com/DrewThomasson/ebook2audiobookXTTS](https://hub.docker.com/repository/docker/athomasson2/ebook2audiobookxtts/general)).
146
+
147
+ ## 🌐 Fine Tuned Xtts models
148
+
149
+ To find already fine-tuned XTTS models, visit [this Hugging Face link](https://huggingface.co/drewThomasson) 🌐. Search for models that include "xtts fine tune" in their names.
150
+
151
+ ## 🎥 Demos
152
+
153
+ https://github.com/user-attachments/assets/8486603c-38b1-43ce-9639-73757dfb1031
154
+
155
+ ## 🤗 [Huggingface space demo](https://huggingface.co/spaces/drewThomasson/ebook2audiobookXTTS)
156
+ - Huggingface space is running on free cpu tier so expect very slow or timeout lol, just don't give it giant files is all
157
+ - Best to duplicate space or run locally.
158
+ ## 📚 Supported eBook Formats
159
+
160
+ - `.epub`, `.pdf`, `.mobi`, `.txt`, `.html`, `.rtf`, `.chm`, `.lit`, `.pdb`, `.fb2`, `.odt`, `.cbr`, `.cbz`, `.prc`, `.lrf`, `.pml`, `.snb`, `.cbc`, `.rb`, `.tcr`
161
+ - **Best results**: `.epub` or `.mobi` for automatic chapter detection
162
+
163
+ ## 📂 Output
164
+
165
+ - Creates an `.m4b` file with metadata and chapters.
166
+ - **Example Output**: ![Example](https://github.com/DrewThomasson/VoxNovel/blob/dc5197dff97252fa44c391dc0596902d71278a88/readme_files/example_in_app.jpeg)
167
+
168
+ ## 🙏 Special Thanks
169
+
170
+ - **Coqui TTS**: [Coqui TTS GitHub](https://github.com/coqui-ai/TTS)
171
+ - **Calibre**: [Calibre Website](https://calibre-ebook.com)
ebook2audiobookXTTS/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)
ebook2audiobookXTTS/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)
ebook2audiobookXTTS/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)
ebook2audiobookXTTS/default_voice.wav ADDED
Binary file (291 kB). View file
 
ebook2audiobookXTTS/demo_mini_story_chapters_Drew.epub ADDED
Binary file (415 kB). View file
 
ebook2audiobookXTTS/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)
ebook2audiobookXTTS/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)
ebook2audiobookXTTS/trash.py ADDED
@@ -0,0 +1,366 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 tqdm import tqdm
10
+ import gradio as gr
11
+ import nltk
12
+ import ebooklib
13
+ import bs4
14
+ from ebooklib import epub
15
+ from bs4 import BeautifulSoup
16
+ from gradio import Progress
17
+ import sys
18
+ from nltk.tokenize import sent_tokenize
19
+ import csv
20
+
21
+
22
+ # Ensure necessary models are downloaded
23
+ # nltk.download('punkt')
24
+
25
+ def is_folder_empty(folder_path):
26
+ if os.path.exists(folder_path) and os.path.isdir(folder_path):
27
+ return not os.listdir(folder_path)
28
+ else:
29
+ print(f"The path {folder_path} is not a valid folder.")
30
+ return None
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
+ def wipe_folder(folder_path):
40
+ if not os.path.exists(folder_path):
41
+ print(f"The folder {folder_path} does not exist.")
42
+ return
43
+ for item in os.listdir(folder_path):
44
+ item_path = os.path.join(folder_path, item)
45
+ if os.path.isfile(item_path):
46
+ os.remove(item_path)
47
+ elif os.path.isdir(item_path):
48
+ shutil.rmtree(item_path)
49
+
50
+ print(f"All contents wiped from {folder_path}.")
51
+
52
+ def create_m4b_from_chapters(input_dir, ebook_file, output_dir):
53
+ def sort_key(chapter_file):
54
+ numbers = re.findall(r'\d+', chapter_file)
55
+ return int(numbers[0]) if numbers else 0
56
+
57
+ def extract_metadata_and_cover(ebook_path):
58
+ try:
59
+ cover_path = ebook_path.rsplit('.', 1)[0] + '.jpg'
60
+ subprocess.run(['ebook-meta', ebook_path, '--get-cover', cover_path], check=True)
61
+ if (os.path.exists(cover_path)):
62
+ return cover_path
63
+ except Exception as e:
64
+ print(f"Error extracting eBook metadata or cover: {e}")
65
+ return None
66
+
67
+ def combine_wav_files(chapter_files, output_path):
68
+ combined_audio = AudioSegment.empty()
69
+
70
+ for chapter_file in chapter_files:
71
+ audio_segment = AudioSegment.from_wav(chapter_file)
72
+ combined_audio += audio_segment
73
+ combined_audio.export(output_path, format='wav')
74
+ print(f"Combined audio saved to {output_path}")
75
+
76
+ def generate_ffmpeg_metadata(chapter_files, metadata_file):
77
+ with open(metadata_file, 'w') as file:
78
+ file.write(';FFMETADATA1\n')
79
+ start_time = 0
80
+ for index, chapter_file in enumerate(chapter_files):
81
+ duration_ms = len(AudioSegment.from_wav(chapter_file))
82
+ file.write(f'[CHAPTER]\nTIMEBASE=1/1000\nSTART={start_time}\n')
83
+ file.write(f'END={start_time + duration_ms}\ntitle=Chapter {index + 1}\n')
84
+ start_time += duration_ms
85
+
86
+ def create_m4b(combined_wav, metadata_file, cover_image, output_m4b):
87
+ os.makedirs(os.path.dirname(output_m4b), exist_ok=True)
88
+
89
+ ffmpeg_cmd = ['ffmpeg', '-i', combined_wav, '-i', metadata_file]
90
+ if cover_image:
91
+ ffmpeg_cmd += ['-i', cover_image, '-map', '0:a', '-map', '2:v']
92
+ else:
93
+ ffmpeg_cmd += ['-map', '0:a']
94
+
95
+ ffmpeg_cmd += ['-map_metadata', '1', '-c:a', 'aac', '-b:a', '192k']
96
+ if cover_image:
97
+ ffmpeg_cmd += ['-c:v', 'png', '-disposition:v', 'attached_pic']
98
+ ffmpeg_cmd += [output_m4b]
99
+
100
+ subprocess.run(ffmpeg_cmd, check=True)
101
+
102
+ chapter_files = sorted([os.path.join(input_dir, f) for f in os.listdir(input_dir) if f.endswith('.wav')], key=sort_key)
103
+ temp_dir = tempfile.gettempdir()
104
+ temp_combined_wav = os.path.join(temp_dir, 'combined.wav')
105
+ metadata_file = os.path.join(temp_dir, 'metadata.txt')
106
+ cover_image = extract_metadata_and_cover(ebook_file)
107
+ output_m4b = os.path.join(output_dir, os.path.splitext(os.path.basename(ebook_file))[0] + '.m4b')
108
+
109
+ combine_wav_files(chapter_files, temp_combined_wav)
110
+ generate_ffmpeg_metadata(chapter_files, metadata_file)
111
+ create_m4b(temp_combined_wav, metadata_file, cover_image, output_m4b)
112
+
113
+ if os.path.exists(temp_combined_wav):
114
+ os.remove(temp_combined_wav)
115
+ if os.path.exists(metadata_file):
116
+ os.remove(metadata_file)
117
+ if cover_image and os.path.exists(cover_image):
118
+ os.remove(cover_image)
119
+
120
+ def create_chapter_labeled_book(ebook_file_path):
121
+ def ensure_directory(directory_path):
122
+ if not os.path.exists(directory_path):
123
+ os.makedirs(directory_path)
124
+ print(f"Created directory: {directory_path}")
125
+
126
+ ensure_directory(os.path.join(".", 'Working_files', 'Book'))
127
+
128
+ def convert_to_epub(input_path, output_path):
129
+ try:
130
+ subprocess.run(['ebook-convert', input_path, output_path], check=True)
131
+ except subprocess.CalledProcessError as e:
132
+ print(f"An error occurred while converting the eBook: {e}")
133
+ return False
134
+ return True
135
+
136
+ def save_chapters_as_text(epub_path):
137
+ directory = os.path.join(".", "Working_files", "temp_ebook")
138
+ ensure_directory(directory)
139
+
140
+ book = epub.read_epub(epub_path)
141
+
142
+ previous_filename = ''
143
+ chapter_counter = 0
144
+
145
+ for item in book.get_items():
146
+ if item.get_type() == ebooklib.ITEM_DOCUMENT:
147
+ soup = BeautifulSoup(item.get_content(), 'html.parser')
148
+ text = soup.get_text()
149
+
150
+ if text.strip():
151
+ if len(text) < 2300 and previous_filename:
152
+ with open(previous_filename, 'a', encoding='utf-8') as file:
153
+ file.write('\n' + text)
154
+ else:
155
+ previous_filename = os.path.join(directory, f"chapter_{chapter_counter}.txt")
156
+ chapter_counter += 1
157
+ with open(previous_filename, 'w', encoding='utf-8') as file:
158
+ file.write(text)
159
+ print(f"Saved chapter: {previous_filename}")
160
+
161
+ input_ebook = ebook_file_path
162
+ output_epub = os.path.join(".", "Working_files", "temp.epub")
163
+
164
+ if os.path.exists(output_epub):
165
+ os.remove(output_epub)
166
+ print(f"File {output_epub} has been removed.")
167
+ else:
168
+ print(f"The file {output_epub} does not exist.")
169
+
170
+ if convert_to_epub(input_ebook, output_epub):
171
+ save_chapters_as_text(output_epub)
172
+
173
+ # nltk.download('punkt')
174
+
175
+ def process_chapter_files(folder_path, output_csv):
176
+ with open(output_csv, 'w', newline='', encoding='utf-8') as csvfile:
177
+ writer = csv.writer(csvfile)
178
+ writer.writerow(['Text', 'Start Location', 'End Location', 'Is Quote', 'Speaker', 'Chapter'])
179
+
180
+ chapter_files = sorted(os.listdir(folder_path), key=lambda x: int(x.split('_')[1].split('.')[0]))
181
+ for filename in chapter_files:
182
+ if filename.startswith('chapter_') and filename.endswith('.txt'):
183
+ chapter_number = int(filename.split('_')[1].split('.')[0])
184
+ file_path = os.path.join(folder_path, filename)
185
+
186
+ try:
187
+ with open(file_path, 'r', encoding='utf-8') as file:
188
+ text = file.read()
189
+ if text:
190
+ text = "NEWCHAPTERABC" + text
191
+ sentences = nltk.tokenize.sent_tokenize(text)
192
+ for sentence in sentences:
193
+ start_location = text.find(sentence)
194
+ end_location = start_location + len(sentence)
195
+ writer.writerow([sentence, start_location, end_location, 'True', 'Narrator', chapter_number])
196
+ except Exception as e:
197
+ print(f"Error processing file {filename}: {e}")
198
+
199
+ folder_path = os.path.join(".", "Working_files", "temp_ebook")
200
+ output_csv = os.path.join(".", "Working_files", "Book", "Other_book.csv")
201
+
202
+ process_chapter_files(folder_path, output_csv)
203
+
204
+ def sort_key(filename):
205
+ match = re.search(r'chapter_(\d+)\.txt', filename)
206
+ return int(match.group(1)) if match else 0
207
+
208
+ def combine_chapters(input_folder, output_file):
209
+ os.makedirs(os.path.dirname(output_file), exist_ok=True)
210
+
211
+ files = [f for f in os.listdir(input_folder) if f.endswith('.txt')]
212
+ sorted_files = sorted(files, key=sort_key)
213
+
214
+ with open(output_file, 'w', encoding='utf-8') as outfile:
215
+ for i, filename in enumerate(sorted_files):
216
+ with open(os.path.join(input_folder, filename), 'r', encoding='utf-8') as infile:
217
+ outfile.write(infile.read())
218
+ if i < len(sorted_files) - 1:
219
+ outfile.write("\nNEWCHAPTERABC\n")
220
+
221
+ input_folder = os.path.join(".", 'Working_files', 'temp_ebook')
222
+ output_file = os.path.join(".", 'Working_files', 'Book', 'Chapter_Book.txt')
223
+
224
+ combine_chapters(input_folder, output_file)
225
+
226
+ ensure_directory(os.path.join(".", "Working_files", "Book"))
227
+
228
+ def convert_chapters_to_audio_espeak(chapters_dir, output_audio_dir, speed="170", pitch="50", voice="en"):
229
+ if not os.path.exists(output_audio_dir):
230
+ os.makedirs(output_audio_dir)
231
+
232
+ for chapter_file in sorted(os.listdir(chapters_dir)):
233
+ if chapter_file.endswith('.txt'):
234
+ match = re.search(r"chapter_(\d+).txt", chapter_file)
235
+ if match:
236
+ chapter_num = int(match.group(1))
237
+ else:
238
+ print(f"Skipping file {chapter_file} as it does not match the expected format.")
239
+ continue
240
+
241
+ chapter_path = os.path.join(chapters_dir, chapter_file)
242
+ output_file_name = f"audio_chapter_{chapter_num}.wav"
243
+ output_file_path = os.path.join(output_audio_dir, output_file_name)
244
+
245
+ with open(chapter_path, 'r', encoding='utf-8') as file:
246
+ chapter_text = file.read()
247
+ sentences = nltk.tokenize.sent_tokenize(chapter_text)
248
+ combined_audio = AudioSegment.empty()
249
+
250
+ for sentence in tqdm(sentences, desc=f"Chapter {chapter_num}"):
251
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_wav:
252
+ subprocess.run(["espeak-ng", "-v", voice, "-w", temp_wav.name, f"-s{speed}", f"-p{pitch}", sentence])
253
+ combined_audio += AudioSegment.from_wav(temp_wav.name)
254
+ os.remove(temp_wav.name)
255
+
256
+ combined_audio.export(output_file_path, format='wav')
257
+ print(f"Converted chapter {chapter_num} to audio.")
258
+
259
+ def convert_ebook_to_audio(ebook_file, speed, pitch, voice, progress=gr.Progress()):
260
+ ebook_file_path = ebook_file.name
261
+ working_files = os.path.join(".", "Working_files", "temp_ebook")
262
+ full_folder_working_files = os.path.join(".", "Working_files")
263
+ chapters_directory = os.path.join(".", "Working_files", "temp_ebook")
264
+ output_audio_directory = os.path.join(".", 'Chapter_wav_files')
265
+ remove_folder_with_contents(full_folder_working_files)
266
+ remove_folder_with_contents(output_audio_directory)
267
+
268
+ try:
269
+ progress(0.1, desc="Creating chapter-labeled book")
270
+ except Exception as e:
271
+ print(f"Error updating progress: {e}")
272
+
273
+ create_chapter_labeled_book(ebook_file_path)
274
+ audiobook_output_path = os.path.join(".", "Audiobooks")
275
+
276
+ try:
277
+ progress(0.3, desc="Converting chapters to audio")
278
+ except Exception as e:
279
+ print(f"Error updating progress: {e}")
280
+
281
+ convert_chapters_to_audio_espeak(chapters_directory, output_audio_directory, speed, pitch, voice.split()[0])
282
+
283
+ try:
284
+ progress(0.9, desc="Creating M4B from chapters")
285
+ except Exception as e:
286
+ print(f"Error updating progress: {e}")
287
+
288
+ create_m4b_from_chapters(output_audio_directory, ebook_file_path, audiobook_output_path)
289
+
290
+ m4b_filename = os.path.splitext(os.path.basename(ebook_file_path))[0] + '.m4b'
291
+ m4b_filepath = os.path.join(audiobook_output_path, m4b_filename)
292
+
293
+ try:
294
+ progress(1.0, desc="Conversion complete")
295
+ except Exception as e:
296
+ print(f"Error updating progress: {e}")
297
+ print(f"Audiobook created at {m4b_filepath}")
298
+ return f"Audiobook created at {m4b_filepath}", m4b_filepath
299
+
300
+ def list_audiobook_files(audiobook_folder):
301
+ files = []
302
+ for filename in os.listdir(audiobook_folder):
303
+ if filename.endswith('.m4b'):
304
+ files.append(os.path.join(audiobook_folder, filename))
305
+ return files
306
+
307
+ def download_audiobooks():
308
+ audiobook_output_path = os.path.join(".", "Audiobooks")
309
+ return list_audiobook_files(audiobook_output_path)
310
+
311
+ def get_available_voices():
312
+ result = subprocess.run(['espeak-ng', '--voices'], stdout=subprocess.PIPE, text=True)
313
+ lines = result.stdout.splitlines()[1:] # Skip the header line
314
+ voices = []
315
+ for line in lines:
316
+ parts = line.split()
317
+ if len(parts) > 1:
318
+ voice_name = parts[1]
319
+ description = ' '.join(parts[2:])
320
+ voices.append((voice_name, description))
321
+ return voices
322
+
323
+ theme = gr.themes.Soft(
324
+ primary_hue="blue",
325
+ secondary_hue="blue",
326
+ neutral_hue="blue",
327
+ text_size=gr.themes.sizes.text_md,
328
+ )
329
+
330
+ # Gradio UI setup
331
+ with gr.Blocks(theme=theme) as demo:
332
+ gr.Markdown(
333
+ """
334
+ # eBook to Audiobook Converter
335
+
336
+ Convert your eBooks into audiobooks using eSpeak-NG.
337
+ """
338
+ )
339
+
340
+ with gr.Row():
341
+ with gr.Column(scale=3):
342
+ ebook_file = gr.File(label="eBook File")
343
+ speed = gr.Slider(minimum=80, maximum=450, value=170, step=1, label="Speed")
344
+ pitch = gr.Slider(minimum=0, maximum=99, value=50, step=1, label="Pitch")
345
+ voices = get_available_voices()
346
+ voice_choices = [f"{voice} ({desc})" for voice, desc in voices]
347
+ voice_dropdown = gr.Dropdown(choices=voice_choices, label="Select Voice", value=voice_choices[0])
348
+
349
+ convert_btn = gr.Button("Convert to Audiobook", variant="primary")
350
+ output = gr.Textbox(label="Conversion Status")
351
+ audio_player = gr.Audio(label="Audiobook Player", type="filepath")
352
+ download_btn = gr.Button("Download Audiobook Files")
353
+ download_files = gr.File(label="Download Files", interactive=False)
354
+
355
+ convert_btn.click(
356
+ convert_ebook_to_audio,
357
+ inputs=[ebook_file, speed, pitch, voice_dropdown],
358
+ outputs=[output, audio_player]
359
+ )
360
+
361
+ download_btn.click(
362
+ download_audiobooks,
363
+ outputs=[download_files]
364
+ )
365
+
366
+ demo.launch(share=True)