code: def run_with_flags(generator):
      """Generates melodies and saves them as MIDI files.
  
      Uses the options specified by the flags defined in this module.
  
      Args:
        generator: The ImprovRnnSequenceGenerator to use for generation.
      """
      if not FLAGS.output_dir:
          tf.logging.fatal("--output_dir required")
          return
      FLAGS.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(FLAGS.output_dir):
          tf.gfile.MakeDirs(FLAGS.output_dir)
  
      primer_sequence = None
      qpm = (
          FLAGS.qpm
          if FLAGS.qpm
          else note_seq.DEFAULT_QUARTERS_PER_MINUTE
      )
      if 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 a single middle C."
          )
          primer_melody = note_seq.Melody([60])
          primer_sequence = primer_melody.to_sequence(qpm=qpm)
  
      # Create backing chord progression from flags.
      raw_chords = FLAGS.backing_chords.split()
      repeated_chords = [
          chord
          for chord in raw_chords
          for _ in range(FLAGS.steps_per_chord)
      ]
      backing_chords = note_seq.ChordProgression(repeated_chords)
  
      # Derive the total number of seconds to generate based on the QPM of the
      # priming sequence and the length of the backing chord progression.
      seconds_per_step = 60.0 / qpm / generator.steps_per_quarter
      total_seconds = len(backing_chords) * 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()
      if primer_sequence:
          input_sequence = primer_sequence
          # Set the start time to begin on the next step after the last note ends.
          if primer_sequence.notes:
              last_end_time = max(
                  n.end_time for n in primer_sequence.notes
              )
          else:
              last_end_time = 0
          generate_section = generator_options.generate_sections.add(
              start_time=last_end_time + seconds_per_step,
              end_time=total_seconds,
          )
  
          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, Generation length "
                  "requested: %s",
                  generate_section.start_time,
                  total_seconds,
              )
              return
      else:
          input_sequence = music_pb2.NoteSequence()
          input_sequence.tempos.add().qpm = qpm
          generate_section = generator_options.generate_sections.add(
              start_time=0, end_time=total_seconds
          )
  
      # Add the backing chords to the input sequence.
      chord_sequence = backing_chords.to_sequence(
          sequence_start_time=0.0, qpm=qpm
      )
      for text_annotation in chord_sequence.text_annotations:
          if text_annotation.annotation_type == CHORD_SYMBOL:
              chord = input_sequence.text_annotations.add()
              chord.CopyFrom(text_annotation)
      input_sequence.total_time = len(backing_chords) * seconds_per_step
  
      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
      tf.logging.debug("input_sequence: %s", input_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(
              input_sequence, generator_options
          )
  
          if FLAGS.render_chords:
              renderer = note_seq.BasicChordRenderer(
                  velocity=CHORD_VELOCITY
              )
              renderer.render(generated_sequence)
  
          midi_filename = "%s_%s.mid" % (
              date_and_time,
              str(i + 1).zfill(digits),
          )
          midi_path = os.path.join(FLAGS.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,
          FLAGS.output_dir,
      )
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import ast
  import os
  import time
  from magenta.models.improv_rnn import improv_rnn_config_flags
  from magenta.models.improv_rnn import improv_rnn_model
  from magenta.models.improv_rnn import improv_rnn_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