Datasets:

ArXiv:
File size: 30,949 Bytes
5b552c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
# evaluate_cli.py
import argparse
import importlib.metadata
import json
import logging
import os
import platform
import subprocess
import sys
from datetime import datetime
from functools import partial
from typing import Any, Dict, List, Optional, Tuple, Union

from datasets import Dataset as HFDataset

from . import evaluate, get_logger, load_dataset
from .artifact import UnitxtArtifactNotFoundError
from .benchmark import Benchmark

# Use HFAutoModelInferenceEngine for local models
from .inference import (
    CrossProviderInferenceEngine,
    HFAutoModelInferenceEngine,
    InferenceEngine,
)
from .metric_utils import EvaluationResults
from .parsing_utils import parse_key_equals_value_string_to_dict
from .settings_utils import settings
from .standard import DatasetRecipe

# Define logger early so it can be used in initial error handling
# Basic config for initial messages, will be reconfigured in main()
logger = get_logger()


def try_parse_json(value: str) -> Union[str, dict, None]:
    """Attempts to parse a string as JSON or key=value pairs.

    Returns the original string if parsing fails
    and the string doesn't look like JSON/kv pairs.
    Raises ArgumentTypeError if it looks like JSON but is invalid.
    """
    if value is None:
        return None
    try:
        # Handle simple key-value pairs like "key=value,key2=value2"
        if "=" in value and "{" not in value:
            parsed_dict = parse_key_equals_value_string_to_dict(value)
            if parsed_dict:
                return parsed_dict

        # Attempt standard JSON parsing
        return json.loads(value)

    except json.JSONDecodeError as e:
        if value.strip().startswith("{") or value.strip().startswith("["):
            raise argparse.ArgumentTypeError(
                f"Invalid JSON: '{value}'. Hint: Use double quotes for JSON strings and check syntax."
            ) from e
        return value  # Return as string if not JSON-like
    except Exception as e:
        logger.error(f"Error parsing argument '{value}': {e}")
        raise argparse.ArgumentTypeError(f"Could not parse argument: '{value}'") from e


def setup_parser() -> argparse.ArgumentParser:
    """Sets up the argument parser."""
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawTextHelpFormatter,
        description="CLI utility for running evaluations with unitxt.",
    )

    # --- Task/Dataset Arguments ---
    parser.add_argument(
        "--tasks",  # Changed to plural to better reflect it holds a list
        "-t",
        dest="tasks",  # Explicitly set the attribute name to 'tasks'
        type=partial(str.split, sep="+"),  # Use the custom function for type conversion
        required=True,
        help="Plus-separated (+) list of Unitxt task/dataset identifier strings.\n"
        "Each task format: 'card=<card_ref>,template=<template_ref>,...'\n"
        "Example: 'card=cards.mmlu,t=t.mmlu.all+card=cards.hellaswag,t=t.hellaswag.no'",
    )

    parser.add_argument(
        "--split",
        type=str,
        default="test",
        help="Dataset split to use (e.g., 'train', 'validation', 'test'). Default: 'test'.",
    )
    parser.add_argument(
        "--num_fewshots",
        type=int,
        default=None,
        help="number of fewshots to use",
    )
    parser.add_argument(
        "--limit",
        "-L",
        type=int,
        default=None,
        metavar="N",
        help="Limit the number of examples per task/dataset.",
    )

    parser.add_argument(
        "--batch_size",
        "-b",
        type=int,
        default=1,
        help="Batch size for use in inference when selected model is hf. Default 1",
    )

    # --- Model Arguments (Explicit Types) ---
    parser.add_argument(
        "--model",
        "-m",
        type=str,
        default="hf",
        choices=["hf", "cross_provider"],
        help="Specifies the model type/engine.\n"
        "- 'hf': Local Hugging Face model via HFAutoModel (default). Requires 'pretrained=...' in --model_args.\n"
        "- 'cross_provider': Remote model via CrossProviderInferenceEngine. Requires 'model_name=...' in --model_args.",
    )
    parser.add_argument(
        "--model_args",
        "-a",
        type=try_parse_json,
        default={},
        help="Comma separated string or JSON formatted arguments for the model/inference engine.\n"
        "Examples:\n"
        "- For --model hf (default): 'pretrained=meta-llama/Llama-3.1-8B-Instruct,torch_dtype=bfloat16,device=cuda'\n"
        "  (Note: 'pretrained' key is REQUIRED. Other args like 'torch_dtype', 'device', generation params are passed too)\n"
        "- For --model generic_remote: 'model_name=llama-3-3-70b-instruct,max_tokens=256,temperature=0.7'\n"
        "  (Note: 'model_name' key is REQUIRED)\n"
        '- JSON format: \'{"pretrained": "my_model", "torch_dtype": "float32"}\' or \'{"model_name": "openai/gpt-4o"}\'',
    )

    parser.add_argument(
        "--gen_kwargs",
        type=try_parse_json,
        default=None,
        help=(
            "Comma delimited string for model generation on greedy_until tasks,"
            """ e.g. temperature=0,top_p=0.1."""
        ),
    )

    parser.add_argument(
        "--chat_template_kwargs",
        type=try_parse_json,
        default=None,
        help=(
            "Comma delimited string for tokenizer kwargs"
            "e.g. thinking=True (https://github.com/huggingface/transformers/blob/9a1c1fe7edaefdb25ab37116a979832df298d6ea/src/transformers/tokenization_utils_base.py#L1542)"
        ),
    )

    # --- Output and Logging Arguments ---
    parser.add_argument(
        "--output_path",
        "-o",
        type=str,
        default=".",
        help="Directory to save evaluation results and logs. Default: current directory.",
    )
    parser.add_argument(
        "--output_file_prefix",
        type=str,
        default="evaluation_results",
        help="Prefix for the output JSON file names. Default: 'evaluation_results'.",
    )
    parser.add_argument(
        "--log_samples",
        "-s",
        action="store_true",
        default=False,
        help="If True, save individual predictions and scores to a separate JSON file.",
    )
    parser.add_argument(
        "--verbosity",
        "-v",
        type=str.upper,
        default="INFO",
        choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
        help="Controls logging verbosity level. Default: INFO.",
    )

    parser.add_argument(
        "--apply_chat_template",
        action="store_true",
        default=False,
    )

    # --- Unitxt Settings ---
    parser.add_argument(
        "--trust_remote_code",
        action="store_true",
        default=False,
        help="Allow execution of unverified code from the HuggingFace Hub (used by datasets/unitxt).",
    )
    parser.add_argument(
        "--disable_hf_cache",
        action="store_true",
        default=False,
        help="Disable HuggingFace datasets caching.",
    )
    parser.add_argument(
        "--cache_dir",
        type=str,
        default=None,
        help="Directory for HuggingFace datasets cache (overrides default).",
    )

    return parser


def setup_logging(verbosity: str) -> None:
    """Configures logging based on verbosity level."""
    logging.basicConfig(
        level=verbosity,
        format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
        force=True,  # Ensures reconfiguration works if basicConfig was called before
    )
    # Re-get the logger instance after basicConfig is set
    global logger
    logger = get_logger()
    logger.setLevel(verbosity)


def prepare_output_paths(output_path: str, prefix: str) -> Tuple[str, str]:
    """Creates output directory and defines file paths.

    Args:
        output_path (str): The directory where output files will be saved.
        prefix (str): The prefix for the output file names.

    Returns:
        Tuple[str, str]: A tuple containing the path for the results summary file
                         and the path for the detailed samples file.
    """
    os.makedirs(output_path, exist_ok=True)
    results_file_path = os.path.join(output_path, f"{prefix}.json")
    samples_file_path = os.path.join(output_path, f"{prefix}_samples.json")
    return results_file_path, samples_file_path


def configure_unitxt_settings(args: argparse.Namespace):
    """Configures unitxt settings and returns a context manager.

    Args:
        args (argparse.Namespace): Parsed command-line arguments.

    Returns:
        ContextManager: A context manager for applying unitxt settings.
    """
    unitxt_settings_dict = {
        "disable_hf_datasets_cache": args.disable_hf_cache,
        "allow_unverified_code": args.trust_remote_code,
    }
    if args.cache_dir:
        unitxt_settings_dict["hf_cache_dir"] = args.cache_dir
        # Also set environment variable as some HF parts might read it directly
        os.environ["HF_DATASETS_CACHE"] = args.cache_dir
        os.environ["HF_HOME"] = args.cache_dir
        logger.info(f"Set HF_DATASETS_CACHE to: {args.cache_dir}")

    if args.disable_hf_cache:
        os.environ["UNITXT_DISABLE_HF_DATASETS_CACHE"] = "True"

    logger.info(f"Applying unitxt settings: {unitxt_settings_dict}")
    return settings.context(**unitxt_settings_dict)


def cli_load_dataset(args: argparse.Namespace) -> HFDataset:
    """Loads the dataset based on command line arguments.

    Args:
        args (argparse.Namespace): Parsed command-line arguments.

    Returns:
        HFDataset: The loaded dataset.

    Raises:
        UnitxtArtifactNotFoundError: If the specified card or template artifact is not found.
        FileNotFoundError: If a specified file (e.g., in a local card path) is not found.
        AttributeError: If there's an issue accessing attributes during loading.
        ValueError: If there's a value-related error during loading (e.g., parsing).
    """
    logger.info(
        f"Loading task/dataset using identifier: '{args.tasks}' with split '{args.split}'"
    )

    benchmark_subsets = {}
    for task_str in args.tasks:
        dataset_args = task_str_to_dataset_args(task_str, args)

        benchmark_subsets[task_str] = DatasetRecipe(**dataset_args)

    benchmark = Benchmark(subsets=benchmark_subsets)

    test_dataset = load_dataset(benchmark, split=args.split)
    logger.info(
        f"Dataset loaded successfully. Number of instances: {len(test_dataset)}"
    )
    return test_dataset


def task_str_to_dataset_args(task_str, args):
    dataset_args = parse_key_equals_value_string_to_dict(task_str)

    if args.limit is not None:
        assert f"max_{args.split}_instances" not in dataset_args, (
            "limit was inputted both as an arg and as a task parameter"
        )
        # Check if limit or loader_limit is already present
        # dataset_args[f"max_{args.split}_instances"] = args.limit
        dataset_args[f"max_{args.split}_instances"] = args.limit
        # Use loader_limit for unitxt compatibility
        logger.info(
            f"Applying limit from --limit argument: max_{args.split}_instances={args.limit}"
        )

    if args.num_fewshots:
        assert "num_demos" not in dataset_args, (
            "num_demos was inputted both as an arg and as a task parameter"
        )
        dataset_args["num_demos"] = args.num_fewshots
        dataset_args.update(
            {
                "demos_taken_from": "train",
                "demos_pool_size": -1,
                "demos_removed_from_data": True,
            }
        )  # Use loader_limit for unitxt compatibility
        logger.info(
            f"Applying limit from --limit argument: num_demos={args.num_fewshots}"
        )

    if args.apply_chat_template:
        assert "format" not in dataset_args, (
            "format was inputted as a task parameter, but chat_api was requested"
        )
        dataset_args["format"] = "formats.chat_api"
        logger.info(
            "Applying chat template from --apply_chat_template argument: format=formats.chat_api"
        )

    return dataset_args


def prepare_kwargs(kwargs: dict) -> Dict[str, Any]:
    """Prepares the model arguments dictionary.

    Args:
        kwargs (dict): Parsed command-line arguments.

    Returns:
        Dict[str, Any]: The processed model arguments dictionary.
    """
    # Ensure model_args is a dictionary, handling potential string return from try_parse_json
    kwargs_dict = kwargs if isinstance(kwargs, dict) else {}
    if not isinstance(kwargs, dict) and kwargs is not None:
        logger.warning(
            f"Could not parse kwargs '{kwargs}' as JSON or key-value pairs. Treating as empty."
        )

    logger.info(f"Using kwargs: {kwargs_dict}")
    return kwargs_dict


def initialize_inference_engine(
    args: argparse.Namespace,
    model_args_dict: Dict[str, Any],
    chat_kwargs_dict: Dict[str, Any],
) -> InferenceEngine:
    """Initializes the appropriate inference engine based on arguments.

    Args:
        args (argparse.Namespace): Parsed command-line arguments.
        model_args_dict (Dict[str, Any]): Processed model arguments.
        chat_kwargs_dict (Dict[str, Any]): Processed chat arguments.

    Returns:
        InferenceEngine: The initialized inference engine instance.

    Raises:
        SystemExit: If required dependencies are missing for the selected model type.
        ValueError: If required keys are missing in model_args for the selected model type.
    """
    inference_model = None
    # --- Local Hugging Face Model (using HFAutoModelInferenceEngine) ---
    if args.model.lower() == "hf":
        if "pretrained" not in model_args_dict:
            logger.error(
                "Missing 'pretrained=<model_id_or_path>' in --model_args for '--model hf'."
            )
            raise ValueError(
                "Argument 'pretrained' is required in --model_args when --model is 'hf'"
            )

        local_model_name = model_args_dict.pop("pretrained")
        logger.info(
            f"Initializing HFAutoModelInferenceEngine for model: {local_model_name}"
        )

        model_args_dict.update({"batch_size": args.batch_size})
        logger.info(f"HFAutoModelInferenceEngine args: {model_args_dict}")

        inference_model = HFAutoModelInferenceEngine(
            model_name=local_model_name,
            **model_args_dict,
            chat_kwargs_dict=chat_kwargs_dict,
        )

    # --- Remote Model (CrossProviderInferenceEngine) ---
    elif args.model.lower() == "cross_provider":
        if "model_name" not in model_args_dict:
            logger.error(
                "Missing 'model_name=<provider/model_id>' in --model_args for '--model cross_provider'."
            )
            raise ValueError(
                "Argument 'model_name' is required in --model_args when --model is 'cross_provider'"
            )

        remote_model_name = model_args_dict.pop("model_name")
        logger.info(
            f"Initializing CrossProviderInferenceEngine for model: {remote_model_name}"
        )

        if (
            "max_tokens" not in model_args_dict
            and "max_new_tokens" not in model_args_dict
        ):
            logger.warning(
                f"'max_tokens' or 'max_new_tokens' not found in --model_args, {remote_model_name} might require it."
            )

        logger.info(f"CrossProviderInferenceEngine args: {model_args_dict}")

        # Note: CrossProviderInferenceEngine expects 'model' parameter, not 'model_name'
        inference_model = CrossProviderInferenceEngine(
            model=remote_model_name,
            **model_args_dict,
        )
    else:
        # This case should not be reached due to argparse choices
        logger.error(
            f"Invalid --model type specified: {args.model}. Use 'hf' or 'cross_provider'."
        )
        sys.exit(1)  # Exit here as it's an invalid configuration

    return inference_model


def run_inference(engine: InferenceEngine, dataset: HFDataset) -> List[Any]:
    """Runs inference using the initialized engine.

    Args:
        engine (InferenceEngine): The inference engine instance.
        dataset (HFDataset): The dataset to run inference on.

    Returns:
        List[Any]: A list of predictions.

    Raises:
        Exception: If an error occurs during inference.
    """
    logger.info("Starting inference...")
    try:
        predictions = engine.infer(dataset)
        logger.info("Inference completed.")
        if not predictions:
            logger.warning("Inference returned no predictions.")
            return []  # Return empty list if no predictions
        if len(predictions) != len(dataset):
            logger.error(
                f"Inference returned an unexpected number of predictions ({len(predictions)}). Expected {len(dataset)}."
            )
            # Don't exit, but log error. Evaluation might still work partially or fail later.
        return predictions
    except Exception:
        logger.exception("An error occurred during inference")  # Use logger.exception
        raise  # Re-raise after logging


def run_evaluation(predictions: List[Any], dataset: HFDataset) -> EvaluationResults:
    """Runs evaluation on the predictions.

    Args:
        predictions (List[Any]): The list of predictions from the model.
        dataset (HFDataset): The dataset containing references and other data.

    Returns:
        EvaluationResults: The evaluated dataset (list of instances with scores).

    Raises:
        RuntimeError: If evaluation returns no results or an unexpected type.
        Exception: If any other error occurs during evaluation.
    """
    logger.info("Starting evaluation...")
    if not predictions:
        logger.warning("Skipping evaluation as there are no predictions.")
        return []  # Return empty list if no predictions to evaluate

    try:
        evaluation_results = evaluate(predictions=predictions, data=dataset)
        logger.info("Evaluation completed.")
        if not evaluation_results:
            logger.error("Evaluation returned no results (empty list/None).")
            # Raise an error as this indicates a problem in the evaluation process
            raise RuntimeError("Evaluation returned no results.")
        if not isinstance(evaluation_results, EvaluationResults):
            logger.error(
                f"Evaluation returned unexpected type: {type(evaluation_results)}. Expected list."
            )
            raise RuntimeError(
                f"Evaluation returned unexpected type: {type(evaluation_results)}"
            )

        return evaluation_results
    except Exception:
        logger.exception("An error occurred during evaluation")  # Use logger.exception
        raise  # Re-raise after logging


def _get_unitxt_commit_hash() -> Optional[str]:
    """Tries to get the git commit hash of the installed unitxt package."""
    try:
        # Find the directory of the unitxt package
        # Use inspect to be more robust finding the package path

        current_script_path = os.path.abspath(__file__)
        package_dir = os.path.dirname(current_script_path)

        # Check if it's a git repository and get the commit hash
        # Use absolute path for git command
        git_command = ["git", "-C", os.path.abspath(package_dir), "rev-parse", "HEAD"]
        logger.debug(f"Running git command: {' '.join(git_command)}")
        result = subprocess.run(
            git_command,
            capture_output=True,
            text=True,
            check=False,  # Don't raise error if git command fails
            encoding="utf-8",
            errors="ignore",  # Ignore potential decoding errors
        )
        if result.returncode == 0:
            commit_hash = result.stdout.strip()
            logger.info(f"Found unitxt git commit hash: {commit_hash}")
            # Verify it looks like a hash (e.g., 40 hex chars)
            if len(commit_hash) == 40 and all(
                c in "0123456789abcdef" for c in commit_hash
            ):
                return commit_hash
            logger.warning(
                f"Git command output '{commit_hash}' doesn't look like a valid commit hash."
            )
            return None
        stderr_msg = result.stderr.strip() if result.stderr else "No stderr"
        logger.warning(
            f"Could not get unitxt git commit hash (git command failed with code {result.returncode}): {stderr_msg}"
        )
        return None
    except ImportError:
        logger.warning("unitxt package not found, cannot determine commit hash.")
        return None
    except FileNotFoundError:
        logger.warning(
            "'git' command not found in PATH. Cannot determine unitxt commit hash."
        )
        return None
    except Exception as e:
        logger.warning(
            f"Error getting unitxt commit hash: {e}", exc_info=True
        )  # Log traceback
        return None


def _get_installed_packages() -> Dict[str, str]:
    """Gets a dictionary of installed packages and their versions."""
    packages = {}
    try:
        for dist in importlib.metadata.distributions():
            # Handle potential missing metadata gracefully
            name = dist.metadata.get("Name")
            version = dist.metadata.get("Version")
            if name and version:
                packages[name] = version
            elif name:
                packages[name] = "N/A"  # Record package even if version is missing
                logger.debug(f"Could not find version for package: {name}")

        logger.info(f"Collected versions for {len(packages)} installed packages.")
    except Exception as e:
        logger.warning(f"Could not retrieve installed package list: {e}", exc_info=True)
    return packages


def _get_unitxt_version() -> str:
    """Gets the installed unitxt version using importlib.metadata."""
    try:
        version = importlib.metadata.version("unitxt")
        logger.info(f"Found unitxt version using importlib.metadata: {version}")
        return version
    except importlib.metadata.PackageNotFoundError:
        logger.warning(
            "Could not find 'unitxt' package version using importlib.metadata. Is it installed correctly?"
        )
        return "N/A"
    except Exception as e:
        logger.warning(
            f"Error getting unitxt version using importlib.metadata: {e}", exc_info=True
        )
        return "N/A"


def prepend_timestamp_to_path(original_path, timestamp):
    """Takes a path string and a timestamp string, prepends the timestamp to the filename part of the path, and returns the new path string."""
    directory, filename = os.path.split(original_path)
    # Use an f-string to create the new filename with the timestamp prepended
    new_filename = f"{timestamp}_{filename}"
    # Join the directory and the new filename back together
    return os.path.join(directory, new_filename)


def _save_results_to_disk(
    args: argparse.Namespace,
    global_scores: Dict[str, Any],
    all_samples_data: Dict[str, List[Dict[str, Any]]],
    results_path: str,
    samples_path: str,
) -> None:
    """Saves the configuration, environment info, global scores, and samples to JSON files.

    Args:
        args (argparse.Namespace): Parsed command-line arguments.
        global_scores (Dict[str, Any]): Dictionary of global scores.
        all_samples_data (Dict[str, List[Dict[str, Any]]]): List of processed sample data.
        results_path (str): Path to save the summary results JSON file.
        samples_path (str): Path to save the detailed samples JSON file.
    """
    # --- Gather Configuration ---
    config_to_save = {}
    for k, v in vars(args).items():
        # Ensure complex objects are represented as strings
        if isinstance(v, (str, int, float, bool, list, dict, type(None))):
            config_to_save[k] = v
        else:
            try:
                # Try standard repr first
                config_to_save[k] = repr(v)
            except Exception:
                # Fallback if repr fails
                config_to_save[k] = (
                    f"<Object of type {type(v).__name__} could not be represented>"
                )

    # --- Gather Environment Info ---
    unitxt_commit = _get_unitxt_commit_hash()
    # Get version using the dedicated function
    unitxt_pkg_version = _get_unitxt_version()

    environment_info = {
        "timestamp_utc": datetime.utcnow().isoformat() + "Z",
        "command_line_invocation": sys.argv,
        "parsed_arguments": config_to_save,  # Include parsed args here as well
        "unitxt_version": unitxt_pkg_version,  # Use version from importlib.metadata
        "unitxt_commit_hash": unitxt_commit if unitxt_commit else "N/A",
        "python_version": platform.python_version(),
        "system": platform.system(),
        "system_version": platform.version(),
        "installed_packages": _get_installed_packages(),
    }

    # --- Prepare Final Results Structure ---
    results_summary = {
        "environment_info": environment_info,
        "results": global_scores,
    }

    # prepend to the results_path name the time in a wat like this: 2025-04-04T11:37:32

    timestamp = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")

    results_path = prepend_timestamp_to_path(results_path, timestamp)
    samples_path = prepend_timestamp_to_path(samples_path, timestamp)

    # --- Save Summary ---
    logger.info(f"Saving global results summary to: {results_path}")
    try:
        with open(results_path, "w", encoding="utf-8") as f:
            json.dump(results_summary, f, indent=4, ensure_ascii=False)
    except OSError as e:
        logger.error(f"Failed to write results summary file {results_path}: {e}")
    except TypeError as e:
        logger.error(
            f"Failed to serialize results summary to JSON: {e}. Check data types."
        )
        # Log the problematic structure if possible (might be large)
        # logger.debug(f"Problematic results_summary structure: {results_summary}")

    # --- Save Samples (if requested) ---
    if args.log_samples:
        logger.info(f"Saving detailed samples to: {samples_path}")
        # Structure samples file with environment info as well for self-containment
        samples_output = {
            "environment_info": environment_info,  # Repeat env info here
            "samples": all_samples_data,
        }
        try:
            with open(samples_path, "w", encoding="utf-8") as f:
                json.dump(samples_output, f, indent=4, ensure_ascii=False)
        except OSError as e:
            logger.error(f"Failed to write samples file {samples_path}: {e}")
        except TypeError as e:
            logger.error(f"Failed to serialize samples to JSON: {e}. Check data types.")


def process_and_save_results(
    args: argparse.Namespace,
    evaluation_results: EvaluationResults,
    results_path: str,
    samples_path: str,
) -> None:
    """Processes, prints, and saves the evaluation results.

    Args:
        args (argparse.Namespace): Parsed command-line arguments.
        evaluation_results (EvaluationResults): The list of evaluated instances.
        results_path (str): Path to save the summary results JSON file.
        samples_path (str): Path to save the detailed samples JSON file.

    Raises:
        Exception: If an error occurs during result processing or saving (re-raised).
    """
    try:
        # global_scores, all_samples_data = _extract_scores_and_samples(evaluated_dataset)

        subsets_scores = evaluation_results.subsets_scores
        instances_results = evaluation_results.instance_scores

        subset_instances = {}
        for instance in instances_results:
            if instance["subset"][0] not in subset_instances:
                subset_instances[instance["subset"][0]] = []
            del instance["postprocessors"]
            subset_instances[instance["subset"][0]].append(instance)

        logger.info(f"\n{subsets_scores.summary}")

        # --- Save Results ---
        # Pass all necessary data to the saving function
        _save_results_to_disk(
            args, subsets_scores, subset_instances, results_path, samples_path
        )

    except Exception:
        logger.exception(
            "An error occurred during result processing or saving"
        )  # Use logger.exception
        raise  # Re-raise after logging


def main():
    """Main function to parse arguments and run evaluation."""
    parser = setup_parser()
    args = parser.parse_args()

    # Setup logging ASAP
    setup_logging(args.verbosity)

    logger.info("Starting Unitxt Evaluation CLI")
    # Log raw and parsed args at DEBUG level
    logger.debug(f"Raw command line arguments: {sys.argv}")
    logger.debug(f"Parsed arguments: {vars(args)}")  # Log the vars(args) dict
    logger.debug(
        f"Parsed model_args type: {type(args.model_args)}, value: {args.model_args}"
    )

    try:
        results_path, samples_path = prepare_output_paths(
            args.output_path, args.output_file_prefix
        )

        # Apply unitxt settings within a context manager
        with configure_unitxt_settings(args):
            test_dataset = cli_load_dataset(args)
            model_args_dict = prepare_kwargs(args.model_args)
            gen_kwargs_dict = prepare_kwargs(args.gen_kwargs)
            chat_kwargs_dict = prepare_kwargs(args.chat_template_kwargs)

            model_args_dict.update(gen_kwargs_dict)
            inference_model = initialize_inference_engine(
                args, model_args_dict, chat_kwargs_dict
            )
            predictions = run_inference(inference_model, test_dataset)
            evaluation_results = run_evaluation(predictions, test_dataset)
            process_and_save_results(
                args, evaluation_results, results_path, samples_path
            )

    # --- More Specific Error Handling ---
    except (UnitxtArtifactNotFoundError, FileNotFoundError) as e:
        logger.exception(f"Error loading artifact or file: {e}")
        sys.exit(1)
    except (AttributeError, ValueError) as e:
        # Catch issues like missing keys in args, parsing errors, etc.
        logger.exception(f"Configuration or value error: {e}")
        sys.exit(1)
    except ImportError as e:
        # Catch missing optional dependencies
        logger.exception(f"Missing dependency: {e}")
        sys.exit(1)
    except RuntimeError as e:
        # Catch errors explicitly raised during execution (e.g., evaluation failure)
        logger.exception(f"Runtime error during processing: {e}")
        sys.exit(1)
    except Exception as e:
        # Catch any other unexpected errors
        logger.exception(f"An unexpected error occurred: {e}")
        sys.exit(1)

    logger.info("Unitxt Evaluation CLI finished successfully.")


if __name__ == "__main__":
    main()