Sahithi27 commited on
Commit
c55937a
·
verified ·
1 Parent(s): f72a5d6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import joblib
3
+ import gradio as gr
4
+
5
+ # Load model artifact once
6
+ artifact = joblib.load("pue_artifact.joblib")
7
+
8
+ def predict_pue(
9
+ zone_id, hour, day_of_week, is_weekend,
10
+ weather_factor, event_factor, traffic_index,
11
+ distance_km, duration_min, base_fare,
12
+ avg_distance, avg_duration, avg_fare,
13
+ discount_usage_rate, total_rides
14
+ ):
15
+
16
+ input_data = {
17
+ "zone_id": int(zone_id),
18
+ "hour": int(hour),
19
+ "day_of_week": int(day_of_week),
20
+ "is_weekend": int(is_weekend),
21
+ "weather_factor": float(weather_factor),
22
+ "event_factor": float(event_factor),
23
+ "traffic_index": float(traffic_index),
24
+ "distance_km": float(distance_km),
25
+ "duration_min": float(duration_min),
26
+ "base_fare": float(base_fare),
27
+ "avg_distance": float(avg_distance),
28
+ "avg_duration": float(avg_duration),
29
+ "avg_fare": float(avg_fare),
30
+ "discount_usage_rate": float(discount_usage_rate),
31
+ "total_rides": int(total_rides)
32
+ }
33
+
34
+ X = pd.DataFrame([input_data])
35
+ X = X.reindex(columns=artifact["features"], fill_value=0)
36
+
37
+ ride = artifact["ride_encoder"].inverse_transform(
38
+ artifact["ride_model"].predict(X)
39
+ )[0]
40
+
41
+ discount_prob = artifact["discount_model"].predict_proba(X)[0][1]
42
+
43
+ route = artifact["route_encoder"].inverse_transform(
44
+ artifact["route_model"].predict(X)
45
+ )[0]
46
+
47
+ return {
48
+ "Recommended Ride Type": ride,
49
+ "Discount Probability": round(float(discount_prob), 2),
50
+ "Preferred Route": route
51
+ }
52
+
53
+ # Gradio UI
54
+ app = gr.Interface(
55
+ fn=predict_pue,
56
+ inputs=[
57
+ gr.Number(label="Zone ID"),
58
+ gr.Slider(0,23,step=1,label="Hour"),
59
+ gr.Slider(0,6,step=1,label="Day of Week"),
60
+ gr.Radio([0,1],label="Weekend"),
61
+ gr.Slider(1,1.5,label="Weather Factor"),
62
+ gr.Slider(1,1.6,label="Event Factor"),
63
+ gr.Slider(0.5,2,label="Traffic Index"),
64
+ gr.Number(label="Distance (km)"),
65
+ gr.Number(label="Duration (min)"),
66
+ gr.Number(label="Base Fare"),
67
+ gr.Number(label="Avg Distance"),
68
+ gr.Number(label="Avg Duration"),
69
+ gr.Number(label="Avg Fare"),
70
+ gr.Slider(0,1,label="Discount Usage Rate"),
71
+ gr.Number(label="Total Rides")
72
+ ],
73
+ outputs="json",
74
+ title="Personalized User Experience – Real-Time ML",
75
+ description="Real-time personalization for ride-hailing apps"
76
+ )
77
+
78
+ app.launch()