Sahithi27 commited on
Commit
d67f394
·
verified ·
1 Parent(s): f93f8d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -61
app.py CHANGED
@@ -9,7 +9,7 @@ import time
9
  # WEATHER CONFIG
10
  # ================================
11
  API_KEY = "YOUR_API_KEY"
12
- CITY = "Visakhapatnam"
13
 
14
  _last_weather = None
15
  _last_time = 0
@@ -17,7 +17,7 @@ _last_time = 0
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
 
@@ -29,30 +29,27 @@ def get_weather_features():
29
  condition = data["weather"][0]["main"]
30
  temp = data["main"]["temp"]
31
 
32
- if condition == "Clear":
33
- weather_factor = 1.0
34
- elif condition == "Clouds":
35
- weather_factor = 1.05
36
- elif condition == "Rain":
37
- weather_factor = 1.20
38
- elif condition == "Thunderstorm":
39
- weather_factor = 1.30
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"]
58
  FEATURES = artifact["features"]
@@ -71,13 +68,13 @@ def predict_dynamic_price(
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 = {
@@ -103,51 +100,20 @@ def predict_dynamic_price(
103
  if k in row:
104
  row[k] = float(v)
105
 
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
- ]
138
-
139
- outputs = [
140
- gr.Number(label="Base Price"),
141
- gr.Number(label="Surge Factor"),
142
- gr.Number(label="Final Dynamic Price"),
143
- ]
144
-
145
- demo = gr.Interface(
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()
 
9
  # WEATHER CONFIG
10
  # ================================
11
  API_KEY = "YOUR_API_KEY"
12
+ CITY = "Visakhapatnam,IN"
13
 
14
  _last_weather = None
15
  _last_time = 0
 
17
  def get_weather_features():
18
  global _last_weather, _last_time
19
 
20
+ # Cache for 5 minutes
21
  if _last_weather and (time.time() - _last_time < 300):
22
  return _last_weather
23
 
 
29
  condition = data["weather"][0]["main"]
30
  temp = data["main"]["temp"]
31
 
32
+ mapping = {
33
+ "Clear": 1.0,
34
+ "Clouds": 1.05,
35
+ "Rain": 1.20,
36
+ "Thunderstorm": 1.30
37
+ }
38
+
39
+ weather_factor = mapping.get(condition, 1.0)
 
 
40
 
41
  _last_weather = (weather_factor, temp)
42
  _last_time = time.time()
43
  return _last_weather
44
 
45
  except:
46
+ return (1.0, 30)
47
 
48
 
49
  # ================================
50
+ # LOAD MODEL
51
  # ================================
52
+ artifact = joblib.load("dynamic_pricing_artifact_v1.joblib")
 
53
 
54
  model = artifact["model"]
55
  FEATURES = artifact["features"]
 
68
  is_holiday, is_festival
69
  ):
70
 
71
+ # -------- WEATHER --------
72
  weather_factor, temperature = get_weather_features()
73
 
74
+ # -------- BASE FARE --------
75
  base_price = FIXED_FARE + (distance_km * RATE_PER_KM)
76
 
77
+ # -------- BUILD FEATURE VECTOR --------
78
  row = {f: 0.0 for f in FEATURES}
79
 
80
  inputs = {
 
100
  if k in row:
101
  row[k] = float(v)
102
 
103
+ # -------- CRITICAL: DEMAND/SUPPLY RATIO --------
104
+ row["demand_supply_ratio"] = np.clip((demand + 1) / (supply + 1), 0, 50)
105
 
106
  df_row = pd.DataFrame([[row[f] for f in FEATURES]], columns=FEATURES)
107
 
108
+ # -------- DEBUG (Remove later) --------
109
+ print("Demand:", demand, "Supply:", supply, "Drivers:", driver_availability)
110
+ print("Ratio:", row["demand_supply_ratio"])
 
 
111
 
112
+ # -------- MODEL PREDICTION --------
113
+ surge = float(model.predict(df_row)[0])
114
+ print("Raw Surge:", surge)
115
 
116
+ # Avoid constant clipping hiding variation
117
+ surge = np.clip(surge, 1.0, 3.5)
118
 
119
+ final_price = base_price * surge