Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,104 +4,115 @@ import pandas as pd
|
|
4 |
import plotly.graph_objects as go
|
5 |
import random
|
6 |
|
7 |
-
#
|
8 |
-
AREAS = ['
|
9 |
-
POLES_PER_AREA =
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
# Simulate
|
12 |
-
def
|
13 |
-
|
14 |
for area in AREAS:
|
15 |
for i in range(1, POLES_PER_AREA + 1):
|
16 |
-
solar = round(random.uniform(
|
17 |
-
wind = round(random.uniform(0.
|
18 |
-
tilt = round(random.uniform(0,
|
19 |
-
vibration = round(random.uniform(0, 3), 2)
|
20 |
-
|
21 |
-
total_power = solar + wind
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
alert =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
'Camera': camera_status,
|
40 |
-
'Alert': alert
|
41 |
})
|
42 |
-
return pd.DataFrame(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
#
|
45 |
-
df =
|
46 |
-
|
47 |
-
|
|
|
48 |
|
49 |
-
# Create heatmap
|
50 |
-
def
|
51 |
-
pivot_df = df.pivot(index='Area', columns='Pole', values='AlertLevel')
|
52 |
-
color_scale = [
|
53 |
-
[0, "green"],
|
54 |
-
[0.5, "yellow"],
|
55 |
-
[1.0, "red"]
|
56 |
-
]
|
57 |
fig = go.Figure(data=go.Heatmap(
|
58 |
-
z=
|
59 |
-
x=
|
60 |
-
y=
|
61 |
-
colorscale=
|
62 |
zmin=1,
|
63 |
zmax=3,
|
64 |
-
|
65 |
-
hovertemplate="Pole: %{x}<br>Area: %{y}<br>Alert Level: %{z}<extra></extra>"
|
66 |
))
|
67 |
-
fig.update_layout(title="Smart Pole
|
68 |
return fig
|
69 |
|
70 |
-
#
|
71 |
app = dash.Dash(__name__)
|
72 |
server = app.server # for Hugging Face
|
73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
app.layout = html.Div([
|
75 |
-
html.H1("Vedavathi Smart Pole Monitoring", style={"textAlign": "center"}),
|
76 |
|
77 |
-
dcc.Graph(figure=
|
78 |
|
79 |
html.Div([
|
80 |
-
html.H3("🚨 Red Alert Poles", style={"color": "red", "textAlign": "center"}),
|
81 |
html.Div([
|
82 |
-
html.Span(f"{row['Area']} - {row['Pole']}", style=
|
83 |
-
|
84 |
-
"color": "red",
|
85 |
-
"fontWeight": "bold",
|
86 |
-
"fontSize": "18px",
|
87 |
-
"margin": "8px",
|
88 |
-
"display": "inline-block"
|
89 |
-
})
|
90 |
-
for _, row in df[df["Alert"] == "Red"].iterrows()
|
91 |
], style={"textAlign": "center"})
|
92 |
]),
|
93 |
|
94 |
dcc.Markdown('''
|
95 |
-
<style>
|
96 |
-
@keyframes blinker {
|
97 |
-
|
98 |
-
}
|
99 |
-
</style>
|
100 |
-
''', dangerously_allow_html=True)
|
101 |
-
|
102 |
])
|
103 |
|
|
|
104 |
if __name__ == "__main__":
|
105 |
app.run(debug=False, host="0.0.0.0", port=7860)
|
106 |
-
|
107 |
-
|
|
|
4 |
import plotly.graph_objects as go
|
5 |
import random
|
6 |
|
7 |
+
# Constants
|
8 |
+
AREAS = ['Hyderabad', 'Ballari', 'Gadwal', 'Kurnool']
|
9 |
+
POLES_PER_AREA = 4000
|
10 |
+
ALERT_THRESHOLDS = {
|
11 |
+
"Red": {"tilt": 9, "vibration": 2.8, "solar": 3.0, "wind": 0.6},
|
12 |
+
"Yellow": {"tilt": 7, "vibration": 2.4, "solar": 4.0, "wind": 0.8}
|
13 |
+
}
|
14 |
|
15 |
+
# Simulate large dataset
|
16 |
+
def simulate_pole_data():
|
17 |
+
data = []
|
18 |
for area in AREAS:
|
19 |
for i in range(1, POLES_PER_AREA + 1):
|
20 |
+
solar = round(random.uniform(2.5, 7.5), 2)
|
21 |
+
wind = round(random.uniform(0.3, 2.5), 2)
|
22 |
+
tilt = round(random.uniform(0, 12), 2)
|
23 |
+
vibration = round(random.uniform(0, 3.5), 2)
|
24 |
+
alert = "Green"
|
|
|
25 |
|
26 |
+
if (
|
27 |
+
solar < ALERT_THRESHOLDS["Red"]["solar"]
|
28 |
+
or wind < ALERT_THRESHOLDS["Red"]["wind"]
|
29 |
+
or tilt > ALERT_THRESHOLDS["Red"]["tilt"]
|
30 |
+
or vibration > ALERT_THRESHOLDS["Red"]["vibration"]
|
31 |
+
):
|
32 |
+
alert = "Red"
|
33 |
+
elif (
|
34 |
+
solar < ALERT_THRESHOLDS["Yellow"]["solar"]
|
35 |
+
or wind < ALERT_THRESHOLDS["Yellow"]["wind"]
|
36 |
+
or tilt > ALERT_THRESHOLDS["Yellow"]["tilt"]
|
37 |
+
or vibration > ALERT_THRESHOLDS["Yellow"]["vibration"]
|
38 |
+
):
|
39 |
+
alert = "Yellow"
|
40 |
|
41 |
+
data.append({
|
42 |
+
"Area": area,
|
43 |
+
"Pole": f"P{i}",
|
44 |
+
"Solar": solar,
|
45 |
+
"Wind": wind,
|
46 |
+
"Tilt": tilt,
|
47 |
+
"Vibration": vibration,
|
48 |
+
"AlertLevel": alert
|
|
|
|
|
49 |
})
|
50 |
+
return pd.DataFrame(data)
|
51 |
+
|
52 |
+
df = simulate_pole_data()
|
53 |
+
|
54 |
+
# Map alerts to values for heatmap coloring
|
55 |
+
alert_to_num = {'Green': 1, 'Yellow': 2, 'Red': 3}
|
56 |
+
num_to_color = {1: 'green', 2: 'yellow', 3: 'red'}
|
57 |
+
df["AlertNum"] = df["AlertLevel"].map(alert_to_num)
|
58 |
|
59 |
+
# Reduce for heatmap by showing average alert level by 100 pole blocks
|
60 |
+
df['PoleIndex'] = df.groupby('Area').cumcount() // 100 # 40 blocks per area
|
61 |
+
df_heatmap = df.groupby(['Area', 'PoleIndex'])['AlertNum'].mean().reset_index()
|
62 |
+
df_heatmap['PoleBlock'] = df_heatmap['PoleIndex'].apply(lambda x: f"Block {x+1}")
|
63 |
+
pivot = df_heatmap.pivot(index='Area', columns='PoleBlock', values='AlertNum')
|
64 |
|
65 |
+
# Create heatmap figure
|
66 |
+
def create_heatmap(data):
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
fig = go.Figure(data=go.Heatmap(
|
68 |
+
z=data.values,
|
69 |
+
x=data.columns,
|
70 |
+
y=data.index,
|
71 |
+
colorscale=[[0, "green"], [0.5, "yellow"], [1.0, "red"]],
|
72 |
zmin=1,
|
73 |
zmax=3,
|
74 |
+
colorbar=dict(title="Alert Level (1=Green, 2=Yellow, 3=Red)")
|
|
|
75 |
))
|
76 |
+
fig.update_layout(title="Smart Pole Health Heatmap (Aggregated)", height=600)
|
77 |
return fig
|
78 |
|
79 |
+
# Dash app
|
80 |
app = dash.Dash(__name__)
|
81 |
server = app.server # for Hugging Face
|
82 |
|
83 |
+
# Blinking style (for red alerts)
|
84 |
+
blinking_style = {
|
85 |
+
"animation": "blinker 1s linear infinite",
|
86 |
+
"color": "red",
|
87 |
+
"fontWeight": "bold",
|
88 |
+
"fontSize": "16px",
|
89 |
+
"margin": "5px",
|
90 |
+
"display": "inline-block"
|
91 |
+
}
|
92 |
+
|
93 |
+
# Layout
|
94 |
app.layout = html.Div([
|
95 |
+
html.H1("Vedavathi Smart Pole Monitoring - Heatmap", style={"textAlign": "center"}),
|
96 |
|
97 |
+
dcc.Graph(figure=create_heatmap(pivot)),
|
98 |
|
99 |
html.Div([
|
100 |
+
html.H3("🚨 Red Alert Poles (Sample View)", style={"color": "red", "textAlign": "center"}),
|
101 |
html.Div([
|
102 |
+
html.Span(f"{row['Area']} - {row['Pole']}", style=blinking_style)
|
103 |
+
for _, row in df[df["AlertLevel"] == "Red"].sample(n=20).iterrows()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
], style={"textAlign": "center"})
|
105 |
]),
|
106 |
|
107 |
dcc.Markdown('''
|
108 |
+
<style>
|
109 |
+
@keyframes blinker {
|
110 |
+
50% { opacity: 0; }
|
111 |
+
}
|
112 |
+
</style>
|
113 |
+
''', dangerously_allow_html=True)
|
|
|
114 |
])
|
115 |
|
116 |
+
# Run the app
|
117 |
if __name__ == "__main__":
|
118 |
app.run(debug=False, host="0.0.0.0", port=7860)
|
|
|
|