Sahithi27 commited on
Commit
7a18aad
·
verified ·
1 Parent(s): e9e6427

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -55
app.py CHANGED
@@ -1,39 +1,20 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import numpy as np
4
- import pickle
5
 
6
- # ---------------------------
7
- # LOAD TRAINED MODEL
8
- # ---------------------------
9
- with open("dynamic_pricing_model.pkl", "rb") as f:
10
- model = pickle.load(f)
11
 
12
- # ---------------------------
13
- # PREDICTION FUNCTION
14
- # ---------------------------
15
  def predict_dynamic_pricing(
16
- zone_id,
17
- demand,
18
- supply,
19
- driver_availability,
20
- weather_factor,
21
- event_factor,
22
- temperature,
23
- traffic_index,
24
- distance_km,
25
- duration_min,
26
- base_fare,
27
- hour,
28
- day_of_week,
29
- is_weekend,
30
- rate_per_km=6
31
  ):
32
 
33
- # Recalculate demand/supply ratio
34
  demand_supply_ratio = np.clip(demand / (supply + 1), 0, 3)
35
 
36
- # Create input row
37
  row = pd.DataFrame([{
38
  "zone_id": zone_id,
39
  "hour": hour,
@@ -52,45 +33,38 @@ def predict_dynamic_pricing(
52
  "demand_supply_ratio": demand_supply_ratio
53
  }])
54
 
55
- # Predict surge factor
56
- surge_factor = float(model.predict(row)[0])
 
57
 
58
- # Base price
59
- base_price = float(base_fare)
60
 
61
- # Final dynamic price
62
- final_price = base_price * surge_factor
63
-
64
- return base_price, surge_factor, final_price
65
-
66
- # ---------------------------
67
- # GRADIO UI
68
- # ---------------------------
69
  demo = gr.Interface(
70
  fn=predict_dynamic_pricing,
71
  inputs=[
72
- gr.Number(label="Zone ID", value=1),
73
- gr.Number(label="Demand", value=150),
74
- gr.Number(label="Supply", value=80),
75
- gr.Number(label="Driver Availability", value=60),
76
- gr.Number(label="Weather Factor (1.0–1.35)", value=1.0),
77
- gr.Number(label="Event Factor (1.0–1.5)", value=1.0),
78
- gr.Number(label="Temperature °C", value=30),
79
- gr.Number(label="Traffic Index (0–1)", value=0.5),
80
- gr.Number(label="Distance (km)", value=10),
81
- gr.Number(label="Duration (min)", value=20),
82
- gr.Number(label="Base Fare", value=120),
83
- gr.Number(label="Hour (0–23)", value=18),
84
- gr.Number(label="Day of Week (0=Mon)", value=4),
85
- gr.Number(label="Is Weekend? (0/1)", value=0),
86
  ],
87
  outputs=[
88
  gr.Number(label="Base Price"),
89
- gr.Number(label="Predicted Surge Factor"),
90
- gr.Number(label="Final Dynamic Price"),
91
  ],
92
  title="Dynamic Pricing Model",
93
- description="Adjusts fare using demand, supply, weather, events, and time-of-day.",
94
  )
95
 
96
  demo.launch()
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  import numpy as np
4
+ import joblib # Use joblib instead of pickle
5
 
6
+ # Load model
7
+ model = joblib.load("dynamic_pricing_model.joblib")
 
 
 
8
 
 
 
 
9
  def predict_dynamic_pricing(
10
+ zone_id, demand, supply, driver_availability,
11
+ weather_factor, event_factor, temperature, traffic_index,
12
+ distance_km, duration_min, base_fare,
13
+ hour, day_of_week, is_weekend, rate_per_km=6
 
 
 
 
 
 
 
 
 
 
 
14
  ):
15
 
 
16
  demand_supply_ratio = np.clip(demand / (supply + 1), 0, 3)
17
 
 
18
  row = pd.DataFrame([{
19
  "zone_id": zone_id,
20
  "hour": hour,
 
33
  "demand_supply_ratio": demand_supply_ratio
34
  }])
35
 
36
+ surge = float(model.predict(row)[0])
37
+ base_price = base_fare + distance_km * rate_per_km
38
+ final_price = base_price * surge
39
 
40
+ return base_price, surge, final_price
 
41
 
 
 
 
 
 
 
 
 
42
  demo = gr.Interface(
43
  fn=predict_dynamic_pricing,
44
  inputs=[
45
+ gr.Number(label="Zone ID"),
46
+ gr.Number(label="Demand"),
47
+ gr.Number(label="Supply"),
48
+ gr.Number(label="Driver Availability"),
49
+ gr.Number(label="Weather Factor"),
50
+ gr.Number(label="Event Factor"),
51
+ gr.Number(label="Temperature"),
52
+ gr.Number(label="Traffic Index"),
53
+ gr.Number(label="Distance (km)"),
54
+ gr.Number(label="Duration (min)"),
55
+ gr.Number(label="Base Fare"),
56
+ gr.Number(label="Hour"),
57
+ gr.Number(label="Day of Week"),
58
+ gr.Number(label="Is Weekend")
59
  ],
60
  outputs=[
61
  gr.Number(label="Base Price"),
62
+ gr.Number(label="Surge Factor"),
63
+ gr.Number(label="Final Dynamic Price")
64
  ],
65
  title="Dynamic Pricing Model",
66
+ description="AI-based real-time surge pricing."
67
  )
68
 
69
  demo.launch()
70
+