File size: 21,184 Bytes
8f08de8
9c986ec
91607dd
9c986ec
8f08de8
 
 
 
9c986ec
8f08de8
120d220
8f08de8
0718fad
91607dd
 
 
f79bf21
120d220
9c986ec
8f08de8
91607dd
0718fad
91607dd
 
 
 
4a4dcbd
8f08de8
 
9c986ec
8f08de8
0718fad
8f08de8
de1701c
 
 
91607dd
 
 
 
 
 
 
 
 
 
 
 
 
8f08de8
9c986ec
91607dd
 
 
 
 
7617f65
de1701c
7617f65
91607dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7617f65
de1701c
0718fad
 
9c986ec
de1701c
0718fad
91607dd
de1701c
7617f65
de1701c
7617f65
0718fad
 
91607dd
 
 
 
 
4a4dcbd
 
91607dd
 
4a4dcbd
 
91607dd
 
 
 
4a4dcbd
91607dd
 
4a4dcbd
 
91607dd
 
 
 
 
7617f65
91607dd
 
 
 
9c986ec
 
91607dd
 
 
 
 
 
 
 
 
 
 
4a4dcbd
91607dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de1701c
91607dd
 
 
 
 
 
 
de1701c
 
9c986ec
 
 
 
 
91607dd
9c986ec
0718fad
9c986ec
 
 
de1701c
 
 
 
 
 
 
 
 
 
9c986ec
de1701c
 
a46418e
 
 
 
 
 
9c986ec
 
91607dd
 
 
 
 
 
 
 
 
 
 
 
4a4dcbd
 
 
 
 
 
 
 
 
91607dd
4a4dcbd
91607dd
 
 
4a4dcbd
91607dd
 
 
 
 
 
 
 
 
de1701c
91607dd
 
 
 
 
4a4dcbd
91607dd
 
 
 
 
4a4dcbd
 
 
91607dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de1701c
0718fad
120d220
 
8f08de8
de1701c
8f08de8
91607dd
f79bf21
 
7617f65
91607dd
 
 
 
 
 
f79bf21
 
7617f65
91607dd
 
 
9c986ec
91607dd
7617f65
9c986ec
91607dd
 
 
 
 
 
 
 
9c986ec
91607dd
 
 
 
 
 
 
 
 
 
 
de1701c
0718fad
91607dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8f08de8
91607dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de1701c
 
91607dd
 
 
 
 
de1701c
 
 
91607dd
4a4dcbd
8f08de8
7617f65
91607dd
f79bf21
91607dd
 
 
8f08de8
 
91607dd
 
 
 
 
 
 
 
 
 
 
8f08de8
0718fad
91607dd
120d220
91607dd
a46418e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c986ec
91607dd
72ede43
8f08de8
 
9c986ec
91607dd
 
7617f65
9c986ec
 
91607dd
 
 
 
 
9c986ec
 
 
 
 
 
 
 
4a4dcbd
9c986ec
 
4a4dcbd
9c986ec
 
 
91607dd
9c986ec
 
 
 
91607dd
 
 
 
9c986ec
 
 
7617f65
9c986ec
 
de1701c
 
91607dd
 
 
4a4dcbd
 
9c986ec
91607dd
 
de1701c
 
8f08de8
 
91607dd
8f08de8
91607dd
 
9c986ec
91607dd
 
9c986ec
 
91607dd
 
 
 
 
 
 
 
 
 
 
8f08de8
 
120d220
 
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
import os
import time
from typing import Optional, Tuple, List, Dict

import numpy as np
import pandas as pd
import gradio as gr
import torch
import plotly.graph_objects as go

from chronos import Chronos2Pipeline


# =========================
# Config
# =========================
MODEL_ID_DEFAULT = os.getenv("CHRONOS_MODEL_ID", "amazon/chronos-2")
DATA_DIR = "data"
OUT_DIR = "/tmp"

DEFAULT_FREQ = "D"  # se il CSV non ha timestamp, generiamo daily


# =========================
# Utils: files + device
# =========================
def available_test_csv() -> List[str]:
    if not os.path.isdir(DATA_DIR):
        return []
    return sorted([f for f in os.listdir(DATA_DIR) if f.lower().endswith(".csv")])


def pick_device(ui_choice: str) -> str:
    return "cuda" if (ui_choice or "").startswith("cuda") and torch.cuda.is_available() else "cpu"


# =========================
# Sample series
# =========================
def make_sample_df(
    n: int,
    seed: int,
    trend: float,
    season_period: int,
    season_amp: float,
    noise: float,
    freq: str = DEFAULT_FREQ,
    start: str = "2020-01-01",
) -> pd.DataFrame:
    rng = np.random.default_rng(int(seed))
    t = np.arange(int(n), dtype=np.float32)
    y = (
        float(trend) * t
        + float(season_amp) * np.sin(2 * np.pi * t / max(1, int(season_period)))
        + rng.normal(0.0, float(noise), size=int(n))
    ).astype(np.float32)
    if float(np.min(y)) < 0:
        y -= float(np.min(y))

    ts = pd.date_range(start=start, periods=int(n), freq=freq)
    return pd.DataFrame({"id": 0, "timestamp": ts, "target": y})


# =========================
# CSV loader -> context_df format (id,timestamp,target)
# =========================
def _guess_timestamp_column(df: pd.DataFrame) -> Optional[str]:
    # prova colonne con nome tipico
    for c in df.columns:
        lc = str(c).lower()
        if lc in ["ds", "date", "datetime", "timestamp", "time"]:
            return c
    # prova parsing: se una colonna ha tanti valori parseabili a datetime
    for c in df.columns:
        if df[c].dtype == object:
            parsed = pd.to_datetime(df[c], errors="coerce", utc=False)
            if parsed.notna().sum() >= max(10, int(0.6 * len(df))):
                return c
    return None


def _guess_numeric_target_column(df: pd.DataFrame, user_col: Optional[str]) -> str:
    if user_col and user_col.strip():
        col = user_col.strip()
        if col not in df.columns:
            raise ValueError(f"Colonna '{col}' non trovata. Disponibili: {list(df.columns)}")
        return col

    # numeric dtype first
    numeric_cols = [c for c in df.columns if pd.api.types.is_numeric_dtype(df[c])]
    if numeric_cols:
        return numeric_cols[0]

    # try coercion
    best = None
    best_count = 0
    for c in df.columns:
        coerced = pd.to_numeric(df[c], errors="coerce")
        cnt = coerced.notna().sum()
        if cnt > best_count:
            best = c
            best_count = cnt
    if best is None or best_count < 10:
        raise ValueError("Non trovo una colonna numerica valida (>=10 valori) nel CSV.")
    return best


def load_context_df_from_csv(path: str, user_target_col: Optional[str], user_time_col: Optional[str], freq: str) -> Tuple[pd.DataFrame, str, Optional[str]]:
    df = pd.read_csv(path)
    if df.shape[0] < 10:
        raise ValueError("Serie troppo corta (minimo consigliato: 10 righe).")

    target_col = _guess_numeric_target_column(df, user_target_col)

    time_col = user_time_col.strip() if (user_time_col and user_time_col.strip()) else _guess_timestamp_column(df)

    # target
    y = pd.to_numeric(df[target_col], errors="coerce").dropna().astype(np.float32).to_numpy()
    if len(y) < 10:
        raise ValueError("Troppi NaN: la colonna target ha meno di 10 valori numerici.")

    # timestamp
    if time_col and time_col in df.columns:
        ts = pd.to_datetime(df[time_col], errors="coerce")
        # allinea su target non-NaN (stesso mask del target coercito)
        mask = pd.to_numeric(df[target_col], errors="coerce").notna()
        ts = ts[mask]
        ts = ts.dropna()
        # se timestamp troppo sporchi, fallback a range
        if len(ts) < 10:
            time_col = None

    if not time_col:
        ts = pd.date_range(start="2020-01-01", periods=len(y), freq=freq)

    context_df = pd.DataFrame({"id": 0, "timestamp": ts[: len(y)], "target": y[: len(ts)]})
    context_df = context_df.sort_values("timestamp").reset_index(drop=True)
    return context_df, target_col, (time_col if time_col else None)


# =========================
# Pipeline cache
# =========================
_PIPE = None
_META = {"model_id": None, "device": None}


def get_pipeline(model_id: str, device: str) -> Chronos2Pipeline:
    global _PIPE, _META
    model_id = (model_id or MODEL_ID_DEFAULT).strip()
    device = "cuda" if (device == "cuda" and torch.cuda.is_available()) else "cpu"
    if _PIPE is None or _META["model_id"] != model_id or _META["device"] != device:
        _PIPE = Chronos2Pipeline.from_pretrained(model_id, device_map=device)
        _META = {"model_id": model_id, "device": device}
    return _PIPE


# =========================
# Metrics
# =========================
def mae(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    return float(np.mean(np.abs(y_true - y_pred)))


def rmse(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    return float(np.sqrt(np.mean((y_true - y_pred) ** 2)))


def mape(y_true: np.ndarray, y_pred: np.ndarray) -> float:
    denom = np.maximum(1e-8, np.abs(y_true))
    return float(np.mean(np.abs((y_true - y_pred) / denom)) * 100.0)


def coverage(y_true: np.ndarray, low: np.ndarray, high: np.ndarray) -> float:
    return float(np.mean((y_true >= low) & (y_true <= high)) * 100.0)


def avg_width(low: np.ndarray, high: np.ndarray) -> float:
    return float(np.mean(high - low))


# =========================
# Plotly
# =========================
def plot_forecast(context_df: pd.DataFrame, pred_df: pd.DataFrame, q_low: float, q_high: float, title: str) -> go.Figure:
    ctx = context_df.copy()
    pred = pred_df.copy()

    fig = go.Figure()
    fig.add_trace(go.Scatter(x=ctx["timestamp"], y=ctx["target"], mode="lines", name="History"))

    # pred_df from predict_df typically has:
    # - timestamp
    # - predictions (median or q=0.5)
    # - columns for quantiles like "0.1", "0.9"
    if "predictions" in pred.columns:
        y_med = pred["predictions"].to_numpy()
    else:
        # fallback: if "0.5" exists
        y_med = pred.get("0.5", pred.iloc[:, -1]).to_numpy()

    fig.add_trace(go.Scatter(x=pred["timestamp"], y=y_med, mode="lines", name="Forecast (median)"))

    low_col = f"{q_low:.1f}".rstrip("0").rstrip(".")
    high_col = f"{q_high:.1f}".rstrip("0").rstrip(".")

    # columns in pred_df are often exactly "0.1", "0.5", "0.9" as strings
    if str(q_low) in pred.columns:
        low_series = pred[str(q_low)].to_numpy()
    elif low_col in pred.columns:
        low_series = pred[low_col].to_numpy()
    else:
        low_series = None

    if str(q_high) in pred.columns:
        high_series = pred[str(q_high)].to_numpy()
    elif high_col in pred.columns:
        high_series = pred[high_col].to_numpy()
    else:
        high_series = None

    if low_series is not None and high_series is not None:
        fig.add_trace(go.Scatter(
            x=pred["timestamp"], y=high_series,
            mode="lines", line=dict(width=0), showlegend=False, hoverinfo="skip"
        ))
        fig.add_trace(go.Scatter(
            x=pred["timestamp"], y=low_series,
            mode="lines", fill="tonexty", line=dict(width=0),
            name=f"Band [{q_low:.2f}, {q_high:.2f}]"
        ))

    fig.update_layout(
        title=title,
        hovermode="x unified",
        margin=dict(l=10, r=10, t=55, b=10),
        legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="left", x=0),
        xaxis_title="timestamp",
        yaxis_title="value",
    )
    return fig


def kpi_card(label: str, value: str, hint: str = "") -> str:
    hint_html = f"<div style='opacity:.75;font-size:12px;margin-top:6px;'>{hint}</div>" if hint else ""
    return f"""
    <div style="border:1px solid rgba(255,255,255,.12); border-radius:16px; padding:14px 16px;
                background: rgba(255,255,255,.04);">
      <div style="font-size:12px;opacity:.8;">{label}</div>
      <div style="font-size:22px;font-weight:700;margin-top:4px;">{value}</div>
      {hint_html}
    </div>
    """


def kpi_grid(cards: List[str]) -> str:
    return f"""
    <div class="kpi-grid">
      {''.join(cards)}
    </div>
    """



def explain_natural(context_df: pd.DataFrame, pred_df: pd.DataFrame, q_low: float, q_high: float, backtest_metrics: Optional[Dict[str, float]]) -> str:
    ctx_y = context_df["target"].to_numpy(dtype=float)

    if "predictions" in pred_df.columns:
        med = pred_df["predictions"].to_numpy(dtype=float)
    elif "0.5" in pred_df.columns:
        med = pred_df["0.5"].to_numpy(dtype=float)
    else:
        med = pred_df.iloc[:, -1].to_numpy(dtype=float)

    base = float(np.mean(ctx_y))
    delta = float(med[-1] - med[0])
    pct = (delta / max(1e-6, base)) * 100.0

    if abs(pct) < 2:
        trend_txt = "sostanzialmente stabile"
    elif pct > 0:
        trend_txt = "in crescita"
    else:
        trend_txt = "in calo"

    txt = f"""### 🧠 Spiegazione

Nei prossimi **{len(med)} step**, la previsione mediana è **{trend_txt}** (variazione complessiva ≈ **{pct:+.1f}%** rispetto al livello medio storico).

- **Ultimo valore mediano previsto:** **{med[-1]:.2f}**
"""

    # band, if present
    low_key = str(q_low)
    high_key = str(q_high)
    if low_key in pred_df.columns and high_key in pred_df.columns:
        low = pred_df[low_key].to_numpy(dtype=float)
        high = pred_df[high_key].to_numpy(dtype=float)
        txt += f"- **Intervallo [{q_low:.0%}{q_high:.0%}] ultimo step:** **[{low[-1]:.2f}{high[-1]:.2f}]**\n"
        txt += f"- **Larghezza media banda:** **{avg_width(low, high):.2f}**\n"
    else:
        txt += "- **Banda di incertezza:** non disponibile (manca nel pred_df).\n"

    if backtest_metrics:
        txt += f"""
### 🧪 Backtest (holdout)

- **MAE:** {backtest_metrics["mae"]:.3f}
- **RMSE:** {backtest_metrics["rmse"]:.3f}
- **MAPE:** {backtest_metrics["mape"]:.2f}%
- **Coverage banda:** {backtest_metrics["coverage"]:.1f}%
"""
    return txt


# =========================
# Run core (predict_df)
# =========================
def run_dashboard(
    input_mode: str,
    test_csv_name: str,
    upload_csv,
    target_col: str,
    time_col: str,
    freq: str,

    n: int,
    seed: int,
    trend: float,
    season_period: int,
    season_amp: float,
    noise: float,

    prediction_length: int,
    q_low: float,
    q_high: float,

    do_backtest: bool,
    holdout: int,

    device_ui: str,
    model_id: str,
):
    if q_low >= q_high:
        raise gr.Error("Quantile low deve essere < quantile high.")

    device = pick_device(device_ui)
    pipe = get_pipeline(model_id, device)

    # ---- build context_df
    if input_mode == "Test CSV":
        if not test_csv_name:
            raise gr.Error("Seleziona un Test CSV.")
        csv_path = os.path.join(DATA_DIR, test_csv_name)
        if not os.path.exists(csv_path):
            raise gr.Error(f"Non trovo {csv_path}")
        context_df, used_target, used_time = load_context_df_from_csv(csv_path, target_col, time_col, freq)
        source = f"Test CSV: {test_csv_name} • target={used_target} • time={used_time or 'generated'}"

    elif input_mode == "Upload CSV":
        if upload_csv is None:
            raise gr.Error("Carica un CSV.")
        context_df, used_target, used_time = load_context_df_from_csv(upload_csv.name, target_col, time_col, freq)
        source = f"Upload CSV • target={used_target} • time={used_time or 'generated'}"

    else:
        context_df = make_sample_df(n, seed, trend, season_period, season_amp, noise, freq=freq)
        source = "Sample series"

    if len(context_df) < 10:
        raise gr.Error("Serie troppo corta.")

    if do_backtest and holdout >= len(context_df):
        raise gr.Error("Holdout deve essere più piccolo della lunghezza dello storico.")

    quantiles = sorted(list(set([float(q_low), 0.5, float(q_high)])))

    t0 = time.time()

    # ---- forecast (future_df not needed if no covariates)
    pred_df = pipe.predict_df(
        context_df,
        prediction_length=int(prediction_length),
        quantile_levels=quantiles,
        id_column="id",
        timestamp_column="timestamp",
        target="target",
    )

    latency = time.time() - t0

    # ---- exports
    forecast_path = os.path.join(OUT_DIR, "chronos2_forecast_df.csv")
    pred_df.to_csv(forecast_path, index=False)

    # ---- backtest
    backtest_metrics = None
    backtest_path = None
    backtest_df_out = pd.DataFrame()
    backtest_fig = go.Figure().update_layout(title="Backtest disabled", margin=dict(l=10, r=10, t=55, b=10))

    if do_backtest:
        train_df = context_df.iloc[:-int(holdout)].copy()
        true_df = context_df.iloc[-int(holdout):].copy()

        bt_pred_df = pipe.predict_df(
            train_df,
            prediction_length=int(holdout),
            quantile_levels=quantiles,
            id_column="id",
            timestamp_column="timestamp",
            target="target",
        )

        # extract arrays
        y_true = true_df["target"].to_numpy(dtype=float)
        if "predictions" in bt_pred_df.columns:
            y_hat = bt_pred_df["predictions"].to_numpy(dtype=float)
        elif "0.5" in bt_pred_df.columns:
            y_hat = bt_pred_df["0.5"].to_numpy(dtype=float)
        else:
            y_hat = bt_pred_df.iloc[:, -1].to_numpy(dtype=float)

        # band
        if str(q_low) in bt_pred_df.columns and str(q_high) in bt_pred_df.columns:
            low = bt_pred_df[str(q_low)].to_numpy(dtype=float)
            high = bt_pred_df[str(q_high)].to_numpy(dtype=float)
            cov = coverage(y_true, low, high)
        else:
            low = y_hat.copy()
            high = y_hat.copy()
            cov = float("nan")

        backtest_metrics = {
            "mae": mae(y_true, y_hat),
            "rmse": rmse(y_true, y_hat),
            "mape": mape(y_true, y_hat),
            "coverage": cov,
        }

        # plot backtest
        fig = go.Figure()
        fig.add_trace(go.Scatter(x=train_df["timestamp"], y=train_df["target"], mode="lines", name="Train"))
        fig.add_trace(go.Scatter(x=true_df["timestamp"], y=true_df["target"], mode="lines", name="True (holdout)"))
        fig.add_trace(go.Scatter(x=bt_pred_df["timestamp"], y=y_hat, mode="lines", name="Pred (median)"))

        if str(q_low) in bt_pred_df.columns and str(q_high) in bt_pred_df.columns:
            fig.add_trace(go.Scatter(
                x=bt_pred_df["timestamp"], y=high, mode="lines", line=dict(width=0),
                showlegend=False, hoverinfo="skip"
            ))
            fig.add_trace(go.Scatter(
                x=bt_pred_df["timestamp"], y=low, mode="lines", fill="tonexty",
                line=dict(width=0), name=f"Band [{q_low:.2f}, {q_high:.2f}]"
            ))

        fig.update_layout(
            title="Backtest (holdout) — interactive",
            hovermode="x unified",
            margin=dict(l=10, r=10, t=55, b=10),
            legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="left", x=0),
            xaxis_title="timestamp",
            yaxis_title="value",
        )
        backtest_fig = fig

        backtest_path = os.path.join(OUT_DIR, "chronos2_backtest_df.csv")
        bt_pred_df.to_csv(backtest_path, index=False)
        backtest_df_out = bt_pred_df

    # ---- main plot
    forecast_fig = plot_forecast(context_df, pred_df, q_low, q_high, f"Forecast — {source}")

    # ---- KPIs
    cards = [
        kpi_card("Device", device.upper(), f"cuda_available={torch.cuda.is_available()}"),
        kpi_card("Model", (model_id or MODEL_ID_DEFAULT), "Chronos-2"),
        kpi_card("Latency", f"{latency:.2f}s", "predict_df()"),
        kpi_card("History", str(len(context_df)), "points"),
        kpi_card("Horizon", str(prediction_length), "steps"),
        kpi_card("Quantiles", f"{q_low:.2f}, 0.50, {q_high:.2f}", "levels"),
    ]
    kpis_html = kpi_grid(cards)

    explanation_md = explain_natural(context_df, pred_df, q_low, q_high, backtest_metrics)

    info = {
        "source": source,
        "history_points": int(len(context_df)),
        "prediction_length": int(prediction_length),
        "quantile_levels": quantiles,
        "backtest": bool(do_backtest),
        "holdout": int(holdout) if do_backtest else None,
    }

    return (
        kpis_html,
        explanation_md,
        forecast_fig,
        backtest_fig,
        pred_df,
        backtest_df_out,
        forecast_path,
        backtest_path,
        info,
    )


# =========================
# UI
# =========================
css = """
.gradio-container { max-width: 1200px !important; }

/* KPI grid */
.kpi-grid{
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 14px;
  padding: 10px 8px;     /* <-- spazio “esterno” */
  margin-top: 6px;       /* <-- separa dal titolo / contenuto sopra */
}

/* opzionale: un filo di aria sotto ogni card */
.kpi-grid > div{
  min-height: 84px;
}
"""


with gr.Blocks(title="Chronos-2 • Forecast Dashboard (predict_df)", css=css) as demo:
    gr.Markdown("# Chronos-2 Dashboard")

    with gr.Row():
        with gr.Column(scale=1, min_width=360):
            gr.Markdown("## Input")
            input_mode = gr.Radio(["Sample", "Test CSV", "Upload CSV"], value="Sample", label="Sorgente")
            test_csv_name = gr.Dropdown(choices=available_test_csv(), label="Test CSV (data/)")
            upload_csv = gr.File(label="Upload CSV", file_types=[".csv"])

            target_col = gr.Textbox(label="Colonna target (opzionale)", placeholder="es: value")
            time_col = gr.Textbox(label="Colonna timestamp (opzionale)", placeholder="es: timestamp / date / ds")
            freq = gr.Dropdown(["D", "H", "W", "M"], value=DEFAULT_FREQ, label="Freq (se timestamp mancante)")

            gr.Markdown("## Sistema")
            device_ui = gr.Dropdown(
                ["cpu", "cuda (se disponibile)"],
                value="cuda (se disponibile)" if torch.cuda.is_available() else "cpu",
                label="Device",
            )
            model_id = gr.Textbox(value=MODEL_ID_DEFAULT, label="Model ID")

            with gr.Accordion("Sample generator", open=False):
                n = gr.Slider(60, 2000, value=300, step=10, label="History length")
                seed = gr.Number(value=42, precision=0, label="Seed")
                trend = gr.Slider(0.0, 0.2, value=0.03, step=0.005, label="Trend")
                season_period = gr.Slider(2, 240, value=14, step=1, label="Season period")
                season_amp = gr.Slider(0.0, 12.0, value=3.0, step=0.1, label="Season amplitude")
                noise = gr.Slider(0.0, 6.0, value=0.8, step=0.05, label="Noise")

            gr.Markdown("## Forecast")
            prediction_length = gr.Slider(1, 365, value=30, step=1, label="Prediction length")
            q_low = gr.Slider(0.01, 0.49, value=0.10, step=0.01, label="Quantile low")
            q_high = gr.Slider(0.51, 0.99, value=0.90, step=0.01, label="Quantile high")

            gr.Markdown("## Backtest")
            do_backtest = gr.Checkbox(value=True, label="Esegui backtest holdout")
            holdout = gr.Slider(5, 365, value=30, step=1, label="Holdout points")

            run_btn = gr.Button("Run", variant="primary")

        with gr.Column(scale=2):
            kpis = gr.HTML()
            with gr.Tabs():
                with gr.Tab("Forecast"):
                    forecast_plot = gr.Plot()
                    forecast_table = gr.Dataframe(interactive=False)
                with gr.Tab("Backtest"):
                    backtest_plot = gr.Plot()
                    backtest_table = gr.Dataframe(interactive=False)
                with gr.Tab("Spiegazione"):
                    explanation = gr.Markdown()
                with gr.Tab("Export"):
                    forecast_download = gr.File(label="Forecast CSV")
                    backtest_download = gr.File(label="Backtest CSV")
                with gr.Tab("Info"):
                    info = gr.JSON()

    run_btn.click(
        fn=run_dashboard,
        inputs=[
            input_mode, test_csv_name, upload_csv,
            target_col, time_col, freq,
            n, seed, trend, season_period, season_amp, noise,
            prediction_length, q_low, q_high,
            do_backtest, holdout,
            device_ui, model_id,
        ],
        outputs=[
            kpis,
            explanation,
            forecast_plot,
            backtest_plot,
            forecast_table,
            backtest_table,
            forecast_download,
            backtest_download,
            info,
        ],
    )

demo.queue()
demo.launch(ssr_mode=False)