-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquickstart_server.py
More file actions
197 lines (162 loc) · 6.12 KB
/
Copy pathquickstart_server.py
File metadata and controls
197 lines (162 loc) · 6.12 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
"""Sheaf multi-model server quickstart.
Deploys three heterogeneous model backends on the same Ray Serve instance
and queries each via HTTP — the core value proposition of sheaf.
Models used (all CPU-friendly, no GPU required):
- time_series : amazon/chronos-bolt-tiny (~80 MB)
- tabular : TabPFN v2 (requires TABPFN_TOKEN env var)
- audio : openai/whisper-tiny (~150 MB)
Usage:
pip install "sheaf-serve[time-series,tabular,audio]"
export TABPFN_TOKEN=<your-token> # https://ux.priorlabs.ai
python examples/quickstart_server.py
Each deployment gets its own URL:
POST http://127.0.0.1:8000/forecaster/predict
POST http://127.0.0.1:8000/classifier/predict
POST http://127.0.0.1:8000/transcriber/predict
"""
from __future__ import annotations
import base64
import pathlib
import time
import requests
from sheaf import ModelServer, ModelSpec
from sheaf.api.base import ModelType
from sheaf.spec import ResourceConfig
# ---------------------------------------------------------------------------
# 1. Declare what to serve
# ---------------------------------------------------------------------------
forecaster_spec = ModelSpec(
name="forecaster",
model_type=ModelType.TIME_SERIES,
backend="chronos2",
backend_kwargs={
"model_id": "amazon/chronos-bolt-tiny",
"device_map": "cpu",
"torch_dtype": "float32",
},
resources=ResourceConfig(num_cpus=1, replicas=1),
)
classifier_spec = ModelSpec(
name="classifier",
model_type=ModelType.TABULAR,
backend="tabpfn",
resources=ResourceConfig(num_cpus=1, replicas=1),
)
transcriber_spec = ModelSpec(
name="transcriber",
model_type=ModelType.AUDIO,
backend="whisper",
backend_kwargs={"model_size": "tiny"},
resources=ResourceConfig(num_cpus=1, replicas=1),
)
# ---------------------------------------------------------------------------
# 2. Start the server
# ---------------------------------------------------------------------------
server = ModelServer(
models=[forecaster_spec, classifier_spec, transcriber_spec],
host="127.0.0.1",
port=8000,
)
print("Starting Ray Serve... (first run downloads model weights)")
server.run()
BASE = "http://127.0.0.1:8000"
def _wait_ready(name: str, timeout: int = 120) -> None:
deadline = time.time() + timeout
while time.time() < deadline:
try:
r = requests.get(f"{BASE}/{name}/health", timeout=2)
if r.status_code == 200:
print(f" {name}: ready")
return
except requests.exceptions.ConnectionError:
pass
time.sleep(1)
raise TimeoutError(f"{name} did not become ready within {timeout}s")
print("\nWaiting for deployments...")
for deployment in ("forecaster", "classifier", "transcriber"):
_wait_ready(deployment)
# ---------------------------------------------------------------------------
# 3. Time series forecast
# ---------------------------------------------------------------------------
print("\n--- Time Series Forecast ---")
ts_payload = {
"model_type": "time_series",
"model_name": "forecaster",
"history": [312, 298, 275, 260, 255, 263, 285, 320, 368, 402, 421, 435],
"horizon": 6,
"frequency": "1h",
"output_mode": "quantiles",
"quantile_levels": [0.1, 0.5, 0.9],
}
r = requests.post(f"{BASE}/forecaster/predict", json=ts_payload)
r.raise_for_status()
ts = r.json()
print(f"{'Hour':>5} {'P10':>7} {'Median':>7} {'P90':>7}")
print("-" * 35)
p10 = ts["quantiles"]["0.1"]
p50 = ts["quantiles"]["0.5"]
p90 = ts["quantiles"]["0.9"]
for i in range(ts["horizon"]):
print(f"{i + 1:>5} {p10[i]:>7.1f} {p50[i]:>7.1f} {p90[i]:>7.1f}")
# ---------------------------------------------------------------------------
# 4. Tabular classification
# ---------------------------------------------------------------------------
print("\n--- Tabular Classification (Iris) ---")
# Iris dataset: 3 features (sepal_length, sepal_width, petal_length)
# Context = 6 labeled examples (2 per class); query = 2 unseen rows
tab_payload = {
"model_type": "tabular",
"model_name": "classifier",
"context_X": [
[5.1, 3.5, 1.4], # setosa
[4.9, 3.0, 1.4], # setosa
[7.0, 3.2, 4.7], # versicolor
[6.4, 3.2, 4.5], # versicolor
[6.3, 3.3, 6.0], # virginica
[5.8, 2.7, 5.1], # virginica
],
"context_y": [0, 0, 1, 1, 2, 2],
"query_X": [
[5.0, 3.4, 1.5], # likely setosa
[6.1, 2.8, 4.7], # likely versicolor
],
"task": "classification",
"output_mode": "probabilities",
}
r = requests.post(f"{BASE}/classifier/predict", json=tab_payload)
r.raise_for_status()
tab = r.json()
species = {0: "setosa", 1: "versicolor", 2: "virginica"}
for i, (pred, probs) in enumerate(zip(tab["predictions"], tab["probabilities"])):
label = species.get(int(pred), str(pred))
prob_str = " ".join(f"{p:.2f}" for p in probs)
print(f" Query {i + 1}: predicted={label} P(0|1|2)=[{prob_str}]")
# ---------------------------------------------------------------------------
# 5. Audio transcription
# ---------------------------------------------------------------------------
print("\n--- Audio Transcription ---")
sample_wav = pathlib.Path(__file__).parent / "sample.wav"
if not sample_wav.exists():
print(f" sample.wav not found at {sample_wav} — skipping transcription demo")
else:
audio_b64 = base64.b64encode(sample_wav.read_bytes()).decode()
audio_payload = {
"model_type": "audio",
"model_name": "transcriber",
"audio_b64": audio_b64,
"language": "en",
"task": "transcribe",
}
r = requests.post(f"{BASE}/transcriber/predict", json=audio_payload)
r.raise_for_status()
aud = r.json()
print(f" Text : {aud['text'].strip()}")
print(f" Language : {aud['language']}")
if aud.get("duration"):
print(f" Duration : {aud['duration']:.2f}s")
# ---------------------------------------------------------------------------
# 6. Shut down
# ---------------------------------------------------------------------------
print("\nShutting down Ray Serve...")
server.shutdown()
print("Done.")