Sanjayraju30 commited on
Commit
aa9bb13
·
verified ·
1 Parent(s): 73510df

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -71
app.py CHANGED
@@ -4,104 +4,115 @@ import pandas as pd
4
  import plotly.graph_objects as go
5
  import random
6
 
7
- # Configuration
8
- AREAS = ['Kurnool', 'Hyderabad', 'Ballari', 'Gadwal']
9
- POLES_PER_AREA = 12
 
 
 
 
10
 
11
- # Simulate poles
12
- def simulate_poles():
13
- poles = []
14
  for area in AREAS:
15
  for i in range(1, POLES_PER_AREA + 1):
16
- solar = round(random.uniform(3.0, 7.5), 2)
17
- wind = round(random.uniform(0.5, 2.0), 2)
18
- tilt = round(random.uniform(0, 10), 2)
19
- vibration = round(random.uniform(0, 3), 2)
20
- camera_status = random.choice(['Online', 'Offline'])
21
- total_power = solar + wind
22
 
23
- # Determine alert level
24
- if total_power < 3 or tilt > 8 or vibration > 2.8:
25
- alert = 'Red'
26
- elif total_power < 4 or tilt > 6 or vibration > 2.5:
27
- alert = 'Yellow'
28
- else:
29
- alert = 'Green'
 
 
 
 
 
 
 
30
 
31
- poles.append({
32
- 'Area': area,
33
- 'Pole': f'P{i}',
34
- 'Solar (kWh)': solar,
35
- 'Wind (kWh)': wind,
36
- 'Total Power': total_power,
37
- 'Tilt (°)': tilt,
38
- 'Vibration (g)': vibration,
39
- 'Camera': camera_status,
40
- 'Alert': alert
41
  })
42
- return pd.DataFrame(poles)
 
 
 
 
 
 
 
43
 
44
- # Create data
45
- df = simulate_poles()
46
- alert_map = {'Green': 1, 'Yellow': 2, 'Red': 3}
47
- df['AlertLevel'] = df['Alert'].map(alert_map)
 
48
 
49
- # Create heatmap
50
- def create_alert_heatmap(df):
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=pivot_df.values,
59
- x=pivot_df.columns,
60
- y=pivot_df.index,
61
- colorscale=color_scale,
62
  zmin=1,
63
  zmax=3,
64
- showscale=False,
65
- hovertemplate="Pole: %{x}<br>Area: %{y}<br>Alert Level: %{z}<extra></extra>"
66
  ))
67
- fig.update_layout(title="Smart Pole Alert Heatmap", height=500)
68
  return fig
69
 
70
- # App
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=create_alert_heatmap(df)),
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
- "animation": "blinker 1s linear infinite",
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
- 50% { opacity: 0; }
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)