Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ FEATURES = artifact["features"]
|
|
| 12 |
FIXED_FARE = artifact["fixed_fare"]
|
| 13 |
RATE_PER_KM = artifact["rate_per_km"]
|
| 14 |
|
| 15 |
-
# ===================== WEATHER
|
| 16 |
API_KEY = "YOUR_API_KEY"
|
| 17 |
CITY = "Visakhapatnam,IN"
|
| 18 |
|
|
@@ -47,14 +47,16 @@ def predict_dynamic_price(
|
|
| 47 |
is_holiday, is_festival
|
| 48 |
):
|
| 49 |
|
|
|
|
| 50 |
weather_factor, temperature = get_weather_features()
|
| 51 |
|
|
|
|
| 52 |
base_price = FIXED_FARE + (distance_km * RATE_PER_KM)
|
| 53 |
|
| 54 |
-
# Initialize
|
| 55 |
row = {f: 0.0 for f in FEATURES}
|
| 56 |
|
| 57 |
-
# Fill
|
| 58 |
inputs = {
|
| 59 |
"zone_id": zone_id,
|
| 60 |
"hour": hour,
|
|
@@ -71,19 +73,42 @@ def predict_dynamic_price(
|
|
| 71 |
"traffic_index": traffic_index,
|
| 72 |
"distance_km": distance_km,
|
| 73 |
"duration_min": duration_min,
|
| 74 |
-
"base_fare": base_price
|
| 75 |
-
"demand_supply_ratio": (demand + 1) / (supply + 1)
|
| 76 |
}
|
| 77 |
|
| 78 |
for k, v in inputs.items():
|
| 79 |
if k in row:
|
| 80 |
row[k] = float(v)
|
| 81 |
|
| 82 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
df_row = pd.DataFrame([[row[f] for f in FEATURES]], columns=FEATURES)
|
| 84 |
|
| 85 |
-
# Predict
|
| 86 |
surge = float(model.predict(df_row)[0])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
surge = np.clip(surge, 1.0, 3.5)
|
| 88 |
|
| 89 |
final_price = base_price * surge
|
|
@@ -118,7 +143,8 @@ demo = gr.Interface(
|
|
| 118 |
fn=predict_dynamic_price,
|
| 119 |
inputs=inputs,
|
| 120 |
outputs=outputs,
|
| 121 |
-
title="Dynamic Pricing (
|
| 122 |
)
|
| 123 |
|
| 124 |
demo.launch()
|
|
|
|
|
|
| 12 |
FIXED_FARE = artifact["fixed_fare"]
|
| 13 |
RATE_PER_KM = artifact["rate_per_km"]
|
| 14 |
|
| 15 |
+
# ===================== WEATHER =====================
|
| 16 |
API_KEY = "YOUR_API_KEY"
|
| 17 |
CITY = "Visakhapatnam,IN"
|
| 18 |
|
|
|
|
| 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 |
+
# --- Initialize feature vector ---
|
| 57 |
row = {f: 0.0 for f in FEATURES}
|
| 58 |
|
| 59 |
+
# --- Fill primary inputs ---
|
| 60 |
inputs = {
|
| 61 |
"zone_id": zone_id,
|
| 62 |
"hour": hour,
|
|
|
|
| 73 |
"traffic_index": traffic_index,
|
| 74 |
"distance_km": distance_km,
|
| 75 |
"duration_min": duration_min,
|
| 76 |
+
"base_fare": base_price
|
|
|
|
| 77 |
}
|
| 78 |
|
| 79 |
for k, v in inputs.items():
|
| 80 |
if k in row:
|
| 81 |
row[k] = float(v)
|
| 82 |
|
| 83 |
+
# =========================================================
|
| 84 |
+
# CRITICAL FIX — FORCE DEMAND/SUPPLY EFFECT STRONG
|
| 85 |
+
# =========================================================
|
| 86 |
+
ratio = (demand + 1) / (supply + 1)
|
| 87 |
+
row["demand_supply_ratio"] = np.clip(ratio, 0, 50)
|
| 88 |
+
|
| 89 |
+
# =========================================================
|
| 90 |
+
# HANDLE SEASON FEATURES (avoid all-zero vector)
|
| 91 |
+
# =========================================================
|
| 92 |
+
if "season_winter" in row:
|
| 93 |
+
row["season_winter"] = 0
|
| 94 |
+
if "season_summer" in row:
|
| 95 |
+
row["season_summer"] = 1 # set one season active
|
| 96 |
+
if "season_monsoon" in row:
|
| 97 |
+
row["season_monsoon"] = 0
|
| 98 |
+
if "season_autumn" in row:
|
| 99 |
+
row["season_autumn"] = 0
|
| 100 |
+
|
| 101 |
+
# --- Create dataframe in correct order ---
|
| 102 |
df_row = pd.DataFrame([[row[f] for f in FEATURES]], columns=FEATURES)
|
| 103 |
|
| 104 |
+
# --- Predict ---
|
| 105 |
surge = float(model.predict(df_row)[0])
|
| 106 |
+
|
| 107 |
+
# =========================================================
|
| 108 |
+
# ADD EXTRA RESPONSE FROM DEMAND/SUPPLY (if model too flat)
|
| 109 |
+
# =========================================================
|
| 110 |
+
surge += 0.15 * (ratio - 1)
|
| 111 |
+
|
| 112 |
surge = np.clip(surge, 1.0, 3.5)
|
| 113 |
|
| 114 |
final_price = base_price * surge
|
|
|
|
| 143 |
fn=predict_dynamic_price,
|
| 144 |
inputs=inputs,
|
| 145 |
outputs=outputs,
|
| 146 |
+
title="Dynamic Pricing (Demand/Supply Fixed)"
|
| 147 |
)
|
| 148 |
|
| 149 |
demo.launch()
|
| 150 |
+
|