Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# --- Given weights ---
|
| 6 |
+
weights = {
|
| 7 |
+
"GradientBoosting": 0.239,
|
| 8 |
+
"RandomForest": 0.573,
|
| 9 |
+
"XGBoost": 0.188
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
# --- Given model paths (place models in the same directory as app.py for HF Spaces) ---
|
| 13 |
+
model_paths = {
|
| 14 |
+
"GradientBoosting": "GradientBoosting_model.pkl",
|
| 15 |
+
"RandomForest": "RandomForest_model.pkl",
|
| 16 |
+
"XGBoost": "xgb_model.pkl"
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
# --- Load models ---
|
| 20 |
+
models = {name: joblib.load(path) for name, path in model_paths.items()}
|
| 21 |
+
|
| 22 |
+
# --- Prediction function ---
|
| 23 |
+
def predict(
|
| 24 |
+
dep_delay_x_congestion, dep_congestion, block_time_diff, enroute_delay,
|
| 25 |
+
dep_delay_rolling_mean, dep_congestion_roll3h, route_hour_delay_mean,
|
| 26 |
+
taxi_in_ratio, taxi_out_ratio, arr_congestion_roll3h, month_cos,
|
| 27 |
+
pres_delta, wspd_delta, arr_congestion, month_sin, season_wind,
|
| 28 |
+
season, distance_partofday, part_of_day_ord, segment_peak_hours,
|
| 29 |
+
wind_speed_cat_ord, humidity_cat_ord, prcp_delta, pressure_cat_ord,
|
| 30 |
+
distance_bin_ord
|
| 31 |
+
):
|
| 32 |
+
# Prepare input DataFrame
|
| 33 |
+
X_input = pd.DataFrame([{
|
| 34 |
+
'dep_delay_x_congestion': dep_delay_x_congestion,
|
| 35 |
+
'dep_congestion': dep_congestion,
|
| 36 |
+
'block_time_diff': block_time_diff,
|
| 37 |
+
'enroute_delay': enroute_delay,
|
| 38 |
+
'dep_delay_rolling_mean': dep_delay_rolling_mean,
|
| 39 |
+
'dep_congestion_roll3h': dep_congestion_roll3h,
|
| 40 |
+
'route_hour_delay_mean': route_hour_delay_mean,
|
| 41 |
+
'taxi_in_ratio': taxi_in_ratio,
|
| 42 |
+
'taxi_out_ratio': taxi_out_ratio,
|
| 43 |
+
'arr_congestion_roll3h': arr_congestion_roll3h,
|
| 44 |
+
'month_cos': month_cos,
|
| 45 |
+
'pres_delta': pres_delta,
|
| 46 |
+
'wspd_delta': wspd_delta,
|
| 47 |
+
'arr_congestion': arr_congestion,
|
| 48 |
+
'month_sin': month_sin,
|
| 49 |
+
'season_wind': season_wind,
|
| 50 |
+
'season': season,
|
| 51 |
+
'distance_partofday': distance_partofday,
|
| 52 |
+
'part_of_day_ord': part_of_day_ord,
|
| 53 |
+
'segment_peak_hours': segment_peak_hours,
|
| 54 |
+
'wind_speed_cat_ord': wind_speed_cat_ord,
|
| 55 |
+
'humidity_cat_ord': humidity_cat_ord,
|
| 56 |
+
'prcp_delta': prcp_delta,
|
| 57 |
+
'pressure_cat_ord': pressure_cat_ord,
|
| 58 |
+
'distance_bin_ord': distance_bin_ord
|
| 59 |
+
}])
|
| 60 |
+
|
| 61 |
+
# Get predictions from each model
|
| 62 |
+
preds = {name: models[name].predict(X_input)[0] for name in models}
|
| 63 |
+
|
| 64 |
+
# Weighted ensemble prediction
|
| 65 |
+
final_pred = sum(preds[name] * weights[name] for name in preds)
|
| 66 |
+
|
| 67 |
+
# Categorize delay
|
| 68 |
+
if 20 <= final_pred < 30:
|
| 69 |
+
delay_category = "Minimal Delay"
|
| 70 |
+
elif 30 <= final_pred <= 60:
|
| 71 |
+
delay_category = "Moderate Delay"
|
| 72 |
+
elif final_pred > 60:
|
| 73 |
+
delay_category = "Excessive Delay"
|
| 74 |
+
else:
|
| 75 |
+
delay_category = "No Significant Delay"
|
| 76 |
+
|
| 77 |
+
return preds, final_pred, delay_category
|
| 78 |
+
|
| 79 |
+
# --- Gradio interface ---
|
| 80 |
+
inputs = [
|
| 81 |
+
gr.Number(label="Departure Delay × Congestion Index"),
|
| 82 |
+
gr.Number(label="Departure Congestion"),
|
| 83 |
+
gr.Number(label="Block Time Difference (min)"),
|
| 84 |
+
gr.Number(label="En‑Route Delay (min)"),
|
| 85 |
+
gr.Number(label="Avg Departure Delay (last 3 flights)"),
|
| 86 |
+
gr.Number(label="Departure Congestion (3‑hour rolling)"),
|
| 87 |
+
gr.Number(label="Avg Delay for Route & Hour"),
|
| 88 |
+
gr.Number(label="Taxi‑In Time ÷ Block Time"),
|
| 89 |
+
gr.Number(label="Taxi‑Out Time ÷ Block Time"),
|
| 90 |
+
gr.Number(label="Arrival Congestion (3‑hour rolling)"),
|
| 91 |
+
gr.Number(label="Month (cosine encoding)"),
|
| 92 |
+
gr.Number(label="Pressure Change (Dest − Origin)"),
|
| 93 |
+
gr.Number(label="Wind Speed Change (Dest − Origin)"),
|
| 94 |
+
gr.Number(label="Arrival Congestion"),
|
| 95 |
+
gr.Number(label="Month (sine encoding)"),
|
| 96 |
+
gr.Number(label="Seasonal Wind Category"),
|
| 97 |
+
gr.Number(label="Season (ordinal)"),
|
| 98 |
+
gr.Number(label="Distance × Part of Day"),
|
| 99 |
+
gr.Number(label="Part of Day (ordinal)"),
|
| 100 |
+
gr.Number(label="Peak Hour Segment Flag"),
|
| 101 |
+
gr.Number(label="Wind Speed Category (ordinal)"),
|
| 102 |
+
gr.Number(label="Humidity Category (ordinal)"),
|
| 103 |
+
gr.Number(label="Precipitation Change (Dest − Origin)"),
|
| 104 |
+
gr.Number(label="Pressure Category (ordinal)"),
|
| 105 |
+
gr.Number(label="Distance Bin (ordinal)")
|
| 106 |
+
]
|
| 107 |
+
|
| 108 |
+
outputs = [
|
| 109 |
+
gr.JSON(label="Model Predictions"),
|
| 110 |
+
gr.Number(label="Weighted Ensemble Prediction (minutes)"),
|
| 111 |
+
gr.Textbox(label="Delay Category")
|
| 112 |
+
]
|
| 113 |
+
|
| 114 |
+
demo = gr.Interface(
|
| 115 |
+
fn=predict,
|
| 116 |
+
inputs=inputs,
|
| 117 |
+
outputs=outputs,
|
| 118 |
+
title="Flight Delay Prediction (Weighted Ensemble)",
|
| 119 |
+
description="Enter flight features to get predictions from GradientBoosting, RandomForest, and XGBoost, plus a weighted ensemble result and delay category."
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
demo.launch()
|