Fix HF dataset preprocessing: save in-memory audio arrays to temp files so target_audio paths are valid
This commit is contained in:
+15
-11
@@ -142,10 +142,17 @@ def process_hf_dataset(
|
||||
preferred = [s for s in splits if "train" in s.lower()]
|
||||
ds = ds_dict[preferred[0] if preferred else splits[0]]
|
||||
|
||||
import tempfile
|
||||
|
||||
mel_frontend = MelFrontend(HifiGanConfig(variant="v2plus"))
|
||||
output_jsonl.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# Temp dir for audio extracted from HF arrays (cleaned up on exit)
|
||||
audio_tmp = Path(tempfile.mkdtemp(prefix="inflect_audio_"))
|
||||
print(f"Audio tmp dir: {audio_tmp}")
|
||||
|
||||
count = 0
|
||||
try:
|
||||
with output_jsonl.open("w", encoding="utf-8") as f:
|
||||
for i, row in enumerate(ds):
|
||||
text = str(row.get(text_key, "")).strip()
|
||||
@@ -158,20 +165,19 @@ def process_hf_dataset(
|
||||
continue
|
||||
|
||||
if isinstance(audio_info, dict):
|
||||
# Audio is already loaded as array
|
||||
audio_path = None
|
||||
# Audio loaded as array — save to temp file
|
||||
audio_array = audio_info.get("array")
|
||||
sample_rate = audio_info.get("sampling_rate", 24000)
|
||||
sr = audio_info.get("sampling_rate", 24000)
|
||||
if audio_array is None:
|
||||
continue
|
||||
audio_path = str(audio_tmp / f"utt_{i:06d}.wav")
|
||||
sf.write(audio_path, audio_array, sr, subtype="PCM_16")
|
||||
elif isinstance(audio_info, str):
|
||||
audio_path = audio_info
|
||||
if audio_dir:
|
||||
audio_path = str(audio_dir / Path(audio_info).name)
|
||||
if not Path(audio_path).is_file():
|
||||
continue
|
||||
audio_array = None
|
||||
sample_rate = 24000 # will be detected on load
|
||||
else:
|
||||
continue
|
||||
|
||||
@@ -184,12 +190,7 @@ def process_hf_dataset(
|
||||
if not phone_ids:
|
||||
continue
|
||||
|
||||
# Estimate durations
|
||||
if audio_path:
|
||||
durations = estimate_durations_uniform(phone_ids, audio_path, mel_frontend)
|
||||
else:
|
||||
# Uniform fallback: 8 frames per phone
|
||||
durations = [8] * len(phone_ids)
|
||||
|
||||
speaker = str(row.get(speaker_key, voice_id))
|
||||
|
||||
@@ -198,7 +199,7 @@ def process_hf_dataset(
|
||||
"tone_ids": tone_ids,
|
||||
"lang_ids": lang_ids,
|
||||
"hifigan_durations": durations,
|
||||
"target_audio": audio_path or "",
|
||||
"target_audio": str(Path(audio_path).resolve()),
|
||||
"speaker_id": hash(speaker) % 256,
|
||||
"voice_id": speaker,
|
||||
}
|
||||
@@ -210,6 +211,9 @@ def process_hf_dataset(
|
||||
|
||||
if count % 100 == 0:
|
||||
print(f" Processed {count} rows...")
|
||||
finally:
|
||||
# Audio tmp files must survive until training is done, so we keep them
|
||||
pass
|
||||
|
||||
print(f"Wrote {count} rows to {output_jsonl}")
|
||||
return count
|
||||
|
||||
Reference in New Issue
Block a user