"""Timestral Backtest Lab — Gradio Space on FREE ZeroGPU (Spanish UI).""" from __future__ import annotations import spaces # MUST be first: before torch / timestral / any CUDA-touching import import os import tempfile import traceback from pathlib import Path from typing import Any os.environ.setdefault("OMP_NUM_THREADS", "1") os.environ.setdefault("MKL_NUM_THREADS", "1") os.environ.setdefault("OPENBLAS_NUM_THREADS", "1") os.environ.setdefault("NUMEXPR_NUM_THREADS", "1") os.environ.setdefault("TORCH_NUM_THREADS", "1") os.environ.setdefault("GRADIO_MAX_FILE_SIZE", str(1_000_000)) import gradio as gr import pandas as pd from lab.constants import GPU_DURATION_S, MODEL_ID, MODEL_REVISION from lab.demos import DEMO_SPECS, EXAMPLES_DIR, generate_demos from lab.export_csv import write_forecast_csv, write_metrics_csv from lab.forecast_engine import run_backtest_and_forecast from lab.plotting import build_figure, metrics_markdown from lab.validation import ( DEFAULT_HORIZON, MAX_FILE_BYTES, MAX_HORIZON, ValidationError, parse_series_csv, validate_run_params, ) # Ensure synthetic demos exist next to the app. generate_demos(EXAMPLES_DIR) DEMO_CHOICES = [ ("(subir CSV propio)", ""), (DEMO_SPECS["synthetic_daily_demand"]["label"], "synthetic_daily_demand"), (DEMO_SPECS["synthetic_hourly_energy"]["label"], "synthetic_hourly_energy"), (DEMO_SPECS["synthetic_monthly_trend"]["label"], "synthetic_monthly_trend"), ] def _safe_error(exc: BaseException) -> str: """User-facing error without server paths or secrets.""" msg = str(exc) for token in ("/home/", "/workspace/", "/tmp/", "/root/", "/data/", "/user/"): if token in msg: msg = "Error de validación o inferencia. Revisa el CSV y los parámetros." break return msg def _resolve_input( demo_key: str, csv_file: Any, value_column: str, date_column: str, ): demo_key = (demo_key or "").strip() if demo_key: if demo_key not in DEMO_SPECS: raise ValidationError("Ejemplo no reconocido.") spec = DEMO_SPECS[demo_key] path = EXAMPLES_DIR / spec["filename"] series = parse_series_csv( path, value_column=spec["value_column"], date_column=spec["date_column"], source_label=f"synthetic:{demo_key}", ) return series, spec series = parse_series_csv( csv_file, value_column=value_column or None, date_column=date_column or None, source_label="upload", ) return series, None def predict( demo_key: str, csv_file: Any, value_column: str, date_column: str, holdout: int, future_horizon: int, seasonality: int, symmetric_averaging: bool, clip_nonnegative: bool, history_to_show: int, ): """Backtest Timestral-28M vs baselines on holdout, then forecast the future. Endpoint API name: `/predict` (verify with Client.view_api when live). Validation / metrics / CSV export stay on CPU; both Timestral forecasts share one `@spaces.GPU` invocation inside `run_backtest_and_forecast`. """ try: series, spec = _resolve_input(demo_key, csv_file, value_column, date_column) if spec is not None: seasonality = int(seasonality) if seasonality else spec["seasonality"] params = validate_run_params( n_points=series.n_points, holdout=int(holdout), future_horizon=int(future_horizon), seasonality=int(seasonality), ) result = run_backtest_and_forecast( series.values, holdout=params.holdout, future_horizon=params.future_horizon, seasonality=params.seasonality, symmetric_averaging=bool(symmetric_averaging), clip_nonnegative=bool(clip_nonnegative), seed=20261107, ) title = ( f"Timestral-28M · {series.value_column} · " f"holdout={params.holdout}, futuro={params.future_horizon}" ) if series.source_label.startswith("synthetic:"): title = "[SINTÉTICO] " + title fig = build_figure( series.values, result, timestamps=series.timestamps, title=title, history_to_show=int(history_to_show), ) md = metrics_markdown(result.metrics) ev = result.evidence or {} md += ( f"\n\nModelo: `{MODEL_ID}` @ `{MODEL_REVISION[:12]}` · " f"device=`{ev.get('forecaster_device', '?')}` · dtype=`{ev.get('dtype', '?')}` · " f"gpu_wall_s=`{ev.get('gpu_wall_s', '?')}` · presupuesto `@spaces.GPU`={GPU_DURATION_S}s · " "inferencia oficial Timestral en GPU (ZeroGPU). " "Sin promesa de victoria frente a baselines ni de latencia fija." ) # Unique temp dir per request (concurrency-safe; no static output paths). tmp = Path(tempfile.mkdtemp(prefix="tblab_")) n = series.n_points if series.timestamps is not None: ts = pd.DatetimeIndex(series.timestamps) delta = pd.Series(ts).diff().median() if pd.isna(delta): fut_times: list[Any] = list(range(n, n + params.future_horizon)) else: fut_times = list( pd.date_range(ts[-1] + delta, periods=params.future_horizon, freq=delta) ) else: fut_times = list(range(n, n + params.future_horizon)) forecast_path = write_forecast_csv( tmp / "forecast_future.csv", times=fut_times, median=result.future.median, q_low=result.future.q_low, q_high=result.future.q_high, low_level=result.future.low_level, high_level=result.future.high_level, ) metrics_path = write_metrics_csv(tmp / "metrics_holdout.csv", result.metrics) status = ( f"OK · puntos={series.n_points} · train={len(result.train)} · " f"holdout={params.holdout} · futuro={params.future_horizon} · " f"device={ev.get('forecaster_device', '?')} · " f"gpu_wall_s={ev.get('gpu_wall_s', '?')} · " f"sha={MODEL_REVISION[:12]}" ) return fig, md, str(forecast_path), str(metrics_path), status except ValidationError as exc: raise gr.Error(_safe_error(exc)) from exc except gr.Error: raise except Exception as exc: # noqa: BLE001 detail = _safe_error(exc) err = ( f"Falló la inferencia Timestral en GPU (sin resultado inventado): " f"{type(exc).__name__}: {detail}" ) traceback.print_exc() raise gr.Error(err) from exc def _on_demo_change(demo_key: str): demo_key = (demo_key or "").strip() if demo_key in DEMO_SPECS: spec = DEMO_SPECS[demo_key] return ( spec["default_holdout"], spec["default_future"], spec["seasonality"], spec["value_column"], spec["date_column"], "Ejemplo sintético cargado (no son datos reales de negocio).", ) return ( DEFAULT_HORIZON, DEFAULT_HORIZON, 7, "", "", "Sube un CSV o elige un ejemplo sintético.", ) with gr.Blocks(title="Timestral Backtest Lab") as demo: with gr.Column(elem_id="col-container"): gr.Markdown( """ # Timestral Backtest Lab Laboratorio de backtest para **Timestral-28M** (Mistral AI) en **ZeroGPU** (GPU compartida gratis para el creador; los visitantes consumen [cuota diaria de ZeroGPU](https://huggingface.co/docs/hub/spaces-zerogpu)): sube un CSV o elige un ejemplo **sintético**, reserva los últimos N puntos (holdout sin lookahead), compara MAE/MASE frente a *last-value* y *seasonal-naive*, y genera un pronóstico futuro con bandas q10–q90. Puede haber **cola** y **cold-start**; no prometemos latencia fija hasta medir en vivo. La pregunta útil: **¿el modelo fundacional supera una regla simple en *tus* datos?** No prometemos que gane. """ ) with gr.Row(): with gr.Column(scale=1): demo_dd = gr.Dropdown( label="Ejemplo sintético (o CSV propio)", choices=DEMO_CHOICES, value="", ) csv_in = gr.File( label="CSV (máx. 1 MB, ≤10 000 filas)", file_types=[".csv"], ) value_col = gr.Textbox( label="Columna numérica", placeholder="vacío = última columna numérica", ) date_col = gr.Textbox( label="Columna fecha (opcional)", placeholder="vacío = autodetectar", ) holdout = gr.Slider( 1, MAX_HORIZON, value=DEFAULT_HORIZON, step=1, label="Holdout (últimos N puntos)", ) future_horizon = gr.Slider( 1, MAX_HORIZON, value=DEFAULT_HORIZON, step=1, label="Horizonte futuro", ) seasonality = gr.Number( value=7, precision=0, label="Estacionalidad (para MASE / seasonal-naive)", ) with gr.Accordion("Opciones avanzadas", open=False): sym = gr.Checkbox(value=True, label="Symmetric averaging (clampsym TTA)") clip = gr.Checkbox(value=True, label="Clip ≥0 si el contexto es no negativo") hist = gr.Slider(32, 2000, value=400, step=16, label="Puntos de historia en el gráfico") run = gr.Button("Ejecutar backtest + pronóstico", variant="primary") status = gr.Textbox(label="Estado", interactive=False) with gr.Column(scale=2): plot = gr.Plot(label="Serie, holdout y pronóstico") metrics_md = gr.Markdown(value="Los resultados aparecerán aquí.") forecast_file = gr.File(label="Descargar pronóstico futuro (CSV)") metrics_file = gr.File(label="Descargar métricas holdout (CSV)") demo_dd.change( _on_demo_change, inputs=[demo_dd], outputs=[holdout, future_horizon, seasonality, value_col, date_col, status], ) inputs = [ demo_dd, csv_in, value_col, date_col, holdout, future_horizon, seasonality, sym, clip, hist, ] outputs = [plot, metrics_md, forecast_file, metrics_file, status] run.click( predict, inputs=inputs, outputs=outputs, api_name="predict", ) # cache_examples=False: avoid startup GPU caching on ZeroGPU (no GPU at boot). gr.Examples( examples=[ ["synthetic_daily_demand", None, "demanda_sintetica", "fecha", 14, 14, 7, True, True, 400], ["synthetic_hourly_energy", None, "consumo_mw_sintetico", "timestamp", 48, 24, 24, True, True, 400], ["synthetic_monthly_trend", None, "indice_sintetico", "mes", 12, 12, 12, True, True, 400], ], inputs=inputs, outputs=outputs, fn=predict, cache_examples=False, label="Ejemplos sintéticos (1 clic, inferencia real en GPU)", ) gr.Markdown( f""" Modelo: {MODEL_ID} (revisión {MODEL_REVISION}) · código oficial mistralai/timestral · hardware: ZeroGPU (@spaces.GPU(duration={GPU_DURATION_S}), holdout+futuro en una sola invocación). Pesos: Modified MIT (2026 Mistral AI; restricción si ingresos mensuales consolidados > USD 20M). Esta app: MIT. Demos sintéticos (semilla fija). Sin analítica ni telemetría. Modo CPU local documentado: TIMESTRAL_DEVICE=cpu (no usar en el Space ZeroGPU). """ ) demo.queue(default_concurrency_limit=1, max_size=4) if __name__ == "__main__": demo.launch( server_name="0.0.0.0", server_port=int(os.environ.get("PORT", "7860")), max_file_size=MAX_FILE_BYTES, show_error=True, ssr_mode=False, )