Upload inference.py with huggingface_hub
Browse files- inference.py +6 -4
inference.py
CHANGED
|
@@ -37,6 +37,7 @@ def load_model_and_artifacts(
|
|
| 37 |
feature_names_path="feature_names.json",
|
| 38 |
config_path="model_config.json"
|
| 39 |
):
|
|
|
|
| 40 |
if not all(os.path.exists(path) for path in [model_path, scaler_path, feature_names_path, config_path]):
|
| 41 |
missing = [path for path in [model_path, scaler_path, feature_names_path, config_path] if not os.path.exists(path)]
|
| 42 |
raise FileNotFoundError(f"Missing files: {missing}")
|
|
@@ -63,6 +64,7 @@ def load_model_and_artifacts(
|
|
| 63 |
return model, scaler, feature_names, config
|
| 64 |
|
| 65 |
def predict(model, scaler, sequences):
|
|
|
|
| 66 |
device = torch.device("cpu")
|
| 67 |
model.to(device)
|
| 68 |
|
|
@@ -86,9 +88,9 @@ def predict(model, scaler, sequences):
|
|
| 86 |
rescaled = np.maximum(rescaled, 0)
|
| 87 |
rescaled = np.clip(rescaled, 3000, 19372) # Training sales range: $3069–19372
|
| 88 |
|
| 89 |
-
# Estimate uncertainty: std of predictions per sequence, repeated for each timestep
|
| 90 |
-
uncertainties = np.std(rescaled, axis=1, keepdims=True) + 100 # Shape: (batch_size, 1)
|
| 91 |
uncertainties = np.repeat(uncertainties, rescaled.shape[1], axis=1) # Shape: (batch_size, 13)
|
| 92 |
-
uncertainties = np.clip(uncertainties, 100,
|
| 93 |
|
| 94 |
-
return rescaled, uncertainties
|
|
|
|
| 37 |
feature_names_path="feature_names.json",
|
| 38 |
config_path="model_config.json"
|
| 39 |
):
|
| 40 |
+
"""Load model, scaler, feature names, and configuration."""
|
| 41 |
if not all(os.path.exists(path) for path in [model_path, scaler_path, feature_names_path, config_path]):
|
| 42 |
missing = [path for path in [model_path, scaler_path, feature_names_path, config_path] if not os.path.exists(path)]
|
| 43 |
raise FileNotFoundError(f"Missing files: {missing}")
|
|
|
|
| 64 |
return model, scaler, feature_names, config
|
| 65 |
|
| 66 |
def predict(model, scaler, sequences):
|
| 67 |
+
"""Generate 13-week sales forecasts from input sequences."""
|
| 68 |
device = torch.device("cpu")
|
| 69 |
model.to(device)
|
| 70 |
|
|
|
|
| 88 |
rescaled = np.maximum(rescaled, 0)
|
| 89 |
rescaled = np.clip(rescaled, 3000, 19372) # Training sales range: $3069–19372
|
| 90 |
|
| 91 |
+
# Estimate uncertainty: scaled std of predictions per sequence, repeated for each timestep
|
| 92 |
+
uncertainties = 0.2 * np.std(rescaled, axis=1, keepdims=True) + 100 # Shape: (batch_size, 1)
|
| 93 |
uncertainties = np.repeat(uncertainties, rescaled.shape[1], axis=1) # Shape: (batch_size, 13)
|
| 94 |
+
uncertainties = np.clip(uncertainties, 100, 1000) # Wider bounds to avoid constant clipping
|
| 95 |
|
| 96 |
+
return rescaled, uncertainties
|