anno-cli 0.10.0

CLI for anno: extract entities, coreference chains, relations, and PII from text, HTML, and URLs
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
//! Benchmark command - Comprehensive evaluation across all task-dataset-backend combinations

use clap::{ArgAction, Parser, ValueEnum};

#[cfg(feature = "eval")]
use anno_eval::eval::loader::DatasetId;
#[cfg(feature = "eval")]
use anno_eval::eval::task_evaluator::TaskEvaluator;
#[cfg(feature = "eval")]
use anno_eval::eval::task_mapping::Task;

/// Comprehensive evaluation across all task-dataset-backend combinations
#[derive(Parser, Debug)]
pub struct BenchmarkArgs {
    /// Optional curated profile (panel) to make comparisons meaningful.
    ///
    /// If provided, and you do not explicitly pass `--tasks/--datasets/--backends`,
    /// the benchmark will use a curated slice instead of “everything”.
    #[arg(long, value_enum)]
    pub profile: Option<BenchmarkProfile>,

    /// Multi-seed mode: run the benchmark for each seed (comma-separated).
    ///
    /// If set, this runs one full benchmark per seed and writes per-seed artifacts by
    /// suffixing the `--output/--output-json` paths with `-seed<SEED>`.
    #[arg(long, value_delimiter = ',')]
    pub seeds: Option<Vec<u64>>,

    /// When multiple backends are requested, run heavy ONNX backends in separate processes.
    ///
    /// This reduces memory coupling (notably GLiNER) at the cost of multiple runs.
    #[arg(long, default_value_t = true, action = ArgAction::Set)]
    pub split_heavy_backends: bool,

    /// Tasks to evaluate (comma-separated: ner,coref,relation). Default: all
    #[arg(short, long, value_delimiter = ',')]
    pub tasks: Option<Vec<String>>,

    /// Datasets to use (comma-separated). Default: all suitable datasets
    #[arg(short, long, value_delimiter = ',')]
    pub datasets: Option<Vec<String>>,

    /// Backends to test (comma-separated). Default: all compatible backends
    #[arg(short, long, value_delimiter = ',')]
    pub backends: Option<Vec<String>>,

    /// Maximum examples per dataset (for quick testing)
    #[arg(short, long)]
    pub max_examples: Option<usize>,

    /// Random seed for sampling (for reproducibility and varied testing)
    #[arg(long)]
    pub seed: Option<u64>,

    /// Only use cached datasets (skip downloads)
    #[arg(long)]
    pub cached_only: bool,

    /// Output file for markdown report (default: stdout)
    #[arg(short, long)]
    pub output: Option<String>,

    /// Optional JSON output path (machine-readable results).
    ///
    /// This writes the full `ComprehensiveEvalResults` structure so other tools can
    /// aggregate/rank backends without parsing markdown.
    #[arg(long, value_name = "PATH")]
    pub output_json: Option<String>,
}

/// Curated benchmark profiles (panels) for meaningful comparisons.
#[derive(Debug, Clone, Copy, ValueEnum)]
pub enum BenchmarkProfile {
    /// Standard NER label space (PER/ORG/LOC/MISC-ish).
    NerStandard,
    /// Zero-shot NER on multilingual/fine-grained datasets (compare zero-shot backends only).
    NerZeroshotMultilingual,
    /// Coreference sanity (single dataset).
    CorefStandard,
    /// Relation extraction sanity (single dataset).
    RelationStandard,
}

fn suffix_path(p: &str, suffix: &str) -> String {
    match p.rsplit_once('.') {
        Some((stem, ext)) => format!("{stem}{suffix}.{ext}"),
        None => format!("{p}{suffix}"),
    }
}

fn is_heavy_backend(name: &str) -> bool {
    matches!(
        name,
        "gliner_onnx" | "gliner_multitask" | "gliner_poly" | "gliner_pii" | "gliner_relex"
    )
}

fn profile_defaults(profile: BenchmarkProfile) -> (Vec<String>, Vec<String>, Vec<String>) {
    match profile {
        BenchmarkProfile::NerStandard => (
            vec!["ner".to_string()],
            vec![
                "WikiGold".to_string(),
                "Wnut17".to_string(),
                "CoNLL2003Sample".to_string(),
            ],
            vec![
                "bert_onnx".to_string(),
                "stacked".to_string(),
                "heuristic".to_string(),
            ],
        ),
        BenchmarkProfile::NerZeroshotMultilingual => (
            vec!["ner".to_string()],
            vec![
                "WikiANN".to_string(),
                "MasakhaNER".to_string(),
                "MultiNERD".to_string(),
                "MultiCoNERv2".to_string(),
            ],
            vec!["gliner_onnx".to_string(), "nuner".to_string()],
        ),
        BenchmarkProfile::CorefStandard => (
            vec!["coref".to_string()],
            vec!["GAP".to_string()],
            vec!["coref_resolver".to_string()],
        ),
        BenchmarkProfile::RelationStandard => (
            vec!["relation".to_string()],
            vec!["DocRED".to_string()],
            vec!["tplinker".to_string()],
        ),
    }
}

/// Execute the benchmark command.
pub fn run(args: BenchmarkArgs) -> Result<(), String> {
    #[cfg(not(feature = "eval"))]
    {
        let _ = args;
        Err("Benchmark command requires --features eval".to_string())
    }

    #[cfg(feature = "eval")]
    {
        println!("=== Comprehensive Task-Dataset-Backend Evaluation ===\n");

        // Multi-seed: run one benchmark per seed and suffix outputs.
        if let Some(seeds) = &args.seeds {
            if seeds.is_empty() {
                return Err("--seeds was provided but empty".to_string());
            }
            for seed in seeds {
                let per = BenchmarkArgs {
                    profile: args.profile,
                    seeds: None,
                    split_heavy_backends: args.split_heavy_backends,
                    tasks: args.tasks.clone(),
                    datasets: args.datasets.clone(),
                    backends: args.backends.clone(),
                    max_examples: args.max_examples,
                    seed: Some(*seed),
                    cached_only: args.cached_only,
                    output: args
                        .output
                        .as_ref()
                        .map(|p| suffix_path(p, &format!("-seed{seed}"))),
                    output_json: args
                        .output_json
                        .as_ref()
                        .map(|p| suffix_path(p, &format!("-seed{seed}"))),
                };
                run(per)?;
            }
            return Ok(());
        }

        // Apply curated defaults if profile was provided and user didn't override.
        let (profile_tasks, profile_datasets, profile_backends) = match args.profile {
            Some(p) => profile_defaults(p),
            None => (Vec::new(), Vec::new(), Vec::new()),
        };

        let task_strs = args.tasks.clone().unwrap_or(profile_tasks);
        let dataset_strs = args.datasets.clone().unwrap_or(profile_datasets);
        let backend_strs = args.backends.clone().unwrap_or(profile_backends);

        // Parse tasks
        let tasks = if !task_strs.is_empty() {
            let mut parsed = Vec::new();
            for t in task_strs {
                match t.to_lowercase().as_str() {
                    "ner" | "ner_task" => parsed.push(Task::NER),
                    "coref" | "coreference" | "intradoc_coref" => parsed.push(Task::IntraDocCoref),
                    "relation" | "relation_extraction" => parsed.push(Task::RelationExtraction),
                    other => {
                        return Err(format!(
                            "Unknown task: {}. Use: ner, coref, relation",
                            other
                        ));
                    }
                }
            }
            parsed
        } else {
            Task::all().to_vec()
        };

        // Parse datasets
        let datasets = if !dataset_strs.is_empty() {
            let mut parsed = Vec::new();
            for d in dataset_strs {
                let dataset_id: DatasetId = d
                    .parse()
                    .map_err(|e| format!("Invalid dataset '{}': {}", d, e))?;
                parsed.push(dataset_id);
            }
            parsed
        } else {
            vec![] // Empty = use all suitable datasets
        };

        // Parse backends
        let backends = backend_strs;

        // Heavy backend splitting: run heavy ONNX backends in subprocesses to reduce memory coupling.
        //
        // If `--output-json` is provided, we merge subprocess JSON artifacts so the final report
        // still compares all requested backends in one table.
        if args.split_heavy_backends && backends.len() > 1 {
            let (heavy, light): (Vec<String>, Vec<String>) =
                backends.into_iter().partition(|b| is_heavy_backend(b));

            // Run each heavy backend as its own `anno benchmark` invocation (same args, one backend).
            for hb in &heavy {
                let exe = std::env::current_exe()
                    .map_err(|e| format!("Failed to locate current executable: {}", e))?;

                let mut cmd = std::process::Command::new(exe);
                cmd.arg("benchmark");
                if let Some(profile) = args.profile {
                    cmd.arg("--profile")
                        .arg(profile.to_possible_value().unwrap().get_name());
                }
                // Use canonical short codes for tasks (ensures we don't leak debug formatting).
                cmd.arg("--tasks")
                    .arg(tasks.iter().map(|t| t.code()).collect::<Vec<_>>().join(","));
                if !datasets.is_empty() {
                    cmd.arg("--datasets").arg(
                        datasets
                            .iter()
                            .map(|d| d.to_string())
                            .collect::<Vec<_>>()
                            .join(","),
                    );
                }
                cmd.arg("--backends").arg(hb);
                if let Some(max) = args.max_examples {
                    cmd.arg("--max-examples").arg(max.to_string());
                }
                if let Some(seed) = args.seed {
                    cmd.arg("--seed").arg(seed.to_string());
                }
                if args.cached_only {
                    cmd.arg("--cached-only");
                }
                if let Some(out) = &args.output {
                    cmd.arg("--output").arg(suffix_path(out, &format!("-{hb}")));
                }
                if let Some(outj) = &args.output_json {
                    cmd.arg("--output-json")
                        .arg(suffix_path(outj, &format!("-{hb}")));
                }
                // Disable splitting in child.
                cmd.arg("--split-heavy-backends=false");

                let status = cmd
                    .status()
                    .map_err(|e| format!("Failed to spawn benchmark subprocess: {}", e))?;
                if !status.success() {
                    return Err(format!("Heavy backend subprocess failed for '{hb}'"));
                }
            }

            // Continue with light backends in this process (if any).
            if light.is_empty() {
                return Ok(());
            }

            // If we cannot merge (no JSON path), fall back to the old behavior: emit
            // per-backend artifacts only.
            if args.output_json.is_none() {
                let per = BenchmarkArgs {
                    profile: args.profile,
                    seeds: None,
                    split_heavy_backends: false,
                    tasks: Some(tasks.iter().map(|t| t.code().to_string()).collect()),
                    datasets: if datasets.is_empty() {
                        None
                    } else {
                        Some(datasets.iter().map(|d| d.to_string()).collect())
                    },
                    backends: Some(light),
                    max_examples: args.max_examples,
                    seed: args.seed,
                    cached_only: args.cached_only,
                    output: args.output.clone(),
                    output_json: args.output_json.clone(),
                };
                return run(per);
            }

            // Otherwise: run light backends here, then merge heavy JSON artifacts into a single
            // combined report/JSON at the original `--output`/`--output-json` paths.
            use anno_eval::eval::config_builder::TaskEvalConfigBuilder;
            use anno_eval::eval::task_evaluator::{
                ComprehensiveEvalResults, EvalSummary, TaskEvalResult,
            };
            use std::collections::HashSet;

            fn summarize(results: &[TaskEvalResult]) -> EvalSummary {
                let skipped = results.iter().filter(|r| r.is_skipped()).count();
                let failed = results
                    .iter()
                    .filter(|r| !r.success && !r.is_skipped())
                    .count();

                let mut tasks: Vec<Task> = Vec::new();
                let mut datasets: Vec<DatasetId> = Vec::new();
                let mut backends: Vec<String> = Vec::new();

                for r in results {
                    if !tasks.contains(&r.task) {
                        tasks.push(r.task);
                    }
                    if !datasets.contains(&r.dataset) {
                        datasets.push(r.dataset);
                    }
                    if !backends.contains(&r.backend) {
                        backends.push(r.backend.clone());
                    }
                }

                EvalSummary {
                    total_combinations: results.len(),
                    successful: results.iter().filter(|r| r.success).count(),
                    failed,
                    skipped,
                    tasks,
                    datasets,
                    backends,
                }
            }

            let evaluator =
                TaskEvaluator::new().map_err(|e| format!("Failed to create evaluator: {}", e))?;

            let mut builder = TaskEvalConfigBuilder::new()
                .with_tasks(tasks.clone())
                .with_datasets(datasets.clone())
                .with_backends(light)
                .require_cached(args.cached_only)
                .with_confidence_intervals(true)
                .with_familiarity(true);

            if let Some(max) = args.max_examples {
                if max > 0 {
                    builder = builder.with_max_examples(max);
                }
            }
            if let Some(seed) = args.seed {
                builder = builder.with_seed(seed);
            }

            let config = builder.build();

            println!("Running comprehensive evaluation...");
            println!("Tasks: {:?}", config.tasks);
            if !config.datasets.is_empty() {
                println!("Datasets: {:?}", config.datasets);
            } else {
                println!("Datasets: all suitable datasets");
            }
            if !config.backends.is_empty() {
                println!("Backends: {:?}", config.backends);
            } else {
                println!("Backends: all compatible backends");
            }
            if let Some(max) = config.max_examples {
                println!("Max examples per dataset: {}", max);
            }
            if let Some(seed) = config.seed {
                println!("Random seed: {}", seed);
            }
            println!();

            let mut combined = evaluator
                .evaluate_all(config)
                .map_err(|e| format!("Evaluation failed: {}", e))?;

            let json_path = args
                .output_json
                .as_ref()
                .expect("checked output_json Some above");

            // Merge heavy artifacts (JSON) into the combined result set.
            // Deduplicate exact (task,dataset,backend) triples in case of retries.
            let mut seen: HashSet<(Task, DatasetId, String)> = combined
                .results
                .iter()
                .map(|r| (r.task, r.dataset, r.backend.clone()))
                .collect();

            for hb in &heavy {
                let p = suffix_path(json_path, &format!("-{hb}"));
                let raw = std::fs::read_to_string(&p)
                    .map_err(|e| format!("Failed to read heavy JSON artifact {}: {}", p, e))?;
                let parsed: ComprehensiveEvalResults = serde_json::from_str(&raw)
                    .map_err(|e| format!("Failed to parse heavy JSON artifact {}: {}", p, e))?;
                for r in parsed.results {
                    let key = (r.task, r.dataset, r.backend.clone());
                    if seen.insert(key) {
                        combined.results.push(r);
                    }
                }
            }

            combined.summary = summarize(&combined.results);

            // Print summary for the combined run.
            println!("=== Evaluation Summary ===");
            println!(
                "Total combinations: {}",
                combined.summary.total_combinations
            );
            println!("Successful: {}", combined.summary.successful);
            println!(
                "Skipped (feature not available): {}",
                combined.summary.skipped
            );
            println!("Failed (actual errors): {}", combined.summary.failed);
            println!("\nTasks evaluated: {}", combined.summary.tasks.len());
            println!("Datasets used: {}", combined.summary.datasets.len());
            println!("Backends tested: {}", combined.summary.backends.len());
            println!();

            // Write combined JSON and markdown to the original paths.
            let json = serde_json::to_string_pretty(&combined)
                .map_err(|e| format!("Failed to serialize results as JSON: {}", e))?;
            std::fs::write(json_path, json)
                .map_err(|e| format!("Failed to write JSON report to {}: {}", json_path, e))?;
            println!("JSON saved to: {}", json_path);

            let report = combined.to_markdown();
            if let Some(output_path) = &args.output {
                std::fs::write(output_path, &report)
                    .map_err(|e| format!("Failed to write report to {}: {}", output_path, e))?;
                println!("Report saved to: {}", output_path);
            } else {
                println!("=== Markdown Report ===");
                println!("{}", report);
            }

            return Ok(());
        }

        // Create evaluator
        let evaluator =
            TaskEvaluator::new().map_err(|e| format!("Failed to create evaluator: {}", e))?;

        // Configure evaluation using builder pattern
        use anno_eval::eval::config_builder::TaskEvalConfigBuilder;
        let mut builder = TaskEvalConfigBuilder::new()
            .with_tasks(tasks)
            .with_datasets(datasets)
            .with_backends(backends)
            .require_cached(args.cached_only)
            .with_confidence_intervals(true)
            .with_familiarity(true);

        // Set max_examples (None means "all examples", 0 also means "all examples")
        if let Some(max) = args.max_examples {
            if max > 0 {
                builder = builder.with_max_examples(max);
            }
            // If max == 0, don't set it (None = unlimited)
        }

        // Only set seed if provided (default is 42 in builder)
        if let Some(seed) = args.seed {
            builder = builder.with_seed(seed);
        }

        let config = builder.build();

        println!("Running comprehensive evaluation...");
        println!("Tasks: {:?}", config.tasks);
        if !config.datasets.is_empty() {
            println!("Datasets: {:?}", config.datasets);
        } else {
            println!("Datasets: all suitable datasets");
        }
        if !config.backends.is_empty() {
            println!("Backends: {:?}", config.backends);
        } else {
            println!("Backends: all compatible backends");
        }
        if let Some(max) = config.max_examples {
            println!("Max examples per dataset: {}", max);
        }
        if let Some(seed) = config.seed {
            println!("Random seed: {}", seed);
        }
        println!();

        // Run evaluation
        let results = evaluator
            .evaluate_all(config)
            .map_err(|e| format!("Evaluation failed: {}", e))?;

        // Print summary
        println!("=== Evaluation Summary ===");
        println!("Total combinations: {}", results.summary.total_combinations);
        println!("Successful: {}", results.summary.successful);
        println!(
            "Skipped (feature not available): {}",
            results.summary.skipped
        );
        println!("Failed (actual errors): {}", results.summary.failed);
        println!("\nTasks evaluated: {}", results.summary.tasks.len());
        println!("Datasets used: {}", results.summary.datasets.len());
        println!("Backends tested: {}", results.summary.backends.len());
        println!();

        // Generate markdown report
        let report = results.to_markdown();

        // Optional JSON artifact (stable-ish, for aggregation)
        if let Some(json_path) = &args.output_json {
            let json = serde_json::to_string_pretty(&results)
                .map_err(|e| format!("Failed to serialize results as JSON: {}", e))?;
            std::fs::write(json_path, json)
                .map_err(|e| format!("Failed to write JSON report to {}: {}", json_path, e))?;
            println!("JSON saved to: {}", json_path);
        }

        // Output report
        if let Some(output_path) = &args.output {
            std::fs::write(output_path, &report)
                .map_err(|e| format!("Failed to write report to {}: {}", output_path, e))?;
            println!("Report saved to: {}", output_path);
        } else {
            println!("=== Markdown Report ===");
            println!("{}", report);
        }

        Ok(())
    }
}