text: import re
  import os
  import json
  import argparse
  from string import punctuation
  
  import torch
  import yaml
  import numpy as np
  from torch.utils.data import DataLoader
  from g2p_en import G2p
  
  from utils.model import get_model, get_vocoder
  from utils.tools import to_device, synth_samples
  from dataset import TextDataset
  from text import text_to_sequence, grapheme_to_phoneme
  
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  
  
  def preprocess_english(text, preprocess_config):
      text = text.rstrip(punctuation)
  
      phones = grapheme_to_phoneme(text)
      phones = "{" + " ".join(phones) + "}"
  
      print("Raw Text Sequence: {}".format(text))
      print("Phoneme Sequence: {}".format(phones))
      sequence = np.array(
          text_to_sequence(
              phones, preprocess_config["preprocessing"]["text"]["text_cleaners"]
          )
      )
  
      return np.array(sequence)
  
  
  def synthesize(model, step, configs, vocoder, batchs, mel_stats):
      preprocess_config, model_config, train_config = configs
  
      for batch in batchs:
          batch = to_device(batch, device, mel_stats)
          with torch.no_grad():
              # Forward
              output = model(
                  *(batch[2:]),
              )
              synth_samples(
                  batch,
                  output,
                  vocoder,
                  model_config,
                  preprocess_config,
                  train_config["path"]["result_path"],
                  mel_stats,
              )
  
  
  if __name__ == "__main__":
  
      parser = argparse.ArgumentParser()
      parser.add_argument("--restore_step", type=int, required=True)
      parser.add_argument(
          "--mode",
          type=str,
          choices=["batch", "single"],
          required=True,
          help="Synthesize a whole dataset or a single sentence",
      )
      parser.add_argument(
          "--source",
          type=str,
          default=None,
          help="path to a source file with format like train.txt and val.txt, for batch mode only",
      )
      parser.add_argument(
          "--text",
          type=str,
          default=None,
          help="raw text to synthesize, for single-sentence mode only",
      )
      parser.add_argument(
          "--speaker_id",
          type=int,
          default=0,
          help="speaker ID for multi-speaker synthesis, for single-sentence mode only",
      )
      parser.add_argument(
          "-p",
          "--preprocess_config",
          type=str,
          required=True,
          help="path to preprocess.yaml",
      )
      parser.add_argument(
          "-m", "--model_config", type=str, required=True, help="path to model.yaml"
      )
      parser.add_argument(
          "-t", "--train_config", type=str, required=True, help="path to train.yaml"
      )
      args = parser.parse_args()
  
      # Check source texts
      if args.mode == "batch":
          assert args.source is not None and args.text is None
      if args.mode == "single":
          assert args.source is None and args.text is not None
  
      # Read Config
      preprocess_config = yaml.load(
          open(args.preprocess_config, "r"), Loader=yaml.FullLoader
      )
      model_config = yaml.load(open(args.model_config, "r"), Loader=yaml.FullLoader)
      train_config = yaml.load(open(args.train_config, "r"), Loader=yaml.FullLoader)
      configs = (preprocess_config, model_config, train_config)
      with open(
          os.path.join(preprocess_config["path"]["preprocessed_path"], "stats.json")
      ) as f:
          stats = json.load(f)
          mel_stats = stats["mel"]
  
      # Get model
      model = get_model(args, configs, device, train=False)
  
      # Load vocoder
      vocoder = get_vocoder(model_config, device)
  
      # Preprocess texts
      if args.mode == "batch":
          # Get dataset
          dataset = TextDataset(args.source, preprocess_config)
          batchs = DataLoader(
              dataset,
              batch_size=8,
              collate_fn=dataset.collate_fn,
          )
      if args.mode == "single":
          ids = raw_texts = [args.text[:100]]
          speakers = np.array([args.speaker_id])
          if preprocess_config["preprocessing"]["text"]["language"] == "en":
              texts = np.array([preprocess_english(args.text, preprocess_config)])
          text_lens = np.array([len(texts[0])])
          batchs = [(ids, raw_texts, speakers, texts, text_lens, max(text_lens))]
  
      synthesize(model, args.restore_step, configs, vocoder, batchs, mel_stats)
  