Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
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,16 +8,14 @@ locations = {
|
|
9 |
"School Zone": {"lat": -1.93, "lon": 30.03}
|
10 |
}
|
11 |
|
12 |
-
def
|
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"
|
@@ -31,21 +28,20 @@ def simulate_map_only():
|
|
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}}.
|
|
|
49 |
maxZoom: 19
|
50 |
}}).addTo(map);
|
51 |
L.control.zoom({{ position: 'topright' }}).addTo(map);
|
@@ -56,13 +52,12 @@ def simulate_map_only():
|
|
56 |
return "\n".join(alerts), html
|
57 |
|
58 |
with gr.Blocks() as demo:
|
59 |
-
gr.Markdown("## πΊοΈ
|
60 |
|
61 |
-
btn = gr.Button("π
|
62 |
-
report = gr.Textbox(label="Traffic
|
63 |
-
|
64 |
|
65 |
-
btn.click(fn=
|
66 |
|
67 |
demo.launch()
|
68 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import random
|
3 |
|
|
|
4 |
locations = {
|
5 |
"Downtown": {"lat": -1.95, "lon": 30.05},
|
6 |
"Highway": {"lat": -1.92, "lon": 30.06},
|
|
|
8 |
"School Zone": {"lat": -1.93, "lon": 30.03}
|
9 |
}
|
10 |
|
11 |
+
def simulate_map_google_style():
|
12 |
markers = ""
|
13 |
alerts = []
|
14 |
|
15 |
for name, info in locations.items():
|
|
|
16 |
speed = round(random.gauss(30, 15), 1)
|
17 |
vehicles = int(random.gauss(40, 15))
|
18 |
|
|
|
19 |
if speed < 20 and vehicles > 50:
|
20 |
status = "π¦ Heavy Traffic"
|
21 |
color = "red"
|
|
|
28 |
|
29 |
alerts.append(f"π {name}: {status} β {speed} km/h, {vehicles} cars")
|
30 |
|
|
|
31 |
markers += f"""
|
32 |
L.circleMarker([{info['lat']}, {info['lon']}], {{
|
33 |
color: '{color}',
|
34 |
radius: 10
|
35 |
}}).addTo(map).bindPopup("<b>{name}</b><br>Speed: {speed} km/h<br>Cars: {vehicles}<br>Status: {status}");"""
|
36 |
|
|
|
37 |
html = f"""
|
38 |
<div id="map" style="width:100%; height:500px;"></div>
|
39 |
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css"/>
|
40 |
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
|
41 |
<script>
|
42 |
var map = L.map('map').setView([-1.935, 30.05], 13);
|
43 |
+
L.tileLayer('https://{{s}}.basemaps.cartocdn.com/light_all/{{z}}/{{x}}/{{y}}.png', {{
|
44 |
+
attribution: '© <a href="https://carto.com/">CartoDB</a> contributors',
|
45 |
maxZoom: 19
|
46 |
}}).addTo(map);
|
47 |
L.control.zoom({{ position: 'topright' }}).addTo(map);
|
|
|
52 |
return "\n".join(alerts), html
|
53 |
|
54 |
with gr.Blocks() as demo:
|
55 |
+
gr.Markdown("## πΊοΈ Google-Like Traffic Map with Leaflet")
|
56 |
|
57 |
+
btn = gr.Button("π Refresh Traffic Data")
|
58 |
+
report = gr.Textbox(label="Traffic Report", lines=8)
|
59 |
+
map_html = gr.HTML()
|
60 |
|
61 |
+
btn.click(fn=simulate_map_google_style, inputs=[], outputs=[report, map_html])
|
62 |
|
63 |
demo.launch()
|
|