Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,119 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import joblib
|
| 3 |
-
import numpy as np
|
| 4 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
-
|
| 19 |
-
Uses separate feature sets for classification and regression.
|
| 20 |
"""
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
X_scaled_clf = scaler_clf.transform(df_clf)
|
| 26 |
-
pred1 = clf1.predict_proba(X_scaled_clf)
|
| 27 |
-
pred2 = clf2.predict_proba(X_scaled_clf)
|
| 28 |
-
pred3 = clf3.predict_proba(df_clf) # RF-like model doesn't need scaling
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
-
confidence = float(ensemble_proba[0, class_idx])
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
|
| 43 |
-
df_reg = pd.DataFrame([input_dict_reg])
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
pred_ridge = ridge_model_reg.predict(X_scaled_reg)
|
| 48 |
-
pred_rf = rf_model_reg.predict(df_reg)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
# Use the union of both feature sets so all required inputs are present
|
| 58 |
-
all_features = list(dict.fromkeys(feature_names_clf + feature_names_reg))
|
| 59 |
-
inputs = [gr.Number(label=col) for col in all_features]
|
| 60 |
|
| 61 |
demo = gr.Interface(
|
| 62 |
-
fn=
|
| 63 |
inputs=inputs,
|
| 64 |
-
outputs=
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
],
|
| 68 |
-
title="Conditional Classification → Regression",
|
| 69 |
-
description="Runs classification first; if class != 0, runs regression."
|
| 70 |
)
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib
|
| 5 |
+
|
| 6 |
+
# -----------------------------
|
| 7 |
+
# Config
|
| 8 |
+
# -----------------------------
|
| 9 |
+
THRESHOLD = 0.35 # tuned threshold
|
| 10 |
+
MODEL_PATH = "ensemble_model.pkl"
|
| 11 |
|
| 12 |
+
FEATURES = [
|
| 13 |
+
'month_sin', 'month_cos', 'dep_dayofweek', 'is_weekend',
|
| 14 |
+
'distance_bin_ord', 'season', 'part_of_day_ord',
|
| 15 |
+
'wind_speed_cat_ord', 'humidity_cat_ord', 'precip_cat_ord',
|
| 16 |
+
'pressure_cat_ord', 'late_aircraft_flag', 'is_congested',
|
| 17 |
+
'origin_3day_delay_rate', 'carrier_3day_delay_rate'
|
| 18 |
+
]
|
| 19 |
|
| 20 |
+
# -----------------------------
|
| 21 |
+
# Load model
|
| 22 |
+
# -----------------------------
|
| 23 |
+
model = joblib.load("main/ensemble_model_DEP_Class.pkl")
|
| 24 |
|
| 25 |
+
# -----------------------------
|
| 26 |
+
# Feature preparation
|
| 27 |
+
# -----------------------------
|
| 28 |
+
def prepare_features_from_simple_month(month, dep_dayofweek, is_weekend,
|
| 29 |
+
distance_bin_ord, season, part_of_day_ord,
|
| 30 |
+
wind_speed_cat_ord, humidity_cat_ord, precip_cat_ord,
|
| 31 |
+
pressure_cat_ord, late_aircraft_flag, is_congested,
|
| 32 |
+
origin_3day_delay_rate, carrier_3day_delay_rate):
|
| 33 |
"""
|
| 34 |
+
Take human-friendly month number and other features, return model-ready DataFrame.
|
|
|
|
| 35 |
"""
|
| 36 |
+
# Compute cyclical month encoding
|
| 37 |
+
month_sin = np.sin(2 * np.pi * month / 12)
|
| 38 |
+
month_cos = np.cos(2 * np.pi * month / 12)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Build dict in correct order
|
| 41 |
+
input_dict = {
|
| 42 |
+
'month_sin': month_sin,
|
| 43 |
+
'month_cos': month_cos,
|
| 44 |
+
'dep_dayofweek': dep_dayofweek,
|
| 45 |
+
'is_weekend': is_weekend,
|
| 46 |
+
'distance_bin_ord': distance_bin_ord,
|
| 47 |
+
'season': season,
|
| 48 |
+
'part_of_day_ord': part_of_day_ord,
|
| 49 |
+
'wind_speed_cat_ord': wind_speed_cat_ord,
|
| 50 |
+
'humidity_cat_ord': humidity_cat_ord,
|
| 51 |
+
'precip_cat_ord': precip_cat_ord,
|
| 52 |
+
'pressure_cat_ord': pressure_cat_ord,
|
| 53 |
+
'late_aircraft_flag': late_aircraft_flag,
|
| 54 |
+
'is_congested': is_congested,
|
| 55 |
+
'origin_3day_delay_rate': origin_3day_delay_rate,
|
| 56 |
+
'carrier_3day_delay_rate': carrier_3day_delay_rate
|
| 57 |
+
}
|
| 58 |
|
| 59 |
+
return pd.DataFrame([[input_dict[f] for f in FEATURES]], columns=FEATURES)
|
|
|
|
| 60 |
|
| 61 |
+
# -----------------------------
|
| 62 |
+
# Prediction function
|
| 63 |
+
# -----------------------------
|
| 64 |
+
def predict_delay_gradio(month, dep_dayofweek, is_weekend,
|
| 65 |
+
distance_bin_ord, season, part_of_day_ord,
|
| 66 |
+
wind_speed_cat_ord, humidity_cat_ord, precip_cat_ord,
|
| 67 |
+
pressure_cat_ord, late_aircraft_flag, is_congested,
|
| 68 |
+
origin_3day_delay_rate, carrier_3day_delay_rate):
|
| 69 |
+
# Prepare features
|
| 70 |
+
X_ready = prepare_features_from_simple_month(
|
| 71 |
+
month, dep_dayofweek, is_weekend,
|
| 72 |
+
distance_bin_ord, season, part_of_day_ord,
|
| 73 |
+
wind_speed_cat_ord, humidity_cat_ord, precip_cat_ord,
|
| 74 |
+
pressure_cat_ord, late_aircraft_flag, is_congested,
|
| 75 |
+
origin_3day_delay_rate, carrier_3day_delay_rate
|
| 76 |
+
)
|
| 77 |
|
| 78 |
+
# Predict probability
|
| 79 |
+
proba = model.predict_proba(X_ready)[:, 1][0]
|
|
|
|
| 80 |
|
| 81 |
+
# Apply threshold
|
| 82 |
+
pred = int(proba >= THRESHOLD)
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
return {
|
| 85 |
+
"Predicted_Class": "Delay" if pred == 1 else "On-time",
|
| 86 |
+
"Probability_of_Delay": round(float(proba), 3)
|
| 87 |
+
}
|
| 88 |
|
| 89 |
+
# -----------------------------
|
| 90 |
+
# Gradio UI
|
| 91 |
+
# -----------------------------
|
| 92 |
+
inputs = [
|
| 93 |
+
gr.Number(label="Month (1-12)"),
|
| 94 |
+
gr.Number(label="dep_dayofweek"),
|
| 95 |
+
gr.Number(label="is_weekend"),
|
| 96 |
+
gr.Number(label="distance_bin_ord"),
|
| 97 |
+
gr.Number(label="season"),
|
| 98 |
+
gr.Number(label="part_of_day_ord"),
|
| 99 |
+
gr.Number(label="wind_speed_cat_ord"),
|
| 100 |
+
gr.Number(label="humidity_cat_ord"),
|
| 101 |
+
gr.Number(label="precip_cat_ord"),
|
| 102 |
+
gr.Number(label="pressure_cat_ord"),
|
| 103 |
+
gr.Number(label="late_aircraft_flag"),
|
| 104 |
+
gr.Number(label="is_congested"),
|
| 105 |
+
gr.Number(label="origin_3day_delay_rate"),
|
| 106 |
+
gr.Number(label="carrier_3day_delay_rate")
|
| 107 |
+
]
|
| 108 |
|
| 109 |
+
outputs = gr.JSON(label="Prediction Result")
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
demo = gr.Interface(
|
| 112 |
+
fn=predict_delay_gradio,
|
| 113 |
inputs=inputs,
|
| 114 |
+
outputs=outputs,
|
| 115 |
+
title="Flight Delay Predictor",
|
| 116 |
+
description="Enter simple month number and other features to predict flight delay probability."
|
|
|
|
|
|
|
|
|
|
| 117 |
)
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|