File size: 27,687 Bytes
d87fc8a
 
 
 
84cf73f
d87fc8a
 
 
 
 
84cf73f
 
 
 
 
 
d87fc8a
 
 
 
 
 
 
 
 
 
 
 
84cf73f
 
 
 
 
 
d87fc8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84cf73f
 
 
 
 
 
 
 
 
 
d87fc8a
 
84cf73f
d87fc8a
 
 
 
 
 
 
 
 
 
 
be26939
d87fc8a
 
 
 
 
 
 
84cf73f
 
 
 
 
 
 
 
d87fc8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be26939
 
 
 
 
 
 
 
 
 
d87fc8a
be26939
 
 
 
 
 
 
 
d87fc8a
be26939
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a12ef5d
 
 
 
 
84cf73f
 
 
 
 
 
 
 
 
a12ef5d
 
 
 
 
84cf73f
 
 
 
 
 
 
 
 
a12ef5d
 
 
 
d87fc8a
84cf73f
 
 
 
 
 
 
 
 
d87fc8a
 
 
be26939
 
a12ef5d
 
 
84cf73f
 
 
 
 
 
 
 
 
a12ef5d
 
 
 
be26939
84cf73f
 
 
 
 
 
 
 
 
be26939
 
d87fc8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84cf73f
 
 
 
d87fc8a
 
 
 
 
 
 
a12ef5d
84cf73f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d87fc8a
 
84cf73f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d87fc8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84cf73f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a12ef5d
84cf73f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a12ef5d
84cf73f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be26939
 
 
 
 
 
84cf73f
 
 
 
 
a12ef5d
84cf73f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be26939
 
 
 
84cf73f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be26939
 
d87fc8a
 
 
 
 
 
 
 
 
 
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
import matplotlib.pyplot as plt
import matplotlib
import pandas as pd
import gradio as gr
from gradio_toggle import Toggle

from data import CIResults
from utils import logger
from summary_page import create_summary_page
from model_page import plot_model_stats
from time_series_gradio import (
    create_time_series_summary_gradio,
    create_model_time_series_gradio,
    get_time_series_summary_dfs,
    get_model_time_series_dfs,
)


# Configure matplotlib to prevent memory warnings and set dark background
matplotlib.rcParams['figure.facecolor'] = '#000000'
matplotlib.rcParams['axes.facecolor'] = '#000000'
matplotlib.rcParams['savefig.facecolor'] = '#000000'
plt.ioff()  # Turn off interactive mode to prevent figure accumulation


# Load data once at startup
Ci_results = CIResults()
Ci_results.load_data()
# Preload historical data at startup
if Ci_results.available_dates:
    start_date_val = Ci_results.available_dates[-1]  # Last date (oldest)
    end_date_val = Ci_results.available_dates[0]     # First date (newest)
    Ci_results.load_historical_data(start_date_val, end_date_val)
    logger.info(f"Preloaded historical data: {len(Ci_results.historical_df)} records")
# Start the auto-reload scheduler
Ci_results.schedule_data_reload()


# Function to check if a model has failures
def model_has_failures(model_name):
    """Check if a model has any failures (AMD or NVIDIA)."""
    if Ci_results.df is None or Ci_results.df.empty:
        return False
    
    # Normalize model name to match DataFrame index
    model_name_lower = model_name.lower()
    
    # Check if model exists in DataFrame
    if model_name_lower not in Ci_results.df.index:
        return False
    row = Ci_results.df.loc[model_name_lower]
    
    # Check for failures in both AMD and NVIDIA
    amd_multi_failures = row.get('failed_multi_no_amd', 0)
    amd_single_failures = row.get('failed_single_no_amd', 0)
    nvidia_multi_failures = row.get('failed_multi_no_nvidia', 0)
    nvidia_single_failures = row.get('failed_single_no_nvidia', 0)
    return any([
        amd_multi_failures > 0,
        amd_single_failures > 0,
        nvidia_multi_failures > 0,
        nvidia_single_failures > 0,
    ])


# Function to get current description text
def get_description_text():
    """Get description text with integrated last update time."""
    msg = [
        "Transformer CI Dashboard",
        "-",
        "AMD runs on MI325",
        "NVIDIA runs on A10",
    ]
    msg = ["**" + x + "**" for x in msg] + [""]
    if Ci_results.latest_update_msg:
        msg.append(f"*({Ci_results.latest_update_msg})*")
    else:
        msg.append("*(loading...)*")
    return "<br>".join(msg)

# Load CSS from external file
def load_css():
    try:
        with open("styles.css", "r") as f:
            css_content = f.read()
        
        return css_content
    except FileNotFoundError:
        logger.warning("styles.css not found, using minimal default styles")
        return "body { background: #000; color: #fff; }"

js_func = """
function refresh() {
    const url = new URL(window.location);

    if (url.searchParams.get('__theme') !== 'dark') {
        url.searchParams.set('__theme', 'dark');
        window.location.href = url.href;
    }
}
"""

# Create the Gradio interface with sidebar and dark theme
with gr.Blocks(title="Model Test Results Dashboard", css=load_css(), js=js_func) as demo:


    with gr.Row():
        # Sidebar for model selection
        with gr.Column(scale=1, elem_classes=["sidebar"]):
            gr.Markdown("# πŸ€– TCID", elem_classes=["sidebar-title"])

            # Description with integrated last update time
            description_text = get_description_text()
            description_display = gr.Markdown(description_text, elem_classes=["sidebar-description"])

            # Summary button (for current view)
            summary_button = gr.Button(
                "summary\nπŸ“Š",
                variant="primary",
                size="lg",
                elem_classes=["summary-button"]
            )

            history_view_button = Toggle(
                label="History view",
                value=False,
                interactive=True,
                elem_classes=["history-view-button"]
            )


            # Model selection header (clickable toggle)
            model_toggle_button = gr.Button(
                f"β–Ί Select model ({len(Ci_results.available_models)})",
                variant="secondary",
                elem_classes=["model-header"]
            )

            # Model buttons container (collapsible) - start folded
            with gr.Column(elem_classes=["model-list", "model-list-hidden"]) as model_list_container:
                # Create individual buttons for each model
                model_buttons = []
                model_choices = [model.lower() for model in Ci_results.available_models] if Ci_results.available_models else ["auto", "bert", "clip", "llama"]
                
                print(f"Creating {len(model_choices)} model buttons: {model_choices}")

                for model_name in model_choices:
                    # Check if model has failures to determine styling
                    has_failures = model_has_failures(model_name)
                    button_classes = ["model-button"]
                    if has_failures:
                        button_classes.append("model-button-failed")
                    
                    btn = gr.Button(
                        model_name,
                        variant="secondary",
                        size="sm",
                        elem_classes=button_classes
                    )
                    model_buttons.append(btn)

            # CI job links at bottom of sidebar
            ci_links_display = gr.Markdown("πŸ”— **CI Jobs:** *Loading...*", elem_classes=["sidebar-links"])

        # Main content area
        with gr.Column(scale=4, elem_classes=["main-content"]):
            # Current view components
            with gr.Column(visible=True, elem_classes=["current-view"]) as current_view:
                # Summary display (default view)
                summary_display = gr.Plot(
                    value=create_summary_page(Ci_results.df, Ci_results.available_models),
                    label="",
                    format="png",
                    elem_classes=["plot-container"],
                    visible=True
                )

                # Detailed view components (hidden by default)
                with gr.Column(visible=False, elem_classes=["detail-view"]) as detail_view:
                    # Create the plot output
                    plot_output = gr.Plot(
                        label="",
                        format="png",
                        elem_classes=["plot-container"]
                    )

                    # Create two separate failed tests displays in a row layout
                    with gr.Row():
                        with gr.Column(scale=1):
                            amd_failed_tests_output = gr.Textbox(
                                value="",
                                lines=8,
                                max_lines=8,
                                interactive=False,
                                container=False,
                                elem_classes=["failed-tests"]
                            )
                        with gr.Column(scale=1):
                            nvidia_failed_tests_output = gr.Textbox(
                                value="",
                                lines=8,
                                max_lines=8,
                                interactive=False,
                                container=False,
                                elem_classes=["failed-tests"]
                            )

            # Historical view components (hidden by default)
            with gr.Column(visible=False, elem_classes=["historical-view"]) as historical_view:
                
                
                # Time-series summary displays (multiple Gradio plots)
                time_series_failure_rates = gr.LinePlot(
                    label="",
                    x="date",
                    y="failure_rate",
                    color="platform",
                    color_map={"AMD": "#FF6B6B", "NVIDIA": "#4ECDC4"},
                    title="Overall Failure Rates Over Time",
                    tooltip=["failure_rate", "date", "change"],
                    height=300,
                    x_label_angle=45,
                    y_title="Failure Rate (%)",
                    elem_classes=["plot-container"]
                )
                
                time_series_amd_tests = gr.LinePlot(
                    label="",
                    x="date",
                    y="count",
                    color="test_type",
                    color_map={"Passed": "#4CAF50", "Failed": "#E53E3E", "Skipped": "#FFA500"},
                    title="AMD Test Results Over Time",
                    tooltip=["count", "date", "change"],
                    height=300,
                    x_label_angle=45,
                    y_title="Number of Tests",
                    elem_classes=["plot-container"]
                )
                
                time_series_nvidia_tests = gr.LinePlot(
                    label="",
                    x="date",
                    y="count",
                    color="test_type",
                    color_map={"Passed": "#4CAF50", "Failed": "#E53E3E", "Skipped": "#FFA500"},
                    title="NVIDIA Test Results Over Time",
                    tooltip=["count", "date", "change"],
                    height=300,
                    x_label_angle=45,
                    y_title="Number of Tests",
                    elem_classes=["plot-container"]
                )

                # Time-series model view (hidden by default)
                with gr.Column(visible=False, elem_classes=["time-series-detail-view"]) as time_series_detail_view:
                    # Time-series plots for specific model (with spacing)
                    time_series_amd_model_plot = gr.LinePlot(
                        label="",
                        x="date",
                        y="count",
                        color="test_type",
                        color_map={"Passed": "#4CAF50", "Failed": "#E53E3E", "Skipped": "#FFA500"},
                        title="AMD Results Over Time",
                        tooltip=["count", "date", "change"],
                        height=300,
                        x_label_angle=45,
                        y_title="Number of Tests",
                        elem_classes=["plot-container"]
                    )
                    
                    time_series_nvidia_model_plot = gr.LinePlot(
                        label="",
                        x="date",
                        y="count",
                        color="test_type",
                        color_map={"Passed": "#4CAF50", "Failed": "#E53E3E", "Skipped": "#FFA500"},
                        title="NVIDIA Results Over Time",
                        tooltip=["count", "date", "change"],
                        height=300,
                        x_label_angle=45,
                        y_title="Number of Tests",
                        elem_classes=["plot-container"]
                    )

    # Model toggle functionality
    def toggle_model_list(current_visible):
        """Toggle the visibility of the model list."""
        new_visible = not current_visible
        arrow = "β–Ό" if new_visible else "β–Ί"
        button_text = f"{arrow} Select model ({len(Ci_results.available_models)})"
        
        # Use CSS classes instead of Gradio visibility
        css_classes = ["model-list"]
        if new_visible:
            css_classes.append("model-list-visible")
        else:
            css_classes.append("model-list-hidden")
            
        return gr.update(value=button_text), gr.update(elem_classes=css_classes), new_visible

    # Track model list visibility state
    model_list_visible = gr.State(False)
    # Track last selected model for mode switches
    selected_model_state = gr.State(None)
    # Track whether current view is model detail (True) or summary (False)
    in_model_view_state = gr.State(False)

    model_toggle_button.click(
        fn=toggle_model_list,
        inputs=[model_list_visible],
        outputs=[model_toggle_button, model_list_container, model_list_visible]
    )


    # Unified summary handler: respects History toggle
    def handle_summary_click(history_mode: bool):
        description = get_description_text()
        links = get_ci_links()
        fr_plot, amd_plot, nvidia_plot = get_historical_summary_plots()
        if history_mode:
            return (
                description,
                links,
                gr.update(visible=False),  # current_view
                gr.update(visible=True),   # historical_view
                gr.update(visible=False),  # summary_display
                gr.update(visible=False),  # detail_view
                fr_plot,
                amd_plot,
                nvidia_plot,
                gr.update(visible=False),  # time_series_detail_view
                False,                     # in_model_view_state
            )
        else:
            fig = create_summary_page(Ci_results.df, Ci_results.available_models)
            return (
                description,
                links,
                gr.update(visible=True),   # current_view
                gr.update(visible=False),  # historical_view
                gr.update(value=fig, visible=True), # summary_display
                gr.update(visible=False),  # detail_view
                gr.update(visible=False),  # time_series_failure_rates
                gr.update(visible=False),  # time_series_amd_tests
                gr.update(visible=False),  # time_series_nvidia_tests
                gr.update(visible=False),  # time_series_detail_view
                False,                     # in_model_view_state
            )

    summary_button.click(
        fn=handle_summary_click,
        inputs=[history_view_button],
        outputs=[
            description_display,
            ci_links_display,
            current_view,
            historical_view,
            summary_display,
            detail_view,
            time_series_failure_rates,
            time_series_amd_tests,
            time_series_nvidia_tests,
            time_series_detail_view,
            in_model_view_state,
        ],
    )

    # Function to get CI job links
    def get_ci_links():
        """Get CI job links from the most recent data."""
        try:
            # Check if df exists and is not empty
            if Ci_results.df is None or Ci_results.df.empty:
                return "πŸ”— **CI Jobs:** *Loading...*"

            # Get links from any available model (they should be the same for all models in a run)
            amd_multi_link = None
            amd_single_link = None
            nvidia_multi_link = None
            nvidia_single_link = None

            for model_name in Ci_results.df.index:
                row = Ci_results.df.loc[model_name]

                # Extract AMD links
                if pd.notna(row.get('job_link_amd')) and (not amd_multi_link or not amd_single_link):
                    amd_link_raw = row.get('job_link_amd')
                    if isinstance(amd_link_raw, dict):
                        if 'multi' in amd_link_raw and not amd_multi_link:
                            amd_multi_link = amd_link_raw['multi']
                        if 'single' in amd_link_raw and not amd_single_link:
                            amd_single_link = amd_link_raw['single']

                # Extract NVIDIA links
                if pd.notna(row.get('job_link_nvidia')) and (not nvidia_multi_link or not nvidia_single_link):
                    nvidia_link_raw = row.get('job_link_nvidia')
                    if isinstance(nvidia_link_raw, dict):
                        if 'multi' in nvidia_link_raw and not nvidia_multi_link:
                            nvidia_multi_link = nvidia_link_raw['multi']
                        if 'single' in nvidia_link_raw and not nvidia_single_link:
                            nvidia_single_link = nvidia_link_raw['single']

                # Break if we have all links
                if amd_multi_link and amd_single_link and nvidia_multi_link and nvidia_single_link:
                    break


            # Add FAQ link at the bottom
            links_md = "❓ [**FAQ**](https://huggingface.co/spaces/transformers-community/transformers-ci-dashboard/blob/main/README.md)\n\n"
            links_md += "πŸ”— **CI Jobs:**\n\n"

            # AMD links
            if amd_multi_link or amd_single_link:
                links_md += "**AMD:**\n"
                if amd_multi_link:
                    links_md += f"β€’ [Multi GPU]({amd_multi_link})\n"
                if amd_single_link:
                    links_md += f"β€’ [Single GPU]({amd_single_link})\n"
                links_md += "\n"

            # NVIDIA links
            if nvidia_multi_link or nvidia_single_link:
                links_md += "**NVIDIA:**\n"
                if nvidia_multi_link:
                    links_md += f"β€’ [Multi GPU]({nvidia_multi_link})\n"
                if nvidia_single_link:
                    links_md += f"β€’ [Single GPU]({nvidia_single_link})\n"

            if not (amd_multi_link or amd_single_link or nvidia_multi_link or nvidia_single_link):
                links_md += "*No links available*"

            return links_md
        except Exception as e:
            logger.error(f"getting CI links: {e}")
            return "πŸ”— **CI Jobs:** *Error loading links*\n\n❓ **[FAQ](README.md)**"


 

    def get_historical_summary_plots():
        """Get historical summary plots from preloaded data."""
        dfs = get_time_series_summary_dfs(Ci_results.historical_df)
        return (
            gr.update(value=dfs['failure_rates_df'], visible=True),
            gr.update(value=dfs['amd_tests_df'], visible=True),
            gr.update(value=dfs['nvidia_tests_df'], visible=True),
        )
    
    def handle_history_toggle(history_mode, last_selected_model, in_model_view):
        if history_mode:
            # If currently in model view and valid model, show historical model detail
            if in_model_view and last_selected_model:
                amd_ts, nvidia_ts = show_time_series_model(last_selected_model)
                return (
                    gr.update(visible=False),   # current_view
                    gr.update(visible=True),    # historical_view
                    gr.update(visible=False),   # summary_display
                    gr.update(visible=False),   # detail_view
                    gr.update(visible=False),   # time_series_failure_rates
                    gr.update(visible=False),   # time_series_amd_tests
                    gr.update(visible=False),   # time_series_nvidia_tests
                    amd_ts,                     # time_series_amd_model_plot
                    nvidia_ts,                  # time_series_nvidia_model_plot
                    gr.update(visible=True),    # time_series_detail_view
                    gr.update(),                # plot_output
                    gr.update(),                # amd_failed_tests_output
                    gr.update(),                # nvidia_failed_tests_output
                    True,                       # in_model_view_state (still in model view)
                )
            # Otherwise show historical summary
            fr_plot, amd_plot, nvidia_plot = get_historical_summary_plots()
            return (
                gr.update(visible=False),   # current_view
                gr.update(visible=True),    # historical_view
                gr.update(visible=False),   # summary_display
                gr.update(visible=False),   # detail_view
                fr_plot,                    # time_series_failure_rates (value + keep visibility)
                amd_plot,                   # time_series_amd_tests
                nvidia_plot,                # time_series_nvidia_tests
                gr.update(),                # time_series_amd_model_plot
                gr.update(),                # time_series_nvidia_model_plot
                gr.update(visible=False),   # time_series_detail_view
                gr.update(),                # plot_output
                gr.update(),                # amd_failed_tests_output
                gr.update(),                # nvidia_failed_tests_output
                False,                      # in_model_view_state
            )
        else:
            # Switch to current mode: show model if selected; otherwise summary
            if last_selected_model and Ci_results.df is not None and not Ci_results.df.empty and last_selected_model in Ci_results.df.index:
                fig, amd_txt, nvidia_txt = plot_model_stats(Ci_results.df, last_selected_model)
                return (
                    gr.update(visible=True),    # current_view
                    gr.update(visible=False),   # historical_view
                    gr.update(visible=False),   # summary_display
                    gr.update(visible=True),    # detail_view
                    gr.update(visible=False),   # time_series_failure_rates
                    gr.update(visible=False),   # time_series_amd_tests
                    gr.update(visible=False),   # time_series_nvidia_tests
                    gr.update(),                # time_series_amd_model_plot
                    gr.update(),                # time_series_nvidia_model_plot
                    gr.update(visible=False),   # time_series_detail_view
                    fig,                        # plot_output
                    amd_txt,                    # amd_failed_tests_output
                    nvidia_txt,                 # nvidia_failed_tests_output
                    True,                       # in_model_view_state
                )
            else:
                fig = create_summary_page(Ci_results.df, Ci_results.available_models)
                return (
                    gr.update(visible=True),    # current_view
                    gr.update(visible=False),   # historical_view
                    gr.update(value=fig, visible=True),  # summary_display
                    gr.update(visible=False),   # detail_view
                    gr.update(visible=False),   # time_series_failure_rates
                    gr.update(visible=False),   # time_series_amd_tests
                    gr.update(visible=False),   # time_series_nvidia_tests
                    gr.update(),                # time_series_amd_model_plot
                    gr.update(),                # time_series_nvidia_model_plot
                    gr.update(visible=False),   # time_series_detail_view
                    gr.update(),                # plot_output
                    gr.update(),                # amd_failed_tests_output
                    gr.update(),                # nvidia_failed_tests_output
                    False,                      # in_model_view_state
                )

    history_view_button.change(
        fn=handle_history_toggle,
        inputs=[history_view_button, selected_model_state, in_model_view_state],
        outputs=[
            current_view,
            historical_view,
            summary_display,
            detail_view,
            time_series_failure_rates,
            time_series_amd_tests,
            time_series_nvidia_tests,
            time_series_amd_model_plot,
            time_series_nvidia_model_plot,
            time_series_detail_view,
            plot_output,
            amd_failed_tests_output,
            nvidia_failed_tests_output,
            in_model_view_state,
        ],
    )


    # Time-series model selection functionality
    def show_time_series_model(selected_model):
        """Show time-series view for a specific model."""
        dfs = get_model_time_series_dfs(Ci_results.historical_df, selected_model)
        return (
            gr.update(value=dfs['amd_df'], visible=True, title=f"{selected_model.upper()} - AMD Results Over Time"),
            gr.update(value=dfs['nvidia_df'], visible=True, title=f"{selected_model.upper()} - NVIDIA Results Over Time"),
        )

    # Unified model click handler: respects History toggle
    def handle_model_click(selected_model: str, history_mode: bool):
        if history_mode:
            amd_ts, nvidia_ts = show_time_series_model(selected_model)
            return (
                gr.update(),                 # plot_output
                gr.update(),                 # amd_failed_tests_output
                gr.update(),                 # nvidia_failed_tests_output
                gr.update(visible=False),    # current_view
                gr.update(visible=True),     # historical_view
                gr.update(visible=False),    # summary_display
                gr.update(visible=False),    # detail_view
                gr.update(visible=False),    # time_series_failure_rates
                gr.update(visible=False),    # time_series_amd_tests
                gr.update(visible=False),    # time_series_nvidia_tests
                amd_ts,                      # time_series_amd_model_plot
                nvidia_ts,                   # time_series_nvidia_model_plot
                gr.update(visible=True),     # time_series_detail_view
            selected_model, True)            # selected_model_state, in_model_view_state
        else:
            fig, amd_txt, nvidia_txt = plot_model_stats(Ci_results.df, selected_model)
            return (
                fig,
                amd_txt,
                nvidia_txt,
                gr.update(visible=True),     # current_view
                gr.update(visible=False),    # historical_view
                gr.update(visible=False),    # summary_display
                gr.update(visible=True),     # detail_view
                gr.update(),                 # time_series_failure_rates
                gr.update(),                 # time_series_amd_tests
                gr.update(),                 # time_series_nvidia_tests
                gr.update(),                 # time_series_amd_model_plot
                gr.update(),                 # time_series_nvidia_model_plot
                gr.update(visible=False),    # time_series_detail_view
            selected_model, True)            # selected_model_state, in_model_view_state

    for i, btn in enumerate(model_buttons):
        model_name = model_choices[i]
        btn.click(
            fn=lambda history_mode, m=model_name: handle_model_click(m, history_mode),
            inputs=[history_view_button],
            outputs=[
                plot_output,
                amd_failed_tests_output,
                nvidia_failed_tests_output,
                current_view,
                historical_view,
                summary_display,
                detail_view,
                time_series_failure_rates,
                time_series_amd_tests,
                time_series_nvidia_tests,
                time_series_amd_model_plot,
                time_series_nvidia_model_plot,
                time_series_detail_view,
                selected_model_state,
                in_model_view_state,
            ],
        )

    # Auto-update CI links when the interface loads
    demo.load(
        fn=get_ci_links,
        outputs=[ci_links_display]
    )


# Gradio entrypoint
if __name__ == "__main__":
    demo.launch()