Sarvamangalak commited on
Commit
203c8ba
·
verified ·
1 Parent(s): 1c5009f

Create evaluation_dashboard.py

Browse files
Files changed (1) hide show
  1. evaluation_dashboard.py +237 -0
evaluation_dashboard.py ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import sqlite3
3
+ import gradio as gr
4
+ import matplotlib.pyplot as plt
5
+
6
+
7
+ DB_PATH = "traffic_analytics.db"
8
+
9
+
10
+ # --------------------------------------------------
11
+ # Load Analytics Data
12
+ # --------------------------------------------------
13
+
14
+ def load_data():
15
+
16
+ conn = sqlite3.connect(DB_PATH)
17
+
18
+ query = """
19
+ SELECT *
20
+ FROM vehicle_records
21
+ """
22
+
23
+ df = pd.read_sql(query, conn)
24
+
25
+ conn.close()
26
+
27
+ return df
28
+
29
+
30
+ # --------------------------------------------------
31
+ # Calculate Metrics
32
+ # --------------------------------------------------
33
+
34
+ def calculate_metrics():
35
+
36
+ try:
37
+
38
+ df = load_data()
39
+
40
+ total_vehicles = len(df)
41
+
42
+ ev_count = len(
43
+ df[df["is_ev"] == 1]
44
+ )
45
+
46
+ non_ev_count = total_vehicles - ev_count
47
+
48
+ ev_rate = (
49
+ ev_count / total_vehicles * 100
50
+ if total_vehicles > 0
51
+ else 0
52
+ )
53
+
54
+ total_toll = df["original_toll"].sum()
55
+
56
+ toll_discount = (
57
+ df["toll_discount_amount"].sum()
58
+ )
59
+
60
+ total_parking = (
61
+ df["original_parking"].sum()
62
+ )
63
+
64
+ parking_discount = (
65
+ df["parking_discount_amount"].sum()
66
+ )
67
+
68
+ govt_incentive = (
69
+ df["government_incentive"].sum()
70
+ )
71
+
72
+ avg_green_score = (
73
+ df["green_score"].mean()
74
+ )
75
+
76
+ avg_processing_time = (
77
+ df["processing_time"].mean()
78
+ if "processing_time" in df.columns
79
+ else 0
80
+ )
81
+
82
+ return {
83
+ "Total Vehicles":
84
+ total_vehicles,
85
+
86
+ "EV Vehicles":
87
+ ev_count,
88
+
89
+ "Non-EV Vehicles":
90
+ non_ev_count,
91
+
92
+ "EV Adoption Rate (%)":
93
+ round(ev_rate, 2),
94
+
95
+ "Total Toll Revenue":
96
+ round(total_toll, 2),
97
+
98
+ "Toll Discounts":
99
+ round(toll_discount, 2),
100
+
101
+ "Total Parking Revenue":
102
+ round(total_parking, 2),
103
+
104
+ "Parking Discounts":
105
+ round(parking_discount, 2),
106
+
107
+ "Govt Incentives":
108
+ round(govt_incentive, 2),
109
+
110
+ "Avg Green Score":
111
+ round(avg_green_score, 2),
112
+
113
+ "Avg Processing Time":
114
+ round(avg_processing_time, 2)
115
+ }
116
+
117
+ except Exception as e:
118
+
119
+ return {
120
+ "Error": str(e)
121
+ }
122
+
123
+
124
+ # --------------------------------------------------
125
+ # Create Charts
126
+ # --------------------------------------------------
127
+
128
+ def create_charts():
129
+
130
+ df = load_data()
131
+
132
+ # EV vs Non-EV
133
+ plt.figure(figsize=(5, 4))
134
+
135
+ counts = [
136
+ len(df[df["is_ev"] == 1]),
137
+ len(df[df["is_ev"] == 0])
138
+ ]
139
+
140
+ plt.bar(
141
+ ["EV", "Non-EV"],
142
+ counts
143
+ )
144
+
145
+ plt.title("Vehicle Distribution")
146
+
147
+ vehicle_chart = "vehicle_distribution.png"
148
+
149
+ plt.savefig(vehicle_chart)
150
+
151
+ plt.close()
152
+
153
+ # Savings Chart
154
+
155
+ plt.figure(figsize=(5, 4))
156
+
157
+ savings = [
158
+ df["toll_discount_amount"].sum(),
159
+ df["parking_discount_amount"].sum()
160
+ ]
161
+
162
+ plt.bar(
163
+ ["Toll Savings",
164
+ "Parking Savings"],
165
+ savings
166
+ )
167
+
168
+ plt.title("Savings Distribution")
169
+
170
+ savings_chart = "savings_distribution.png"
171
+
172
+ plt.savefig(savings_chart)
173
+
174
+ plt.close()
175
+
176
+ return vehicle_chart, savings_chart
177
+
178
+
179
+ # --------------------------------------------------
180
+ # Dashboard Refresh
181
+ # --------------------------------------------------
182
+
183
+ def refresh_dashboard():
184
+
185
+ metrics = calculate_metrics()
186
+
187
+ metric_text = ""
188
+
189
+ for key, value in metrics.items():
190
+
191
+ metric_text += (
192
+ f"{key}: {value}\n"
193
+ )
194
+
195
+ chart1, chart2 = create_charts()
196
+
197
+ return metric_text, chart1, chart2
198
+
199
+
200
+ # --------------------------------------------------
201
+ # Gradio UI
202
+ # --------------------------------------------------
203
+
204
+ with gr.Blocks() as demo:
205
+
206
+ gr.Markdown(
207
+ "# 🚦 Smart Traffic & EV Analytics Dashboard"
208
+ )
209
+
210
+ refresh_btn = gr.Button(
211
+ "Refresh Dashboard"
212
+ )
213
+
214
+ metrics_box = gr.Textbox(
215
+ label="Evaluation Metrics",
216
+ lines=15
217
+ )
218
+
219
+ vehicle_chart = gr.Image(
220
+ label="EV vs Non-EV Vehicles"
221
+ )
222
+
223
+ savings_chart = gr.Image(
224
+ label="Discount Savings"
225
+ )
226
+
227
+ refresh_btn.click(
228
+ fn=refresh_dashboard,
229
+ inputs=[],
230
+ outputs=[
231
+ metrics_box,
232
+ vehicle_chart,
233
+ savings_chart
234
+ ]
235
+ )
236
+
237
+ demo.launch()