CoolFace
Apppublic

suryacz23/aczen-kronos

sourceHugging Faceupdated 4mo agoView on Hugging Face
0likes
App README

Kronos forecast endpoint

This directory holds the Python server that powers the Kronos forecast card in the finance page. It is deployed separately as a HuggingFace Inference Endpoint — the Supabase Edge Function (supabase/functions/finance) calls it via the forecast action.

What Kronos is

Kronos (shiyu-coder/Kronos) is a foundation model for K-line forecasting. Given a window of recent OHLCV bars, it predicts the next N bars. We run it 30 times with stochastic sampling to produce a mean forecast plus a 10–90% confidence band.

Architecture

browser  ──►  Supabase Edge Fn (finance)  ──►  HF Inference Endpoint (this dir)
              · pulls history from Yahoo            · loads Kronos-small
              · POSTs bars + horizon                 · 30-sample forecast
              · returns predictions + CI            · returns mean + CI

Deploying to HuggingFace Inference Endpoints

  1. 1.Create a new model repo on HuggingFace, e.g. <your-org>/aczen-kronos-handler.
  2. 2.Push the contents of this directory (handler.py, requirements.txt) to that repo. Also include the Kronos model/ directory from <https://github.com/shiyu-coder/Kronos/tree/master/model> so from model import Kronos, KronosTokenizer, KronosPredictor resolves.
  3. 3.Open the repo on HuggingFace → Deploy → Inference Endpoints.
  4. 4.Pick a GPU instance (T4 is enough — a 30-day forecast with 30 samples runs in ~10–15s on T4).
  5. 5.Once deployed, copy the endpoint URL (looks like https://xxxx.us-east-1.aws.endpoints.huggingface.cloud) and a HF access token with read permission for that endpoint.

Wiring it into Supabase

The edge function reads two secrets:

bash
supabase secrets set KRONOS_ENDPOINT_URL=https://xxxx.endpoints.huggingface.cloud
supabase secrets set KRONOS_HF_TOKEN=hf_xxxxxxxxxxxxxxxx

Then redeploy:

bash
supabase functions deploy finance --no-verify-jwt

Local testing

bash
cd src/api/kronos
pip install -r requirements.txt
# Also clone the Kronos repo and copy its `model/` directory next to handler.py
python handler.py    # runs the built-in smoke test on a sine wave

To test the wire format the way Supabase calls it:

bash
python -c "
import handler, json
h = handler.EndpointHandler()
bars = [{'timestamp': f'2024-01-{i+1:02d}', 'open':100,'high':101,'low':99,'close':100+i*0.1,'volume':1e6} for i in range(60)]
print(json.dumps(h({'inputs':{'history':bars,'pred_len':10,'n_samples':4}}), indent=2))
"

Tuning

Environment variables (set on the HF endpoint):

VarDefaultNotes
KRONOS_MODEL_REPONeoQuasar/Kronos-smallSwitch to Kronos-base for higher quality
KRONOS_TOKENIZER_REPONeoQuasar/Kronos-Tokenizer-baseMust match the model checkpoint
KRONOS_MAX_CONTEXT512Max history bars passed to the model
KRONOS_DEFAULT_PRED_LEN30Default forecast horizon if client omits
KRONOS_CI_LOWER0.1Lower quantile of confidence band
KRONOS_CI_UPPER0.9Upper quantile of confidence band

Cost

A T4 endpoint on HuggingFace is roughly $0.60/hr while running. Use the endpoint's auto-scale-to-zero feature so it spins down between requests; the first forecast after idle has a 30–60s cold start.