Sahithi27 commited on
Commit
2413f63
·
verified ·
1 Parent(s): 0a08f08

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -23
app.py CHANGED
@@ -1,34 +1,114 @@
 
 
1
  import pandas as pd
 
2
 
3
- df = pd.read_csv("dynamic_pricing_data.csv")
4
- df.head()
5
- from sklearn.linear_model import Ridge
6
- from sklearn.model_selection import train_test_split
7
 
8
- X = df.drop("surge", axis=1)
9
- y = df["surge"]
10
 
11
- FEATURES = X.columns.tolist()
 
 
 
12
 
13
- X_train, X_test, y_train, y_test = train_test_split(
14
- X, y, test_size=0.2, random_state=42
15
- )
 
 
 
 
 
 
 
16
 
17
- model = Ridge(alpha=1.0)
18
- model.fit(X_train, y_train)
19
 
20
- print("Training completed")
21
- import joblib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
23
- FIXED_FARE = 30
24
- RATE_PER_KM = 6
25
 
26
- artifact = {
27
- "model": model,
28
- "features": FEATURES,
29
- "fixed_fare": FIXED_FARE,
30
- "rate_per_km": RATE_PER_KM
31
- }
32
 
33
- joblib.dump(artifact, "dynamic_pricing_artifact.joblib")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
 
 
1
+ import gradio as gr
2
+ import joblib
3
  import pandas as pd
4
+ import numpy as np
5
 
6
+ # -------------------------------------------------
7
+ # Load trained artifact (NO dataset, NO training)
8
+ # -------------------------------------------------
9
+ ARTIFACT_PATH = "dynamic_pricing_artifact_v1.joblib"
10
 
11
+ artifact = joblib.load(ARTIFACT_PATH)
 
12
 
13
+ model = artifact["model"]
14
+ FEATURES = artifact["features"]
15
+ FIXED_FARE = artifact["fixed_fare"]
16
+ RATE_PER_KM = artifact["rate_per_km"]
17
 
18
+ # -------------------------------------------------
19
+ # Prediction Logic
20
+ # -------------------------------------------------
21
+ def predict_dynamic_price(
22
+ zone_id, demand, supply, driver_availability,
23
+ weather_factor, event_factor, temperature, traffic_index,
24
+ distance_km, duration_min,
25
+ hour, day_of_week, is_weekend, month,
26
+ is_holiday, is_festival
27
+ ):
28
 
29
+ # Base price calculation
30
+ base_price = FIXED_FARE + (distance_km * RATE_PER_KM)
31
 
32
+ # Initialize input row
33
+ row = {f: 0.0 for f in FEATURES}
34
+
35
+ inputs = {
36
+ "zone_id": zone_id,
37
+ "hour": hour,
38
+ "day_of_week": day_of_week,
39
+ "is_weekend": is_weekend,
40
+ "month": month,
41
+ "is_holiday": is_holiday,
42
+ "is_festival": is_festival,
43
+ "demand": demand,
44
+ "supply": supply,
45
+ "driver_availability": driver_availability,
46
+ "weather_factor": weather_factor,
47
+ "event_factor": event_factor,
48
+ "temperature": temperature,
49
+ "traffic_index": traffic_index,
50
+ "distance_km": distance_km,
51
+ "duration_min": duration_min,
52
+ "base_fare": base_price
53
+ }
54
+
55
+ # Fill feature vector
56
+ for k, v in inputs.items():
57
+ if k in row:
58
+ row[k] = float(v)
59
+
60
+ # Derived feature
61
+ row["demand_supply_ratio"] = np.clip(demand / (supply + 1), 0, 20)
62
 
63
+ # Create DataFrame in correct order
64
+ df_row = pd.DataFrame([[row[f] for f in FEATURES]], columns=FEATURES)
65
 
66
+ # Predict surge
67
+ surge = float(model.predict(df_row)[0])
68
+ surge = np.clip(surge, 1.0, 2.5)
 
 
 
69
 
70
+ final_price = base_price * surge
71
+
72
+ return round(base_price, 2), round(surge, 3), round(final_price, 2)
73
+
74
+ # -------------------------------------------------
75
+ # Gradio UI
76
+ # -------------------------------------------------
77
+ inputs = [
78
+ gr.Number(label="Zone ID", value=1),
79
+ gr.Number(label="Demand", value=150),
80
+ gr.Number(label="Supply", value=80),
81
+ gr.Number(label="Driver Availability", value=60),
82
+ gr.Number(label="Weather Factor (1.0–1.35)", value=1.0),
83
+ gr.Number(label="Event Factor (1.0–1.5)", value=1.0),
84
+ gr.Number(label="Temperature (°C)", value=30),
85
+ gr.Number(label="Traffic Index (0–1)", value=0.5),
86
+ gr.Number(label="Distance (km)", value=10),
87
+ gr.Number(label="Duration (min)", value=20),
88
+ gr.Number(label="Hour (0–23)", value=18),
89
+ gr.Number(label="Day of Week (0=Mon)", value=4),
90
+ gr.Number(label="Is Weekend (0/1)", value=0),
91
+ gr.Number(label="Month (1–12)", value=11),
92
+ gr.Number(label="Is Holiday (0/1)", value=0),
93
+ gr.Number(label="Is Festival (0/1)", value=0),
94
+ ]
95
+
96
+ outputs = [
97
+ gr.Number(label="Base Price"),
98
+ gr.Number(label="Surge Factor"),
99
+ gr.Number(label="Final Dynamic Price"),
100
+ ]
101
+
102
+ demo = gr.Interface(
103
+ fn=predict_dynamic_price,
104
+ inputs=inputs,
105
+ outputs=outputs,
106
+ title="Dynamic Pricing Model (CPU)",
107
+ description=(
108
+ "Inference-only Dynamic Pricing model. "
109
+ "Model trained on Hugging Face, exported to ONNX, "
110
+ "and designed for CPU-only deployment."
111
+ )
112
+ )
113
 
114
+ demo.launch()