def predict_and_save(args, adapter_args, model, tokenizer, labels, lang2id, pad_token_label_id, lang_adapter_names, task_name, split):
    output_test_results_file = os.path.join(args.output_dir, f'{split}_results.txt')
    with open(output_test_results_file, 'a') as result_writer:
        for lang in args.predict_langs.split(','):
            if (not os.path.exists(os.path.join(args.data_dir, lang, '{}.{}'.format(split, args.model_name_or_path)))):
                logger.info('Language {}, split {} does not exist'.format(lang, split))
                continue
            adapter_weight = None
            logger.info('Args Adapter Weight = {}'.format(args.adapter_weight))
            logger.info('Adapter Languages = {}'.format(lang_adapter_names))
            if (adapter_weight is not None):
                logger.info('Adapter Weights = {}'.format(adapter_weight))
                logger.info('Sum of Adapter Weights = {}'.format(sum(adapter_weight)))
                logger.info('Length of Adapter Weights = {}'.format(len(adapter_weight)))
            (result, predictions) = evaluate(args, model, tokenizer, labels, pad_token_label_id, mode=split, lang=lang, lang2id=lang2id, adapter_weight=adapter_weight, lang_adapter_names=lang_adapter_names, task_name=task_name, calc_weight_step=args.calc_weight_step)
            if args.get_attr:
                continue
            result_json = {}
            if ((args.predict_save_prefix is not None) and args.predict_save_prefix):
                result_json['language'] = f'{args.predict_save_prefix}_{lang}'
            else:
                result_json['language'] = f'{lang}'
            result_json['seed'] = args.seed
            result_json['language_adapters'] = lang_adapter_names
            if args.adapter_weight:
                result_json['adapter_weights'] = args.adapter_weight
            for key in sorted(result.keys()):
                result_json[key] = result[key]
            result_writer.write((json.dumps(result_json) + '\n'))
            if ((args.predict_save_prefix is not None) and args.predict_save_prefix):
                output_test_predictions_file = os.path.join(args.output_dir, '{}_{}_{}_s{}_predictions.txt'.format(split, args.predict_save_prefix, lang, args.seed))
            else:
                output_test_predictions_file = os.path.join(args.output_dir, '{}_{}_s{}_predictions.txt'.format(split, lang, args.seed))
            infile = os.path.join(args.data_dir, lang, '{}.{}'.format(split, args.model_name_or_path))
            idxfile = (infile + '.idx')
            save_predictions(args, predictions, output_test_predictions_file, infile, idxfile)
def predict_and_save(args, adapter_args, model, tokenizer, labels, lang2id, pad_token_label_id, lang_adapter_names, task_name, split):
    output_test_results_file = os.path.join(args.output_dir, f'{split}_results.txt')
    with open(output_test_results_file, 'a') as result_writer:
        for lang in args.predict_langs.split(','):
            if (not os.path.exists(os.path.join(args.data_dir, lang, '{}.{}'.format(split, args.model_name_or_path)))):
                logger.info('Language {}, split {} does not exist'.format(lang, split))
                continue
            adapter_weight = None
            logger.info('Args Adapter Weight = {}'.format(args.adapter_weight))
            logger.info('Adapter Languages = {}'.format(lang_adapter_names))
            if (adapter_weight is not None):
                logger.info('Adapter Weights = {}'.format(adapter_weight))
                logger.info('Sum of Adapter Weights = {}'.format(sum(adapter_weight)))
                logger.info('Length of Adapter Weights = {}'.format(len(adapter_weight)))
            (result, predictions) = evaluate(args, model, tokenizer, labels, pad_token_label_id, mode=split, lang=lang, lang2id=lang2id, adapter_weight=adapter_weight, lang_adapter_names=lang_adapter_names, task_name=task_name, calc_weight_step=args.calc_weight_step)
            if args.get_attr:
                continue
            result_json = {}
            if ((args.predict_save_prefix is not None) and args.predict_save_prefix):
                result_json['language'] = f'{args.predict_save_prefix}_{lang}'
            else:
                result_json['language'] = f'{lang}'
            result_json['seed'] = args.seed
            result_json['language_adapters'] = lang_adapter_names
            if args.adapter_weight:
                result_json['adapter_weights'] = args.adapter_weight
            for key in sorted(result.keys()):
                result_json[key] = result[key]
            result_writer.write((json.dumps(result_json) + '\n'))
            if ((args.predict_save_prefix is not None) and args.predict_save_prefix):
                output_test_predictions_file = os.path.join(args.output_dir, '{}_{}_{}_s{}_predictions.txt'.format(split, args.predict_save_prefix, lang, args.seed))
            else:
                output_test_predictions_file = os.path.join(args.output_dir, '{}_{}_s{}_predictions.txt'.format(split, lang, args.seed))
            infile = os.path.join(args.data_dir, lang, '{}.{}'.format(split, args.model_name_or_path))
            idxfile = (infile + '.idx')
            save_predictions(args, predictions, output_test_predictions_file, infile, idxfile)