Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -47,16 +47,15 @@ def predict_dynamic_price(
|
|
| 47 |
is_holiday, is_festival
|
| 48 |
):
|
| 49 |
|
| 50 |
-
#
|
| 51 |
weather_factor, temperature = get_weather_features()
|
| 52 |
|
| 53 |
-
#
|
| 54 |
base_price = FIXED_FARE + (distance_km * RATE_PER_KM)
|
| 55 |
|
| 56 |
-
#
|
| 57 |
row = {f: 0.0 for f in FEATURES}
|
| 58 |
|
| 59 |
-
# --- Fill primary inputs ---
|
| 60 |
inputs = {
|
| 61 |
"zone_id": zone_id,
|
| 62 |
"hour": hour,
|
|
@@ -80,51 +79,47 @@ def predict_dynamic_price(
|
|
| 80 |
if k in row:
|
| 81 |
row[k] = float(v)
|
| 82 |
|
| 83 |
-
#
|
| 84 |
-
#
|
| 85 |
-
#
|
| 86 |
ratio = (demand + 1) / (supply + 1)
|
| 87 |
row["demand_supply_ratio"] = np.clip(ratio, 0, 50)
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
#
|
| 91 |
-
#
|
| 92 |
if "season_winter" in row:
|
| 93 |
row["season_winter"] = 0
|
| 94 |
if "season_summer" in row:
|
| 95 |
-
row["season_summer"] = 1
|
| 96 |
if "season_monsoon" in row:
|
| 97 |
row["season_monsoon"] = 0
|
| 98 |
if "season_autumn" in row:
|
| 99 |
row["season_autumn"] = 0
|
| 100 |
|
| 101 |
-
#
|
| 102 |
df_row = pd.DataFrame([[row[f] for f in FEATURES]], columns=FEATURES)
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
surge = float(model.predict(df_row)[0])
|
| 106 |
-
|
| 107 |
-
# =========================================================
|
| 108 |
-
# ADD EXTRA RESPONSE FROM DEMAND/SUPPLY (if model too flat)
|
| 109 |
-
# =========================================================
|
| 110 |
surge = float(model.predict(df_row)[0])
|
| 111 |
|
| 112 |
-
#
|
|
|
|
|
|
|
| 113 |
surge += 0.15 * (ratio - 1)
|
| 114 |
|
| 115 |
# =====================================================
|
| 116 |
-
# DRIVER AVAILABILITY EFFECT (
|
| 117 |
# =====================================================
|
| 118 |
driver_factor = (supply + 1) / (driver_availability + 1)
|
|
|
|
| 119 |
|
| 120 |
-
#
|
| 121 |
-
# If many drivers → driver_factor < 1 → surge decreases
|
| 122 |
-
|
| 123 |
-
surge += 0.10 * (driver_factor - 1)
|
| 124 |
-
|
| 125 |
-
# Final stability clip
|
| 126 |
surge = np.clip(surge, 1.0, 3.5)
|
| 127 |
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
|
| 130 |
# ===================== UI =====================
|
|
@@ -154,8 +149,7 @@ demo = gr.Interface(
|
|
| 154 |
fn=predict_dynamic_price,
|
| 155 |
inputs=inputs,
|
| 156 |
outputs=outputs,
|
| 157 |
-
title="Dynamic Pricing (
|
| 158 |
)
|
| 159 |
|
| 160 |
demo.launch()
|
| 161 |
-
|
|
|
|
| 47 |
is_holiday, is_festival
|
| 48 |
):
|
| 49 |
|
| 50 |
+
# ---- WEATHER ----
|
| 51 |
weather_factor, temperature = get_weather_features()
|
| 52 |
|
| 53 |
+
# ---- BASE FARE ----
|
| 54 |
base_price = FIXED_FARE + (distance_km * RATE_PER_KM)
|
| 55 |
|
| 56 |
+
# ---- BUILD FEATURE VECTOR ----
|
| 57 |
row = {f: 0.0 for f in FEATURES}
|
| 58 |
|
|
|
|
| 59 |
inputs = {
|
| 60 |
"zone_id": zone_id,
|
| 61 |
"hour": hour,
|
|
|
|
| 79 |
if k in row:
|
| 80 |
row[k] = float(v)
|
| 81 |
|
| 82 |
+
# =====================================================
|
| 83 |
+
# DEMAND vs SUPPLY EFFECT
|
| 84 |
+
# =====================================================
|
| 85 |
ratio = (demand + 1) / (supply + 1)
|
| 86 |
row["demand_supply_ratio"] = np.clip(ratio, 0, 50)
|
| 87 |
|
| 88 |
+
# =====================================================
|
| 89 |
+
# SEASON FIX (avoid zero vector)
|
| 90 |
+
# =====================================================
|
| 91 |
if "season_winter" in row:
|
| 92 |
row["season_winter"] = 0
|
| 93 |
if "season_summer" in row:
|
| 94 |
+
row["season_summer"] = 1
|
| 95 |
if "season_monsoon" in row:
|
| 96 |
row["season_monsoon"] = 0
|
| 97 |
if "season_autumn" in row:
|
| 98 |
row["season_autumn"] = 0
|
| 99 |
|
| 100 |
+
# ---- Create dataframe ----
|
| 101 |
df_row = pd.DataFrame([[row[f] for f in FEATURES]], columns=FEATURES)
|
| 102 |
|
| 103 |
+
# ---- MODEL PREDICTION ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
surge = float(model.predict(df_row)[0])
|
| 105 |
|
| 106 |
+
# =====================================================
|
| 107 |
+
# EXTRA RESPONSE: DEMAND vs SUPPLY
|
| 108 |
+
# =====================================================
|
| 109 |
surge += 0.15 * (ratio - 1)
|
| 110 |
|
| 111 |
# =====================================================
|
| 112 |
+
# DRIVER AVAILABILITY EFFECT (STRONG)
|
| 113 |
# =====================================================
|
| 114 |
driver_factor = (supply + 1) / (driver_availability + 1)
|
| 115 |
+
surge += 0.20 * (driver_factor - 1)
|
| 116 |
|
| 117 |
+
# ---- Stability ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
surge = np.clip(surge, 1.0, 3.5)
|
| 119 |
|
| 120 |
+
final_price = base_price * surge
|
| 121 |
+
|
| 122 |
+
return round(base_price, 2), round(surge, 3), round(final_price, 2)
|
| 123 |
|
| 124 |
|
| 125 |
# ===================== UI =====================
|
|
|
|
| 149 |
fn=predict_dynamic_price,
|
| 150 |
inputs=inputs,
|
| 151 |
outputs=outputs,
|
| 152 |
+
title="Dynamic Pricing (Fully Responsive)"
|
| 153 |
)
|
| 154 |
|
| 155 |
demo.launch()
|
|
|