multimodalart's picture
multimodalart HF Staff
Upload app.py with huggingface_hub
59e67b9 verified
Raw History Blame Contribute Delete
22 kB
"""
FlexCast: Flexible Context Learning for Time Series Foundation Models.
Interactive demo of zero-shot probabilistic forecasting: upload a CSV time
series, choose how much history the model sees (any multiple of the 16-step
patch length) and how far ahead to predict, and FlexCast returns a full
99-quantile predictive distribution for every channel β€” decoded in one shot,
no autoregression. If the uploaded file contains at least
`context + horizon` rows, the trailing `horizon` rows are held out and the
forecast is scored against them (MAE / RMSE / 90% interval coverage).
Paper: https://huggingface.co/papers/2611.17862
Code: https://github.com/BenchCouncil/FlexCast
Checkpoint: https://huggingface.co/zuyangsammie/FlexCast (MIT license)
"""
import spaces # ZeroGPU: MUST be imported before torch
import os
import sys
import tempfile
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from types import SimpleNamespace
import numpy as np
import pandas as pd
import torch
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from matplotlib.ticker import MaxNLocator
import gradio as gr
from huggingface_hub import hf_hub_download
from models.FlexCast import Model
# ---------------------------------------------------------------------------
# Constants (patch_len and the reference prediction horizons come from the
# FlexCast paper / repo: exp/exp_inference.py, PRED_LENS = [96, 192, 336, 720])
# ---------------------------------------------------------------------------
REPO_ID = "zuyangsammie/FlexCast"
CKPT_FILE = "FlexCast/checkpoint.pth"
PATCH_LEN = 16 # model patch length (T must be a multiple of it)
MAX_CONTEXT = 3072 # longest context offered in the UI
MAX_TOTAL = 4096 # FlexCast pretraining context ceiling
HORIZONS = [96, 192, 336, 720]
DEFAULT_CONTEXT = 720
DEFAULT_HORIZON = 96
MAX_CHANNELS = 12
ETT_COLS = ["HUFL", "HULL", "MUFL", "MULL", "LUFL", "LULL", "OT"]
DATE_COL_NAMES = {"date", "time", "timestamp", "datetime"}
# Chart tokens (light surface) β€” see the dataviz reference palette.
SURFACE = "#fcfcfb"
INK = "#0b0b0b"
INK2 = "#52514e"
MUTED = "#898781"
GRID = "#e1e0d9"
AXIS = "#c3c2b7"
BLUE = "#2a78d6" # forecast (series slot 1)
ORANGE = "#eb6834" # held-out truth (series slot 2)
# ---------------------------------------------------------------------------
# Model loading (module scope, eager .to("cuda") β€” ZeroGPU streams weights on
# the first @spaces.GPU call)
# ---------------------------------------------------------------------------
def _load_model():
ckpt_path = hf_hub_download(REPO_ID, CKPT_FILE)
ckpt = torch.load(ckpt_path, map_location="cpu", weights_only=True)
config = ckpt["config"]
state_dict = ckpt.get("state_dict", ckpt)
cfg = SimpleNamespace(
d_model=config["d_model"],
n_heads=config["n_heads"],
e_layers=config["e_layers"],
d_ff=config.get("d_ff", config["d_model"] * 4),
patch_len=config.get("patch_len", PATCH_LEN),
stride=config.get("stride", config.get("patch_len", PATCH_LEN)),
dropout=config.get("dropout", 0.1),
factor=config.get("factor", 1),
activation=config.get("activation", "gelu"),
num_quantile=config.get("num_quantile", 99),
)
model = Model(cfg).float()
if all(k.startswith("module.") for k in state_dict.keys()):
state_dict = {k.replace("module.", ""): v for k, v in state_dict.items()}
model.load_state_dict(state_dict)
model.eval()
return model
print("Loading FlexCast checkpoint from the HF Hub ...", flush=True)
model = _load_model().to("cuda")
NUM_QUANTILE = model.num_quantile
QUANTILE_LEVELS = [i / (NUM_QUANTILE + 1) for i in range(1, NUM_QUANTILE + 1)]
def _q_idx(level: float) -> int:
"""Index of the quantile level closest to `level` (levels are i/(Q+1))."""
return int(np.argmin(np.abs(np.array(QUANTILE_LEVELS) - level)))
IDX_Q05, IDX_Q50, IDX_Q95 = _q_idx(0.05), _q_idx(0.50), _q_idx(0.95)
print(f"FlexCast ready: {sum(p.numel() for p in model.parameters()) / 1e6:.1f}M params, "
f"{NUM_QUANTILE} quantiles.", flush=True)
# ---------------------------------------------------------------------------
# Input parsing
# ---------------------------------------------------------------------------
def _parse_series(path: str):
"""Read a CSV into (numeric value frame, optional DatetimeIndex-like series)."""
try:
df = pd.read_csv(path)
except Exception as exc: # noqa: BLE001
raise gr.Error(f"Could not read the CSV file: {exc}")
if df.empty:
raise gr.Error("The CSV file is empty.")
date_col = next(
(c for c in df.columns if str(c).strip().lower() in DATE_COL_NAMES), None
)
stamps = None
if date_col is not None:
try:
stamps = pd.to_datetime(df[date_col])
except Exception: # noqa: BLE001
stamps = None
value_cols = [
c for c in df.columns
if c != date_col and pd.api.types.is_numeric_dtype(df[c])
]
if not value_cols:
raise gr.Error(
"No numeric columns found. The CSV needs at least one column of "
"numeric values (an optional date/time column is used for the axis)."
)
values = df[value_cols].astype(np.float32)
values = values.dropna(axis=1, how="all")
value_cols = list(values.columns)
if len(values) < 2 * PATCH_LEN:
raise gr.Error(
f"Need at least {2 * PATCH_LEN} rows of history; the file has {len(values)}."
)
return values, stamps
def _split_nan_masks(x: torch.Tensor):
"""Reference NaN handling (exp/exp_inference.py): NaNs past the last valid
observation of channel 0 are padding; NaNs inside are missing values."""
B, T, N = x.shape
nan_mask = torch.isnan(x)
has_value = ~torch.isnan(x[:, :, 0])
idx = torch.arange(T, device=x.device).unsqueeze(0).expand_as(has_value)
idx_valid = idx.clone()
idx_valid[~has_value] = 0
eff_lens = idx_valid.max(dim=1).values + 1
arange = torch.arange(T, device=x.device).unsqueeze(0).unsqueeze(2)
pad_mask = (arange >= eff_lens.unsqueeze(1).unsqueeze(2)).expand_as(nan_mask)
miss_mask = nan_mask & ~pad_mask
return pad_mask, miss_mask
def refresh_channels(csv_file):
"""Populate the channel dropdown from the numeric columns of the upload."""
if csv_file is None:
return gr.update(choices=ETT_COLS, value="OT")
path = csv_file.name if hasattr(csv_file, "name") else str(csv_file)
try:
df = pd.read_csv(path, nrows=64)
except Exception: # noqa: BLE001
return gr.update()
value_cols = [
c for c in df.columns
if str(c).strip().lower() not in DATE_COL_NAMES
and pd.api.types.is_numeric_dtype(df[c])
]
if not value_cols:
return gr.update()
return gr.update(choices=value_cols, value=value_cols[-1])
# ---------------------------------------------------------------------------
# Chart
# ---------------------------------------------------------------------------
def _plot_forecast(ctx_x, ctx_y, fc_x, med, lo50, hi50, lo90, hi90, truth,
channel, subtitle, scored, x_is_dates):
"""History (gray) + held-out truth (orange) + forecast median (blue) with
50% / 90% predictive bands (blue washes). One axis, hairline grid,
legend above the plot, endpoint labelled selectively."""
fig = plt.figure(figsize=(9.6, 4.8), dpi=150)
fig.patch.set_facecolor(SURFACE)
ax = fig.add_axes([0.06, 0.12, 0.90, 0.66])
ax.set_facecolor(SURFACE)
# Bands first (washes behind the lines), then lines.
ax.fill_between(fc_x, lo90, hi90, color=BLUE, alpha=0.10, linewidth=0,
label="90% interval", zorder=1.5)
ax.fill_between(fc_x, lo50, hi50, color=BLUE, alpha=0.20, linewidth=0,
label="50% interval", zorder=1.6)
ax.plot(ctx_x, ctx_y, color=MUTED, linewidth=2, solid_capstyle="round",
label="History", zorder=2)
if truth is not None:
ax.plot(fc_x, truth, color=ORANGE, linewidth=2, solid_capstyle="round",
label="Held-out truth", zorder=3)
ax.plot(fc_x, med, color=BLUE, linewidth=2, solid_capstyle="round",
label="Forecast (median)", zorder=4)
# Hairline divider at the forecast origin.
ax.axvline(fc_x[0], color=AXIS, linewidth=1, zorder=1)
# Selective endpoint label: the last forecast value.
ax.annotate(f"{med[-1]:.3g}", xy=(fc_x[-1], med[-1]),
xytext=(7, 0), textcoords="offset points",
va="center", ha="left", fontsize=9, color=INK2,
annotation_clip=False, zorder=5)
# Chrome: hairline solid y-grid, recessive axes, muted tick text.
ax.grid(axis="y", color=GRID, linewidth=1)
ax.set_axisbelow(True)
for side in ("top", "right"):
ax.spines[side].set_visible(False)
for side in ("left", "bottom"):
ax.spines[side].set_color(AXIS)
ax.spines[side].set_linewidth(1)
ax.tick_params(colors=MUTED, labelsize=9, length=3, width=1)
if x_is_dates:
loc = mdates.AutoDateLocator(maxticks=8)
ax.xaxis.set_major_locator(loc)
ax.xaxis.set_major_formatter(mdates.ConciseDateFormatter(loc))
else:
ax.xaxis.set_major_locator(MaxNLocator(integer=True, nbins=8))
ax.set_xlabel("steps", fontsize=9, color=MUTED)
# Room for the endpoint label on the right.
x0, x1 = ax.get_xlim()
ax.set_xlim(x0, x1 + 0.05 * (x1 - x0))
ax.legend(loc="lower right", bbox_to_anchor=(1.0, 1.01), ncol=4,
frameon=False, fontsize=9, labelcolor=INK2,
handlelength=1.6, columnspacing=1.4)
fig.text(0.012, 0.960, f"FlexCast forecast β€” {channel}",
fontsize=13, fontweight="bold", color=INK)
fig.text(0.012, 0.918, subtitle, fontsize=9.5, color=INK2)
return fig
def _fmt(v: float) -> str:
return f"{v:.4g}"
# ---------------------------------------------------------------------------
# Inference
# ---------------------------------------------------------------------------
@spaces.GPU(duration=45)
def forecast(csv_file, context_len=DEFAULT_CONTEXT, horizon=DEFAULT_HORIZON,
channel="OT", progress=gr.Progress(track_tqdm=True)):
"""Forecast a time series with FlexCast.
Args:
csv_file: CSV of the historical series. One column per numeric channel;
an optional date/time column sets the x-axis. If the file has at
least context + horizon rows, the last `horizon` rows are held out
and the forecast is scored against them.
context_len: history steps fed to the model (any multiple of 16,
32..3072 β€” FlexCast handles variable context natively).
horizon: forecast steps (96 / 192 / 336 / 720).
channel: which channel to plot and export.
Returns:
(plot image path, per-channel metrics table, status markdown,
forecast CSV path for the selected channel).
"""
if csv_file is None:
raise gr.Error("Upload a CSV time series, or click one of the examples below.")
path = csv_file.name if hasattr(csv_file, "name") else str(csv_file)
values, stamps = _parse_series(path)
# Snap both lengths to the patch grid.
context_len = max(2 * PATCH_LEN, (int(context_len) // PATCH_LEN) * PATCH_LEN)
horizon = max(PATCH_LEN, (int(horizon) // PATCH_LEN) * PATCH_LEN)
context_len = min(context_len, MAX_TOTAL - horizon, MAX_CONTEXT)
total_rows = len(values)
n_channels = values.shape[1]
dropped = 0
if n_channels > MAX_CHANNELS:
values = values.iloc[:, -MAX_CHANNELS:]
dropped = n_channels - MAX_CHANNELS
n_channels = MAX_CHANNELS
value_cols = list(values.columns)
if channel not in value_cols:
channel = value_cols[-1]
ci = value_cols.index(channel)
scored = total_rows >= context_len + horizon
if scored:
ctx_block = values.iloc[-(context_len + horizon):-horizon]
truth_block = values.iloc[-horizon:]
else:
avail = (min(context_len, total_rows) // PATCH_LEN) * PATCH_LEN
if avail < 2 * PATCH_LEN:
raise gr.Error(
f"Need at least {2 * PATCH_LEN} usable history rows for a "
f"{horizon}-step forecast; this file has {total_rows}."
)
ctx_block = values.iloc[-avail:]
truth_block = None
T = len(ctx_block)
H = horizon
# x positions on the plot axis.
if stamps is not None:
if scored:
ctx_x = stamps.iloc[-(T + H):-H].to_numpy()
fc_x = stamps.iloc[-H:].to_numpy()
else:
ctx_x = stamps.iloc[-T:].to_numpy()
last = stamps.iloc[-1]
diffs = pd.Series(stamps.iloc[-T:]).diff().dropna()
step = diffs.mode().iloc[0] if len(diffs) else pd.Timedelta(hours=1)
fc_x = pd.date_range(start=last, periods=H + 1, freq=step)[1:].to_numpy()
x_is_dates = True
else:
ctx_x = np.arange(T)
fc_x = np.arange(T, T + H)
x_is_dates = False
# --- model forward (reference path: exp/exp_inference.py) --------------
x = torch.from_numpy(
np.ascontiguousarray(ctx_block.to_numpy(dtype=np.float32))
).unsqueeze(0) # [1,T,C]
C = x.shape[2]
x_in = torch.cat([x, torch.zeros(1, H, C)], dim=1) # masked tail (zeroed inside)
mask = torch.ones(1, T + H, C)
mask[:, T:, :] = 0
pad_mask, miss_mask = _split_nan_masks(x_in)
dev = next(model.parameters()).device
x_in, mask = x_in.to(dev), mask.to(dev)
pad_mask, miss_mask = pad_mask.to(dev), miss_mask.to(dev)
with torch.no_grad(), torch.autocast(dev.type, dtype=torch.bfloat16,
enabled=(dev.type == "cuda")):
out = model(x_in, mask, pad_mask=pad_mask, miss_mask=miss_mask)
# out: [1, T+H, C, Q] quantiles in the ORIGINAL scale (RevIN inverse).
out = out.float().cpu()
quants = out[0] # [T+H, C, Q]
fc_quants = quants[T:, :, :] # [H, C, Q]
# Point forecast = reference weighted quantile mean.
ql = torch.tensor(QUANTILE_LEVELS, dtype=torch.float32)
w = 0.5 - (0.5 - ql).abs()
w = w / w.sum()
point = torch.einsum("hcq,q->hc", fc_quants, w).numpy() # [H, C]
med = fc_quants[:, ci, IDX_Q50].numpy()
lo50 = fc_quants[:, ci, _q_idx(0.25)].numpy()
hi50 = fc_quants[:, ci, _q_idx(0.75)].numpy()
lo90 = fc_quants[:, ci, IDX_Q05].numpy()
hi90 = fc_quants[:, ci, IDX_Q95].numpy()
# --- hold-out metrics ---------------------------------------------------
headers = ["channel", "MAE", "RMSE", "90% interval coverage",
"mean 90% interval width"]
metrics = pd.DataFrame(columns=headers)
summary = ""
if scored:
rows = []
for j, cname in enumerate(value_cols):
truth = truth_block[cname].to_numpy(dtype=np.float64)
p = point[:, j].astype(np.float64)
lo = fc_quants[:, j, IDX_Q05].numpy().astype(np.float64)
hi = fc_quants[:, j, IDX_Q95].numpy().astype(np.float64)
valid = ~np.isnan(truth)
if valid.sum() == 0:
continue
mae = float(np.mean(np.abs(p[valid] - truth[valid])))
rmse = float(np.sqrt(np.mean((p[valid] - truth[valid]) ** 2)))
cov = float(np.mean((truth[valid] >= lo[valid]) & (truth[valid] <= hi[valid])))
width = float(np.mean(hi - lo))
rows.append([cname, _fmt(mae), _fmt(rmse), f"{cov * 100:.1f}%",
_fmt(width)])
if rows:
metrics = pd.DataFrame(rows, columns=headers)
sel = metrics[metrics["channel"] == channel]
if len(sel):
r = sel.iloc[0]
summary = (f"**{channel}**: MAE {r['MAE']}, RMSE {r['RMSE']}, "
f"90% coverage {r['90% interval coverage']}.")
status = (
f"**{H}-step forecast** from **{T} steps** of context across "
f"**{n_channels}** channel{'s' if n_channels > 1 else ''} β€” scored "
f"against the last {H} held-out rows. {summary}"
)
else:
status = (
f"**{H}-step forecast** from **{T} steps** of context across "
f"**{n_channels}** channel{'s' if n_channels > 1 else ''}. "
f"Upload β‰₯ {T + H} rows to also score the forecast against "
f"held-out truth β€” here the model predicts beyond the end of the "
f"file, so no ground truth is shown."
)
if dropped:
status += f" (only the last {MAX_CHANNELS} of {n_channels + dropped} channels were used.)"
# --- plot + export ------------------------------------------------------
truth_plot = truth_block[channel].to_numpy() if scored else None
subtitle = (
f"{T} context steps β†’ {H}-step horizon Β· {n_channels} channel"
f"{'s' if n_channels > 1 else ''} Β· one-shot masked-patch prediction"
)
fig = _plot_forecast(ctx_x, ctx_block[channel].to_numpy(), fc_x, med,
lo50, hi50, lo90, hi90, truth_plot, channel,
subtitle, scored, x_is_dates)
out_dir = tempfile.mkdtemp()
plot_path = os.path.join(out_dir, "forecast.png")
fig.savefig(plot_path, facecolor=SURFACE)
plt.close(fig)
if x_is_dates:
axis_col = pd.Series(fc_x).astype(str).tolist()
axis_header = "timestamp"
else:
axis_col = list(range(T, T + H))
axis_header = "step"
fc_df = pd.DataFrame({
axis_header: axis_col,
"median": med,
"q05": lo90,
"q50": med,
"q95": hi90,
})
csv_path = os.path.join(out_dir, "flexcast_forecast.csv")
fc_df.to_csv(csv_path, index=False)
return plot_path, metrics, status, csv_path
# ---------------------------------------------------------------------------
# UI
# ---------------------------------------------------------------------------
HEADER_MD = """
# FlexCast β€” flexible-context time series forecasting
Upload a CSV time series, pick how much history the model sees and how far
ahead to forecast. **FlexCast** treats forecasting as variable-length masked
patch prediction: any context length (multiple of 16) and any horizon are
handled in **one forward pass**, producing a full **99-quantile** predictive
distribution per channel β€” no per-step autoregression, no retraining.
If the file contains at least `context + horizon` rows, the trailing rows are
held out and the forecast is scored (MAE / RMSE / 90% interval coverage) so
you can judge calibration honestly.
[[Paper](https://huggingface.co/papers/2611.17862)] Β·
[[GitHub](https://github.com/BenchCouncil/FlexCast)] Β·
[[Checkpoint](https://huggingface.co/zuyangsammie/FlexCast)] Β·
131M params, d_model 1024, 10 layers, patch 16, 99 quantiles, MIT license.
"""
FOOTER_MD = """
**Notes** β€” NaNs inside the history are treated as missing values; channels
beyond the last 12 numeric columns are ignored. Example data: **ETTh1** from
[ETDataset](https://github.com/zhouhaoyi/ETDataset) (CC-BY-ND 4.0), the hourly
electricity-transformer benchmark used in the FlexCast paper
(Informer, AAAI 2021). Quantile outputs are decoded monotonically
(softplus offsets + cumsum) and mapped back to the input scale by RevIN.
"""
CSS = """
#col-container { max-width: 1100px; margin: 0 auto; }
.dark .gradio-container { color: var(--body-text-color); }
"""
with gr.Blocks(title="FlexCast Forecasting") as demo:
gr.Markdown(HEADER_MD)
with gr.Row(elem_id="col-container"):
with gr.Column(scale=2):
csv_in = gr.File(
label="Time series CSV β€” one column per numeric channel, "
"optional date/time column",
file_types=[".csv"],
type="filepath",
)
channel_dd = gr.Dropdown(
choices=ETT_COLS, value="OT",
label="Channel to forecast", interactive=True,
)
context_sl = gr.Slider(
minimum=96, maximum=MAX_CONTEXT, value=DEFAULT_CONTEXT, step=16,
label="Context length (steps)",
info="History steps fed to the model β€” any multiple of 16.",
)
horizon_dd = gr.Dropdown(
choices=HORIZONS, value=DEFAULT_HORIZON,
label="Forecast horizon (steps)",
info="Reference prediction lengths from the FlexCast paper.",
)
btn = gr.Button("Forecast", variant="primary")
with gr.Column(scale=3):
plot_out = gr.Image(type="filepath", label="Forecast")
status_out = gr.Markdown()
metrics_out = gr.DataFrame(label="Hold-out metrics (all channels)",
interactive=False)
file_out = gr.File(label="Forecast CSV (selected channel)")
csv_in.change(refresh_channels, inputs=csv_in, outputs=channel_dd)
btn.click(forecast,
inputs=[csv_in, context_sl, horizon_dd, channel_dd],
outputs=[plot_out, metrics_out, status_out, file_out])
gr.Examples(
examples=[
["examples/ETTh1_sample.csv", 720, 96],
["examples/ETTh1_sample.csv", 1024, 336],
["examples/ETTh1_sample.csv", 3072, 720],
["examples/ETTh1_univariate_OT.csv", 1024, 96],
],
inputs=[csv_in, context_sl, horizon_dd],
fn=forecast,
outputs=[plot_out, metrics_out, status_out, file_out],
cache_examples=True,
cache_mode="lazy",
label="Examples (hourly ETTh1 electricity-transformer data)",
)
gr.Markdown(FOOTER_MD)
if __name__ == "__main__":
demo.launch(mcp_server=True, theme=gr.themes.Citrus(), css=CSS)