File size: 29,200 Bytes
a388fcc
 
 
5322748
d7e938c
a388fcc
adec6c3
 
 
5322748
adec6c3
 
 
 
 
 
 
213781c
c5b8c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02a02fb
c5b8c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca5d05c
c5b8c18
 
ca5d05c
c5b8c18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca5d05c
 
 
 
 
 
 
 
213781c
ca5d05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
537c1de
ca5d05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
537c1de
ca5d05c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5b8c18
ca5d05c
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
import gradio as gr
import sys
import os
import torch
sys.path.append(".")

def setup_cpu_environment():
    os.environ['CUDA_VISIBLE_DEVICES'] = ''
    
    torch.set_num_threads(4) 
    
    os.environ['TOKENIZERS_PARALLELISM'] = 'false'
    
    os.environ['TRANSFORMERS_CACHE'] = './cache'

setup_cpu_environment()

from RadEval import RadEval, compare_systems

def run_radeval_simple(ref_text, hyp_text, selected_metrics):
    """
    Run RadEval with selected metrics on a pair of reference and hypothesis texts
    """
    try:
        
        refs = [ref_text.strip()]
        hyps = [hyp_text.strip()]
        
        # Configure RadEval based on selected metrics
        config = {
            'do_radgraph': 'RadGraph F1' in selected_metrics,
            'do_bleu': 'BLEU' in selected_metrics,
            'do_rouge': 'ROUGE' in selected_metrics,
            'do_bertscore': 'BERTScore' in selected_metrics,
            'do_chexbert': 'CheXbert F1' in selected_metrics,
            'do_ratescore': 'RaTEScore' in selected_metrics,
            'do_radcliq': 'RadCliQ' in selected_metrics,
            'do_temporal': 'Temporal F1' in selected_metrics,
            'do_radeval_bertsore': 'RadEval BERTScore' in selected_metrics,
            'do_green': 'GREEN' in selected_metrics,
            'do_srr_bert': 'SRR-BERT' in selected_metrics
        }
        
        # Initialize RadEval with selected metrics
        evaluator = RadEval(**config)
        
        # Run evaluation
        results = evaluator(refs=refs, hyps=hyps)
        
        # Prepare results for display
        table_data = []
        analysis_text = "## πŸš€ RadEval Results\n\n"
        analysis_text += f"**Reference:** {ref_text[:100]}{'...' if len(ref_text) > 100 else ''}\n\n"
        analysis_text += f"**Hypothesis:** {hyp_text[:100]}{'...' if len(hyp_text) > 100 else ''}\n\n"
        analysis_text += "### Evaluation Scores:\n\n"
        
        for metric, score in results.items():
            if isinstance(score, (int, float)):
                formatted_score = f"{score:.4f}" if isinstance(score, float) else str(score)
                table_data.append([metric, formatted_score])
                analysis_text += f"- **{metric}**: {formatted_score}\n"
            elif isinstance(score, dict):
                # Handle nested metrics
                for sub_metric, sub_score in score.items():
                    if isinstance(sub_score, (int, float)):
                        formatted_score = f"{sub_score:.4f}" if isinstance(sub_score, float) else str(sub_score)
                        metric_name = f"{metric}_{sub_metric}"
                        table_data.append([metric_name, formatted_score])
                        analysis_text += f"- **{metric_name}**: {formatted_score}\n"
        
        if not table_data:
            return "No metrics were computed. Please select at least one metric.", [["No results", ""]]
            
        return analysis_text, table_data
        
    except ImportError as e:
        error_msg = f"Import Error: {str(e)}. Please ensure RadEval dependencies are installed."
        return error_msg, [["Error", error_msg]]
    except Exception as e:
        error_msg = f"Evaluation Error: {str(e)}"
        return error_msg, [["Error", error_msg]]


# Example pairs for radiology reports
examples = {
    "Normal vs Normal": {
        "ref": "Heart size is normal. Lungs are clear. No pleural effusion or pneumothorax.",
        "hyp": "Cardiac silhouette is within normal limits. Lungs are clear bilaterally. No effusion or pneumothorax identified.",
    },
    "Pneumonia Case": {
        "ref": "Moderate cardiomegaly. Bilateral lower lobe consolidations consistent with pneumonia.",
        "hyp": "Enlarged heart. Worsening bilateral infiltrates in the lower lobes suggestive of pneumonia.",
    },
    "Temporal Comparison": {
        "ref": "Compared to prior study, the pleural effusion has increased in size. New bilateral infiltrates are present.",
        "hyp": "The pleural effusion is larger than on the previous examination. There are new bilateral pulmonary infiltrates.",
    },
    "Discordant Reports": {
        "ref": "No acute cardiopulmonary process. Normal heart size and lung fields.",
        "hyp": "Mild cardiomegaly with bilateral lower lobe atelectasis. Small pleural effusion on the right.",
    },
    "Ambiguous Language": {
        "ref": "There is a small left-sided pleural effusion with adjacent atelectasis.",
        "hyp": "Possible small effusion on the left. Atelectasis cannot be excluded.",
    },
    "Surgical Follow-up": {
        "ref": "Status post coronary artery bypass grafting. No evidence of acute complication.",
        "hyp": "Post-operative changes from CABG are present. No signs of surgical complication.",
    },
    "False Positive": {
        "ref": "No focal consolidation, pleural effusion, or pneumothorax identified.",
        "hyp": "Right lower lobe consolidation concerning for pneumonia.",
    },
    "Textual Hallucination": {
        "ref": "Heart and mediastinum are normal. Lungs are clear.",
        "hyp": "Large left pleural effusion with mediastinal shift to the right.",
    },
    "Negation Challenge": {
        "ref": "No evidence of pneumothorax or pleural effusion.",
        "hyp": "Evidence of small pneumothorax on the right.",
    },
    "Fine-grained Difference": {
        "ref": "Mild interstitial markings at the lung bases, likely chronic.",
        "hyp": "Subtle increased interstitial opacities at both lung bases, likely chronic in nature.",
    }
}

def update_fields(choice):
    """Update text fields based on example selection"""
    if choice == "Custom":
        return gr.update(value="", interactive=True), gr.update(value="", interactive=True)
    else:
        return (
            gr.update(value=examples[choice]["ref"], interactive=False), 
            gr.update(value=examples[choice]["hyp"], interactive=False)
        )


# Available metrics (ordered by computational complexity)
available_metrics = [
    "BLEU",
    "ROUGE", 
    "BERTScore",
    "Temporal F1",
    "RadEval BERTScore",
    "RaTEScore",
    "RadCliQ",
    "SRR-BERT",
    "CheXbert F1",
    "RadGraph F1",
    "GREEN"
]

# Fast metrics for default selection
default_metrics = ["BLEU", "ROUGE", "BERTScore"]


with gr.Blocks(title="RadEval Evaluation", theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        # 🏎️ RadEval Evaluation

        **RadEval** is a lightweight, extensible framework for **evaluating radiology reports** using both standard NLP metrics (e.g. BLEU, ROUGE, BERTScore) and **radiology-specific measures** (e.g. RadGraph, CheXbert, GREEN). Whether you're benchmarking generation systems or validating clinical correctness, RadEval offers **comprehensive and interpretable** metrics out of the box.

        **⚠️ Performance Warning ⚠️**

        The demo is currently running on **CPU**. When using some slower metrics (like RadGraph, CheXbert, GREEN), it may take a while to complete evaluation. Please be patient.
        """
    )

    with gr.Row():
        choice = gr.Radio(
            label="πŸ“‹ Choose Example or Custom Input",
            choices=["Custom"] + list(examples.keys()),
            value="Custom",
            interactive=True
        )

    with gr.Row():
        with gr.Column(scale=1):
            ref_input = gr.Textbox(
                label="πŸ“„ Reference Report (Ground Truth)",
                lines=5,
                placeholder="Enter the reference radiology report here...",
                info="The ground truth or expert-written report"
            )
        with gr.Column(scale=1):
            hyp_input = gr.Textbox(
                label="πŸ€– Hypothesis Report (Generated)",
                lines=5,
                placeholder="Enter the generated/predicted radiology report here...",
                info="The AI-generated or system-produced report"
            )

    choice.change(
        update_fields,
        inputs=choice,
        outputs=[ref_input, hyp_input],
    )

    with gr.Row():
        metrics_selection = gr.CheckboxGroup(
            label="🎯 Select Evaluation Metrics",
            choices=available_metrics,
            value=default_metrics,
            interactive=True,
            info="Select metrics to compute. Some metrics may take longer (RadGraph, CheXbert, GREEN)."
        )

    with gr.Row():
        run_button = gr.Button("πŸš€ Run RadEval", variant="primary", size="lg")
        
    with gr.Row():
        with gr.Column(scale=2):
            analysis_output = gr.Markdown(
                value="πŸ“Š **Results will appear here after evaluation...**\n\nSelect your texts and metrics, then click 'Run RadEval'."
            )
        with gr.Column(scale=1):
            table_output = gr.DataFrame(
                label="πŸ“ˆ Detailed Scores",
                headers=["Metric", "Score"],
                wrap=True
            )

    # Information section
    with gr.Accordion("πŸ’‘ Metric Information", open=False):
        gr.Markdown(
            """
            ### πŸ“Š Available Metrics:
            
            **Traditional NLG Metrics:**
            - **BLEU**: N-gram overlap between reference and hypothesis
            - **ROUGE**: Recall-oriented overlap (ROUGE-1, ROUGE-2, ROUGE-L)
            - **BERTScore**: Semantic similarity using BERT embeddings
            
            **Radiology-Specific Metrics:**
            - **RadGraph F1**: Entity and relation extraction for radiology
            - **CheXbert F1**: Chest X-ray finding classification performance
            - **RaTEScore**: Radiology-aware text evaluation score
            - **RadCliQ**: Composite metric for radiology reports
            - **Temporal F1**: Temporal entity and relationship evaluation
            - **RadEval BERTScore**: Specialized BERT for radiology text
            - **GREEN**: Generative evaluation with natural language explanations
            - **SRR-BERT**: Structured radiology reasoning evaluation
            
            ### ⚑ Performance Notes:
            - **Fast**: BLEU, ROUGE, BERTScore, Temporal F1
            - **Medium**: RadEval BERTScore, RaTEScore, RadCliQ, SRR-BERT
            - **Slow**: CheXbert F1, RadGraph F1, GREEN (requires model downloads)
            """
        )

    run_button.click(
        run_radeval_simple,
        inputs=[ref_input, hyp_input, metrics_selection],
        outputs=[analysis_output, table_output]
    )

# =============================================================================
# πŸ§ͺ Hypothesis Testing Section
# =============================================================================

def run_hypothesis_testing(systems_data, selected_test_metrics, n_samples, significance_level):
    """
    Run statistical significance testing between multiple systems
    """
    try:        
        # Parse systems data (expecting JSON format)
        import json
        systems_dict = json.loads(systems_data)
        
        # Extract references and systems
        if 'references' not in systems_dict or 'systems' not in systems_dict:
            return "Error: Please provide both 'references' and 'systems' in the JSON data.", ""
        
        references = systems_dict['references']
        systems = systems_dict['systems']
        
        # Validate data integrity
        if not references or not systems:
            return "Error: References and systems cannot be empty.", ""
        
        if not isinstance(references, list) or not isinstance(systems, dict):
            return "Error: References must be a list and systems must be a dictionary.", ""
        
        # Check that all systems have the same number of outputs as references
        ref_count = len(references)
        for system_name, system_outputs in systems.items():
            if not isinstance(system_outputs, list):
                return f"Error: System '{system_name}' outputs must be a list.", ""
            if len(system_outputs) != ref_count:
                return f"Error: System '{system_name}' has {len(system_outputs)} outputs but {ref_count} references provided.", ""
        
        # Validate that all texts are non-empty strings
        for i, ref in enumerate(references):
            if not isinstance(ref, str) or not ref.strip():
                return f"Error: Reference {i+1} is empty or not a string.", ""
        
        for system_name, system_outputs in systems.items():
            for i, output in enumerate(system_outputs):
                if not isinstance(output, str) or not output.strip():
                    return f"Error: System '{system_name}' output {i+1} is empty or not a string.", ""
        
        # Initialize evaluators based on selected metrics (fast metrics only)
        evaluators = {}
        if 'BLEU' in selected_test_metrics:
            evaluators['bleu'] = RadEval(do_bleu=True)
        if 'ROUGE' in selected_test_metrics:
            evaluators['rouge'] = RadEval(do_rouge=True)
        if 'BERTScore' in selected_test_metrics:
            evaluators['bertscore'] = RadEval(do_bertscore=True)
        
        # Custom metric: average word count
        def word_count_metric(hyps, refs):
            return sum(len(report.split()) for report in hyps) / len(hyps)
        
        # Build metrics dictionary (following the example structure)
        metrics = {}
        if 'BLEU' in selected_test_metrics:
            # Test the evaluator first
            try:
                test_result = evaluators['bleu'](references[:1], [systems[list(systems.keys())[0]][0]])
                if 'bleu' not in test_result:
                    return "Error: BLEU evaluator doesn't return 'bleu' key. Available keys: " + str(list(test_result.keys())), ""
                metrics['bleu'] = lambda hyps, refs: evaluators['bleu'](refs, hyps)['bleu']
            except Exception as bleu_error:
                return f"Error testing BLEU evaluator: {str(bleu_error)}", ""
                
        if 'ROUGE' in selected_test_metrics:
            try:
                test_result = evaluators['rouge'](references[:1], [systems[list(systems.keys())[0]][0]])
                for rouge_key in ['rouge1', 'rouge2', 'rougeL']:
                    if rouge_key not in test_result:
                        return f"Error: ROUGE evaluator doesn't return '{rouge_key}' key. Available keys: " + str(list(test_result.keys())), ""
                metrics['rouge1'] = lambda hyps, refs: evaluators['rouge'](refs, hyps)['rouge1']
                metrics['rouge2'] = lambda hyps, refs: evaluators['rouge'](refs, hyps)['rouge2']
                metrics['rougeL'] = lambda hyps, refs: evaluators['rouge'](refs, hyps)['rougeL']
            except Exception as rouge_error:
                return f"Error testing ROUGE evaluator: {str(rouge_error)}", ""
                
        if 'BERTScore' in selected_test_metrics:
            try:
                test_result = evaluators['bertscore'](references[:1], [systems[list(systems.keys())[0]][0]])
                if 'bertscore' not in test_result:
                    return "Error: BERTScore evaluator doesn't return 'bertscore' key. Available keys: " + str(list(test_result.keys())), ""
                metrics['bertscore'] = lambda hyps, refs: evaluators['bertscore'](refs, hyps)['bertscore']
            except Exception as bert_error:
                return f"Error testing BERTScore evaluator: {str(bert_error)}", ""
                
        if 'custom: Word Count' in selected_test_metrics:
            metrics['word_count'] = word_count_metric  # ← example of a simple custom-defined metric
        
        if not metrics:
            return "Error: Please select at least one metric for testing.", ""
        
        # Run significance tests
        try:
            signatures, scores = compare_systems(
                systems=systems,
                metrics=metrics,
                references=references,
                n_samples=int(n_samples),
                significance_level=float(significance_level),
                print_results=False  # We don't need print output for online demo
            )
            
        except Exception as compare_error:
            return f"Error during significance testing: {str(compare_error)}\n\nThis might be due to:\n1. Empty or invalid text content\n2. Incompatible metric configurations\n3. RadEval library issues", str(compare_error)
        
        # Format results
        results_text = "## πŸ§ͺ Hypothesis Testing Results\n\n"
        results_text += f"**Parameters:**\n"
        results_text += f"- Randomization samples: {n_samples}\n"
        results_text += f"- Significance level: {significance_level}\n"
        results_text += f"- Number of systems: {len(systems)}\n"
        results_text += f"- Number of references: {len(references)}\n\n"
        
        # Significant differences summary
        results_text += "### πŸ“Š Significant Differences Summary\n\n"
        baseline_name = list(systems.keys())[0]  # Assume first one is the baseline
        results_text += f"**Baseline system:** {baseline_name}\n\n"
        
        has_significant_differences = False
        for system_name in systems.keys():
            if system_name == baseline_name:
                continue
                
            significant_metrics = []
            for metric_name in metrics.keys():
                pvalue_key = f"{metric_name}_pvalue"
                if pvalue_key in scores[system_name]:
                    p_val = scores[system_name][pvalue_key]
                    if p_val < float(significance_level):
                        significant_metrics.append(metric_name)
            
            if significant_metrics:
                results_text += f"**{system_name} vs {baseline_name}:** {', '.join(significant_metrics)} (p < {significance_level})\n\n"
                has_significant_differences = True
            else:
                results_text += f"**{system_name} vs {baseline_name}:** No significant differences\n\n"
        
        if not has_significant_differences:
            results_text += "*No statistically significant differences found between systems.*\n\n"
        
        # Add mean scores in table format
        results_text += "### πŸ“ˆ Mean Scores by System\n\n"
        try:
            baseline_name = list(systems.keys())[0]
            
            # Display each system's results in a clean format
            for system_name in systems.keys():
                results_text += f"**{system_name.upper()}:**\n\n"
                
                # Create table header
                results_text += "| Metric | Score | P-value |\n"
                results_text += "|--------|-------|----------|\n"
                
                # Get system data from scores
                system_scores = scores.get(system_name, {})
                
                # Add rows for each metric
                for metric_name in metrics.keys():
                    if metric_name in system_scores:
                        score = system_scores[metric_name]
                        pvalue_key = f"{metric_name}_pvalue"
                        
                        # Format score
                        score_str = f"{score:.4f}" if isinstance(score, (int, float)) else str(score)
                        
                        # Format p-value (only for non-baseline systems)
                        if system_name != baseline_name and pvalue_key in system_scores:
                            pvalue = system_scores[pvalue_key]
                            pvalue_str = f"{pvalue:.4f}" if isinstance(pvalue, (int, float)) else str(pvalue)
                            # Mark significant p-values
                            if isinstance(pvalue, (int, float)) and pvalue < float(significance_level):
                                pvalue_str += " *"
                        else:
                            pvalue_str = "-" if system_name == baseline_name else "N/A"
                        
                        results_text += f"| {metric_name} | {score_str} | {pvalue_str} |\n"
                
                results_text += "\n"
            
            results_text += "*Note: Baseline system shows scores only. Other systems show scores and p-values comparing to baseline.*\n"
            results_text += f"*P-values marked with * are significant (p < {significance_level}).*\n\n"
            
        except Exception as score_error:
            results_text += f"Error formatting scores: {str(score_error)}\n\n"
        
        return results_text
        
    except ImportError as e:
        return f"Import Error: {str(e)}. Please ensure RadEval with compare_systems is installed."
    except json.JSONDecodeError:
        return "Error: Invalid JSON format in systems data."
    except Exception as e:
        return f"Testing Error: {str(e)}"

# Create Hypothesis Testing UI
with gr.Blocks(title="Null Hypothesis Testing", theme=gr.themes.Soft()) as hypothesis_demo:
    gr.Markdown(
        """
        # πŸ–₯️ Null Hypothesis Testing
        
        **Statistical significance testing** for comparing multiple radiology report generation systems.
        This tool uses **randomization-based significance testing** to determine if differences between systems are statistically meaningful.
        
        **⚠️ Performance Warning ⚠️**
        
        Hypothesis testing with multiple metrics may take some time, especially with larger sample sizes. Please be patient during computation.
        """
    )
    
    with gr.Row():
        with gr.Column(scale=1.5):
            systems_input = gr.Textbox(
                label="πŸ“Š Systems Data (JSON Format)",
                lines=18,
                placeholder="""Enter systems data in JSON format, e.g.:
{
  "references": [
    "No acute cardiopulmonary process.",
    "Mild cardiomegaly with clear lung fields."
  ],
  "systems": {
    "baseline": [
      "No acute findings.",
      "Mild cardiomegaly, clear lungs."
    ],
    "improved": [
      "No acute cardiopulmonary process.",
      "Mild cardiomegaly with clear lung fields bilaterally."
    ]
  }
}""",
                info="Provide reference reports and multiple systems to compare"
            )
        
        with gr.Column(scale=1):
            test_metrics_selection = gr.CheckboxGroup(
                label="🎯 Select Metrics for Testing",
                choices=["BLEU", "ROUGE", "BERTScore", "custom: Word Count"],
                value=["BLEU", "ROUGE", "BERTScore"],
                interactive=True,
                info="Only fast metrics are shown to ensure quick evaluation (slow ones are excluded)"
            )
            
            n_samples_input = gr.Number(
                label="πŸ”„ Randomization Samples",
                value=50,
                minimum=10,
                maximum=1000,
                step=10,
                info="Number of randomisation samples (higher = more confidence, but slower)"
            )
            
            significance_level_input = gr.Number(
                label="πŸ“ˆ Significance Level (Ξ±)",
                value=0.05,
                minimum=0.01,
                maximum=0.10,
                step=0.01,
                info="Alpha level for significance testing"
            )

            example_button = gr.Button("πŸ“ Load Example Data", variant="secondary")
            clear_button = gr.Button("πŸ—‘οΈ Clear Data", variant="secondary")
            
            
    with gr.Row():
        test_button = gr.Button("πŸ§ͺ Run Hypothesis Testing", variant="primary", size="lg")
    
    with gr.Row():
        test_results = gr.Markdown(
            value="πŸ“Š **Test results will appear here...**\n\nClick 'Load Example Data' to see sample input, then click 'Run Hypothesis Testing' to see results."
        )
    
    # Example data button
    def load_example_data():
        example_data = {
            "references": [
                "No acute cardiopulmonary process.",
                "No radiographic findings to suggest pneumonia.",
                "Mild cardiomegaly with clear lung fields.",
                "Small pleural effusion on the right side.",
                "Status post cardiac surgery with stable appearance."
            ],
            "systems": {
                "baseline": [
                    "No acute findings.",
                    "No pneumonia.",
                    "Mild cardiomegaly, clear lungs.",
                    "Small right pleural effusion.",
                    "Post-cardiac surgery, stable."
                ],
                "improved": [
                    "No acute cardiopulmonary process.",
                    "No radiographic findings suggesting pneumonia.",
                    "Mild cardiomegaly with clear lung fields bilaterally.",
                    "Small pleural effusion present on the right side.",
                    "Status post cardiac surgery with stable appearance."
                ],
                "poor": [
                    "Normal.",
                    "OK.",
                    "Heart big.",
                    "Some fluid.",
                    "Surgery done."
                ]
            }
        }
        import json
        return json.dumps(example_data, indent=2)
    
    example_button.click(
        load_example_data,
        outputs=systems_input
    )
    
    clear_button.click(
        lambda: "",
        outputs=systems_input
    )
    
    test_button.click(
        run_hypothesis_testing,
        inputs=[systems_input, test_metrics_selection, n_samples_input, significance_level_input],
        outputs=[test_results]
    )
    
    with gr.Accordion("πŸ’‘ Hypothesis Testing Information", open=False):
        gr.Markdown(
            """
            ### πŸ”¬ How it Works:
            
            This tool performs **randomization-based significance testing** to compare multiple systems:
            
            1. **Null Hypothesis**: No difference between systems
            2. **Randomization**: Randomly permute system outputs multiple times
            3. **P-value Calculation**: Proportion of permutations where random difference β‰₯ observed difference
            4. **Significance**: If p-value < Ξ±, reject null hypothesis (systems are significantly different)
            
            ### πŸ“Š Input Format:
            - **References**: Ground truth reports
            - **Systems**: Multiple systems to compare (each with same number of outputs as references)
            - **Metrics**: Evaluation metrics to use for comparison
            
            ### πŸ“ˆ Output:
            - **Significance Matrix**: P-values for all pairwise system comparisons
            - **Mean Scores**: Average performance of each system on each metric
            - **Bold p-values**: Indicate statistically significant differences
            
            ### ⚑ Performance:
            - **Fast Metrics Only**: This tool only includes BLEU, ROUGE, BERTScore, and Word Count for optimal performance
            - **Excluded Slow Metrics**: RadGraph F1, CheXbert F1 are excluded to ensure reasonable computation time
            - More randomization samples = more accurate p-values but slower computation
            - Recommended: 50-100 samples for quick testing, 1000+ for publication
            """
        )

# Combine both demos using gr.Blocks to add a header
with gr.Blocks(
    title="RadEval: A framework for radiology text evaluation", 
    theme=gr.themes.Soft(),
    css="""
    .tab-nav button {
        font-weight: bold !important;
        border: 2px solid #e0e7ff !important;
        border-radius: 10px !important;
        margin: 0 5px !important;
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
        color: white !important;
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2) !important;
        transition: all 0.3s ease !important;
    }
    .tab-nav button:hover {
        transform: translateY(-2px) !important;
        box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3) !important;
        background: linear-gradient(135deg, #764ba2 0%, #667eea 100%) !important;
    }
    .tab-nav button.selected {
        background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%) !important;
        border-color: #ff6b6b !important;
        transform: translateY(-1px) !important;
        box-shadow: 0 8px 25px rgba(255, 107, 107, 0.4) !important;
    }
    """
) as combined_demo:
    gr.Markdown(
        """
        # 🩺 RadEval: A framework for radiology text evaluation
        ### [Github](https://github.com/jbdel/RadEval) | [PyPI](https://pypi.org/project/RadEval) | [Video](https://justin13601.github.io/files/radeval.mp4) | [arXiv]() | [RadEval_ModernBERT Model](https://huggingface.co/IAMJB/RadEvalModernBERT) | [Expert Dataset]()

        """
    )
    
    tabs = gr.TabbedInterface(
        [demo, hypothesis_demo],
        ["🏎️ RadEval Evaluation", "πŸ–₯️ Null Hypothesis Testing"]
    )

if __name__ == "__main__":
    combined_demo.launch()