Spaces:
Running
Running
File size: 4,623 Bytes
2413f63 77c2e7e 2413f63 3334761 77c2e7e fec65ac 96828b7 3334761 d67f394 269462d 3334761 fec65ac 3334761 d67f394 fec65ac 3334761 fec65ac 269462d 77c2e7e fec65ac 2413f63 269462d 2413f63 269462d 2413f63 88c7e38 ab9c9f0 269462d ab9c9f0 2413f63 77c2e7e ab9c9f0 2413f63 96828b7 2413f63 ab9c9f0 96828b7 ab9c9f0 96828b7 ab9c9f0 96828b7 ab9c9f0 2413f63 77c2e7e ab9c9f0 5c38b1d ab9c9f0 96828b7 5c38b1d ab9c9f0 5c38b1d ab9c9f0 269462d ab9c9f0 5c38b1d fec65ac ab9c9f0 fec65ac ab9c9f0 fec65ac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
import gradio as gr
import joblib
import pandas as pd
import numpy as np
import requests
# ===================== LOAD MODEL =====================
artifact = joblib.load("dynamic_pricing_artifact_v1.joblib")
model = artifact["model"]
FEATURES = artifact["features"]
FIXED_FARE = artifact["fixed_fare"]
RATE_PER_KM = artifact["rate_per_km"]
# ===================== WEATHER =====================
API_KEY = "YOUR_API_KEY"
CITY = "Visakhapatnam,IN"
def get_weather_features():
try:
url = "https://api.openweathermap.org/data/2.5/weather"
params = {"q": CITY, "appid": API_KEY, "units": "metric"}
data = requests.get(url, params=params, timeout=5).json()
condition = data.get("weather", [{}])[0].get("main", "Clear")
temp = data.get("main", {}).get("temp", 30)
mapping = {
"Clear": 1.0,
"Clouds": 1.05,
"Rain": 1.20,
"Thunderstorm": 1.30
}
return mapping.get(condition, 1.0), temp
except:
return 1.0, 30
# ===================== PREDICTION =====================
def predict_dynamic_price(
zone_id, demand, supply, driver_availability,
event_factor, traffic_index,
distance_km, duration_min,
hour, day_of_week, is_weekend,
is_holiday, is_festival
):
# ---- WEATHER ----
weather_factor, temperature = get_weather_features()
# ---- BASE FARE ----
base_price = FIXED_FARE + (distance_km * RATE_PER_KM)
# ---- BUILD FEATURE VECTOR ----
row = {f: 0.0 for f in FEATURES}
inputs = {
"zone_id": zone_id,
"hour": hour,
"day_of_week": day_of_week,
"is_weekend": is_weekend,
"is_holiday": is_holiday,
"is_festival": is_festival,
"demand": demand,
"supply": supply,
"driver_availability": driver_availability,
"weather_factor": weather_factor,
"event_factor": event_factor,
"temperature": temperature,
"traffic_index": traffic_index,
"distance_km": distance_km,
"duration_min": duration_min,
"base_fare": base_price
}
for k, v in inputs.items():
if k in row:
row[k] = float(v)
# =====================================================
# DEMAND vs SUPPLY EFFECT
# =====================================================
ratio = (demand + 1) / (supply + 1)
row["demand_supply_ratio"] = np.clip(ratio, 0, 50)
# =====================================================
# SEASON FIX (avoid zero vector)
# =====================================================
if "season_winter" in row:
row["season_winter"] = 0
if "season_summer" in row:
row["season_summer"] = 1
if "season_monsoon" in row:
row["season_monsoon"] = 0
if "season_autumn" in row:
row["season_autumn"] = 0
# ---- Create dataframe ----
df_row = pd.DataFrame([[row[f] for f in FEATURES]], columns=FEATURES)
# ---- MODEL PREDICTION ----
surge = float(model.predict(df_row)[0])
# =====================================================
# EXTRA RESPONSE: DEMAND vs SUPPLY
# =====================================================
surge += 0.15 * (ratio - 1)
# =====================================================
# DRIVER AVAILABILITY EFFECT (STRONG)
# =====================================================
driver_factor = (supply + 1) / (driver_availability + 1)
surge += 0.20 * (driver_factor - 1)
# ---- Stability ----
surge = np.clip(surge, 1.0, 3.5)
final_price = base_price * surge
return round(base_price, 2), round(surge, 3), round(final_price, 2)
# ===================== UI =====================
inputs = [
gr.Number(label="Zone ID", value=1),
gr.Number(label="Demand", value=150),
gr.Number(label="Supply", value=80),
gr.Number(label="Driver Availability", value=60),
gr.Number(label="Event Factor", value=1.0),
gr.Number(label="Traffic Index", value=0.5),
gr.Number(label="Distance (km)", value=10),
gr.Number(label="Duration (min)", value=20),
gr.Number(label="Hour", value=18),
gr.Number(label="Day of Week", value=4),
gr.Number(label="Is Weekend", value=0),
gr.Number(label="Is Holiday", value=0),
gr.Number(label="Is Festival", value=0),
]
outputs = [
gr.Number(label="Base Price"),
gr.Number(label="Surge Factor"),
gr.Number(label="Final Dynamic Price"),
]
demo = gr.Interface(
fn=predict_dynamic_price,
inputs=inputs,
outputs=outputs,
title="Dynamic Pricing (Fully Responsive)"
)
demo.launch()
|