Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,48 +1,62 @@
|
|
1 |
import gradio as gr
|
2 |
import random
|
|
|
|
|
|
|
3 |
|
4 |
-
# Ahantu na info
|
5 |
locations = {
|
6 |
-
"Downtown": {"lat": -1.95, "lon": 30.05
|
7 |
-
"Highway": {"lat": -1.92, "lon": 30.06
|
8 |
-
"Industrial Area": {"lat": -1.94, "lon": 30.07
|
9 |
-
"School Zone": {"lat": -1.93, "lon": 30.03
|
10 |
}
|
11 |
|
12 |
-
|
|
|
|
|
|
|
13 |
report_lines = []
|
14 |
markers = ""
|
15 |
|
|
|
|
|
16 |
for name, info in locations.items():
|
17 |
-
# Random
|
18 |
-
speed = round(random.gauss(
|
19 |
-
vehicle_count = int(random.gauss(
|
20 |
|
21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
22 |
if speed < 20 and vehicle_count > 50:
|
23 |
-
level = "π¦ High
|
24 |
color = "red"
|
|
|
25 |
elif speed < 40 or vehicle_count > 40:
|
26 |
level = "β οΈ Moderate"
|
27 |
color = "orange"
|
|
|
28 |
else:
|
29 |
level = "β
Light"
|
30 |
color = "green"
|
|
|
31 |
|
32 |
-
report_lines.append(f"
|
33 |
|
34 |
-
#
|
35 |
markers += f"""
|
36 |
L.circleMarker([{info["lat"]}, {info["lon"]}], {{
|
37 |
-
color: '{color}',
|
38 |
-
radius: 10
|
39 |
}}).addTo(map).bindPopup("<b>{name}</b><br>Speed: {speed} km/h<br>Cars: {vehicle_count}<br>Status: {level}");"""
|
40 |
|
41 |
-
#
|
42 |
html = f"""
|
43 |
-
<div id=
|
44 |
-
<link rel=
|
45 |
-
<script src=
|
46 |
<script>
|
47 |
var map = L.map('map').setView([-1.935, 30.05], 13);
|
48 |
L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png', {{
|
@@ -52,17 +66,35 @@ def simulate_all_traffic():
|
|
52 |
{markers}
|
53 |
</script>
|
54 |
"""
|
|
|
55 |
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
with gr.Blocks() as demo:
|
59 |
-
gr.Markdown("##
|
|
|
|
|
|
|
|
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
|
66 |
|
67 |
demo.launch()
|
68 |
|
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
+
import time
|
4 |
+
import plotly.graph_objects as go
|
5 |
+
from datetime import datetime
|
6 |
|
|
|
7 |
locations = {
|
8 |
+
"Downtown": {"lat": -1.95, "lon": 30.05},
|
9 |
+
"Highway": {"lat": -1.92, "lon": 30.06},
|
10 |
+
"Industrial Area": {"lat": -1.94, "lon": 30.07},
|
11 |
+
"School Zone": {"lat": -1.93, "lon": 30.03}
|
12 |
}
|
13 |
|
14 |
+
# Global history logs
|
15 |
+
traffic_log = {loc: {"time": [], "speed": [], "vehicles": []} for loc in locations}
|
16 |
+
|
17 |
+
def update_traffic():
|
18 |
report_lines = []
|
19 |
markers = ""
|
20 |
|
21 |
+
now = datetime.now().strftime("%H:%M:%S")
|
22 |
+
|
23 |
for name, info in locations.items():
|
24 |
+
# Random simulation
|
25 |
+
speed = round(random.gauss(30, 15), 1)
|
26 |
+
vehicle_count = int(random.gauss(40, 20))
|
27 |
|
28 |
+
# Save to history
|
29 |
+
traffic_log[name]["time"].append(now)
|
30 |
+
traffic_log[name]["speed"].append(speed)
|
31 |
+
traffic_log[name]["vehicles"].append(vehicle_count)
|
32 |
+
|
33 |
+
# Decide traffic level
|
34 |
if speed < 20 and vehicle_count > 50:
|
35 |
+
level = "π¦ High"
|
36 |
color = "red"
|
37 |
+
alert = f"π¨ Warning: Heavy traffic in {name}"
|
38 |
elif speed < 40 or vehicle_count > 40:
|
39 |
level = "β οΈ Moderate"
|
40 |
color = "orange"
|
41 |
+
alert = f"β οΈ Caution: Moderate traffic in {name}"
|
42 |
else:
|
43 |
level = "β
Light"
|
44 |
color = "green"
|
45 |
+
alert = f"β
{name} traffic is normal"
|
46 |
|
47 |
+
report_lines.append(f"{alert} β {speed} km/h, {vehicle_count} cars")
|
48 |
|
49 |
+
# Leaflet marker
|
50 |
markers += f"""
|
51 |
L.circleMarker([{info["lat"]}, {info["lon"]}], {{
|
52 |
+
color: '{color}', radius: 10
|
|
|
53 |
}}).addTo(map).bindPopup("<b>{name}</b><br>Speed: {speed} km/h<br>Cars: {vehicle_count}<br>Status: {level}");"""
|
54 |
|
55 |
+
# HTML map view
|
56 |
html = f"""
|
57 |
+
<div id='map' style='width:100%;height:500px;'></div>
|
58 |
+
<link rel='stylesheet' href='https://unpkg.com/leaflet/dist/leaflet.css'/>
|
59 |
+
<script src='https://unpkg.com/leaflet/dist/leaflet.js'></script>
|
60 |
<script>
|
61 |
var map = L.map('map').setView([-1.935, 30.05], 13);
|
62 |
L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png', {{
|
|
|
66 |
{markers}
|
67 |
</script>
|
68 |
"""
|
69 |
+
return "\n".join(report_lines), html, create_traffic_chart()
|
70 |
|
71 |
+
def create_traffic_chart():
|
72 |
+
fig = go.Figure()
|
73 |
+
for name in locations:
|
74 |
+
fig.add_trace(go.Scatter(
|
75 |
+
x=traffic_log[name]["time"],
|
76 |
+
y=traffic_log[name]["vehicles"],
|
77 |
+
mode="lines+markers",
|
78 |
+
name=name
|
79 |
+
))
|
80 |
+
fig.update_layout(title="Traffic History (Vehicles Count Over Time)",
|
81 |
+
xaxis_title="Time",
|
82 |
+
yaxis_title="Number of Vehicles",
|
83 |
+
height=400)
|
84 |
+
return fig
|
85 |
|
86 |
with gr.Blocks() as demo:
|
87 |
+
gr.Markdown("## π¦ Smart Traffic Monitor with Map, Alerts & Charts")
|
88 |
+
|
89 |
+
with gr.Row():
|
90 |
+
live_btn = gr.Button("π Refresh Traffic Now")
|
91 |
+
report_box = gr.Textbox(label="π§Ύ Alerts & Reports", lines=10)
|
92 |
|
93 |
+
with gr.Row():
|
94 |
+
map_box = gr.HTML()
|
95 |
+
chart_box = gr.Plot()
|
96 |
|
97 |
+
live_btn.click(fn=update_traffic, inputs=[], outputs=[report_box, map_box, chart_box])
|
98 |
|
99 |
demo.launch()
|
100 |
|