code: def main(param_file: str, args: argparse.Namespace):
      commit = subprocess.check_output(
          ["git", "rev-parse", "HEAD"], universal_newlines=True
      ).strip()
      docker_image = f"allennlp/allennlp:{commit}"
      overrides = args.overrides
  
      # Reads params and sets environment.
      ext_vars = {}
  
      for var in args.env:
          key, value = var.split("=")
          ext_vars[key] = value
  
      params = Params.from_file(param_file, overrides, ext_vars)
  
      # Write params as json. Otherwise Jsonnet's import feature breaks.
      params_dir = tempfile.mkdtemp(prefix="config")
      compiled_params_path = os.path.join(params_dir, "config.json")
      params.to_file(compiled_params_path)
      print(
          f"Compiled jsonnet config written to {compiled_params_path}."
      )
  
      flat_params = params.as_flat_dict()
      env = {}
      for k, v in flat_params.items():
          k = str(k).replace(".", "_")
          env[k] = str(v)
  
      # If the git repository is dirty, add a random hash.
      result = subprocess.run(
          "git diff-index --quiet HEAD --", shell=True
      )
      if result.returncode != 0:
          dirty_hash = "%x" % random_int
          docker_image += "-" + dirty_hash
  
      if args.image:
          image = args.image
          print(f"Using the specified image: {image}")
      else:
          print(f"Building the Docker image ({docker_image})...")
          subprocess.run(
              f"docker build -t {docker_image} .",
              shell=True,
              check=True,
          )
  
          print("Create a Beaker image...")
          image = subprocess.check_output(
              f"beaker image create --quiet {docker_image}",
              shell=True,
              universal_newlines=True,
          ).strip()
          print(f"  Image created: {docker_image}")
  
      config_dataset_id = subprocess.check_output(
          f"beaker dataset create --quiet {params_dir}/*",
          shell=True,
          universal_newlines=True,
      ).strip()
  
      # Arguments that differ between preemptible and regular machine execution.
      if args.preemptible:
          allennlp_prefix = [
              "/stage/allennlp/resumable_train.sh",
              "/output",
              "/config/config.json",
          ]
      else:
          allennlp_prefix = [
              "python",
              "-m",
              "allennlp.run",
              "train",
              "/config/config.json",
              "-s",
              "/output",
          ]
  
      # All other arguments
      allennlp_suffix = ["--file-friendly-logging"]
      for package_name in args.include_package:
          allennlp_suffix.append("--include-package")
          allennlp_suffix.append(package_name)
  
      allennlp_command = allennlp_prefix + allennlp_suffix
  
      dataset_mounts = []
      for source in args.source + [f"{config_dataset_id}:/config"]:
          datasetId, containerPath = source.split(":")
          dataset_mounts.append(
              {"datasetId": datasetId, "containerPath": containerPath}
          )
  
      for var in args.env:
          key, value = var.split("=")
          env[key] = value
  
      requirements = {}
      if args.cpu:
          requirements["cpu"] = float(args.cpu)
      if args.memory:
          requirements["memory"] = args.memory
      if args.gpu_count:
          requirements["gpuCount"] = int(args.gpu_count)
      if args.preemptible:
          requirements["preemptible"] = True
      config_spec = {
          "description": args.desc,
          "image": image,
          "resultPath": "/output",
          "args": allennlp_command,
          "datasetMounts": dataset_mounts,
          "requirements": requirements,
          "env": env,
      }
      config_task = {"spec": config_spec, "name": "training"}
  
      config = {"tasks": [config_task]}
  
      output_path = (
          args.spec_output_path
          if args.spec_output_path
          else tempfile.mkstemp(".yaml", "beaker-config-")[1]
      )
      with open(output_path, "w") as output:
          output.write(json.dumps(config, indent=4))
      print(f"Beaker spec written to {output_path}.")
  
      experiment_command = [
          "beaker",
          "experiment",
          "create",
          "--quiet",
          "--file",
          output_path,
      ]
      if args.name:
          experiment_command.append("--name")
          experiment_command.append(args.name.replace(" ", "-"))
  
      def resume_command(experiment_id):
          resume_daemon_path = os.path.join(
              os.path.dirname(__file__), "resume_daemon.py"
          )
          return [
              # Run with python (instead of calling directly) in case the
              # executable bit wasn't preserved for some reason.
              "python3",
              resume_daemon_path,
              "--action=start",
              f"--max-resumes={args.max_resumes}",
              f"--experiment-id={experiment_id}",
          ]
  
      if args.dry_run:
          print(
              "This is a dry run (--dry-run).  Launch your job with the following command:"
          )
          print("    " + " ".join(experiment_command))
          if args.max_resumes > 0:
              print(
                  "Configure auto-resumes with the following command:"
              )
              print(
                  "    "
                  + " ".join(resume_command("$YOUR_EXPERIMENT_ID"))
              )
      else:
          print("Running the experiment:")
          print("    " + " ".join(experiment_command))
          experiment_id = subprocess.check_output(
              experiment_command, universal_newlines=True
          ).strip()
          print(
              f"Experiment {experiment_id} submitted. "
              f"See progress at https://beaker.org/ex/{experiment_id}"
          )
          if args.max_resumes > 0:
              print("Configuring auto-resumes:")
              print("    " + " ".join(resume_command(experiment_id)))
              subprocess.run(resume_command(experiment_id))
  
file_path: allennlp/scripts/ai2_internal/run_with_beaker.py
imports: import argparse
  import os
  import json
  import random
  import tempfile
  import subprocess
  import sys
  from allennlp.common.params import Params
label_desc: Use image node_id 1 for calling a global function with example usage: main(param_file, args) and returns: list