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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -59
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
- # 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,35 +52,17 @@ def update_traffic():
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
 
 
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