Download data/hf_dataset_sync.py from ViuAI/TTS_10M: direct link, hf CLI and curl.
- Browser
- Download file 2.93 kB
-
https://huggingface.co/ViuAI/TTS_10M/resolve/main/data/hf_dataset_sync.py
- Command line
-
hf download hf://ViuAI/TTS_10M/data/hf_dataset_sync.py
-
curl -L -o hf_dataset_sync.py https://huggingface.co/ViuAI/TTS_10M/resolve/main/data/hf_dataset_sync.py
2.93 kB
| """ | |
| 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, | |
| ) | |