Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
|
| 4 |
-
# Ahantu na
|
| 5 |
locations = {
|
| 6 |
"Downtown": {"lat": -1.95, "lon": 30.05, "avg_speed": 30, "avg_vehicles": 60},
|
| 7 |
"Highway": {"lat": -1.92, "lon": 30.06, "avg_speed": 100, "avg_vehicles": 40},
|
|
@@ -9,53 +9,60 @@ locations = {
|
|
| 9 |
"School Zone": {"lat": -1.93, "lon": 30.03, "avg_speed": 20, "avg_vehicles": 20}
|
| 10 |
}
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
html = f"""
|
| 34 |
-
<div id="map" style="width:100%;height:
|
| 35 |
<link rel="stylesheet" href="https://unpkg.com/leaflet/dist/leaflet.css"/>
|
| 36 |
<script src="https://unpkg.com/leaflet/dist/leaflet.js"></script>
|
| 37 |
<script>
|
| 38 |
-
var map = L.map('map').setView([
|
| 39 |
L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png', {{
|
| 40 |
maxZoom: 19
|
| 41 |
}}).addTo(map);
|
| 42 |
-
L.
|
| 43 |
-
|
| 44 |
-
.bindPopup("{area}: {traffic_level}")
|
| 45 |
-
.openPopup();
|
| 46 |
</script>
|
| 47 |
"""
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
with gr.Blocks() as demo:
|
| 51 |
-
gr.Markdown("## πΊοΈ Traffic
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
simulate_btn.click(fn=simulate_traffic_map, inputs=location, outputs=[output, map_view])
|
| 59 |
|
| 60 |
demo.launch()
|
| 61 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import random
|
| 3 |
|
| 4 |
+
# Ahantu na info
|
| 5 |
locations = {
|
| 6 |
"Downtown": {"lat": -1.95, "lon": 30.05, "avg_speed": 30, "avg_vehicles": 60},
|
| 7 |
"Highway": {"lat": -1.92, "lon": 30.06, "avg_speed": 100, "avg_vehicles": 40},
|
|
|
|
| 9 |
"School Zone": {"lat": -1.93, "lon": 30.03, "avg_speed": 20, "avg_vehicles": 20}
|
| 10 |
}
|
| 11 |
|
| 12 |
+
def simulate_all_traffic():
|
| 13 |
+
report_lines = []
|
| 14 |
+
markers = ""
|
| 15 |
+
|
| 16 |
+
for name, info in locations.items():
|
| 17 |
+
# Random traffic values
|
| 18 |
+
speed = round(random.gauss(info["avg_speed"], 10), 1)
|
| 19 |
+
vehicle_count = int(random.gauss(info["avg_vehicles"], 10))
|
| 20 |
+
|
| 21 |
+
# Traffic level + color
|
| 22 |
+
if speed < 20 and vehicle_count > 50:
|
| 23 |
+
level = "π¦ High Traffic"
|
| 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"π {name}: {level} β {speed} km/h, {vehicle_count} vehicles")
|
| 33 |
+
|
| 34 |
+
# Add marker for this location
|
| 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 |
+
# Map HTML with all markers
|
| 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', {{
|
| 49 |
maxZoom: 19
|
| 50 |
}}).addTo(map);
|
| 51 |
+
L.control.zoom({{ position: 'topright' }}).addTo(map);
|
| 52 |
+
{markers}
|
|
|
|
|
|
|
| 53 |
</script>
|
| 54 |
"""
|
| 55 |
+
|
| 56 |
+
return "\n".join(report_lines), html
|
| 57 |
|
| 58 |
with gr.Blocks() as demo:
|
| 59 |
+
gr.Markdown("## πΊοΈ Advanced Traffic Map Simulation with Real-Time Markers")
|
| 60 |
+
|
| 61 |
+
simulate = gr.Button("Simulate Traffic Across All Locations")
|
| 62 |
+
report = gr.Textbox(label="Traffic Report", lines=8)
|
| 63 |
+
map_html = gr.HTML()
|
| 64 |
+
|
| 65 |
+
simulate.click(fn=simulate_all_traffic, inputs=[], outputs=[report, map_html])
|
|
|
|
| 66 |
|
| 67 |
demo.launch()
|
| 68 |
|