code: def train_model(args):
      # set up indexes
      tag_to_idx, char_to_idx = build_indexes(args)
      # load training data
      train_data = DataLoader(args.device)
      train_files = [
          f"{args.data_dir}/{x}"
          for x in os.listdir(args.data_dir)
          if "train" in x
      ]
      train_data.load_data(
          args.batch_size,
          train_files,
          char_to_idx,
          tag_to_idx,
          args.randomize,
      )
      # load dev data
      dev_data = DataLoader(args.device)
      dev_files = [
          f"{args.data_dir}/{x}"
          for x in os.listdir(args.data_dir)
          if "dev" in x
      ]
      dev_data.load_data(
          args.batch_size,
          dev_files,
          char_to_idx,
          tag_to_idx,
          randomize=False,
          max_length=args.eval_length,
      )
      # set up trainer
      trainer_config = {
          "model_path": args.save_name,
          "char_to_idx": char_to_idx,
          "tag_to_idx": tag_to_idx,
          "batch_size": args.batch_size,
          "lang_weights": train_data.lang_weights,
      }
      if args.load_name:
          trainer_config["load_name"] = args.load_name
          logger.info(
              f"{datetime.now()}\tLoading model from: {args.load_name}"
          )
      trainer = Trainer(
          trainer_config,
          load_model=args.load_name is not None,
          device=args.device,
      )
      # run training
      best_accuracy = 0.0
      for epoch in range(1, args.num_epochs + 1):
          logger.info(f"{datetime.now()}\tEpoch {epoch}")
          logger.info(
              f"{datetime.now()}\tNum training batches: {len(train_data.batches)}"
          )
  
          batches = train_data.batches
          if not args.batch_mode:
              batches = tqdm(batches)
          for train_batch in batches:
              inputs = (
                  train_batch["sentences"],
                  train_batch["targets"],
              )
              trainer.update(inputs)
  
          logger.info(
              f"{datetime.now()}\tEpoch complete. Evaluating on dev data."
          )
          (
              curr_dev_accuracy,
              curr_confusion_matrix,
              curr_precisions,
              curr_recalls,
              curr_f1s,
          ) = eval_trainer(
              trainer, dev_data, batch_mode=args.batch_mode
          )
          logger.info(
              f"{datetime.now()}\tCurrent dev accuracy: {curr_dev_accuracy}"
          )
          if curr_dev_accuracy > best_accuracy:
              logger.info(
                  f"{datetime.now()}\tNew best score. Saving model."
              )
              model_label = (
                  f"epoch{epoch}" if args.save_best_epochs else None
              )
              trainer.save(label=model_label)
              with open(
                  score_log_path(args.save_name), "w"
              ) as score_log_file:
                  for score_log in [
                      {"dev_accuracy": curr_dev_accuracy},
                      curr_confusion_matrix,
                      curr_precisions,
                      curr_recalls,
                      curr_f1s,
                  ]:
                      score_log_file.write(json.dumps(score_log) + "\n")
              best_accuracy = curr_dev_accuracy
  
          # reload training data
          logger.info(f"{datetime.now()}\tResampling training data.")
          train_data.load_data(
              args.batch_size,
              train_files,
              char_to_idx,
              tag_to_idx,
              args.randomize,
          )
  
file_path: stanza/stanza/models/lang_identifier.py
imports: import argparse
  import json
  import logging
  import os
  import random
  import torch
  from datetime import datetime
  from stanza.models.common import utils
  from stanza.models.langid.data import DataLoader
  from stanza.models.langid.trainer import Trainer
  from stanza.utils.get_tqdm import get_tqdm
label_desc: Use image node_id 5 for calling a global function with example usage: train_model(args) without return types