Spaces:
Sleeping
Sleeping
Upload service.py
Browse files- service.py +30 -13
service.py
CHANGED
|
@@ -24,10 +24,11 @@ class TimesFmService:
|
|
| 24 |
)
|
| 25 |
self.backend = os.getenv("TIMESFM_BACKEND", "hf_cpu").strip() or "hf_cpu"
|
| 26 |
self.device = "cpu"
|
| 27 |
-
self.max_context_length = int(os.getenv("TIMESFM_MAX_CONTEXT_LENGTH", "512"))
|
| 28 |
-
self.max_horizon_step = int(os.getenv("TIMESFM_MAX_HORIZON_STEP", "288"))
|
| 29 |
-
self.
|
| 30 |
-
self.
|
|
|
|
| 31 |
self.min_required_points = int(os.getenv("TIMESFM_MIN_REQUIRED_POINTS", "32"))
|
| 32 |
self.allow_baseline_fallback = os.getenv("TIMESFM_ALLOW_BASELINE_FALLBACK", "false").lower() == "true"
|
| 33 |
|
|
@@ -101,10 +102,10 @@ class TimesFmService:
|
|
| 101 |
assert self._torch is not None
|
| 102 |
assert self._model is not None
|
| 103 |
|
| 104 |
-
torch = self._torch
|
| 105 |
-
context =
|
| 106 |
-
past_values = [torch.tensor(context, dtype=torch.float32)]
|
| 107 |
-
freq = torch.tensor([0], dtype=torch.long)
|
| 108 |
|
| 109 |
with torch.inference_mode():
|
| 110 |
outputs = self._model(
|
|
@@ -130,7 +131,22 @@ class TimesFmService:
|
|
| 130 |
pred_confidence=round(dense_conf[step - 1], 4),
|
| 131 |
)
|
| 132 |
)
|
| 133 |
-
return predictions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
def _timesfm_confidence(self, outputs: Any, dense_mean: list[float]) -> list[float]:
|
| 136 |
full_predictions = getattr(outputs, "full_predictions", None)
|
|
@@ -223,7 +239,8 @@ class TimesFmService:
|
|
| 223 |
"device": self.device,
|
| 224 |
"ready": self.ready,
|
| 225 |
"load_error": self.load_error,
|
| 226 |
-
"max_context_length": self.max_context_length,
|
| 227 |
-
"max_horizon_step": self.max_horizon_step,
|
| 228 |
-
"min_required_points": self.min_required_points,
|
| 229 |
-
|
|
|
|
|
|
| 24 |
)
|
| 25 |
self.backend = os.getenv("TIMESFM_BACKEND", "hf_cpu").strip() or "hf_cpu"
|
| 26 |
self.device = "cpu"
|
| 27 |
+
self.max_context_length = int(os.getenv("TIMESFM_MAX_CONTEXT_LENGTH", "512"))
|
| 28 |
+
self.max_horizon_step = int(os.getenv("TIMESFM_MAX_HORIZON_STEP", "288"))
|
| 29 |
+
self.patch_length = int(os.getenv("TIMESFM_PATCH_LENGTH", "32"))
|
| 30 |
+
self.confidence_floor = float(os.getenv("TIMESFM_CONFIDENCE_FLOOR", "0.20"))
|
| 31 |
+
self.confidence_ceiling = float(os.getenv("TIMESFM_CONFIDENCE_CEILING", "0.85"))
|
| 32 |
self.min_required_points = int(os.getenv("TIMESFM_MIN_REQUIRED_POINTS", "32"))
|
| 33 |
self.allow_baseline_fallback = os.getenv("TIMESFM_ALLOW_BASELINE_FALLBACK", "false").lower() == "true"
|
| 34 |
|
|
|
|
| 102 |
assert self._torch is not None
|
| 103 |
assert self._model is not None
|
| 104 |
|
| 105 |
+
torch = self._torch
|
| 106 |
+
context = self._aligned_hf_context(close_prices)
|
| 107 |
+
past_values = [torch.tensor(context, dtype=torch.float32)]
|
| 108 |
+
freq = torch.tensor([0], dtype=torch.long)
|
| 109 |
|
| 110 |
with torch.inference_mode():
|
| 111 |
outputs = self._model(
|
|
|
|
| 131 |
pred_confidence=round(dense_conf[step - 1], 4),
|
| 132 |
)
|
| 133 |
)
|
| 134 |
+
return predictions
|
| 135 |
+
|
| 136 |
+
def _aligned_hf_context(self, close_prices: list[float]) -> list[float]:
|
| 137 |
+
context = close_prices[-self.max_context_length :]
|
| 138 |
+
usable_length = len(context)
|
| 139 |
+
|
| 140 |
+
if self.patch_length > 0:
|
| 141 |
+
usable_length = (usable_length // self.patch_length) * self.patch_length
|
| 142 |
+
|
| 143 |
+
if usable_length < self.min_required_points:
|
| 144 |
+
raise RuntimeError(
|
| 145 |
+
f"TimesFM usable context length {usable_length} is below "
|
| 146 |
+
f"TIMESFM_MIN_REQUIRED_POINTS={self.min_required_points}"
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
return context[-usable_length:]
|
| 150 |
|
| 151 |
def _timesfm_confidence(self, outputs: Any, dense_mean: list[float]) -> list[float]:
|
| 152 |
full_predictions = getattr(outputs, "full_predictions", None)
|
|
|
|
| 239 |
"device": self.device,
|
| 240 |
"ready": self.ready,
|
| 241 |
"load_error": self.load_error,
|
| 242 |
+
"max_context_length": self.max_context_length,
|
| 243 |
+
"max_horizon_step": self.max_horizon_step,
|
| 244 |
+
"min_required_points": self.min_required_points,
|
| 245 |
+
"patch_length": self.patch_length,
|
| 246 |
+
}
|