Sahithi27's picture
Update app.py
ab9c9f0 verified
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()