code: def textgrid_data_loader(
      data_dir: Path, seq_len: int, batch_size: int, mode: str
  ):
      """load all textgrid files in the directory"""
      tg_files = sorted(data_dir.glob("*.TextGrid"))
      random.Random(42).shuffle(tg_files)
      L = len(tg_files) * 95 // 100
      assert mode in ["train", "val"]
      phonemes = load_phonemes_set()
      if mode == "train":
          tg_files = tg_files[:L]
      if mode == "val":
          tg_files = tg_files[L:]
  
      data = []
      for fn in tg_files:
          ps, ds = zip(*load_textgrid(fn))
          ps = [phonemes.index(p) for p in ps]
          l = len(ps)
          ps = pad_seq(ps, seq_len, 0)
          ds = pad_seq(ds, seq_len, 0)
          data.append((ps, ds, l))
  
      batch = []
      while True:
          random.shuffle(data)
          for e in data:
              batch.append(e)
              if len(batch) == batch_size:
                  ps, ds, lengths = zip(*batch)
                  ps = np.array(ps, dtype=np.int32)
                  ds = np.array(ds, dtype=np.float32)
                  lengths = np.array(lengths, dtype=np.int32)
                  yield DurationInput(ps, lengths, ds)
                  batch = []
  
file_path: underthesea/underthesea/pipeline/say/viettts_/nat/data_loader.py
imports: import random
  from pathlib import Path
  import numpy
  from scipy.io import wavfile
  from .config import FLAGS, AcousticInput, DurationInput
label_desc: Use image node_id 5 for calling a global function with example usage: textgrid_data_loader(data_dir, seq_len, batch_size, mode) without return types