Spaces:
Sleeping
Sleeping
Aadithya commited on
Commit ·
fafe05e
1
Parent(s): 6e24bea
fix: comprehensive production stability fixes
Browse files- api/celery_app.py +0 -1
- api/main.py +1 -1
- supervisord.conf +1 -1
- worker/chronos_client.py +38 -13
api/celery_app.py
CHANGED
|
@@ -23,6 +23,5 @@ celery_app.conf.update(
|
|
| 23 |
worker_max_tasks_per_child=50, # Restart worker process every 50 tasks — prevents memory leaks
|
| 24 |
task_soft_time_limit=300, # Warn at 5 minutes
|
| 25 |
task_time_limit=600, # Hard kill at 10 minutes
|
| 26 |
-
worker_pool='solo',
|
| 27 |
broker_connection_retry_on_startup=True,
|
| 28 |
)
|
|
|
|
| 23 |
worker_max_tasks_per_child=50, # Restart worker process every 50 tasks — prevents memory leaks
|
| 24 |
task_soft_time_limit=300, # Warn at 5 minutes
|
| 25 |
task_time_limit=600, # Hard kill at 10 minutes
|
|
|
|
| 26 |
broker_connection_retry_on_startup=True,
|
| 27 |
)
|
api/main.py
CHANGED
|
@@ -47,7 +47,7 @@ async def health():
|
|
| 47 |
|
| 48 |
# Check if any Celery workers are alive
|
| 49 |
try:
|
| 50 |
-
ping_resp = celery_app.control.ping(timeout=
|
| 51 |
celery_status = 'connected' if ping_resp else 'disconnected'
|
| 52 |
except Exception:
|
| 53 |
celery_status = 'disconnected'
|
|
|
|
| 47 |
|
| 48 |
# Check if any Celery workers are alive
|
| 49 |
try:
|
| 50 |
+
ping_resp = celery_app.control.ping(timeout=3.0)
|
| 51 |
celery_status = 'connected' if ping_resp else 'disconnected'
|
| 52 |
except Exception:
|
| 53 |
celery_status = 'disconnected'
|
supervisord.conf
CHANGED
|
@@ -15,7 +15,7 @@ stderr_logfile=/dev/stderr
|
|
| 15 |
stderr_logfile_maxbytes=0
|
| 16 |
|
| 17 |
[program:celery]
|
| 18 |
-
command=python -m celery -A api.celery_app worker --loglevel=info --concurrency=
|
| 19 |
directory=/app
|
| 20 |
autostart=true
|
| 21 |
autorestart=true
|
|
|
|
| 15 |
stderr_logfile_maxbytes=0
|
| 16 |
|
| 17 |
[program:celery]
|
| 18 |
+
command=python -m celery -A api.celery_app worker --loglevel=info --concurrency=1
|
| 19 |
directory=/app
|
| 20 |
autostart=true
|
| 21 |
autorestart=true
|
worker/chronos_client.py
CHANGED
|
@@ -99,11 +99,24 @@ class LocalChronosClient(BaseChronosClient):
|
|
| 99 |
if self.pipeline is None:
|
| 100 |
from chronos import BaseChronosPipeline
|
| 101 |
logger.info(f'Lazy loading Chronos model ({self.model_name})...')
|
| 102 |
-
|
| 103 |
-
self.
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
_validate_forecast_request(prediction_length, num_samples)
|
| 109 |
context = _prepare_context(context_series, context_limit=512)
|
|
@@ -116,18 +129,28 @@ class LocalChronosClient(BaseChronosClient):
|
|
| 116 |
)
|
| 117 |
quantile_array = quantiles.detach().cpu().numpy().squeeze(0)
|
| 118 |
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
msg = (
|
| 121 |
"Chronos local response had unexpected quantile shape "
|
| 122 |
-
f"{quantile_array.shape}; expected ({prediction_length}, 3)"
|
|
|
|
| 123 |
)
|
| 124 |
raise RuntimeError(msg)
|
| 125 |
|
| 126 |
return {
|
| 127 |
-
"p10":
|
| 128 |
-
"p50":
|
| 129 |
-
"p90":
|
| 130 |
-
"quantile_samples": quantile_array.tolist(),
|
| 131 |
}
|
| 132 |
|
| 133 |
|
|
@@ -279,14 +302,16 @@ class ChronosClient(APIChronosClient):
|
|
| 279 |
|
| 280 |
def get_chronos_client() -> BaseChronosClient:
|
| 281 |
"""Create the configured Chronos client. Local mode is the default."""
|
| 282 |
-
|
| 283 |
-
|
|
|
|
| 284 |
|
| 285 |
mode = os.getenv("CHRONOS_MODE", "local").strip().lower()
|
| 286 |
logger.info("Chronos mode active: {}", mode)
|
| 287 |
|
| 288 |
if mode == "local":
|
| 289 |
model_name = os.getenv("CHRONOS_MODEL_NAME", "amazon/chronos-t5-base")
|
|
|
|
| 290 |
return LocalChronosClient(model_name=model_name)
|
| 291 |
|
| 292 |
if mode == "api":
|
|
|
|
| 99 |
if self.pipeline is None:
|
| 100 |
from chronos import BaseChronosPipeline
|
| 101 |
logger.info(f'Lazy loading Chronos model ({self.model_name})...')
|
| 102 |
+
try:
|
| 103 |
+
self.pipeline = BaseChronosPipeline.from_pretrained(
|
| 104 |
+
self.model_name,
|
| 105 |
+
device_map=self.device,
|
| 106 |
+
torch_dtype=torch.float32,
|
| 107 |
+
)
|
| 108 |
+
except Exception as e:
|
| 109 |
+
fallback = "amazon/chronos-t5-base"
|
| 110 |
+
logger.warning(
|
| 111 |
+
f"Failed to load model '{self.model_name}': {e}. "
|
| 112 |
+
f"Falling back to '{fallback}'."
|
| 113 |
+
)
|
| 114 |
+
self.model_name = fallback
|
| 115 |
+
self.pipeline = BaseChronosPipeline.from_pretrained(
|
| 116 |
+
fallback,
|
| 117 |
+
device_map=self.device,
|
| 118 |
+
torch_dtype=torch.float32,
|
| 119 |
+
)
|
| 120 |
|
| 121 |
_validate_forecast_request(prediction_length, num_samples)
|
| 122 |
context = _prepare_context(context_series, context_limit=512)
|
|
|
|
| 129 |
)
|
| 130 |
quantile_array = quantiles.detach().cpu().numpy().squeeze(0)
|
| 131 |
|
| 132 |
+
# Handle both (prediction_length, 3) and (3, prediction_length) shapes
|
| 133 |
+
if quantile_array.shape == (prediction_length, 3):
|
| 134 |
+
p10 = quantile_array[:, 0]
|
| 135 |
+
p50 = quantile_array[:, 1]
|
| 136 |
+
p90 = quantile_array[:, 2]
|
| 137 |
+
elif quantile_array.shape == (3, prediction_length):
|
| 138 |
+
p10 = quantile_array[0, :]
|
| 139 |
+
p50 = quantile_array[1, :]
|
| 140 |
+
p90 = quantile_array[2, :]
|
| 141 |
+
else:
|
| 142 |
msg = (
|
| 143 |
"Chronos local response had unexpected quantile shape "
|
| 144 |
+
f"{quantile_array.shape}; expected ({prediction_length}, 3) "
|
| 145 |
+
f"or (3, {prediction_length})"
|
| 146 |
)
|
| 147 |
raise RuntimeError(msg)
|
| 148 |
|
| 149 |
return {
|
| 150 |
+
"p10": p10,
|
| 151 |
+
"p50": p50,
|
| 152 |
+
"p90": p90,
|
| 153 |
+
"quantile_samples": quantile_array.tolist(),
|
| 154 |
}
|
| 155 |
|
| 156 |
|
|
|
|
| 302 |
|
| 303 |
def get_chronos_client() -> BaseChronosClient:
|
| 304 |
"""Create the configured Chronos client. Local mode is the default."""
|
| 305 |
+
# Do NOT call load_dotenv() here — in production, HF Spaces injects secrets
|
| 306 |
+
# as env vars. The .env file may contain stale local-dev paths that break
|
| 307 |
+
# model loading inside Docker.
|
| 308 |
|
| 309 |
mode = os.getenv("CHRONOS_MODE", "local").strip().lower()
|
| 310 |
logger.info("Chronos mode active: {}", mode)
|
| 311 |
|
| 312 |
if mode == "local":
|
| 313 |
model_name = os.getenv("CHRONOS_MODEL_NAME", "amazon/chronos-t5-base")
|
| 314 |
+
logger.info("Chronos model name resolved to: {}", model_name)
|
| 315 |
return LocalChronosClient(model_name=model_name)
|
| 316 |
|
| 317 |
if mode == "api":
|