code: def run_with_flags(generator):
      """Generates polyphonic tracks and saves them as MIDI files.
  
      Uses the options specified by the flags defined in this module.
  
      Args:
        generator: The PolyphonyRnnSequenceGenerator to use for generation.
      """
      if not FLAGS.output_dir:
          tf.logging.fatal("--output_dir required")
          return
      output_dir = os.path.expanduser(FLAGS.output_dir)
  
      primer_midi = None
      if FLAGS.primer_midi:
          primer_midi = os.path.expanduser(FLAGS.primer_midi)
  
      if not tf.gfile.Exists(output_dir):
          tf.gfile.MakeDirs(output_dir)
  
      primer_sequence = None
      qpm = (
          FLAGS.qpm
          if FLAGS.qpm
          else note_seq.DEFAULT_QUARTERS_PER_MINUTE
      )
      if FLAGS.primer_pitches:
          primer_sequence = music_pb2.NoteSequence()
          primer_sequence.tempos.add().qpm = qpm
          primer_sequence.ticks_per_quarter = note_seq.STANDARD_PPQ
          for pitch in ast.literal_eval(FLAGS.primer_pitches):
              note = primer_sequence.notes.add()
              note.start_time = 0
              note.end_time = 60.0 / qpm
              note.pitch = pitch
              note.velocity = 100
          primer_sequence.total_time = primer_sequence.notes[
              -1
          ].end_time
      elif FLAGS.primer_melody:
          primer_melody = note_seq.Melody(
              ast.literal_eval(FLAGS.primer_melody)
          )
          primer_sequence = primer_melody.to_sequence(qpm=qpm)
      elif primer_midi:
          primer_sequence = note_seq.midi_file_to_sequence_proto(
              primer_midi
          )
          if primer_sequence.tempos and primer_sequence.tempos[0].qpm:
              qpm = primer_sequence.tempos[0].qpm
      else:
          tf.logging.warning(
              "No priming sequence specified. Defaulting to empty sequence."
          )
          primer_sequence = music_pb2.NoteSequence()
          primer_sequence.tempos.add().qpm = qpm
          primer_sequence.ticks_per_quarter = note_seq.STANDARD_PPQ
  
      # Derive the total number of seconds to generate.
      seconds_per_step = 60.0 / qpm / generator.steps_per_quarter
      generate_end_time = FLAGS.num_steps * seconds_per_step
  
      # Specify start/stop time for generation based on starting generation at the
      # end of the priming sequence and continuing until the sequence is num_steps
      # long.
      generator_options = generator_pb2.GeneratorOptions()
      # Set the start time to begin when the last note ends.
      generate_section = generator_options.generate_sections.add(
          start_time=primer_sequence.total_time,
          end_time=generate_end_time,
      )
  
      if generate_section.start_time >= generate_section.end_time:
          tf.logging.fatal(
              "Priming sequence is longer than the total number of steps "
              "requested: Priming sequence length: %s, Total length "
              "requested: %s",
              generate_section.start_time,
              generate_end_time,
          )
          return
  
      generator_options.args[
          "temperature"
      ].float_value = FLAGS.temperature
      generator_options.args["beam_size"].int_value = FLAGS.beam_size
      generator_options.args[
          "branch_factor"
      ].int_value = FLAGS.branch_factor
      generator_options.args[
          "steps_per_iteration"
      ].int_value = FLAGS.steps_per_iteration
  
      generator_options.args[
          "condition_on_primer"
      ].bool_value = FLAGS.condition_on_primer
      generator_options.args[
          "no_inject_primer_during_generation"
      ].bool_value = not FLAGS.inject_primer_during_generation
  
      tf.logging.debug("primer_sequence: %s", primer_sequence)
      tf.logging.debug("generator_options: %s", generator_options)
  
      # Make the generate request num_outputs times and save the output as midi
      # files.
      date_and_time = time.strftime("%Y-%m-%d_%H%M%S")
      digits = len(str(FLAGS.num_outputs))
      for i in range(FLAGS.num_outputs):
          generated_sequence = generator.generate(
              primer_sequence, generator_options
          )
  
          midi_filename = "%s_%s.mid" % (
              date_and_time,
              str(i + 1).zfill(digits),
          )
          midi_path = os.path.join(output_dir, midi_filename)
          note_seq.sequence_proto_to_midi_file(
              generated_sequence, midi_path
          )
  
      tf.logging.info(
          "Wrote %d MIDI files to %s", FLAGS.num_outputs, output_dir
      )
  
file_path: magenta/magenta/models/polyphony_rnn/polyphony_rnn_generate.py
imports: import ast
  import os
  import time
  from magenta.models.polyphony_rnn import polyphony_model
  from magenta.models.polyphony_rnn import polyphony_sequence_generator
  from magenta.models.shared import sequence_generator
  from magenta.models.shared import sequence_generator_bundle
  import note_seq
  from note_seq.protobuf import generator_pb2
  from note_seq.protobuf import music_pb2
  import tensorflow.compat.v1
label_desc: Use image node_id 3 for calling a global function with example usage: run_with_flags(generator) without return types