Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,24 @@ import joblib
|
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import requests
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
API_KEY = "YOUR_API_KEY"
|
| 8 |
-
CITY = "
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def get_weather_features():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
try:
|
| 12 |
url = "https://api.openweathermap.org/data/2.5/weather"
|
| 13 |
params = {"q": CITY, "appid": API_KEY, "units": "metric"}
|
|
@@ -27,15 +40,18 @@ def get_weather_features():
|
|
| 27 |
else:
|
| 28 |
weather_factor = 1.0
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
| 31 |
|
| 32 |
except:
|
| 33 |
-
return 1.0, 30
|
| 34 |
-
|
| 35 |
-
# Load trained artifact (NO dataset, NO training)
|
| 36 |
-
# -------------------------------------------------
|
| 37 |
-
ARTIFACT_PATH = "dynamic_pricing_artifact_v1.joblib"
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
artifact = joblib.load(ARTIFACT_PATH)
|
| 40 |
|
| 41 |
model = artifact["model"]
|
|
@@ -43,21 +59,25 @@ FEATURES = artifact["features"]
|
|
| 43 |
FIXED_FARE = artifact["fixed_fare"]
|
| 44 |
RATE_PER_KM = artifact["rate_per_km"]
|
| 45 |
|
| 46 |
-
|
| 47 |
-
#
|
| 48 |
-
#
|
|
|
|
| 49 |
def predict_dynamic_price(
|
| 50 |
zone_id, demand, supply, driver_availability,
|
| 51 |
-
|
| 52 |
distance_km, duration_min,
|
| 53 |
-
hour, day_of_week, is_weekend,
|
| 54 |
is_holiday, is_festival
|
| 55 |
):
|
| 56 |
|
| 57 |
-
#
|
|
|
|
|
|
|
|
|
|
| 58 |
base_price = FIXED_FARE + (distance_km * RATE_PER_KM)
|
| 59 |
|
| 60 |
-
#
|
| 61 |
row = {f: 0.0 for f in FEATURES}
|
| 62 |
|
| 63 |
inputs = {
|
|
@@ -65,7 +85,6 @@ def predict_dynamic_price(
|
|
| 65 |
"hour": hour,
|
| 66 |
"day_of_week": day_of_week,
|
| 67 |
"is_weekend": is_weekend,
|
| 68 |
-
"month": month,
|
| 69 |
"is_holiday": is_holiday,
|
| 70 |
"is_festival": is_festival,
|
| 71 |
"demand": demand,
|
|
@@ -80,7 +99,6 @@ def predict_dynamic_price(
|
|
| 80 |
"base_fare": base_price
|
| 81 |
}
|
| 82 |
|
| 83 |
-
# Fill feature vector
|
| 84 |
for k, v in inputs.items():
|
| 85 |
if k in row:
|
| 86 |
row[k] = float(v)
|
|
@@ -88,35 +106,32 @@ def predict_dynamic_price(
|
|
| 88 |
# Derived feature
|
| 89 |
row["demand_supply_ratio"] = np.clip(demand / (supply + 1), 0, 20)
|
| 90 |
|
| 91 |
-
# Create DataFrame in correct order
|
| 92 |
df_row = pd.DataFrame([[row[f] for f in FEATURES]], columns=FEATURES)
|
| 93 |
|
| 94 |
-
# Predict surge
|
| 95 |
surge = float(model.predict(df_row)[0])
|
| 96 |
-
surge = np.clip(surge, 1.0, 2.5)
|
| 97 |
|
| 98 |
final_price = base_price * surge
|
| 99 |
|
| 100 |
return round(base_price, 2), round(surge, 3), round(final_price, 2)
|
| 101 |
|
| 102 |
-
|
| 103 |
-
#
|
| 104 |
-
#
|
|
|
|
| 105 |
inputs = [
|
| 106 |
gr.Number(label="Zone ID", value=1),
|
| 107 |
gr.Number(label="Demand", value=150),
|
| 108 |
gr.Number(label="Supply", value=80),
|
| 109 |
gr.Number(label="Driver Availability", value=60),
|
| 110 |
-
gr.Number(label="Weather Factor (1.0–1.35)", value=1.0),
|
| 111 |
gr.Number(label="Event Factor (1.0–1.5)", value=1.0),
|
| 112 |
-
gr.Number(label="Temperature (°C)", value=30),
|
| 113 |
gr.Number(label="Traffic Index (0–1)", value=0.5),
|
| 114 |
gr.Number(label="Distance (km)", value=10),
|
| 115 |
gr.Number(label="Duration (min)", value=20),
|
| 116 |
gr.Number(label="Hour (0–23)", value=18),
|
| 117 |
gr.Number(label="Day of Week (0=Mon)", value=4),
|
| 118 |
gr.Number(label="Is Weekend (0/1)", value=0),
|
| 119 |
-
gr.Number(label="Month (1–12)", value=11),
|
| 120 |
gr.Number(label="Is Holiday (0/1)", value=0),
|
| 121 |
gr.Number(label="Is Festival (0/1)", value=0),
|
| 122 |
]
|
|
@@ -131,12 +146,8 @@ demo = gr.Interface(
|
|
| 131 |
fn=predict_dynamic_price,
|
| 132 |
inputs=inputs,
|
| 133 |
outputs=outputs,
|
| 134 |
-
title="Dynamic Pricing Model (
|
| 135 |
-
description=
|
| 136 |
-
"Inference-only Dynamic Pricing model. "
|
| 137 |
-
"Model trained on Hugging Face, exported to ONNX, "
|
| 138 |
-
"and designed for CPU-only deployment."
|
| 139 |
-
)
|
| 140 |
)
|
| 141 |
|
| 142 |
demo.launch()
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import requests
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
+
# ================================
|
| 9 |
+
# WEATHER CONFIG
|
| 10 |
+
# ================================
|
| 11 |
API_KEY = "YOUR_API_KEY"
|
| 12 |
+
CITY = "Visakhapatnam"
|
| 13 |
+
|
| 14 |
+
_last_weather = None
|
| 15 |
+
_last_time = 0
|
| 16 |
|
| 17 |
def get_weather_features():
|
| 18 |
+
global _last_weather, _last_time
|
| 19 |
+
|
| 20 |
+
# Cache for 5 minutes (avoid API spam)
|
| 21 |
+
if _last_weather and (time.time() - _last_time < 300):
|
| 22 |
+
return _last_weather
|
| 23 |
+
|
| 24 |
try:
|
| 25 |
url = "https://api.openweathermap.org/data/2.5/weather"
|
| 26 |
params = {"q": CITY, "appid": API_KEY, "units": "metric"}
|
|
|
|
| 40 |
else:
|
| 41 |
weather_factor = 1.0
|
| 42 |
|
| 43 |
+
_last_weather = (weather_factor, temp)
|
| 44 |
+
_last_time = time.time()
|
| 45 |
+
return _last_weather
|
| 46 |
|
| 47 |
except:
|
| 48 |
+
return (1.0, 30) # fallback safe
|
| 49 |
+
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
# ================================
|
| 52 |
+
# LOAD MODEL ARTIFACT
|
| 53 |
+
# ================================
|
| 54 |
+
ARTIFACT_PATH = "dynamic_pricing_artifact_v1.joblib"
|
| 55 |
artifact = joblib.load(ARTIFACT_PATH)
|
| 56 |
|
| 57 |
model = artifact["model"]
|
|
|
|
| 59 |
FIXED_FARE = artifact["fixed_fare"]
|
| 60 |
RATE_PER_KM = artifact["rate_per_km"]
|
| 61 |
|
| 62 |
+
|
| 63 |
+
# ================================
|
| 64 |
+
# PREDICTION FUNCTION
|
| 65 |
+
# ================================
|
| 66 |
def predict_dynamic_price(
|
| 67 |
zone_id, demand, supply, driver_availability,
|
| 68 |
+
event_factor, traffic_index,
|
| 69 |
distance_km, duration_min,
|
| 70 |
+
hour, day_of_week, is_weekend,
|
| 71 |
is_holiday, is_festival
|
| 72 |
):
|
| 73 |
|
| 74 |
+
# ---- Auto fetch weather ----
|
| 75 |
+
weather_factor, temperature = get_weather_features()
|
| 76 |
+
|
| 77 |
+
# ---- Base price ----
|
| 78 |
base_price = FIXED_FARE + (distance_km * RATE_PER_KM)
|
| 79 |
|
| 80 |
+
# ---- Build feature row ----
|
| 81 |
row = {f: 0.0 for f in FEATURES}
|
| 82 |
|
| 83 |
inputs = {
|
|
|
|
| 85 |
"hour": hour,
|
| 86 |
"day_of_week": day_of_week,
|
| 87 |
"is_weekend": is_weekend,
|
|
|
|
| 88 |
"is_holiday": is_holiday,
|
| 89 |
"is_festival": is_festival,
|
| 90 |
"demand": demand,
|
|
|
|
| 99 |
"base_fare": base_price
|
| 100 |
}
|
| 101 |
|
|
|
|
| 102 |
for k, v in inputs.items():
|
| 103 |
if k in row:
|
| 104 |
row[k] = float(v)
|
|
|
|
| 106 |
# Derived feature
|
| 107 |
row["demand_supply_ratio"] = np.clip(demand / (supply + 1), 0, 20)
|
| 108 |
|
|
|
|
| 109 |
df_row = pd.DataFrame([[row[f] for f in FEATURES]], columns=FEATURES)
|
| 110 |
|
| 111 |
+
# ---- Predict surge ----
|
| 112 |
surge = float(model.predict(df_row)[0])
|
| 113 |
+
surge = np.clip(surge, 1.0, 2.5) # stability
|
| 114 |
|
| 115 |
final_price = base_price * surge
|
| 116 |
|
| 117 |
return round(base_price, 2), round(surge, 3), round(final_price, 2)
|
| 118 |
|
| 119 |
+
|
| 120 |
+
# ================================
|
| 121 |
+
# GRADIO UI
|
| 122 |
+
# ================================
|
| 123 |
inputs = [
|
| 124 |
gr.Number(label="Zone ID", value=1),
|
| 125 |
gr.Number(label="Demand", value=150),
|
| 126 |
gr.Number(label="Supply", value=80),
|
| 127 |
gr.Number(label="Driver Availability", value=60),
|
|
|
|
| 128 |
gr.Number(label="Event Factor (1.0–1.5)", value=1.0),
|
|
|
|
| 129 |
gr.Number(label="Traffic Index (0–1)", value=0.5),
|
| 130 |
gr.Number(label="Distance (km)", value=10),
|
| 131 |
gr.Number(label="Duration (min)", value=20),
|
| 132 |
gr.Number(label="Hour (0–23)", value=18),
|
| 133 |
gr.Number(label="Day of Week (0=Mon)", value=4),
|
| 134 |
gr.Number(label="Is Weekend (0/1)", value=0),
|
|
|
|
| 135 |
gr.Number(label="Is Holiday (0/1)", value=0),
|
| 136 |
gr.Number(label="Is Festival (0/1)", value=0),
|
| 137 |
]
|
|
|
|
| 146 |
fn=predict_dynamic_price,
|
| 147 |
inputs=inputs,
|
| 148 |
outputs=outputs,
|
| 149 |
+
title="Dynamic Pricing Model (Auto Weather)",
|
| 150 |
+
description="Dynamic Pricing with real-time weather from OpenWeatherMap."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
)
|
| 152 |
|
| 153 |
demo.launch()
|