-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
404 lines (334 loc) · 16.1 KB
/
Copy pathapi.py
File metadata and controls
404 lines (334 loc) · 16.1 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import os
import pandas as pd
import numpy as np
import io
import base64
import traceback
from flask import Flask, jsonify, request
from flask_cors import CORS
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
from data_loader import DataLoader
from feature_engineering import FeatureEngineer
from forecast_model import ForecastModel
from event_module import EventModule
from green_score import GreenScore
from explainability import ExplainabilityModule
app = Flask(__name__)
CORS(app)
# ─── Global In-Memory State ────────────────────────────────────────────────────
state = {
'model': None,
'feature_names': None,
'processed_df': None,
'raw_df': None,
'train_data': None,
'metrics': None,
'is_trained': False,
}
DEFAULT_API_KEY = 'b736fbf9d44443ee9dd83589303e75a8'
# ─── Helpers ───────────────────────────────────────────────────────────────────
def _get_shap_base64():
if not state['is_trained']:
return None
try:
explainer = ExplainabilityModule(state['model'].model)
fig = explainer.plot_shap_summary(state['train_data'])
buf = io.BytesIO()
fig.savefig(buf, format='png', bbox_inches='tight', dpi=100)
buf.seek(0)
img_b64 = base64.b64encode(buf.read()).decode('utf-8')
plt.close(fig)
return img_b64
except Exception:
return None
def _rolling_forecast_to_date(future_price, future_holiday, future_promo, target_end_date):
"""
Recursive multi-step forecast.
Starts from the last row in processed_df, predicts weekly until target_end_date.
Each week's predicted sales become Lag_1 for the next week — true rolling forecast.
Returns list of dicts:
{ date, predicted_sales, conf_low, conf_high, month, week_label }
"""
processed_df = state['processed_df']
feature_names = state['feature_names']
model = state['model']
last_row = processed_df.iloc[-1]
last_actual_date = pd.to_datetime(last_row['Date'])
# Seed from actual history
recent_sales = list(processed_df['Weekly_Sales_Units'].iloc[-4:].astype(float))
lag_1 = float(last_row['Weekly_Sales_Units'])
lag_2 = float(last_row.get('Lag_1', last_row['Weekly_Sales_Units']))
# Static features (from last known row, overriding price/flags from user)
base_feats = {
'Unit_Price_USD': float(future_price),
'Unit_Cost_USD': float(last_row.get('Unit_Cost_USD', 0)),
'Current_Inventory_Units': float(last_row.get('Current_Inventory_Units', 0)),
'Transport_Distance_km': float(last_row.get('Transport_Distance_km', 0)),
'Fuel_Price_Index': float(last_row.get('Fuel_Price_Index', 0)),
'Holiday_Flag': 1 if future_holiday else 0,
'Promotion_Flag': 1 if future_promo else 0,
}
# Fill any remaining model features from last known row
for feat in feature_names:
if feat not in base_feats and feat not in ('Month', 'Week_Number', 'Lag_1', 'Lag_2', 'Rolling_Mean_4'):
val = last_row.get(feat, 0)
base_feats[feat] = float(val) if val is not None else 0.0
forecasts = []
current_date = last_actual_date + pd.Timedelta(weeks=1)
while current_date.date() <= target_end_date:
rolling_mean_4 = float(np.mean(recent_sales[-4:])) if len(recent_sales) >= 4 else float(np.mean(recent_sales))
input_data = {
**base_feats,
'Month': int(current_date.month),
'Week_Number': int(current_date.isocalendar().week),
'Lag_1': lag_1,
'Lag_2': lag_2,
'Rolling_Mean_4': rolling_mean_4,
}
param_df = pd.DataFrame([input_data])[feature_names]
prediction = float(model.predict(param_df)[0])
conf_low, conf_high = model.get_confidence_interval(prediction)
forecasts.append({
'date': str(current_date.date()),
'predicted_sales': round(prediction, 2),
'conf_low': round(float(conf_low), 2),
'conf_high': round(float(conf_high), 2),
'month': int(current_date.month),
'week_label': current_date.strftime('Week of %b %d'),
})
# Roll forward state
lag_2 = lag_1
lag_1 = prediction
recent_sales.append(prediction)
if len(recent_sales) > 8:
recent_sales = recent_sales[-8:]
current_date += pd.Timedelta(weeks=1)
return forecasts
# ─── Routes ────────────────────────────────────────────────────────────────────
@app.route('/api/status', methods=['GET'])
def status():
return jsonify({'is_trained': state['is_trained']})
@app.route('/api/upload_and_train', methods=['POST'])
def upload_and_train():
"""
Accepts either:
- multipart file upload (key="file")
- or falls back to default local data files
Trains the model and returns metrics + data preview.
"""
file_path = None
if 'file' in request.files:
f = request.files['file']
ext = f.filename.rsplit('.', 1)[-1].lower()
file_path = f'temp_data.{ext}'
f.save(file_path)
else:
for fp in ['temp_data.csv', 'temp_data.xlsx', 'Walmart.csv', '../Walmart.csv']:
if os.path.exists(fp):
file_path = fp
break
if not file_path or not os.path.exists(file_path):
return jsonify({'error': 'No data file found. Please upload a file.'}), 400
try:
loader = DataLoader(file_path)
raw_df = loader.load_data()
fe = FeatureEngineer()
processed_df = fe.create_features(raw_df)
X_train, X_test, y_train, y_test, feature_names = fe.split_data(processed_df)
model = ForecastModel()
model.train(X_train[feature_names], y_train)
metrics = model.evaluate(X_test[feature_names], y_test)
state.update({
'model': model,
'feature_names': feature_names,
'processed_df': processed_df,
'raw_df': raw_df,
'train_data': X_train[feature_names],
'metrics': metrics,
'is_trained': True,
})
preview = raw_df.tail(5).to_dict(orient='records')
for row in preview:
for k, v in row.items():
if hasattr(v, 'item'):
row[k] = v.item()
elif isinstance(v, float) and np.isnan(v):
row[k] = None
last_date = str(raw_df['Date'].max().date())
return jsonify({
'status': 'success',
'rows': len(raw_df),
'date_range': {'start': str(raw_df['Date'].min().date()), 'end': last_date},
'metrics': {k: round(float(v), 2) for k, v in metrics.items()},
'preview': preview,
'columns': raw_df.columns.tolist(),
'avg_price': round(float(raw_df['Unit_Price_USD'].mean()), 2),
'avg_inventory': round(float(raw_df['Current_Inventory_Units'].mean()), 2),
'last_data_date': last_date,
})
except Exception as e:
traceback.print_exc()
return jsonify({'error': str(e)}), 500
@app.route('/api/forecast', methods=['POST'])
def forecast():
"""
Rolling multi-week forecast from the last historical data point through
all remaining weeks of March 2026.
Each predicted week's sales value is fed back as Lag_1 for the next week,
making this a true recursive multi-step forecast rather than a single-step
next-week prediction.
Inputs (JSON):
- future_price float - price to apply to all forecast weeks
- future_holiday bool - holiday flag for forecast weeks
- future_promo bool - promo flag for forecast weeks
- api_key str - NewsAPI key (optional)
Returns:
- weekly_forecasts: per-week breakdown through March 2026
- march_summary: totals and averages across March weeks
- chart: full timeline (8 historical + all forecast weeks)
- event, green, shap_image
"""
if not state['is_trained']:
return jsonify({'error': 'Model not trained yet. Please upload data first.'}), 400
req = request.json or {}
future_price = float(req.get('future_price', float(state['raw_df']['Unit_Price_USD'].mean())))
future_holiday = bool(req.get('future_holiday', False))
future_promo = bool(req.get('future_promo', False))
api_key = req.get('api_key', DEFAULT_API_KEY)
try:
processed_df = state['processed_df']
last_row = processed_df.iloc[-1]
last_actual_date = pd.to_datetime(last_row['Date'])
# Determine target end date: always project through end of March 2026
target_end = pd.Timestamp('2026-03-31').date()
# If data already covers March, still project forward 5 weeks as a fallback
if last_actual_date.date() > pd.Timestamp('2026-03-31').date():
target_end = (last_actual_date + pd.Timedelta(weeks=5)).date()
# ── Generate rolling weekly forecasts ─────────────────────────────────
weekly_forecasts = _rolling_forecast_to_date(
future_price, future_holiday, future_promo, target_end
)
if not weekly_forecasts:
return jsonify({
'error': 'No forecast weeks to generate. Historical data already extends through the target period.'
}), 400
# ── Identify March 2026 weeks ─────────────────────────────────────────
march_weeks = [w for w in weekly_forecasts if w['month'] == 3]
display_weeks = march_weeks if march_weeks else weekly_forecasts
# Headline = first March week (or first predicted week)
headline = display_weeks[0]
base_forecast = headline['predicted_sales']
conf_low_h = headline['conf_low']
conf_high_h = headline['conf_high']
# Aggregate March stats
march_sales = [w['predicted_sales'] for w in display_weeks]
march_total = sum(march_sales)
march_avg = march_total / len(march_sales) if march_sales else 0
# ── Chart data: 8 historical actuals + all forecast weeks ─────────────
last_n = processed_df.iloc[-8:]
chart_dates = [str(d.date()) for d in last_n['Date']]
chart_sales = [float(x) for x in last_n['Weekly_Sales_Units']]
chart_types = ['actual'] * len(last_n)
for w in weekly_forecasts:
chart_dates.append(w['date'])
chart_sales.append(w['predicted_sales'])
# 'bridge' = weeks between last data and March; 'forecast' = March weeks
chart_types.append('forecast' if w['month'] == 3 else 'bridge')
# ── Event adjustment ──────────────────────────────────────────────────
event_mod = EventModule(api_key)
news_texts = event_mod.fetch_news()
impact = event_mod.compute_event_impact_score(news_texts)
alpha = 0.2
adj_factor = 1 + (alpha * impact['adjusted_effect'])
adjusted_forecast = float(base_forecast * adj_factor)
adjustment_pct = round(float(impact['adjusted_effect']) * 20, 2)
march_adj_total = round(march_total * adj_factor, 2)
# ── Green score ───────────────────────────────────────────────────────
transport_km = float(last_row.get('Transport_Distance_km', 0))
fuel_index = float(last_row.get('Fuel_Price_Index', 0))
current_inventory = float(last_row.get('Current_Inventory_Units', 0))
gs = GreenScore()
green_score = gs.compute_score(transport_km, fuel_index, current_inventory, adjusted_forecast)
recommendation = gs.get_recommendation(green_score)
# ── SHAP ─────────────────────────────────────────────────────────────
shap_b64 = _get_shap_base64()
return jsonify({
'status': 'success',
# Headline (first March week)
'future_date': headline['date'],
'base_forecast': round(base_forecast, 2),
'conf_low': round(conf_low_h, 2),
'conf_high': round(conf_high_h, 2),
'adjusted_forecast': round(adjusted_forecast, 2),
'adjustment_pct': adjustment_pct,
# Full weekly breakdown
'weekly_forecasts': weekly_forecasts,
# March 2026 summary
'march_summary': {
'total': round(march_total, 0),
'average_weekly': round(march_avg, 0),
'weeks_count': len(display_weeks),
'adjusted_total': round(march_adj_total, 0),
'last_data_date': str(last_actual_date.date()),
},
# Chart
'chart': {
'dates': chart_dates,
'sales': chart_sales,
'types': chart_types,
},
# Event
'event': {
'raw_score': round(float(impact['score']), 3),
'type': impact['type'],
'sensitivity': impact['sensitivity'],
'direction': impact['direction'],
'interpretation': impact['interpretation'],
'news': news_texts[:5] if news_texts else [],
},
# Green
'green': {
'score': float(green_score),
'recommendation': recommendation,
'transport_km': transport_km,
'fuel_index': fuel_index,
'inventory': current_inventory,
},
'shap_image': shap_b64,
})
except Exception as e:
traceback.print_exc()
return jsonify({'error': str(e)}), 500
@app.route('/api/metrics', methods=['GET'])
def get_metrics():
if not state['is_trained']:
return jsonify({'error': 'Not trained'}), 400
m = state['metrics']
return jsonify({k: round(float(v), 2) for k, v in m.items()})
if __name__ == '__main__':
# Attempt to preload if a local data file exists
with app.app_context():
for fp in ['temp_data.csv', 'temp_data.xlsx', 'Walmart.csv', '../Walmart.csv']:
if os.path.exists(fp):
try:
loader = DataLoader(fp)
raw_df = loader.load_data()
fe = FeatureEngineer()
processed_df = fe.create_features(raw_df)
X_train, X_test, y_train, y_test, feature_names = fe.split_data(processed_df)
model = ForecastModel()
model.train(X_train[feature_names], y_train)
metrics = model.evaluate(X_test[feature_names], y_test)
state.update({
'model': model, 'feature_names': feature_names,
'processed_df': processed_df, 'raw_df': raw_df,
'train_data': X_train[feature_names],
'metrics': metrics, 'is_trained': True,
})
print(f"[API] Auto-loaded & trained from {fp}")
break
except Exception as e:
print(f"[API] Preload failed: {e}")
app.run(port=5000, debug=True, use_reloader=False)