dataset.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. # Copyright (c) Meta Platforms, Inc. and affiliates
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the license found in the
  5. # LICENSE file in the root directory of this source tree.
  6. import argparse
  7. import dataclasses
  8. import json
  9. import logging
  10. import os
  11. from argparse import Namespace
  12. from pathlib import Path
  13. from stopes.hub import load_config
  14. from stopes.speech.tokenizers import SpeechTokenizer, SpeechTokenizerConfig
  15. from seamless_communication.datasets.huggingface import (
  16. Speech2SpeechFleursDatasetBuilder,
  17. )
  18. logging.basicConfig(
  19. level=logging.INFO,
  20. format="%(asctime)s %(levelname)s -- %(name)s: %(message)s",
  21. )
  22. logger = logging.getLogger("dataset")
  23. # List of FLEURS langcodes is available at https://huggingface.co/datasets/google/fleurs
  24. # List of M4T langcodes is available in yaml: src/seamless_communication/assets/cards/unity_nllb-100.yaml
  25. UNITY_TO_FLEURS_LANG_MAPPING = {
  26. "eng": "en_us",
  27. "ita": "it_it",
  28. "kor": "ko_kr",
  29. }
  30. def download_fleurs_dataset(
  31. source_lang: str,
  32. target_lang: str,
  33. split: str,
  34. unit_extractor_config: str,
  35. save_directory: str,
  36. ) -> str:
  37. tokenizer_conf: SpeechTokenizerConfig = load_config(
  38. unit_extractor_config, namespace=""
  39. )
  40. tokenizer: SpeechTokenizer = SpeechTokenizer.build(tokenizer_conf)
  41. dataset_iterator = Speech2SpeechFleursDatasetBuilder(
  42. source_lang=UNITY_TO_FLEURS_LANG_MAPPING[source_lang],
  43. target_lang=UNITY_TO_FLEURS_LANG_MAPPING[target_lang],
  44. dataset_cache_dir=save_directory,
  45. speech_tokenizer=tokenizer,
  46. skip_source_audio=True, # don't extract units from source audio
  47. skip_target_audio=False,
  48. split=split,
  49. )
  50. manifest_path: str = os.path.join(save_directory, f"{split}_manifest.json")
  51. with open(manifest_path, "w") as fp_out:
  52. for idx, sample in enumerate(dataset_iterator, start=1):
  53. # correction as FleursDatasetBuilder return fleurs lang codes
  54. sample.source.lang = source_lang
  55. sample.target.lang = target_lang
  56. sample.target.waveform = None # already extracted units
  57. fp_out.write(json.dumps(dataclasses.asdict(sample)) + "\n")
  58. logger.info(f"Saved {idx} samples for split={split} to {manifest_path}")
  59. return manifest_path
  60. def init_parser() -> argparse.ArgumentParser:
  61. parser = argparse.ArgumentParser(
  62. description=(
  63. "Helper script to download training/evaluation dataset (FLEURS),"
  64. "extract units from target audio and save the dataset as a manifest "
  65. "consumable by `finetune.py`."
  66. )
  67. )
  68. parser.add_argument(
  69. "--source_lang",
  70. type=str,
  71. required=True,
  72. help="M4T langcode of the dataset SOURCE language",
  73. )
  74. parser.add_argument(
  75. "--target_lang",
  76. type=str,
  77. required=True,
  78. help="M4T langcode of the dataset TARGET language",
  79. )
  80. parser.add_argument(
  81. "--split",
  82. type=str,
  83. required=True,
  84. help="Dataset split/shard to download (`train`, `test`)",
  85. )
  86. parser.add_argument(
  87. "--save_dir",
  88. type=Path,
  89. required=True,
  90. help="Directory where the datastets will be stored with HuggingFace datasets cache files",
  91. )
  92. return parser
  93. def main(args: Namespace) -> None:
  94. manifest_path = download_fleurs_dataset(
  95. source_lang=args.source_lang,
  96. target_lang=args.target_lang,
  97. # TODO: remove hardcoded path
  98. unit_extractor_config="/checkpoint/krs/unit_extraction/xlsr1b/lang41_10k_xlsr_lyr35.yaml",
  99. split=args.split,
  100. save_directory=args.save_dir,
  101. )
  102. logger.info(f"Manifest saved to: {manifest_path}")
  103. if __name__ == "__main__":
  104. args = init_parser().parse_args()
  105. main(args)