code: def make_batches(lines, cfg, task, max_positions, encode_fn):
      def encode_fn_target(x):
          return encode_fn(x)
  
      if cfg.generation.constraints:
          # Strip (tab-delimited) contraints, if present, from input lines,
          # store them in batch_constraints
          batch_constraints = [list() for _ in lines]
          for i, line in enumerate(lines):
              if "\t" in line:
                  lines[i], *batch_constraints[i] = line.split("\t")
  
          # Convert each List[str] to List[Tensor]
          for i, constraint_list in enumerate(batch_constraints):
              batch_constraints[i] = [
                  task.target_dictionary.encode_line(
                      encode_fn_target(constraint),
                      append_eos=False,
                      add_if_not_exist=False,
                  )
                  for constraint in constraint_list
              ]
  
      if cfg.generation.constraints:
          constraints_tensor = pack_constraints(batch_constraints)
      else:
          constraints_tensor = None
  
      tokens, lengths = task.get_interactive_tokens_and_lengths(
          lines, encode_fn
      )
  
      itr = task.get_batch_iterator(
          dataset=task.build_dataset_for_inference(
              tokens, lengths, constraints=constraints_tensor
          ),
          max_tokens=cfg.dataset.max_tokens,
          max_sentences=cfg.dataset.batch_size,
          max_positions=max_positions,
          ignore_invalid_inputs=cfg.dataset.skip_invalid_size_inputs_valid_test,
      ).next_epoch_itr(shuffle=False)
      for batch in itr:
          ids = batch["id"]
          src_tokens = batch["net_input"]["src_tokens"]
          src_lengths = batch["net_input"]["src_lengths"]
          constraints = batch.get("constraints", None)
  
          yield Batch(
              ids=ids,
              src_tokens=src_tokens,
              src_lengths=src_lengths,
              constraints=constraints,
          )
  
file_path: fairseq/fairseq_cli/interactive.py
imports: import ast
  import fileinput
  import logging
  import math
  import os
  import sys
  import time
  from argparse import Namespace
  from collections import namedtuple
  import numpy
  import torch
  from fairseq import checkpoint_utils, distributed_utils, options, tasks, utils
  from fairseq.dataclass.configs import FairseqConfig
  from fairseq.dataclass.utils import convert_namespace_to_omegaconf
  from fairseq.token_generation_constraints import pack_constraints, unpack_constraints
  from fairseq_cli.generate import get_symbols_to_strip_from_output
label_desc: Use image node_id 2 for calling a global function with example usage: make_batches(lines, cfg, task, max_positions, encode_fn) and returns: encode_fn