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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -25
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, "avg_speed": 30, "avg_vehicles": 60},
7
- "Highway": {"lat": -1.92, "lon": 30.06, "avg_speed": 100, "avg_vehicles": 40},
8
- "Industrial Area": {"lat": -1.94, "lon": 30.07, "avg_speed": 50, "avg_vehicles": 30},
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', {{
@@ -52,17 +66,35 @@ def simulate_all_traffic():
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
 
 
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