Spaces:
Runtime error
Runtime error
Delete tools
Browse filesFiles changed (17)
- tools/convert_to_gif.shDeleted-43
- tools/generate_ebooks.pyDeleted-85
- tools/gpu_notebook_test.pyDeleted-11
- tools/gpu_test.pyDeleted-32
- tools/m4b_chapter_extractor.pyDeleted-245
- tools/normalize_wav_file.pyDeleted-103
- tools/normalize_wav_folder.pyDeleted-106
- tools/npz_to_wav.pyDeleted-61
- tools/trim_silences.pyDeleted-39
- tools/wav_to_npz.pyDeleted-141
- tools/workflow-testing/long_test.txtDeleted-1
- tools/workflow-testing/test1.txtDeleted-1
- tools/workflow-testing/test2.txtDeleted-1
- tools/workflow-testing/test3.txtDeleted-1
- tools/workflow-testing/test4.txtDeleted-1
- tools/workflow-testing/test5.txtDeleted-1
- tools/workflow-testing/urd-script_davanagari-test.txtDeleted-1
12345678910111213141516171819202122232425262728293031323334353637383940414243#!/bin/bash
# Help functionfunction show_help() { echo "Usage: $0 <input_video>" echo echo "This script converts a video file to a GIF with the following properties:" echo " - Frame rate: 10 fps" echo " - Resolution: 1728x1028 (with padding if needed)" echo echo "Example:" echo " $0 input.mov" echo echo "The output file will be named 'output.gif'."}
# Check if help is requestedif [[ "$1" == "-h" || "$1" == "--h" || "$1" == "-help" || "$1" == "--help" ]]; then show_help exit 0fi
# Check if the input file is providedif [ "$#" -ne 1 ]; then echo "Error: Missing input file." echo "Use --help for usage information." exit 1fi
# Input video fileinput_file="$1"output_file="output.gif"
# Conversion parametersfps=10width=1728height=1028
# Convert the video to GIFffmpeg -i "$input_file" -vf "fps=$fps,scale=${width}:${height}:force_original_aspect_ratio=decrease,pad=${width}:${height}:(ow-iw)/2:(oh-ih)/2" "$output_file"
echo "Conversion complete! Output file: $output_file"
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485import os
import sys
import subprocess
from iso639 import languages
from deep_translator import GoogleTranslator
from tqdm import tqdm
#parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
#sys.path.append(parent_dir)
# Your language mapping dictionary from lang.py
from lib.lang import language_mapping
env = os.environ.copy()
os.environ['COQUI_TOS_AGREED'] = '1'
env["PYTHONIOENCODING"] = "utf-8";
env["LANG"] = "en_US.UTF-8"
# Base text to be translated
base_text = "This is a test from the result of text file to audiobook conversion."
# Output directory
output_dir = "../ebooks/tests"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Path to your base cover image (adjust the path accordingly)
base_cover_image = "../ebooks/tests/__cover.jpg"
# List to keep track of languages that failed
failed_languages = []
# Loop over languages with a progress bar
for lang_code, lang_info in tqdm(language_mapping.items(), desc="Processing languages"):
try:
lang_iso = lang_code
language_array = languages.get(part3=lang_code)
if language_array and language_array.part1:
lang_iso = language_array.part1
if lang_iso == "zh":
lang_iso = "zh-CN"
# Translate the text
translated_text = GoogleTranslator(source='en', target=lang_iso).translate(base_text)
print(f"\nTranslated text for {lang_info['name']} ({lang_iso}): {translated_text}")
# Write the translated text to a txt file
txt_filename = f"test_{lang_code}.txt"
txt_filepath = os.path.join(output_dir, txt_filename)
with open(txt_filepath, 'w', encoding='utf-8') as f:
f.write(translated_text)
# Prepare the ebook-convert command
azw3_filename = f"test_{lang_code}.azw3"
azw3_filepath = os.path.join(output_dir, azw3_filename)
title = f"Ebook {lang_info['name']} Test"
authors = "Dev Team"
language = lang_iso
command = [
"ebook-convert",
txt_filepath,
azw3_filepath,
"--cover", base_cover_image,
"--title", title,
"--authors", authors,
"--language", language,
"--input-encoding", "utf-8"
]
result = subprocess.run(command, env=env, text=True, encoding="utf-8")
print(f"Ebook generated for {lang_info['name']} at {azw3_filepath}\n")
except Exception as e:
print(f"Erro: language {lang_code} not supported!")
failed_languages.append(lang_code)
continue
# After processing all languages, output the list of languages that failed
if failed_languages:
print("\nThe following languages could not be processed:")
for lang_code in failed_languages:
lang_name = language_mapping[lang_code]['name']
print(f"- {lang_name} ({lang_code})")1234567891011import torch
print("Torch version:", torch.__version__)print("CUDA Available:", torch.cuda.is_available())
if torch.cuda.is_available(): print("Number of GPUs:", torch.cuda.device_count()) for i in range(torch.cuda.device_count()): print(f"GPU {i}: {torch.cuda.get_device_name(i)}")else: print("No GPU detected.")1234567891011121314151617181920212223242526272829303132import torch
def gpu_test(): print("Checking PyTorch GPU support...")
# Check if CUDA is available if not torch.cuda.is_available(): print("CUDA is NOT available. Running on CPU.") return
# Get number of GPUs num_gpus = torch.cuda.device_count() print(f"CUDA is available. Number of GPUs: {num_gpus}")
# List all GPUs for i in range(num_gpus): print(f"GPU {i}: {torch.cuda.get_device_name(i)}")
# Run a small test on the first GPU device = torch.device("cuda:0") print(f"Running a small test on {torch.cuda.get_device_name(0)}...")
# Test computation try: x = torch.rand(1000, 1000, device=device) y = torch.mm(x, x) print("GPU computation successful!") except Exception as e: print(f"GPU computation failed: {e}")
if __name__ == "__main__": gpu_test()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245#!/usr/bin/env python3"""M4B Chapter Extractor
A command-line tool to extract chapters from M4B audiobook filesand save them as individual MP3 files.
Requirements:- ffmpeg installed and accessible in PATH- Python 3.6+
Usage: python m4b_chapter_extractor.py input.m4b -o output_folder"""
import argparseimport osimport sysimport subprocessimport jsonimport refrom pathlib import Pathfrom typing import List, Dict, Optional
class M4BChapterExtractor: def __init__(self, input_file: str, output_dir: str, quality: str = "192k"): self.input_file = Path(input_file) self.output_dir = Path(output_dir) self.quality = quality # Validate input file if not self.input_file.exists(): raise FileNotFoundError(f"Input file not found: {input_file}") if not self.input_file.suffix.lower() in ['.m4b', '.m4a']: raise ValueError("Input file must be an M4B or M4A file") # Create output directory self.output_dir.mkdir(parents=True, exist_ok=True) def check_ffmpeg(self) -> bool: """Check if FFmpeg is available in the system PATH.""" try: result = subprocess.run(['ffmpeg', '-version'], capture_output=True, text=True) return result.returncode == 0 except FileNotFoundError: return False def get_chapters(self) -> List[Dict]: """Extract chapter information from the M4B file.""" cmd = [ 'ffprobe', '-v', 'quiet', '-print_format', 'json', '-show_chapters', str(self.input_file) ] try: result = subprocess.run(cmd, capture_output=True, text=True, check=True) data = json.loads(result.stdout) return data.get('chapters', []) except subprocess.CalledProcessError as e: print(f"Error getting chapters: {e}") return [] except json.JSONDecodeError as e: print(f"Error parsing chapter data: {e}") return [] def sanitize_filename(self, filename: str) -> str: """Sanitize filename by removing/replacing invalid characters.""" # Remove or replace invalid characters filename = re.sub(r'[<>:"/\\|?*]', '', filename) filename = re.sub(r'\s+', ' ', filename).strip() # Ensure filename isn't too long (limit to 200 characters) if len(filename) > 200: filename = filename[:200].strip() return filename or "Chapter" def format_time(self, seconds: float) -> str: """Convert seconds to HH:MM:SS.mmm format.""" hours = int(seconds // 3600) minutes = int((seconds % 3600) // 60) secs = seconds % 60 return f"{hours:02d}:{minutes:02d}:{secs:06.3f}" def extract_chapter(self, chapter: Dict, chapter_num: int, total_chapters: int) -> bool: """Extract a single chapter to MP3 file.""" # Get chapter title title = chapter.get('tags', {}).get('title', f"Chapter {chapter_num:02d}") title = self.sanitize_filename(title) # Create output filename output_filename = f"{chapter_num:02d} - {title}.mp3" output_path = self.output_dir / output_filename # Get start and end times start_time = float(chapter['start_time']) end_time = float(chapter['end_time']) duration = end_time - start_time print(f"Extracting [{chapter_num}/{total_chapters}]: {title}") print(f" Duration: {self.format_time(duration)}") # FFmpeg command to extract chapter cmd = [ 'ffmpeg', '-i', str(self.input_file), '-ss', str(start_time), '-t', str(duration), '-acodec', 'libmp3lame', '-ab', self.quality, '-map_metadata', '0', '-id3v2_version', '3', '-metadata', f'title={title}', '-metadata', f'track={chapter_num}/{total_chapters}', '-y', # Overwrite output file str(output_path) ] try: result = subprocess.run(cmd, capture_output=True, text=True) if result.returncode == 0: print(f" ✓ Saved: {output_filename}") return True else: print(f" ✗ Error extracting chapter: {result.stderr}") return False except Exception as e: print(f" ✗ Exception during extraction: {e}") return False def extract_all_chapters(self) -> bool: """Extract all chapters from the M4B file.""" print(f"Processing: {self.input_file.name}") print(f"Output directory: {self.output_dir}") # Check if FFmpeg is available if not self.check_ffmpeg(): print("Error: FFmpeg not found. Please install FFmpeg and ensure it's in your PATH.") return False # Get chapters chapters = self.get_chapters() if not chapters: print("No chapters found in the M4B file.") return False print(f"Found {len(chapters)} chapters") print("-" * 50) # Extract each chapter success_count = 0 for i, chapter in enumerate(chapters, 1): if self.extract_chapter(chapter, i, len(chapters)): success_count += 1 print() # Summary print("-" * 50) print(f"Extraction complete: {success_count}/{len(chapters)} chapters extracted successfully") if success_count == len(chapters): print("All chapters extracted successfully!") return True else: print(f"Warning: {len(chapters) - success_count} chapters failed to extract") return False
def main(): parser = argparse.ArgumentParser( description="Extract chapters from M4B audiobook files as individual MP3 files", formatter_class=argparse.RawDescriptionHelpFormatter, epilog="""Examples: python m4b_chapter_extractor.py audiobook.m4b -o chapters/ python m4b_chapter_extractor.py audiobook.m4b -o output/ -q 128k python m4b_chapter_extractor.py audiobook.m4b -o output/ --quality 256k
Requirements: - FFmpeg must be installed and accessible in PATH - Input file must be M4B or M4A format """ ) parser.add_argument( 'input_file', help='Path to the input M4B audiobook file' ) parser.add_argument( '-o', '--output', required=True, help='Output directory for extracted MP3 chapters' ) parser.add_argument( '-q', '--quality', default='192k', help='MP3 audio quality/bitrate (default: 192k). Examples: 128k, 192k, 256k, 320k' ) parser.add_argument( '-v', '--verbose', action='store_true', help='Enable verbose output' ) args = parser.parse_args() try: # Create extractor instance extractor = M4BChapterExtractor( input_file=args.input_file, output_dir=args.output, quality=args.quality ) # Extract chapters success = extractor.extract_all_chapters() # Exit with appropriate code sys.exit(0 if success else 1) except FileNotFoundError as e: print(f"Error: {e}") sys.exit(1) except ValueError as e: print(f"Error: {e}") sys.exit(1) except KeyboardInterrupt: print("\nOperation cancelled by user") sys.exit(1) except Exception as e: print(f"Unexpected error: {e}") sys.exit(1)
if __name__ == "__main__": main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103import osimport sysimport subprocessimport argparseimport shutilimport torch
def demucs_voice(wav_file, output_dir, models_dir): try: # Set TORCH_HOME for demucs torch.hub.set_dir(models_dir) os.environ['TORCH_HOME'] = models_dir demucs_app = shutil.which('demucs') if not demucs_app: demucs_app = os.path.join('..', 'python_env', 'Scripts', 'demucs') # Run demucs subprocess cmd = [ demucs_app, "--verbose", "--two-stems=vocals", "--out", output_dir, wav_file ]
print(f"🔄 Running: {' '.join(cmd)}") subprocess.run(cmd, check=True)
# Output folder name is based on input filename base_name = os.path.splitext(os.path.basename(wav_file))[0] demucs_output_path = os.path.join(output_dir, "htdemucs", "vocals.wav") if os.path.exists(demucs_output_path): print(f"✅ Voice track saved to: {demucs_output_path}") return demucs_output_path else: raise FileNotFoundError(f"Expected output not found: {demucs_output_path}")
except subprocess.CalledProcessError as e: raise RuntimeError( f"demucs failed with exit code {e.returncode}.\n" f"stdout: {getattr(e, 'output', 'N/A')}\n" f"stderr: {getattr(e, 'stderr', 'N/A')}" ) except FileNotFoundError as e: raise RuntimeError(f"FileNotFoundError: {e}") except Exception as e: raise RuntimeError(f"Unexpected error: {e}")
def normalize_audio_file(input_file, output_file): models_dir = os.path.join('..', 'models', 'tts') output_dir = os.path.dirname(output_file) #demucs_file = demucs_voice(input_file, output_dir, models_dir) ffmpeg_cmd = [shutil.which('ffmpeg'), '-hide_banner', '-nostats', '-i', input_file] filter_complex = ( 'agate=threshold=-25dB:ratio=1.4:attack=10:release=250,' 'afftdn=nf=-70,' 'acompressor=threshold=-20dB:ratio=2:attack=80:release=200:makeup=1dB,' 'loudnorm=I=-14:TP=-3:LRA=7:linear=true,' 'equalizer=f=150:t=q:w=2:g=1,' 'equalizer=f=250:t=q:w=2:g=-3,' 'equalizer=f=3000:t=q:w=2:g=2,' 'equalizer=f=5500:t=q:w=2:g=-4,' 'equalizer=f=9000:t=q:w=2:g=-2,' 'highpass=f=63[audio]' ) ffmpeg_cmd += [ '-filter_complex', filter_complex, '-map', '[audio]', '-ar', '24000', '-y', output_file ] try: process = subprocess.Popen( ffmpeg_cmd, env={}, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, universal_newlines=True, encoding='utf-8' ) for line in process.stdout: print(line, end='') # Print each line of stdout process.wait() if process.returncode != 0: error = f'normalize_audio(): process.returncode: {process.returncode}' elif not os.path.exists(output_file) or os.path.getsize(output_file) == 0: error = f'normalize_audio() error: {output_file} was not created or is empty.' else: print(f"File denoised and normalized!: {output_file}") except subprocess.CalledProcessError as e: error = f'_normalize_audio() ffmpeg.Error: {e.stderr.decode()}' except subprocess.CalledProcessError as e: print(f"Error processing file {input_file}: {e}") except Exception as e: print(f"Unexpected error: {e}")
if __name__ == "__main__": if len(sys.argv) != 3: print(f"Usage: python {os.path.basename(__file__)} <input_file> <output_file>") sys.exit(1) input_file = os.path.abspath(sys.argv[1]) output_file = os.path.abspath(sys.argv[2]) normalize_audio_file(input_file, output_file)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import osimport sysimport subprocessimport argparseimport torchimport shutil
def demucs_voice(wav_file, output_dir, models_dir): try: # Set TORCH_HOME for demucs torch.hub.set_dir(models_dir) os.environ['TORCH_HOME'] = models_dir # Run demucs subprocess cmd = [ os.path.join('..', 'python_env', 'bin', 'demucs'), "--verbose", "--two-stems=vocals", "--out", output_dir, wav_file ] print(f"🔄 Running: {' '.join(cmd)}") subprocess.run(cmd, check=True) # Output folder name is based on input filename base_name = os.path.splitext(os.path.basename(wav_file))[0] demucs_output_path = os.path.join(output_dir, "htdemucs", base_name, "vocals.wav") if os.path.exists(demucs_output_path): print(f"✅ Voice track saved to: {demucs_output_path}") return demucs_output_path else: raise FileNotFoundError(f"Expected output not found: {demucs_output_path}") except subprocess.CalledProcessError as e: raise RuntimeError( f"demucs failed with exit code {e.returncode}.\n" f"stdout: {getattr(e, 'output', 'N/A')}\n" f"stderr: {getattr(e, 'stderr', 'N/A')}" ) except FileNotFoundError as e: raise RuntimeError(f"FileNotFoundError: {e}") except Exception as e: raise RuntimeError(f"Unexpected error: {e}")
def normalize_audio_folder(folder_path): for root, dirs, files in os.walk(folder_path): for file in files: if file.lower().endswith('.wav'): input_file = os.path.join(root, file) models_dir = os.path.join('..', 'models', 'tts') #demucs_file = demucs_voice(input_file, folder_path, models_dir) process_file = os.path.join(root, 'temp_output.wav') # Temporary file to avoid overwriting during processing ffmpeg_cmd = [shutil.which('ffmpeg'), '-hide_banner', '-nostats', '-i', input_file] filter_complex = ( 'agate=threshold=-25dB:ratio=1.4:attack=10:release=250,' 'afftdn=nf=-70,' 'acompressor=threshold=-20dB:ratio=2:attack=80:release=200:makeup=1dB,' 'loudnorm=I=-14:TP=-3:LRA=7:linear=true,' 'equalizer=f=150:t=q:w=2:g=1,' 'equalizer=f=250:t=q:w=2:g=-3,' 'equalizer=f=3000:t=q:w=2:g=2,' 'equalizer=f=5500:t=q:w=2:g=-4,' 'equalizer=f=9000:t=q:w=2:g=-2,' 'highpass=f=63[audio]' ) ffmpeg_cmd += [ '-filter_complex', filter_complex, '-map', '[audio]', '-ar', '24000', '-y', process_file ] try: process = subprocess.Popen( ffmpeg_cmd, env={}, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, universal_newlines=True, encoding='utf-8' ) for line in process.stdout: print(line, end='') # Print each line of stdout process.wait() if process.returncode != 0: error = f'normalize_audio(): process.returncode: {process.returncode}' break elif not os.path.exists(process_file) or os.path.getsize(process_file) == 0: error = f'normalize_audio() error: {process_file} was not created or is empty.' break else: os.replace(process_file, input_file) print(f"File processed and replaced: {input_file}") except subprocess.CalledProcessError as e: error = f'_normalize_audio() ffmpeg.Error: {e.stderr.decode()}' break except subprocess.CalledProcessError as e: print(f"Error processing file {input_file}: {e}") break except Exception as e: print(f"Unexpected error: {e}") break
if __name__ == "__main__": if len(sys.argv) != 2: print(f"Usage: python {os.path.basename(__file__)} <folder_path>") sys.exit(1) folder_path = os.path.abspath(sys.argv[1]) normalize_audio_folder(folder_path)12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import osimport platformimport argparse
tmp_dir = os.path.abspath(os.path.join('..', 'tmp'))models_dir = os.path.abspath(os.path.join('..', 'models'))tts_dir = os.path.join(models_dir, 'tts')
os.environ['PYTHONUTF8'] = '1'os.environ['PYTHONIOENCODING'] = 'utf-8'os.environ['COQUI_TOS_AGREED'] = '1'os.environ['PYTHONIOENCODING'] = 'utf-8'os.environ['CALIBRE_NO_NATIVE_FILEDIALOGS'] = '1'os.environ['DO_NOT_TRACK'] = 'true'os.environ['CALIBRE_TEMP_DIR'] = tmp_diros.environ['CALIBRE_CACHE_DIRECTORY'] = tmp_diros.environ['HUGGINGFACE_HUB_CACHE'] = tts_diros.environ['HF_HOME'] = tts_diros.environ['HF_DATASETS_CACHE'] = tts_diros.environ['BARK_CACHE_DIR'] = tts_diros.environ['TTS_CACHE'] = tts_diros.environ['TORCH_HOME'] = tts_diros.environ['TTS_HOME'] = models_diros.environ['XDG_CACHE_HOME'] = models_diros.environ['ARGOS_TRANSLATE_PACKAGE_PATH'] = os.path.join(models_dir, 'argostranslate')os.environ['HF_TOKEN_PATH'] = os.path.join(os.path.expanduser('~'), '.huggingface_token')os.environ['TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD'] = '1'os.environ['PYTORCH_ENABLE_MPS_FALLBACK'] = '1'os.environ['SUNO_OFFLOAD_CPU'] = 'False' # BARK option: False needs A GPUos.environ['SUNO_USE_SMALL_MODELS'] = 'False' # BARK option: False needs a GPU with VRAM > 4GBif platform.system() == 'Windows': os.environ['ESPEAK_DATA_PATH'] = os.path.expandvars(r"%USERPROFILE%\scoop\apps\espeak-ng\current\eSpeak NG\espeak-ng-data")
import torchimport torchaudioimport numpy as npfrom pathlib import Pathfrom bark import SAMPLE_RATE, preload_modelsfrom bark.generation import codec_decode
def npz_to_wav(npz_path, output_path): preload_models() data = np.load(npz_path) fine_prompt = data["fine_prompt"] audio_array = codec_decode(fine_prompt) audio_tensor = torch.tensor(audio_array).unsqueeze(0) torchaudio.save(output_path, audio_tensor, SAMPLE_RATE) print(f"✅ Saved: {output_path}")
def process_all_npz_in_folder(folder_path): preload_models() for npz_file in Path(folder_path).rglob("*.npz"): output_path = npz_file.with_suffix(".wav") npz_to_wav(str(npz_file), str(output_path))
if __name__ == "__main__": parser = argparse.ArgumentParser(description="Process all NPZ files in a folder.") parser.add_argument("--folder_path", type=str, required=True, help="Path to the folder containing NPZ files") args = parser.parse_args() folder_path = os.path.abspath(args.folder_path) process_all_npz_in_folder(folder_path)123456789101112131415161718192021222324252627282930313233343536373839import osfrom pydub import AudioSegmentfrom pydub.silence import detect_nonsilentimport sys
def trim_silence(audio_path, silence_thresh=-70, min_silence_len=1000): audio = AudioSegment.from_file(audio_path) nonsilent_ranges = detect_nonsilent(audio, min_silence_len=min_silence_len, silence_thresh=silence_thresh)
if not nonsilent_ranges: return None # No nonsilent segment found
start_trim = nonsilent_ranges[0][0] end_trim = nonsilent_ranges[-1][1] trimmed_audio = audio[start_trim:end_trim] return trimmed_audio
def process_folder(folder_path): for root, _, files in os.walk(folder_path): for file in files: if file.endswith(".wav"): wav_path = os.path.join(root, file) print(f"Trimming: {wav_path}") trimmed = trim_silence(wav_path) if trimmed: trimmed.export(wav_path, format="wav") else: print(f"Warning: only silence found in {wav_path}")
if __name__ == "__main__": if len(sys.argv) != 2: print("Usage: python script.py <folder_path>") sys.exit(1)
target_folder = sys.argv[1] process_folder(target_folder)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141# NOTE: to run this script you must move it to the root of ebook2audiobook
import os
os.environ['PYTHONUTF8'] = '1'os.environ['PYTHONIOENCODING'] = 'utf-8'os.environ['COQUI_TOS_AGREED'] = '1'os.environ['PYTHONIOENCODING'] = 'utf-8'os.environ['DO_NOT_TRACK'] = 'true'os.environ['HUGGINGFACE_HUB_CACHE'] = tts_diros.environ['HF_HOME'] = tts_diros.environ['TRANSFORMERS_CACHE'] = tts_diros.environ['HF_DATASETS_CACHE'] = tts_diros.environ['BARK_CACHE_DIR'] = tts_diros.environ['TTS_CACHE'] = tts_diros.environ['TORCH_HOME'] = tts_diros.environ['TTS_HOME'] = models_diros.environ['XDG_CACHE_HOME'] = models_diros.environ['HF_TOKEN_PATH'] = os.path.join(os.path.expanduser('~'), '.huggingface_token')os.environ['TORCH_FORCE_NO_WEIGHTS_ONLY_LOAD'] = '1'os.environ['PYTORCH_ENABLE_MPS_FALLBACK'] = '1'os.environ['SUNO_OFFLOAD_CPU'] = 'False'os.environ['SUNO_USE_SMALL_MODELS'] = 'False'
import argparseimport hashlibimport numpy as npimport regex as reimport shutilimport soundfile as sfimport subprocessimport tempfileimport torchimport torchaudioimport threadingimport uuid
from iso639 import languagesfrom huggingface_hub import hf_hub_downloadfrom pathlib import Pathfrom scipy.io import wavfile as wavfrom scipy.signal import find_peaksfrom TTS.tts.configs.bark_config import BarkConfigfrom TTS.tts.models.bark import Bark
from lib import *
import logginglogging.basicConfig(level=logging.DEBUG)
torch.hub.set_dir(models_dir)
loaded_tts = {}
def load_checkpoint(**kwargs): try: key = kwargs.get('key') tts_engine = kwargs.get('tts_engine') device = kwargs.get('device') checkpoint_dir = kwargs.get('checkpoint_dir') config = BarkConfig() config.CACHE_DIR = tts_dir config.USE_SMALLER_MODELS = os.environ.get('SUNO_USE_SMALL_MODELS', '').lower() == 'true' tts = Bark.init_from_config(config) tts.load_checkpoint( config, checkpoint_dir=checkpoint_dir, eval=True ) if tts: if device == 'cuda': tts.cuda() else: tts.to(device) loaded_tts[key] = {"engine": tts, "config": config} msg = f'{tts_engine} Loaded!' print(msg) return tts else: error = 'TTS engine could not be created!' print(error) except Exception as e: error = f'_load_checkpoint() error: {e}' return False
def wav_to_npz(bark_dir, wav_dir): try: tts_internal_key = f"TTS_ENGINES['BARK']-internal" hf_repo = models[TTS_ENGINES['BARK']]['internal']['repo'] hf_sub = models[TTS_ENGINES['BARK']]['internal']['sub'] text_model_path = hf_hub_download(repo_id=hf_repo, filename=f"{hf_sub}{models[TTS_ENGINES['BARK']]['internal']['files'][0]}", cache_dir=tts_dir) coarse_model_path = hf_hub_download(repo_id=hf_repo, filename=f"{hf_sub}{models[TTS_ENGINES['BARK']]['internal']['files'][1]}", cache_dir=tts_dir) fine_model_path = hf_hub_download(repo_id=hf_repo, filename=f"{hf_sub}{models[TTS_ENGINES['BARK']]['internal']['files'][2]}", cache_dir=tts_dir) checkpoint_dir = os.path.dirname(text_model_path) tts = load_checkpoint(tts_engine=TTS_ENGINES['BARK'], key=tts_internal_key, checkpoint_dir=checkpoint_dir, device='cpu') if tts: fine_tuned_params = { "text_temp": default_engine_settings[TTS_ENGINES['BARK']]['text_temp'], "waveform_temp": default_engine_settings[TTS_ENGINES['BARK']]['waveform_temp'] } for root, dirs, files in os.walk(wav_dir): for file in files: if file.lower().endswith('.wav'): match = re.match(r"^([a-z]{2})_", file) if match: speaker = os.path.splitext(file)[0] npz_file = f'{speaker}.npz' iso1_lang = match.group(1) lang_array = languages.get(part1=iso1_lang) if lang_array: iso3_lang = lang_array.part3 default_text_file = os.path.join(voices_dir, iso3_lang, 'default.txt') default_text = Path(default_text_file).read_text(encoding="utf-8") with torch.no_grad(): torch.manual_seed(67878789) audio_data = tts.synthesize( default_text, loaded_tts[tts_internal_key]['config'], speaker_id=speaker, voice_dirs=bark_dir, silent=True, **fine_tuned_params ) del audio_data msg = f"Saved NPZ file: {npz_file}" print(msg) else: print('tts bark not loaded') except Exception as e: print(f'wav_to_npz() error: {e}')
if __name__ == "__main__": parser = argparse.ArgumentParser(description="Convert WAV files to Bark NPZ format.") parser.add_argument("--bark_dir", type=str, required=True, help="Path to the Bark asset directory") parser.add_argument("--wav_dir", type=str, required=True, help="Path to the output WAV directory") args = parser.parse_args() bark_dir = os.path.abspath(args.bark_dir) wav_dir = os.path.abspath(args.wav_dir) wav_to_npz(bark_dir, wav_dir)
1Alright here you go just a long stream of words flowing endlessly without any stops or breaks just moving forward like a river that never ends carrying thoughts and ideas without pause or hesitation like a mind racing through the night trying to grasp onto something solid but finding only more thoughts and more words and more movement like a dream that never quite settles into focus always shifting always changing always just beyond reach like running through a field with the wind in your hair feeling free and untethered by the rules of language or structure or anything at all just existing in the purest form of expression where nothing has to make perfect sense and yet it all still feels like it does in some strange and beautiful way like the way music can make you feel something without ever needing words at all just rhythm and motion and the way it carries you forward into something bigger than yourself into something vast and infinite and endless.1This is test file number 1.1This is test file number 2.1This is test file number 3.1This is test file number 4.1This is test file number 5.1यह टेस्ट फ़ाइल नंबर १ है।tools/convert_to_gif.shDeleted-43
tools/generate_ebooks.pyDeleted-85
tools/gpu_notebook_test.pyDeleted-11
tools/gpu_test.pyDeleted-32
tools/m4b_chapter_extractor.pyDeleted-245
tools/normalize_wav_file.pyDeleted-103
tools/normalize_wav_folder.pyDeleted-106
tools/npz_to_wav.pyDeleted-61
tools/trim_silences.pyDeleted-39
tools/wav_to_npz.pyDeleted-141
tools/workflow-testing/long_test.txtDeleted-1
tools/workflow-testing/test1.txtDeleted-1
tools/workflow-testing/test2.txtDeleted-1
tools/workflow-testing/test3.txtDeleted-1
tools/workflow-testing/test4.txtDeleted-1
tools/workflow-testing/test5.txtDeleted-1
tools/workflow-testing/urd-script_davanagari-test.txtDeleted-1