suryacz23/aczen-kronos
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 + CIDeploying 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_xxxxxxxxxxxxxxxxThen redeploy:
supabase functions deploy finance --no-verify-jwtLocal 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 waveTo 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):
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.
