VIATEUR-AI commited on
Commit
ee77ee9
·
verified ·
1 Parent(s): 325ffa3

import gradio as gr import folium import networkx as nx from folium import Map, Marker, PolyLine from math import radians, cos, sin, sqrt, atan2 # Uturere two mu Rwanda hamwe na coordinates (latitude, longitude) places = { "Nyarugenge": (-1.9577, 30.0619), "Gasabo": (-1.9400, 30.0861), "Kicukiro": (-1.9781, 30.0597), "Burera": (-1.4800, 29.7300), "Gakenke": (-1.5700, 29.7561), "Rulindo": (-1.8333, 30.0833), "Musanze": (-1.5014, 29.6344), "Gicumbi": (-1.5794, 30.0542), "Nyagatare": (-1.3100, 30.3000), "Gatsibo": (-1.6800, 30.3900), "Kayonza": (-2.0000, 30.5667), "Kirehe": (-2.3553, 30.7767), "Ngoma": (-2.1600, 30.4700), "Rwamagana": (-1.9491, 30.4349), "Bugesera": (-2.2083, 30.2576), "Kamonyi": (-2.0833, 29.9000), "Muhanga": (-2.1200, 29.7561), "Ruhango": (-2.2136, 29.7628), "Nyamagabe": (-2.4978, 29.4897), "Nyaruguru": (-2.5806, 29.4306), "Huye": (-2.5921, 29.7408), "Gisagara": (-2.6283, 29.6820), "Nyanza": (-2.3566, 29.7507), "Rutsiro": (-2.0986, 29.3269), "Karongi": (-2.0667, 29.4677), "Rubavu": (-1.7481, 29.2730), "Rusizi": (-2.5406, 29.3737), "Nyamasheke": (-2.4700, 29.3222), "Ngororero": (-1.8733, 29.5811) } # Function yo kubara intera hagati y’ahantu habiri (heuristic) def haversine(coord1, coord2): R = 6371 # Radius y'isi muri km lat1, lon1 = coord1 lat2, lon2 = coord2 dlat = radians(lat2 - lat1) dlon = radians(lon2 - lon1) a = sin(dlat/2)**2 + cos(radians(lat1)) * cos(radians(lat2)) * sin(dlon/2)**2 c = 2 * atan2(sqrt(a), sqrt(1 - a)) return R * c # Kubaka graph y’inzira G = nx.Graph() # Twongera edges hagati y’uturere dushobora guhuza (weight ni intera ya km ukoreshheje haversine) edges = [ ("Nyarugenge", "Gasabo"), ("Gasabo", "Kicukiro"), ("Burera", "Gakenke"), ("Gakenke", "Rulindo"), ("Musanze", "Gicumbi"), ("Nyagatare", "Gatsibo"), ("Kayonza", "Kirehe"), ("Ngoma", "Rwamagana"), ("Bugesera", "Kamonyi"), ("Muhanga", "Ruhango"), ("Nyamagabe", "Nyaruguru"), ("Huye", "Gisagara"), ("Nyanza", "Ruhango"), ("Rutsiro", "Karongi"), ("Rubavu", "Rusizi"), ("Nyamasheke", "Ngororero"), ("Rulindo", "Gasabo"), ("Gicumbi", "Nyagatare"), ("Kicukiro", "Kamonyi"), ("Kamonyi", "Muhanga"), ("Ruhango", "Nyamagabe"), ("Karongi", "Rubavu"), ("Rusizi", "Nyamasheke"), ("Ngororero", "Rutsiro") ] for edge in edges: coord1 = places[edge[0]] coord2 = places[edge[1]] distance = haversine(coord1, coord2) G.add_edge(edge[0], edge[1], weight=distance) # Function yerekana intera hagati y’uturere mu graph nk'heuristic def heuristic(u, v): return haversine(places[u], places[v]) def generate_map(start, end): m = Map(location=[-2.0, 30.0], zoom_start=8) for name, coords in places.items(): Marker(location=coords, popup=name).add_to(m) if start == end: return "Hitamo aho utangiriye n’aho ugiye bitandukanye.", m._repr_html_() try: path = nx.astar_path(G, start, end, heuristic=heuristic, weight='weight') coords = [places[p] for p in path] PolyLine(coords, color="blue", weight=5).add_to(m) return "Inzira ngufi ni: " + " ➔ ".join(path), m._repr_html_() except Exception as e: return f"Ntibishoboka kubona inzira: {str(e)}", m._repr_html_() iface = gr.Interface( fn=generate_map, inputs=[ gr.Dropdown(list(places.keys()), label="Hitamo aho uri"), gr.Dropdown(list(places.keys()), label="Hitamo aho ugiye") ], outputs=[ gr.Textbox(label="Ubutumwa"), gr.HTML(label="Ikarita") ], title="🗺️ VIATEUR AI Rwanda Smart Route Planner" ) iface.launch()

Browse files
Files changed (1) hide show
  1. app.py +86 -35
app.py CHANGED
@@ -2,45 +2,95 @@ import gradio as gr
2
  import folium
3
  import networkx as nx
4
  from folium import Map, Marker, PolyLine
 
5
 
 
6
  places = {
7
- "Nyamirambo": (-1.9706, 30.0423),
8
- "Downtown": (-1.9441, 30.0619),
9
- "Kacyiru": (-1.9444, 30.0912),
10
- "Kimironko": (-1.9300, 30.1250),
11
- "Remera": (-1.9350, 30.1150),
12
- "Gikondo": (-1.9780, 30.0300),
13
- "Nyarugenge": (-1.9517, 30.0587),
14
- "Kicukiro": (-1.9761, 30.0992),
15
- "Kimihurura": (-1.9485, 30.0741),
16
- "Nyamata": (-2.0633, 30.3203),
17
- "Rwamagana": (-1.9489, 30.4367),
18
  "Musanze": (-1.5014, 29.6344),
19
- "Huye": (-2.5906, 29.7399),
20
- "Rubavu": (-1.7044, 29.2597),
21
- "Gisenyi": (-1.7011, 29.2568)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  }
23
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  G = nx.Graph()
25
- G.add_edge("Nyamirambo", "Downtown", weight=10)
26
- G.add_edge("Downtown", "Kacyiru", weight=5)
27
- G.add_edge("Kacyiru", "Kimironko", weight=7)
28
- G.add_edge("Nyamirambo", "Kimironko", weight=20)
29
- G.add_edge("Kacyiru", "Remera", weight=3)
30
- G.add_edge("Remera", "Kimihurura", weight=4)
31
- G.add_edge("Kimihurura", "Kacyiru", weight=6)
32
- G.add_edge("Gikondo", "Nyamirambo", weight=6)
33
- G.add_edge("Nyarugenge", "Downtown", weight=2)
34
- G.add_edge("Kicukiro", "Gikondo", weight=4)
35
- G.add_edge("Nyamata", "Rwamagana", weight=18)
36
- G.add_edge("Rwamagana", "Kacyiru", weight=22)
37
- G.add_edge("Musanze", "Rubavu", weight=60)
38
- G.add_edge("Rubavu", "Gisenyi", weight=3)
39
- G.add_edge("Huye", "Nyamata", weight=75)
40
- G.add_edge("Kicukiro", "Kimihurura", weight=12)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  def generate_map(start, end):
43
- m = Map(location=[-1.95, 30.08], zoom_start=11)
44
  for name, coords in places.items():
45
  Marker(location=coords, popup=name).add_to(m)
46
 
@@ -48,12 +98,12 @@ def generate_map(start, end):
48
  return "Hitamo aho utangiriye n’aho ugiye bitandukanye.", m._repr_html_()
49
 
50
  try:
51
- path = nx.astar_path(G, start, end, heuristic=lambda u,v: 0)
52
  coords = [places[p] for p in path]
53
  PolyLine(coords, color="blue", weight=5).add_to(m)
54
  return "Inzira ngufi ni: " + " ➔ ".join(path), m._repr_html_()
55
- except:
56
- return "Ntibishoboka kubona inzira.", m._repr_html_()
57
 
58
  iface = gr.Interface(
59
  fn=generate_map,
@@ -63,9 +113,10 @@ iface = gr.Interface(
63
  ],
64
  outputs=[
65
  gr.Textbox(label="Ubutumwa"),
66
- gr.HTML(label="Map")
67
  ],
68
  title="🗺️ VIATEUR AI Rwanda Smart Route Planner"
69
  )
70
 
71
  iface.launch()
 
 
2
  import folium
3
  import networkx as nx
4
  from folium import Map, Marker, PolyLine
5
+ from math import radians, cos, sin, sqrt, atan2
6
 
7
+ # Uturere two mu Rwanda hamwe na coordinates (latitude, longitude)
8
  places = {
9
+ "Nyarugenge": (-1.9577, 30.0619),
10
+ "Gasabo": (-1.9400, 30.0861),
11
+ "Kicukiro": (-1.9781, 30.0597),
12
+ "Burera": (-1.4800, 29.7300),
13
+ "Gakenke": (-1.5700, 29.7561),
14
+ "Rulindo": (-1.8333, 30.0833),
 
 
 
 
 
15
  "Musanze": (-1.5014, 29.6344),
16
+ "Gicumbi": (-1.5794, 30.0542),
17
+ "Nyagatare": (-1.3100, 30.3000),
18
+ "Gatsibo": (-1.6800, 30.3900),
19
+ "Kayonza": (-2.0000, 30.5667),
20
+ "Kirehe": (-2.3553, 30.7767),
21
+ "Ngoma": (-2.1600, 30.4700),
22
+ "Rwamagana": (-1.9491, 30.4349),
23
+ "Bugesera": (-2.2083, 30.2576),
24
+ "Kamonyi": (-2.0833, 29.9000),
25
+ "Muhanga": (-2.1200, 29.7561),
26
+ "Ruhango": (-2.2136, 29.7628),
27
+ "Nyamagabe": (-2.4978, 29.4897),
28
+ "Nyaruguru": (-2.5806, 29.4306),
29
+ "Huye": (-2.5921, 29.7408),
30
+ "Gisagara": (-2.6283, 29.6820),
31
+ "Nyanza": (-2.3566, 29.7507),
32
+ "Rutsiro": (-2.0986, 29.3269),
33
+ "Karongi": (-2.0667, 29.4677),
34
+ "Rubavu": (-1.7481, 29.2730),
35
+ "Rusizi": (-2.5406, 29.3737),
36
+ "Nyamasheke": (-2.4700, 29.3222),
37
+ "Ngororero": (-1.8733, 29.5811)
38
  }
39
 
40
+ # Function yo kubara intera hagati y’ahantu habiri (heuristic)
41
+ def haversine(coord1, coord2):
42
+ R = 6371 # Radius y'isi muri km
43
+ lat1, lon1 = coord1
44
+ lat2, lon2 = coord2
45
+ dlat = radians(lat2 - lat1)
46
+ dlon = radians(lon2 - lon1)
47
+ a = sin(dlat/2)**2 + cos(radians(lat1)) * cos(radians(lat2)) * sin(dlon/2)**2
48
+ c = 2 * atan2(sqrt(a), sqrt(1 - a))
49
+ return R * c
50
+
51
+ # Kubaka graph y’inzira
52
  G = nx.Graph()
53
+
54
+ # Twongera edges hagati y’uturere dushobora guhuza (weight ni intera ya km ukoreshheje haversine)
55
+ edges = [
56
+ ("Nyarugenge", "Gasabo"),
57
+ ("Gasabo", "Kicukiro"),
58
+ ("Burera", "Gakenke"),
59
+ ("Gakenke", "Rulindo"),
60
+ ("Musanze", "Gicumbi"),
61
+ ("Nyagatare", "Gatsibo"),
62
+ ("Kayonza", "Kirehe"),
63
+ ("Ngoma", "Rwamagana"),
64
+ ("Bugesera", "Kamonyi"),
65
+ ("Muhanga", "Ruhango"),
66
+ ("Nyamagabe", "Nyaruguru"),
67
+ ("Huye", "Gisagara"),
68
+ ("Nyanza", "Ruhango"),
69
+ ("Rutsiro", "Karongi"),
70
+ ("Rubavu", "Rusizi"),
71
+ ("Nyamasheke", "Ngororero"),
72
+ ("Rulindo", "Gasabo"),
73
+ ("Gicumbi", "Nyagatare"),
74
+ ("Kicukiro", "Kamonyi"),
75
+ ("Kamonyi", "Muhanga"),
76
+ ("Ruhango", "Nyamagabe"),
77
+ ("Karongi", "Rubavu"),
78
+ ("Rusizi", "Nyamasheke"),
79
+ ("Ngororero", "Rutsiro")
80
+ ]
81
+
82
+ for edge in edges:
83
+ coord1 = places[edge[0]]
84
+ coord2 = places[edge[1]]
85
+ distance = haversine(coord1, coord2)
86
+ G.add_edge(edge[0], edge[1], weight=distance)
87
+
88
+ # Function yerekana intera hagati y’uturere mu graph nk'heuristic
89
+ def heuristic(u, v):
90
+ return haversine(places[u], places[v])
91
 
92
  def generate_map(start, end):
93
+ m = Map(location=[-2.0, 30.0], zoom_start=8)
94
  for name, coords in places.items():
95
  Marker(location=coords, popup=name).add_to(m)
96
 
 
98
  return "Hitamo aho utangiriye n’aho ugiye bitandukanye.", m._repr_html_()
99
 
100
  try:
101
+ path = nx.astar_path(G, start, end, heuristic=heuristic, weight='weight')
102
  coords = [places[p] for p in path]
103
  PolyLine(coords, color="blue", weight=5).add_to(m)
104
  return "Inzira ngufi ni: " + " ➔ ".join(path), m._repr_html_()
105
+ except Exception as e:
106
+ return f"Ntibishoboka kubona inzira: {str(e)}", m._repr_html_()
107
 
108
  iface = gr.Interface(
109
  fn=generate_map,
 
113
  ],
114
  outputs=[
115
  gr.Textbox(label="Ubutumwa"),
116
+ gr.HTML(label="Ikarita")
117
  ],
118
  title="🗺️ VIATEUR AI Rwanda Smart Route Planner"
119
  )
120
 
121
  iface.launch()
122
+