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
- Create a new model repo on HuggingFace, e.g.
<your-org>/aczen-kronos-handler. - Push the contents of this directory (
handler.py,requirements.txt) to that repo. Also include the Kronosmodel/directory from https://github.com/shiyu-coder/Kronos/tree/master/model sofrom model import Kronos, KronosTokenizer, KronosPredictorresolves. - Open the repo on HuggingFace β Deploy β Inference Endpoints.
- Pick a GPU instance (T4 is enough β a 30-day forecast with 30 samples runs in ~10β15s on T4).
- Once deployed, copy the endpoint URL (looks like
https://xxxx.us-east-1.aws.endpoints.huggingface.cloud) and a HF access token withreadpermission for that endpoint.
Wiring it into Supabase
The edge function reads two secrets:
supabase secrets set KRONOS_ENDPOINT_URL=https://xxxx.endpoints.huggingface.cloud
supabase secrets set KRONOS_HF_TOKEN=hf_xxxxxxxxxxxxxxxx
Then redeploy:
supabase functions deploy finance --no-verify-jwt
Local testing
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:
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):
| Var | Default | Notes |
|---|---|---|
KRONOS_MODEL_REPO |
NeoQuasar/Kronos-small |
Switch to Kronos-base for higher quality |
KRONOS_TOKENIZER_REPO |
NeoQuasar/Kronos-Tokenizer-base |
Must match the model checkpoint |
KRONOS_MAX_CONTEXT |
512 |
Max history bars passed to the model |
KRONOS_DEFAULT_PRED_LEN |
30 |
Default forecast horizon if client omits |
KRONOS_CI_LOWER |
0.1 |
Lower quantile of confidence band |
KRONOS_CI_UPPER |
0.9 |
Upper 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.