Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pickle
|
| 5 |
+
|
| 6 |
+
# Load trained model safely
|
| 7 |
+
with open("dynamic_pricing_model.pkl", "rb") as f:
|
| 8 |
+
model = pickle.load(f)
|
| 9 |
+
|
| 10 |
+
def predict_dynamic_pricing(
|
| 11 |
+
zone_id, demand, supply, driver_availability,
|
| 12 |
+
weather_factor, event_factor, temperature, traffic_index,
|
| 13 |
+
distance_km, duration_min, base_fare,
|
| 14 |
+
hour, day_of_week, is_weekend, rate_per_km=6
|
| 15 |
+
):
|
| 16 |
+
|
| 17 |
+
demand_supply_ratio = np.clip(demand / (supply + 1), 0, 3)
|
| 18 |
+
|
| 19 |
+
row = pd.DataFrame([{
|
| 20 |
+
"zone_id": zone_id,
|
| 21 |
+
"hour": hour,
|
| 22 |
+
"day_of_week": day_of_week,
|
| 23 |
+
"is_weekend": is_weekend,
|
| 24 |
+
"demand": demand,
|
| 25 |
+
"supply": supply,
|
| 26 |
+
"driver_availability": driver_availability,
|
| 27 |
+
"weather_factor": weather_factor,
|
| 28 |
+
"event_factor": event_factor,
|
| 29 |
+
"temperature": temperature,
|
| 30 |
+
"traffic_index": traffic_index,
|
| 31 |
+
"distance_km": distance_km,
|
| 32 |
+
"duration_min": duration_min,
|
| 33 |
+
"base_fare": base_fare,
|
| 34 |
+
"demand_supply_ratio": demand_supply_ratio
|
| 35 |
+
}])
|
| 36 |
+
|
| 37 |
+
surge = float(model.predict(row)[0])
|
| 38 |
+
base_price = base_fare + distance_km * rate_per_km
|
| 39 |
+
final_price = base_price * surge
|
| 40 |
+
|
| 41 |
+
return base_price, surge, final_price
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
demo = gr.Interface(
|
| 45 |
+
fn=predict_dynamic_pricing,
|
| 46 |
+
inputs=[
|
| 47 |
+
gr.Number(label="Zone ID"),
|
| 48 |
+
gr.Number(label="Demand"),
|
| 49 |
+
gr.Number(label="Supply"),
|
| 50 |
+
gr.Number(label="Driver Availability"),
|
| 51 |
+
gr.Number(label="Weather Factor"),
|
| 52 |
+
gr.Number(label="Event Factor"),
|
| 53 |
+
gr.Number(label="Temperature"),
|
| 54 |
+
gr.Number(label="Traffic Index"),
|
| 55 |
+
gr.Number(label="Distance (km)"),
|
| 56 |
+
gr.Number(label="Duration (min)"),
|
| 57 |
+
gr.Number(label="Base Fare"),
|
| 58 |
+
gr.Number(label="Hour"),
|
| 59 |
+
gr.Number(label="Day of Week"),
|
| 60 |
+
gr.Number(label="Is Weekend")
|
| 61 |
+
],
|
| 62 |
+
outputs=[
|
| 63 |
+
gr.Number(label="Base Price"),
|
| 64 |
+
gr.Number(label="Surge Factor"),
|
| 65 |
+
gr.Number(label="Final Dynamic Price")
|
| 66 |
+
],
|
| 67 |
+
title="Dynamic Pricing Model",
|
| 68 |
+
description="AI-based real-time surge pricing."
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|