Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,7 @@
|
|
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},
|
@@ -11,52 +9,40 @@ locations = {
|
|
11 |
"School Zone": {"lat": -1.93, "lon": 30.03}
|
12 |
}
|
13 |
|
14 |
-
|
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 |
-
#
|
25 |
speed = round(random.gauss(30, 15), 1)
|
26 |
-
|
27 |
|
28 |
-
#
|
29 |
-
|
30 |
-
|
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 |
-
|
38 |
-
|
39 |
-
level = "β οΈ Moderate"
|
40 |
color = "orange"
|
41 |
-
alert = f"β οΈ Caution: Moderate traffic in {name}"
|
42 |
else:
|
43 |
-
|
44 |
color = "green"
|
45 |
-
alert = f"β
{name} traffic is normal"
|
46 |
|
47 |
-
|
48 |
|
49 |
-
#
|
50 |
markers += f"""
|
51 |
-
L.circleMarker([{info[
|
52 |
-
color: '{color}',
|
53 |
-
|
|
|
54 |
|
55 |
-
#
|
56 |
html = f"""
|
57 |
-
<div id=
|
58 |
-
<link rel=
|
59 |
-
<script src=
|
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,35 +52,17 @@ def update_traffic():
|
|
66 |
{markers}
|
67 |
</script>
|
68 |
"""
|
69 |
-
return "\n".join(report_lines), html, create_traffic_chart()
|
70 |
|
71 |
-
|
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("##
|
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 |
-
|
94 |
-
|
95 |
-
|
96 |
|
97 |
-
|
98 |
|
99 |
demo.launch()
|
100 |
|
|
|
1 |
import gradio as gr
|
2 |
import random
|
|
|
|
|
|
|
3 |
|
4 |
+
# Ahantu n'aho biherereye
|
5 |
locations = {
|
6 |
"Downtown": {"lat": -1.95, "lon": 30.05},
|
7 |
"Highway": {"lat": -1.92, "lon": 30.06},
|
|
|
9 |
"School Zone": {"lat": -1.93, "lon": 30.03}
|
10 |
}
|
11 |
|
12 |
+
def simulate_map_only():
|
|
|
|
|
|
|
|
|
13 |
markers = ""
|
14 |
+
alerts = []
|
|
|
15 |
|
16 |
for name, info in locations.items():
|
17 |
+
# Simulate traffic data
|
18 |
speed = round(random.gauss(30, 15), 1)
|
19 |
+
vehicles = int(random.gauss(40, 15))
|
20 |
|
21 |
+
# Define traffic level
|
22 |
+
if speed < 20 and vehicles > 50:
|
23 |
+
status = "π¦ Heavy Traffic"
|
|
|
|
|
|
|
|
|
|
|
24 |
color = "red"
|
25 |
+
elif speed < 40 or vehicles > 40:
|
26 |
+
status = "β οΈ Moderate Traffic"
|
|
|
27 |
color = "orange"
|
|
|
28 |
else:
|
29 |
+
status = "β
Light Traffic"
|
30 |
color = "green"
|
|
|
31 |
|
32 |
+
alerts.append(f"π {name}: {status} β {speed} km/h, {vehicles} cars")
|
33 |
|
34 |
+
# Add marker to map
|
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: {vehicles}<br>Status: {status}");"""
|
40 |
|
41 |
+
# Leaflet Map HTML
|
42 |
html = f"""
|
43 |
+
<div id="map" style="width:100%; height:500px;"></div>
|
44 |
+
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css"/>
|
45 |
+
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
|
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 |
{markers}
|
53 |
</script>
|
54 |
"""
|
|
|
55 |
|
56 |
+
return "\n".join(alerts), html
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
with gr.Blocks() as demo:
|
59 |
+
gr.Markdown("## πΊοΈ Real-Time Traffic Visualization on Map Only")
|
|
|
|
|
|
|
|
|
60 |
|
61 |
+
btn = gr.Button("π Update Traffic")
|
62 |
+
report = gr.Textbox(label="Traffic Status", lines=8)
|
63 |
+
mapview = gr.HTML()
|
64 |
|
65 |
+
btn.click(fn=simulate_map_only, inputs=[], outputs=[report, mapview])
|
66 |
|
67 |
demo.launch()
|
68 |
|