vansh0003 commited on
Commit
29b54a1
·
verified ·
1 Parent(s): bc76a9b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -51
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
- # ===== Load Saved Models =====
7
- # Classification ensemble
8
- clf1, clf2, clf3, scaler_clf, weights_clf = joblib.load("main/ensemble_model_class.pkl")
9
- feature_names_clf = joblib.load("main/feature_names_class.pkl")
 
 
 
10
 
11
- # Regression ensemble
12
- linear_model_reg, ridge_model_reg, rf_model_reg, scaler_reg, weights_reg = joblib.load("main/ensemble_model.pkl")
13
- feature_names_reg = joblib.load("main/feature_names_regression.pkl")
 
14
 
15
- # ===== Prediction Function =====
16
- def classify_then_regress(*args):
 
 
 
 
 
 
17
  """
18
- Runs classification first; if class != 0, runs regression.
19
- Uses separate feature sets for classification and regression.
20
  """
21
- # --- Classification step ---
22
- input_dict_clf = dict(zip(feature_names_clf, args[:len(feature_names_clf)]))
23
- df_clf = pd.DataFrame([input_dict_clf])
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
- ensemble_proba = (pred1 * weights_clf[0] +
31
- pred2 * weights_clf[1] +
32
- pred3 * weights_clf[2])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- class_idx = int(np.argmax(ensemble_proba, axis=1)[0])
35
- confidence = float(ensemble_proba[0, class_idx])
36
 
37
- # If classification result is 0 → stop here
38
- if class_idx == 0:
39
- return f"Class {class_idx} (Confidence: {confidence:.2%})", "Skipped — class is 0"
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
- # --- Regression step ---
42
- input_dict_reg = dict(zip(feature_names_reg, args[:len(feature_names_reg)]))
43
- df_reg = pd.DataFrame([input_dict_reg])
44
 
45
- X_scaled_reg = scaler_reg.transform(df_reg)
46
- pred_linear = linear_model_reg.predict(X_scaled_reg)
47
- pred_ridge = ridge_model_reg.predict(X_scaled_reg)
48
- pred_rf = rf_model_reg.predict(df_reg)
49
 
50
- ensemble_pred = (pred_linear * weights_reg[0] +
51
- pred_ridge * weights_reg[1] +
52
- pred_rf * weights_reg[2])
 
53
 
54
- return f"Class {class_idx} (Confidence: {confidence:.2%})", float(ensemble_pred)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
- # ===== Build Gradio Inputs =====
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=classify_then_regress,
63
  inputs=inputs,
64
- outputs=[
65
- gr.Textbox(label="Classification Result"),
66
- gr.Textbox(label="Regression Prediction")
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__":