File size: 2,929 Bytes
79b2661 | 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 | """
Hugging Face Dataset Creator & Sync Utility.
Creates a Hugging Face Audio Dataset and pushes it to the Hugging Face Hub.
Supports both 2-column headerless (wav|text) and multi-column headered metadata CSVs.
"""
import os
if __package__:
from .metadata import read_manifest
else:
from metadata import read_manifest
import argparse
from datasets import Dataset, Audio
from huggingface_hub import login
def prepare_and_push_to_hf(
data_dir: str,
metadata_file: str,
hf_repo_id: str,
token: str = None,
private: bool = False,
):
"""
Reads local audio and transcript metadata, formats as a HF Dataset,
and pushes to Hugging Face Hub.
"""
if token:
login(token=token)
print(f"Loading metadata from {metadata_file}...")
audio_paths = []
texts = []
speakers = []
languages = []
for row in read_manifest(metadata_file):
full_path = os.path.join(data_dir, row["audio_path"])
if os.path.isfile(full_path):
audio_paths.append(full_path)
texts.append(row["text"])
speakers.append(row["speaker"])
languages.append(row["language"])
print(f"Found {len(audio_paths)} valid audio-text pairs.")
if len(audio_paths) == 0:
raise ValueError("No valid audio files found. Check paths in metadata.")
data_dict = {
"audio": audio_paths,
"text": texts,
}
if any(speakers):
data_dict["speaker"] = speakers
if any(languages):
data_dict["language"] = languages
# Build Hugging Face Dataset
print("Building Hugging Face Dataset with 24kHz audio features...")
ds = Dataset.from_dict(data_dict)
ds = ds.cast_column("audio", Audio(sampling_rate=24000))
print(f"Pushing dataset to Hugging Face: {hf_repo_id}...")
ds.push_to_hub(hf_repo_id, private=private)
print(f"Dataset successfully published to: https://huggingface.co/datasets/{hf_repo_id}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Push local TTS dataset to Hugging Face Hub")
parser.add_argument("--data_dir", type=str, default="dataset", help="Directory containing audio files")
parser.add_argument("--metadata", type=str, default="dataset/metadata.csv", help="Path to metadata CSV")
parser.add_argument("--repo_id", type=str, required=True, help="HF dataset repo, e.g. username/tts-hindi-dataset")
parser.add_argument("--token", type=str, default=None, help="Hugging Face write token (optional if logged in)")
parser.add_argument("--private", action="store_true", help="Make dataset private")
args = parser.parse_args()
prepare_and_push_to_hf(
data_dir=args.data_dir,
metadata_file=args.metadata,
hf_repo_id=args.repo_id,
token=args.token,
private=args.private,
)
|