Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,114 +1,61 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
| 3 |
-
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
# ---------------------------------------------------------
|
| 10 |
-
ARTIFACT_PATH = "dynamic_pricing_artifact.joblib"
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
# ---------------------------------------------------------
|
| 21 |
-
# 2) PREDICTION FUNCTION
|
| 22 |
-
# ---------------------------------------------------------
|
| 23 |
-
def gh_predict(
|
| 24 |
-
zone_id, demand, supply, driver_availability, weather_factor, event_factor,
|
| 25 |
-
temperature, traffic_index, distance_km, duration_min, base_fare_fixed,
|
| 26 |
-
hour, day_of_week, is_weekend, month, is_holiday, is_festival
|
| 27 |
):
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
"weather_factor": weather_factor,
|
| 48 |
-
"event_factor": event_factor,
|
| 49 |
-
"temperature": temperature,
|
| 50 |
-
"traffic_index": traffic_index,
|
| 51 |
-
"distance_km": distance_km,
|
| 52 |
-
"duration_min": duration_min,
|
| 53 |
-
"base_fare": base_price, # model needs this
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
# demand supply ratio
|
| 57 |
-
inp["demand_supply_ratio"] = np.clip(demand / (supply + 1), 0, 10)
|
| 58 |
-
|
| 59 |
-
# ------ Build single row DF in correct order -------
|
| 60 |
-
row = {f: 0.0 for f in FEATURES}
|
| 61 |
-
for k, v in inp.items():
|
| 62 |
-
if k in row:
|
| 63 |
-
row[k] = float(v)
|
| 64 |
-
|
| 65 |
-
# create DataFrame
|
| 66 |
-
df_row = pd.DataFrame([[row[f] for f in FEATURES]], columns=FEATURES)
|
| 67 |
-
|
| 68 |
-
# -------- Predict Surge --------
|
| 69 |
-
surge = float(model.predict(df_row)[0])
|
| 70 |
-
|
| 71 |
-
# -------- Final Price ----------
|
| 72 |
final_price = base_price * surge
|
| 73 |
|
| 74 |
-
return round(base_price,
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# ---------------------------------------------------------
|
| 78 |
-
# 3) GRADIO UI
|
| 79 |
-
# ---------------------------------------------------------
|
| 80 |
-
inputs = [
|
| 81 |
-
gr.Number(label="Zone ID", value=1),
|
| 82 |
-
gr.Number(label="Demand", value=150),
|
| 83 |
-
gr.Number(label="Supply", value=80),
|
| 84 |
-
gr.Number(label="Driver Availability", value=60),
|
| 85 |
-
gr.Number(label="Weather Factor (1.0–1.35)", value=1.0),
|
| 86 |
-
gr.Number(label="Event Factor (1.0–1.5)", value=1.0),
|
| 87 |
-
gr.Number(label="Temperature °C", value=30),
|
| 88 |
-
gr.Number(label="Traffic Index (0–1)", value=0.5),
|
| 89 |
-
gr.Number(label="Distance (km)", value=10),
|
| 90 |
-
gr.Number(label="Duration (min)", value=20),
|
| 91 |
-
gr.Number(label="Base Fare (Fixed)", value=30),
|
| 92 |
-
gr.Number(label="Hour (0–23)", value=18),
|
| 93 |
-
gr.Number(label="Day of Week (0=Mon)", value=4),
|
| 94 |
-
gr.Number(label="Is Weekend? (0/1)", value=0),
|
| 95 |
-
gr.Number(label="Month", value=11),
|
| 96 |
-
gr.Number(label="Is Holiday (0/1)", value=0),
|
| 97 |
-
gr.Number(label="Is Festival (0/1)", value=0),
|
| 98 |
-
]
|
| 99 |
-
|
| 100 |
-
outputs = [
|
| 101 |
-
gr.Number(label="Base Price (Distance Based)"),
|
| 102 |
-
gr.Number(label="Predicted Surge Factor"),
|
| 103 |
-
gr.Number(label="Final Dynamic Price"),
|
| 104 |
-
]
|
| 105 |
|
| 106 |
demo = gr.Interface(
|
| 107 |
-
fn=
|
| 108 |
-
inputs=
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
)
|
| 113 |
|
| 114 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import joblib
|
|
|
|
| 3 |
import numpy as np
|
|
|
|
| 4 |
|
| 5 |
+
# Load model DIRECTLY
|
| 6 |
+
model = joblib.load("dynamic_pricing_artifact.joblib")
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
FIXED_FARE = 30
|
| 9 |
+
RATE_PER_KM = 6
|
| 10 |
|
| 11 |
+
def predict_price(
|
| 12 |
+
demand, supply, weather_factor, event_factor,
|
| 13 |
+
traffic_index, is_weekend, is_holiday, is_festival,
|
| 14 |
+
hour, distance_km
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
):
|
| 16 |
+
demand_supply_ratio = demand / (supply + 1)
|
| 17 |
+
|
| 18 |
+
X = np.array([[
|
| 19 |
+
demand,
|
| 20 |
+
supply,
|
| 21 |
+
demand_supply_ratio,
|
| 22 |
+
weather_factor,
|
| 23 |
+
event_factor,
|
| 24 |
+
traffic_index,
|
| 25 |
+
is_weekend,
|
| 26 |
+
is_holiday,
|
| 27 |
+
is_festival,
|
| 28 |
+
hour
|
| 29 |
+
]])
|
| 30 |
+
|
| 31 |
+
surge = float(model.predict(X)[0])
|
| 32 |
+
surge = min(max(surge, 1.0), 2.2) # realistic cap
|
| 33 |
+
|
| 34 |
+
base_price = FIXED_FARE + distance_km * RATE_PER_KM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
final_price = base_price * surge
|
| 36 |
|
| 37 |
+
return round(base_price,2), round(surge,2), round(final_price,2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
demo = gr.Interface(
|
| 40 |
+
fn=predict_price,
|
| 41 |
+
inputs=[
|
| 42 |
+
gr.Number(value=150, label="Demand"),
|
| 43 |
+
gr.Number(value=80, label="Supply"),
|
| 44 |
+
gr.Number(value=1.0, label="Weather Factor"),
|
| 45 |
+
gr.Number(value=1.0, label="Event Factor"),
|
| 46 |
+
gr.Number(value=0.5, label="Traffic Index"),
|
| 47 |
+
gr.Number(value=0, label="Weekend (0/1)"),
|
| 48 |
+
gr.Number(value=0, label="Holiday (0/1)"),
|
| 49 |
+
gr.Number(value=0, label="Festival (0/1)"),
|
| 50 |
+
gr.Number(value=18, label="Hour"),
|
| 51 |
+
gr.Number(value=10, label="Distance (km)")
|
| 52 |
+
],
|
| 53 |
+
outputs=[
|
| 54 |
+
gr.Number(label="Base Price"),
|
| 55 |
+
gr.Number(label="Surge Factor"),
|
| 56 |
+
gr.Number(label="Final Price")
|
| 57 |
+
],
|
| 58 |
+
title="Dynamic Pricing Model"
|
| 59 |
)
|
| 60 |
|
| 61 |
demo.launch()
|