text: import logging
  import os
  import shutil
  import warnings
  import sys
  import tempfile
  warnings.simplefilter("ignore")
  
  if sys.platform == 'darwin':
      os.environ['OMP_NUM_THREADS'] = '1'
  
  import matplotlib
  import pandas as pd
  matplotlib.use('agg')  # no need for tk
  
  from autogluon.tabular import TabularPredictor
  from autogluon.core.utils.savers import save_pd, save_pkl
  import autogluon.core.metrics as metrics
  from autogluon.tabular.version import __version__
  
  from frameworks.shared.callee import call_run, result, output_subdir
  from frameworks.shared.utils import Timer, zip_path
  
  log = logging.getLogger(__name__)
  
  
  def run(dataset, config):
      log.info(config)
      log.info(f"\n**** AutoGluon [v{__version__}] ****\n")
  
      metrics_mapping = dict(
          acc=metrics.accuracy,
          auc=metrics.roc_auc,
          f1=metrics.f1,
          logloss=metrics.log_loss,
          mae=metrics.mean_absolute_error,
          mse=metrics.mean_squared_error,
          r2=metrics.r2,
          rmse=metrics.root_mean_squared_error,
      )
  
      perf_metric = metrics_mapping[config.metric] if config.metric in metrics_mapping else None
      if perf_metric is None:
          # TODO: figure out if we are going to blindly pass metrics through, or if we use a strict mapping
          log.warning("Performance metric %s not supported.", config.metric)
  
      is_classification = config.type == 'classification'
      training_params = {k: v for k, v in config.framework_params.items() if not k.startswith('_')}
  
      train, test = dataset.train.path, dataset.test.path
      label = dataset.target.name
      problem_type = dataset.problem_type
  
      models_dir = tempfile.mkdtemp() + os.sep  # passed to AG
  
      with Timer() as training:
          predictor = TabularPredictor(
              label=label,
              eval_metric=perf_metric.name,
              path=models_dir,
              problem_type=problem_type,
          ).fit(
              train_data=train,
              time_limit=config.max_runtime_seconds,
              **training_params
          )
  
      del train
  
      if is_classification:
          with Timer() as predict:
              probabilities = predictor.predict_proba(test, as_multiclass=True)
          predictions = probabilities.idxmax(axis=1).to_numpy()
      else:
          with Timer() as predict:
              predictions = predictor.predict(test, as_pandas=False)
          probabilities = None
  
      prob_labels = probabilities.columns.values.astype(str).tolist() if probabilities is not None else None
  
      _leaderboard_extra_info = config.framework_params.get('_leaderboard_extra_info', False)  # whether to get extra model info (very verbose)
      _leaderboard_test = config.framework_params.get('_leaderboard_test', False)  # whether to compute test scores in leaderboard (expensive)
      leaderboard_kwargs = dict(silent=True, extra_info=_leaderboard_extra_info)
      # Disabled leaderboard test data input by default to avoid long running computation, remove 7200s timeout limitation to re-enable
      if _leaderboard_test:
          leaderboard_kwargs['data'] = test
  
      leaderboard = predictor.leaderboard(**leaderboard_kwargs)
      with pd.option_context('display.max_rows', None, 'display.max_columns', None, 'display.width', 1000):
          log.info(leaderboard)
  
      num_models_trained = len(leaderboard)
      if predictor._trainer.model_best is not None:
          num_models_ensemble = len(predictor._trainer.get_minimum_model_set(predictor._trainer.model_best))
      else:
          num_models_ensemble = 1
  
      debug_info = predictor._learner.trainer._debug_info
      default_proxy_debug_info = {'exceptions': [],
                                  'index_trajectory': [],
                                  'layer_fit_time': 0.,
                                  'total_prune_time': 0.,
                                  'total_prune_fit_time': 0.,
                                  'total_prune_fi_time': 0.,
                                  'score_improvement_from_proxy_yes': 0,
                                  'score_improvement_from_proxy_no': 0,
                                  'kept_ratio': 0.}
      if len(debug_info.get('proxy_model', [])) == 0:
          proxy_debug_info = [default_proxy_debug_info]
      else:
          proxy_debug_info = debug_info.get('proxy_model')
      dfs = []
      for layer, layer_debug_info in enumerate(proxy_debug_info):
          pruned = len(layer_debug_info) > 0 and\
                  (layer_debug_info['score_improvement_from_proxy_yes'] > 0 or
                   layer_debug_info['score_improvement_from_proxy_no'] > 0)
          info_content = {'name': config.name, 'fold': config.fold,
                          'pruned': pruned, 'layer': layer}
          info_content.update(layer_debug_info)
          dfs.append(pd.DataFrame({k: [v] for k, v in info_content.items()}))
      df = pd.concat(dfs)
      save_path = os.path.join(config.output_dir, 'debug_info.csv')
      df.to_csv(save_path, index=False)
  
      save_artifacts(predictor, leaderboard, config)
      shutil.rmtree(predictor.path, ignore_errors=True)
  
      return result(output_file=config.output_predictions_file,
                    predictions=predictions,
                    probabilities=probabilities,
                    probabilities_labels=prob_labels,
                    target_is_encoded=False,
                    models_count=num_models_trained,
                    models_ensemble_count=num_models_ensemble,
                    training_duration=training.duration,
                    predict_duration=predict.duration)
  
  
  def save_artifacts(predictor, leaderboard, config):
      artifacts = config.framework_params.get('_save_artifacts', ['leaderboard'])
      try:
          if 'leaderboard' in artifacts:
              leaderboard_dir = output_subdir("leaderboard", config)
              save_pd.save(path=os.path.join(leaderboard_dir, "leaderboard.csv"), df=leaderboard)
  
          if 'info' in artifacts:
              ag_info = predictor.info()
              info_dir = output_subdir("info", config)
              save_pkl.save(path=os.path.join(info_dir, "info.pkl"), object=ag_info)
  
          if 'models' in artifacts:
              shutil.rmtree(os.path.join(predictor.path, "utils"), ignore_errors=True)
              models_dir = output_subdir("models", config)
              zip_path(predictor.path, os.path.join(models_dir, "models.zip"))
      except Exception:
          log.warning("Error when saving artifacts.", exc_info=True)
  
  
  if __name__ == '__main__':
      call_run(run)
  