Spaces:
Running
Running
File size: 12,052 Bytes
d722140 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
#!/usr/bin/env python3
"""
Dataset Preparation Script for Indian Language TTS Training
This script prepares speech datasets for training VITS models on Indian languages.
It handles data from multiple sources and creates a unified format.
Supported Datasets:
- OpenSLR Indian Language Datasets
- Mozilla Common Voice (Indian subsets)
- IndicTTS Dataset (IIT Madras)
- Custom recordings
Output Format:
- audio/: Normalized WAV files (22050Hz, mono, 16-bit)
- metadata.csv: text|audio_path|speaker_id|duration
"""
import os
import sys
import csv
import json
import argparse
import logging
from pathlib import Path
from typing import List, Tuple, Optional
from dataclasses import dataclass
from concurrent.futures import ProcessPoolExecutor
import numpy as np
# Try to import audio processing libraries
try:
import librosa
import soundfile as sf
HAS_AUDIO = True
except ImportError:
HAS_AUDIO = False
print("Warning: librosa/soundfile not installed. Audio processing disabled.")
# Dataset configurations
DATASET_CONFIGS = {
"openslr_hindi": {
"url": "https://www.openslr.org/resources/103/",
"name": "OpenSLR Hindi ASR Corpus",
"language": "hindi",
"sample_rate": 16000,
},
"openslr_bengali": {
"url": "https://www.openslr.org/resources/37/",
"name": "OpenSLR Bengali Multi-speaker",
"language": "bengali",
"sample_rate": 16000,
},
"openslr_marathi": {
"url": "https://www.openslr.org/resources/64/",
"name": "OpenSLR Marathi",
"language": "marathi",
"sample_rate": 16000,
},
"openslr_telugu": {
"url": "https://www.openslr.org/resources/66/",
"name": "OpenSLR Telugu",
"language": "telugu",
"sample_rate": 16000,
},
"openslr_kannada": {
"url": "https://www.openslr.org/resources/79/",
"name": "OpenSLR Kannada",
"language": "kannada",
"sample_rate": 16000,
},
"openslr_gujarati": {
"url": "https://www.openslr.org/resources/78/",
"name": "OpenSLR Gujarati",
"language": "gujarati",
"sample_rate": 16000,
},
"commonvoice_hindi": {
"url": "https://commonvoice.mozilla.org/en/datasets",
"name": "Mozilla Common Voice Hindi",
"language": "hindi",
"sample_rate": 48000,
},
"indictts": {
"url": "https://www.iitm.ac.in/donlab/tts/",
"name": "IndicTTS Dataset (IIT Madras)",
"languages": ["hindi", "bengali", "marathi", "telugu", "kannada", "gujarati"],
"sample_rate": 22050,
},
}
@dataclass
class AudioSample:
"""Represents a single audio sample"""
audio_path: Path
text: str
speaker_id: str
language: str
duration: float = 0.0
sample_rate: int = 22050
class DatasetProcessor:
"""Process and prepare datasets for TTS training"""
TARGET_SAMPLE_RATE = 22050
MIN_DURATION = 0.5 # seconds
MAX_DURATION = 15.0 # seconds
def __init__(self, output_dir: Path, language: str):
self.output_dir = output_dir
self.language = language
self.audio_dir = output_dir / "audio"
self.audio_dir.mkdir(parents=True, exist_ok=True)
logging.basicConfig(level=logging.INFO)
self.logger = logging.getLogger(__name__)
def process_audio(self, input_path: Path, output_path: Path) -> Optional[float]:
"""
Process a single audio file:
- Resample to target sample rate
- Convert to mono
- Normalize volume
- Trim silence
"""
if not HAS_AUDIO:
return None
try:
# Load audio
audio, sr = librosa.load(input_path, sr=None, mono=True)
# Resample if necessary
if sr != self.TARGET_SAMPLE_RATE:
audio = librosa.resample(
audio, orig_sr=sr, target_sr=self.TARGET_SAMPLE_RATE
)
# Trim silence
audio, _ = librosa.effects.trim(audio, top_db=20)
# Normalize
audio = audio / np.abs(audio).max() * 0.95
# Calculate duration
duration = len(audio) / self.TARGET_SAMPLE_RATE
# Filter by duration
if duration < self.MIN_DURATION or duration > self.MAX_DURATION:
return None
# Save processed audio
sf.write(output_path, audio, self.TARGET_SAMPLE_RATE)
return duration
except Exception as e:
self.logger.warning(f"Error processing {input_path}: {e}")
return None
def process_openslr(self, data_dir: Path) -> List[AudioSample]:
"""Process OpenSLR format dataset"""
samples = []
# OpenSLR typically has transcripts.txt or similar
transcript_file = data_dir / "transcripts.txt"
if not transcript_file.exists():
transcript_file = data_dir / "text"
if transcript_file.exists():
with open(transcript_file, "r", encoding="utf-8") as f:
for line in f:
parts = line.strip().split("|")
if len(parts) >= 2:
audio_id, text = parts[0], parts[1]
audio_path = data_dir / "audio" / f"{audio_id}.wav"
if audio_path.exists():
output_path = self.audio_dir / f"{audio_id}.wav"
duration = self.process_audio(audio_path, output_path)
if duration:
samples.append(
AudioSample(
audio_path=output_path,
text=text,
speaker_id="spk_001",
language=self.language,
duration=duration,
)
)
return samples
def process_commonvoice(self, data_dir: Path) -> List[AudioSample]:
"""Process Mozilla Common Voice format"""
samples = []
# Common Voice uses validated.tsv
tsv_file = data_dir / "validated.tsv"
clips_dir = data_dir / "clips"
if tsv_file.exists():
with open(tsv_file, "r", encoding="utf-8") as f:
reader = csv.DictReader(f, delimiter="\t")
for row in reader:
audio_path = clips_dir / row["path"]
text = row["sentence"]
speaker_id = row.get("client_id", "unknown")[:8]
if audio_path.exists():
output_name = f"cv_{audio_path.stem}.wav"
output_path = self.audio_dir / output_name
duration = self.process_audio(audio_path, output_path)
if duration:
samples.append(
AudioSample(
audio_path=output_path,
text=text,
speaker_id=speaker_id,
language=self.language,
duration=duration,
)
)
return samples
def process_indictts(self, data_dir: Path) -> List[AudioSample]:
"""Process IndicTTS format dataset"""
samples = []
# IndicTTS has wav/ folder and txt/ folder
wav_dir = data_dir / "wav"
txt_dir = data_dir / "txt"
if wav_dir.exists() and txt_dir.exists():
for wav_file in wav_dir.glob("*.wav"):
txt_file = txt_dir / f"{wav_file.stem}.txt"
if txt_file.exists():
with open(txt_file, "r", encoding="utf-8") as f:
text = f.read().strip()
output_path = self.audio_dir / wav_file.name
duration = self.process_audio(wav_file, output_path)
if duration:
samples.append(
AudioSample(
audio_path=output_path,
text=text,
speaker_id="indic_001",
language=self.language,
duration=duration,
)
)
return samples
def save_metadata(self, samples: List[AudioSample]):
"""Save processed samples to metadata CSV"""
metadata_path = self.output_dir / "metadata.csv"
with open(metadata_path, "w", encoding="utf-8", newline="") as f:
writer = csv.writer(f, delimiter="|")
writer.writerow(["audio_path", "text", "speaker_id", "duration"])
for sample in samples:
writer.writerow(
[
sample.audio_path.name,
sample.text,
sample.speaker_id,
f"{sample.duration:.3f}",
]
)
self.logger.info(f"Saved {len(samples)} samples to {metadata_path}")
# Save statistics
stats = {
"total_samples": len(samples),
"total_duration_hours": sum(s.duration for s in samples) / 3600,
"language": self.language,
"speakers": len(set(s.speaker_id for s in samples)),
}
with open(self.output_dir / "stats.json", "w") as f:
json.dump(stats, f, indent=2)
self.logger.info(f"Dataset stats: {stats}")
def create_train_val_split(metadata_path: Path, train_ratio: float = 0.95):
"""Split metadata into train and validation sets"""
with open(metadata_path, "r", encoding="utf-8") as f:
reader = csv.reader(f, delimiter="|")
header = next(reader)
rows = list(reader)
# Shuffle
np.random.shuffle(rows)
# Split
split_idx = int(len(rows) * train_ratio)
train_rows = rows[:split_idx]
val_rows = rows[split_idx:]
# Save splits
for name, data in [("train", train_rows), ("val", val_rows)]:
output_path = metadata_path.parent / f"metadata_{name}.csv"
with open(output_path, "w", encoding="utf-8", newline="") as f:
writer = csv.writer(f, delimiter="|")
writer.writerow(header)
writer.writerows(data)
print(f"Saved {len(data)} samples to {output_path}")
def main():
parser = argparse.ArgumentParser(description="Prepare datasets for TTS training")
parser.add_argument(
"--input", type=str, required=True, help="Input dataset directory"
)
parser.add_argument("--output", type=str, required=True, help="Output directory")
parser.add_argument("--language", type=str, required=True, help="Target language")
parser.add_argument(
"--format",
type=str,
default="openslr",
choices=["openslr", "commonvoice", "indictts"],
help="Dataset format",
)
parser.add_argument("--split", action="store_true", help="Create train/val split")
args = parser.parse_args()
processor = DatasetProcessor(
output_dir=Path(args.output),
language=args.language,
)
# Process based on format
if args.format == "openslr":
samples = processor.process_openslr(Path(args.input))
elif args.format == "commonvoice":
samples = processor.process_commonvoice(Path(args.input))
elif args.format == "indictts":
samples = processor.process_indictts(Path(args.input))
# Save metadata
processor.save_metadata(samples)
# Create train/val split if requested
if args.split:
create_train_val_split(Path(args.output) / "metadata.csv")
if __name__ == "__main__":
main()
|