VIATEUR-AI commited on
Commit
6ec8d71
Β·
verified Β·
1 Parent(s): 33e50c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -37
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import gradio as gr
2
  import random
3
 
4
- # Ahantu na coordinates
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 simulate_traffic_map(area):
13
- info = locations[area]
14
- speed = round(random.gauss(info["avg_speed"], 10), 1)
15
- vehicle_count = int(random.gauss(info["avg_vehicles"], 10))
16
- if speed < 20 and vehicle_count > 50:
17
- traffic_level = "🚦 High Traffic Jam"
18
- status = "Blocked"
19
- elif speed < 40 or vehicle_count > 40:
20
- traffic_level = "⚠️ Moderate Traffic"
21
- status = "Restricted"
22
- else:
23
- traffic_level = "βœ… Light Traffic"
24
- status = "Clear"
25
-
26
- report = (f"πŸ“ **{area}**\n"
27
- f"πŸš— Speed: {speed} km/h\n"
28
- f"πŸš™ Vehicles: {vehicle_count}\n"
29
- f"πŸ“Š Status: {status}\n"
30
- f"πŸ“£ Traffic: {traffic_level}")
31
-
32
- lat, lon = info["lat"], info["lon"]
 
 
 
 
 
 
 
 
 
33
  html = f"""
34
- <div id="map" style="width:100%;height:400px;"></div>
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([{lat}, {lon}], 14);
39
  L.tileLayer('https://{{s}}.tile.openstreetmap.org/{{z}}/{{x}}/{{y}}.png', {{
40
  maxZoom: 19
41
  }}).addTo(map);
42
- L.marker([{lat}, {lon}])
43
- .addTo(map)
44
- .bindPopup("{area}: {traffic_level}")
45
- .openPopup();
46
  </script>
47
  """
48
- return report, html
 
49
 
50
  with gr.Blocks() as demo:
51
- gr.Markdown("## πŸ—ΊοΈ Traffic Monitoring with Real Map View")
52
-
53
- location = gr.Dropdown(list(locations.keys()), label="Choose Location", value="Downtown")
54
- simulate_btn = gr.Button("Simulate Traffic")
55
- output = gr.Markdown()
56
- map_view = gr.HTML()
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