Imp3rtinence commited on
Commit ·
a094858
1
Parent(s): cd51738
Add custom handler and requirements
Browse files- handler.py +26 -0
- requirements.txt +2 -0
handler.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from chronos import ChronosPipeline
|
| 3 |
+
|
| 4 |
+
class EndpointHandler:
|
| 5 |
+
def __init__(self, path=""):
|
| 6 |
+
self.pipeline = ChronosPipeline.from_pretrained(
|
| 7 |
+
path,
|
| 8 |
+
device_map="auto",
|
| 9 |
+
torch_dtype=torch.float32,
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
def __call__(self, data):
|
| 13 |
+
inputs = data.get("inputs", [])
|
| 14 |
+
parameters = data.get("parameters", {})
|
| 15 |
+
prediction_length = parameters.get("prediction_length", 8)
|
| 16 |
+
|
| 17 |
+
if isinstance(inputs[0], list):
|
| 18 |
+
closes = [candle[3] for candle in inputs]
|
| 19 |
+
else:
|
| 20 |
+
closes = inputs
|
| 21 |
+
|
| 22 |
+
context = torch.tensor([closes], dtype=torch.float32)
|
| 23 |
+
forecast = self.pipeline.predict(context, prediction_length=prediction_length, num_samples=20)
|
| 24 |
+
median_forecast = forecast.median(dim=1).values[0].tolist()
|
| 25 |
+
|
| 26 |
+
return {"predictions": median_forecast}
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
chronos-forecasting
|