Spaces:
Running
Running
File size: 5,744 Bytes
826dae2 | 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 | """
Chronos-2 Zero-Shot Demo - FastAPI Backend
Standalone version for HF Spaces deployment.
Run locally: uvicorn server:app --reload --port 7860
"""
from __future__ import annotations
from pathlib import Path
import pandas as pd
import torch
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from pydantic import BaseModel, Field
# Chronos-2 imports
try:
from chronos import Chronos2Pipeline
except ImportError:
raise ImportError(
"Please install chronos-forecasting>=2.0: pip install 'chronos-forecasting[scripts]>=2.0'"
)
DEMO_DIR = Path(__file__).resolve().parent
STATIC_DIR = DEMO_DIR / "static"
# Model configuration
MODEL_NAME = "amazon/chronos-2"
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
# =============================================================================
# Chronos-2 Forecaster (standalone)
# =============================================================================
class Chronos2Forecaster:
"""Wrapper for Chronos-2 time series forecasting."""
def __init__(self, model_name: str = MODEL_NAME, device: str = DEVICE):
self.model_name = model_name
self.device = device
self.pipeline = None
def load_model(self) -> None:
"""Load the Chronos-2 model pipeline."""
print(f"Loading Chronos-2 model: {self.model_name}")
print(f"Device: {self.device}")
self.pipeline = Chronos2Pipeline.from_pretrained(
self.model_name,
device_map=self.device,
)
print("Model loaded successfully!")
def forecast(
self,
context_df: pd.DataFrame,
prediction_length: int = 12,
quantile_levels: list[float] | None = None,
) -> dict:
"""Generate probabilistic forecasts."""
if self.pipeline is None:
self.load_model()
if quantile_levels is None:
quantile_levels = [0.1, 0.5, 0.9]
pred_df = self.pipeline.predict_df(
context_df,
prediction_length=prediction_length,
quantile_levels=quantile_levels,
id_column="item_id",
timestamp_column="timestamp",
target="target",
)
return {
"median": pred_df["0.5"].values,
"low": pred_df["0.1"].values,
"high": pred_df["0.9"].values,
"pred_df": pred_df,
}
def to_chronos2_context(
df: pd.DataFrame,
target_col: str = "sale_qty",
item_id: str = "gfk_sales",
) -> pd.DataFrame:
"""Convert DataFrame to Chronos-2 long-format context."""
context = df[["period", target_col]].copy()
context = context.rename(columns={"period": "timestamp", target_col: "target"})
context["item_id"] = item_id
return context[["item_id", "timestamp", "target"]]
# =============================================================================
# FastAPI App
# =============================================================================
_forecaster: Chronos2Forecaster | None = None
def get_forecaster() -> Chronos2Forecaster:
global _forecaster
if _forecaster is None:
_forecaster = Chronos2Forecaster()
_forecaster.load_model()
return _forecaster
class ForecastRequest(BaseModel):
values: list[float] = Field(..., description="Time series values")
prediction_length: int = Field(1, ge=1, le=24, description="Steps to forecast")
class ForecastPoint(BaseModel):
index: int
median: float
low: float
high: float
class ForecastResponse(BaseModel):
historical: list[dict]
forecast: list[ForecastPoint]
def values_to_context_df(values: list[float]) -> pd.DataFrame:
if not values:
raise ValueError("values cannot be empty")
n = len(values)
periods = pd.date_range(start="2020-01-01", periods=n, freq="MS")
df = pd.DataFrame({"period": periods, "sale_qty": values})
return df
app = FastAPI(title="Chronos-2 Zero-Shot Demo", version="1.0.0")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.post("/api/forecast", response_model=ForecastResponse)
def forecast(req: ForecastRequest) -> ForecastResponse:
if not req.values:
raise HTTPException(status_code=400, detail="values cannot be empty")
try:
df = values_to_context_df(req.values)
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
context_df = to_chronos2_context(df, target_col="sale_qty", item_id="ts1")
forecaster = get_forecaster()
result = forecaster.forecast(context_df=context_df, prediction_length=req.prediction_length)
historical = [{"index": i, "value": float(v)} for i, v in enumerate(req.values)]
forecast_points = [
ForecastPoint(
index=len(req.values) + i,
median=float(result["median"][i]),
low=float(result["low"][i]),
high=float(result["high"][i]),
)
for i in range(req.prediction_length)
]
return ForecastResponse(historical=historical, forecast=forecast_points)
@app.get("/")
def index():
index_path = STATIC_DIR / "index.html"
if not index_path.exists():
raise HTTPException(status_code=404, detail="index.html not found")
return FileResponse(index_path)
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
|