Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| import joblib | |
| # ----------------------------- | |
| # Config | |
| # ----------------------------- | |
| THRESHOLD = 0.35 # tuned threshold | |
| MODEL_PATH = "ensemble_model.pkl" | |
| FEATURES = [ | |
| 'month_sin', 'month_cos', 'dep_dayofweek', 'is_weekend', | |
| 'distance_bin_ord', 'season', 'part_of_day_ord', | |
| 'wind_speed_cat_ord', 'humidity_cat_ord', 'precip_cat_ord', | |
| 'pressure_cat_ord', 'late_aircraft_flag', 'is_congested', | |
| 'origin_3day_delay_rate', 'carrier_3day_delay_rate' | |
| ] | |
| # ----------------------------- | |
| # Load model | |
| # ----------------------------- | |
| model = joblib.load("main/ensemble_model_DEP_Class.pkl") | |
| # ----------------------------- | |
| # Feature preparation | |
| # ----------------------------- | |
| def prepare_features_from_simple_month(month, dep_dayofweek, is_weekend, | |
| distance_bin_ord, season, part_of_day_ord, | |
| wind_speed_cat_ord, humidity_cat_ord, precip_cat_ord, | |
| pressure_cat_ord, late_aircraft_flag, is_congested, | |
| origin_3day_delay_rate, carrier_3day_delay_rate): | |
| """ | |
| Take human-friendly month number and other features, return model-ready DataFrame. | |
| """ | |
| # Compute cyclical month encoding | |
| month_sin = np.sin(2 * np.pi * month / 12) | |
| month_cos = np.cos(2 * np.pi * month / 12) | |
| # Build dict in correct order | |
| input_dict = { | |
| 'month_sin': month_sin, | |
| 'month_cos': month_cos, | |
| 'dep_dayofweek': dep_dayofweek, | |
| 'is_weekend': is_weekend, | |
| 'distance_bin_ord': distance_bin_ord, | |
| 'season': season, | |
| 'part_of_day_ord': part_of_day_ord, | |
| 'wind_speed_cat_ord': wind_speed_cat_ord, | |
| 'humidity_cat_ord': humidity_cat_ord, | |
| 'precip_cat_ord': precip_cat_ord, | |
| 'pressure_cat_ord': pressure_cat_ord, | |
| 'late_aircraft_flag': late_aircraft_flag, | |
| 'is_congested': is_congested, | |
| 'origin_3day_delay_rate': origin_3day_delay_rate, | |
| 'carrier_3day_delay_rate': carrier_3day_delay_rate | |
| } | |
| return pd.DataFrame([[input_dict[f] for f in FEATURES]], columns=FEATURES) | |
| # ----------------------------- | |
| # Prediction function | |
| # ----------------------------- | |
| def predict_delay_gradio(month, dep_dayofweek, is_weekend, | |
| distance_bin_ord, season, part_of_day_ord, | |
| wind_speed_cat_ord, humidity_cat_ord, precip_cat_ord, | |
| pressure_cat_ord, late_aircraft_flag, is_congested, | |
| origin_3day_delay_rate, carrier_3day_delay_rate): | |
| # Prepare features | |
| X_ready = prepare_features_from_simple_month( | |
| month, dep_dayofweek, is_weekend, | |
| distance_bin_ord, season, part_of_day_ord, | |
| wind_speed_cat_ord, humidity_cat_ord, precip_cat_ord, | |
| pressure_cat_ord, late_aircraft_flag, is_congested, | |
| origin_3day_delay_rate, carrier_3day_delay_rate | |
| ) | |
| # Predict probability | |
| proba = model.predict_proba(X_ready)[:, 1][0] | |
| # Apply threshold | |
| pred = int(proba >= THRESHOLD) | |
| return { | |
| "Predicted_Class": "Delay" if pred == 1 else "On-time", | |
| "Probability_of_Delay": round(float(proba), 3) | |
| } | |
| # ----------------------------- | |
| # Gradio UI | |
| # ----------------------------- | |
| inputs = [ | |
| gr.Number(label="Month (1-12)"), | |
| gr.Number(label="dep_dayofweek"), | |
| gr.Number(label="is_weekend"), | |
| gr.Number(label="distance_bin_ord"), | |
| gr.Number(label="season"), | |
| gr.Number(label="part_of_day_ord"), | |
| gr.Number(label="wind_speed_cat_ord"), | |
| gr.Number(label="humidity_cat_ord"), | |
| gr.Number(label="precip_cat_ord"), | |
| gr.Number(label="pressure_cat_ord"), | |
| gr.Number(label="late_aircraft_flag"), | |
| gr.Number(label="is_congested"), | |
| gr.Number(label="origin_3day_delay_rate"), | |
| gr.Number(label="carrier_3day_delay_rate") | |
| ] | |
| outputs = gr.JSON(label="Prediction Result") | |
| demo = gr.Interface( | |
| fn=predict_delay_gradio, | |
| inputs=inputs, | |
| outputs=outputs, | |
| title="Flight Delay Predictor", | |
| description="Enter simple month number and other features to predict flight delay probability." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |