| |
| """ |
| Fine-tune XTTS v2 for Nigerian Languages (Yoruba, Hausa, Igbo, Pidgin). |
| |
| This script uses Coqui TTS to fine-tune the XTTS model for better |
| Nigerian language support. |
| """ |
| import os |
| import sys |
| import json |
| from pathlib import Path |
| import torch |
|
|
| |
| print("=" * 60) |
| print("XTTS Nigerian Languages Fine-tuning") |
| print("=" * 60) |
| print(f"\nPyTorch: {torch.__version__}") |
| print(f"CUDA available: {torch.cuda.is_available()}") |
| if torch.cuda.is_available(): |
| print(f"GPU: {torch.cuda.get_device_name(0)}") |
| mem = torch.cuda.get_device_properties(0).total_memory / 1024**3 |
| print(f"GPU Memory: {mem:.1f} GB") |
| else: |
| print("WARNING: No GPU found. Training will be very slow on CPU.") |
|
|
| BASE_DIR = Path.home() / "voice-training" |
| PREPARED_DIR = BASE_DIR / "prepared_data" |
| OPENSLR_DIR = BASE_DIR / "datasets" / "openslr_yoruba" |
| OUTPUT_DIR = BASE_DIR / "output" |
|
|
| def check_data(): |
| """Check available training data.""" |
| print("\n=== Available Training Data ===") |
| |
| total_files = 0 |
| |
| |
| for lang in ["yoruba", "hausa", "igbo"]: |
| manifest_file = PREPARED_DIR / lang / "manifest.json" |
| if manifest_file.exists(): |
| with open(manifest_file) as f: |
| data = json.load(f) |
| print(f" {lang.upper()}: {len(data)} samples") |
| total_files += len(data) |
| |
| |
| if OPENSLR_DIR.exists(): |
| wav_count = len(list(OPENSLR_DIR.glob("*.wav"))) |
| print(f" OpenSLR Yoruba: {wav_count} high-quality WAV files") |
| total_files += wav_count |
| |
| print(f"\nTotal audio files: {total_files}") |
| return total_files > 0 |
|
|
| def prepare_xtts_dataset(): |
| """Prepare dataset in XTTS format.""" |
| print("\n=== Preparing XTTS Dataset ===") |
| |
| OUTPUT_DIR.mkdir(parents=True, exist_ok=True) |
| |
| all_samples = [] |
| |
| |
| for lang in ["yoruba", "hausa", "igbo"]: |
| manifest_file = PREPARED_DIR / lang / "manifest.json" |
| if manifest_file.exists(): |
| with open(manifest_file) as f: |
| samples = json.load(f) |
| for s in samples: |
| s['lang_code'] = lang[:2] |
| all_samples.extend(samples) |
| |
| |
| if OPENSLR_DIR.exists(): |
| tsv_file = OPENSLR_DIR / "line_index.tsv" |
| if tsv_file.exists(): |
| with open(tsv_file, 'r', encoding='utf-8') as f: |
| for line in f: |
| parts = line.strip().split('\t') |
| if len(parts) >= 2: |
| wav_file = OPENSLR_DIR / f"{parts[0]}.wav" |
| if wav_file.exists(): |
| all_samples.append({ |
| "audio_file": str(wav_file), |
| "text": parts[1], |
| "language": "yoruba", |
| "lang_code": "yo" |
| }) |
| |
| |
| dataset_file = OUTPUT_DIR / "nigerian_tts_dataset.json" |
| with open(dataset_file, 'w', encoding='utf-8') as f: |
| json.dump(all_samples, f, indent=2, ensure_ascii=False) |
| |
| print(f" Created dataset with {len(all_samples)} samples") |
| print(f" Saved to: {dataset_file}") |
| |
| return all_samples |
|
|
| def run_xtts_finetuning(): |
| """Run XTTS fine-tuning using Coqui TTS.""" |
| print("\n=== Starting XTTS Fine-tuning ===") |
| |
| try: |
| from TTS.tts.configs.xtts_config import XttsConfig |
| from TTS.tts.models.xtts import Xtts |
| from TTS.utils.manage import ModelManager |
| |
| print(" TTS modules loaded successfully") |
| |
| |
| print(" Downloading base XTTS v2 model...") |
| model_manager = ModelManager() |
| |
| |
| model_path = model_manager.download_model("tts_models/multilingual/multi-dataset/xtts_v2") |
| print(f" Model path: {model_path}") |
| |
| print("\n To fine-tune XTTS, use the Coqui TTS training recipes:") |
| print(" https://github.com/coqui-ai/TTS/tree/dev/recipes/ljspeech/xtts_v2") |
| print("\n Or use the XTTS fine-tuning demo:") |
| print(" python -m TTS.demos.xtts_ft_demo") |
| |
| return True |
| |
| except Exception as e: |
| print(f" Error: {e}") |
| return False |
|
|
| def main(): |
| if not check_data(): |
| print("ERROR: No training data found!") |
| sys.exit(1) |
| |
| samples = prepare_xtts_dataset() |
| |
| if samples: |
| print("\n" + "=" * 60) |
| print("Dataset prepared! Next steps:") |
| print("=" * 60) |
| print(f"1. Dataset: {OUTPUT_DIR / 'nigerian_tts_dataset.json'}") |
| print(f"2. Total samples: {len(samples)}") |
| print("\nTo start training:") |
| print(" python -m TTS.demos.xtts_ft_demo") |
| print("\nOr for voice cloning (no training needed):") |
| print(" from TTS.api import TTS") |
| print(" tts = TTS('tts_models/multilingual/multi-dataset/xtts_v2')") |
| print(" tts.tts_to_file('Hello', speaker_wav='your_voice.wav', language='en')") |
| |
| run_xtts_finetuning() |
|
|
| if __name__ == "__main__": |
| main() |
|
|