Spaces:
Sleeping
Sleeping
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() # Edges zihuje uturere, weight ni intera ya km ukoresheje 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"), # Edges zunganira connectivity ("Gasabo", "Rwamagana"), ("Rwamagana", "Nyagatare"), ("Nyagatare", "Kayonza"), ("Kicukiro", "Muhanga"), ("Muhanga", "Nyamagabe"), ("Nyamagabe", "Rusizi"), ("Rusizi", "Rubavu"), ("Rubavu", "Nyamasheke"), ("Nyamasheke", "Karongi"), ("Karongi", "Rutsiro"), ("Rutsiro", "Ngororero"), ("Ngororero", "Gakenke"), ("Gakenke", "Burera") ] 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) # Heuristic function yerekana intera hagati y’uturere mu graph 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_() if not nx.has_path(G, start, end): return f"Nta nzira ibaho hagati ya {start} na {end}.", 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="🗺️ Rwanda Smart Route Planner" ) iface.launch()
Browse files
app.py
CHANGED
@@ -51,7 +51,7 @@ def haversine(coord1, coord2):
|
|
51 |
# Kubaka graph y’inzira
|
52 |
G = nx.Graph()
|
53 |
|
54 |
-
#
|
55 |
edges = [
|
56 |
("Nyarugenge", "Gasabo"),
|
57 |
("Gasabo", "Kicukiro"),
|
@@ -76,7 +76,22 @@ edges = [
|
|
76 |
("Ruhango", "Nyamagabe"),
|
77 |
("Karongi", "Rubavu"),
|
78 |
("Rusizi", "Nyamasheke"),
|
79 |
-
("Ngororero", "Rutsiro")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
]
|
81 |
|
82 |
for edge in edges:
|
@@ -85,7 +100,7 @@ for edge in edges:
|
|
85 |
distance = haversine(coord1, coord2)
|
86 |
G.add_edge(edge[0], edge[1], weight=distance)
|
87 |
|
88 |
-
#
|
89 |
def heuristic(u, v):
|
90 |
return haversine(places[u], places[v])
|
91 |
|
@@ -97,6 +112,9 @@ def generate_map(start, end):
|
|
97 |
if start == end:
|
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]
|
@@ -115,8 +133,9 @@ iface = gr.Interface(
|
|
115 |
gr.Textbox(label="Ubutumwa"),
|
116 |
gr.HTML(label="Ikarita")
|
117 |
],
|
118 |
-
title="🗺️
|
119 |
)
|
120 |
|
121 |
iface.launch()
|
122 |
|
|
|
|
51 |
# Kubaka graph y’inzira
|
52 |
G = nx.Graph()
|
53 |
|
54 |
+
# Edges zihuje uturere, weight ni intera ya km ukoresheje haversine
|
55 |
edges = [
|
56 |
("Nyarugenge", "Gasabo"),
|
57 |
("Gasabo", "Kicukiro"),
|
|
|
76 |
("Ruhango", "Nyamagabe"),
|
77 |
("Karongi", "Rubavu"),
|
78 |
("Rusizi", "Nyamasheke"),
|
79 |
+
("Ngororero", "Rutsiro"),
|
80 |
+
|
81 |
+
# Edges zunganira connectivity
|
82 |
+
("Gasabo", "Rwamagana"),
|
83 |
+
("Rwamagana", "Nyagatare"),
|
84 |
+
("Nyagatare", "Kayonza"),
|
85 |
+
("Kicukiro", "Muhanga"),
|
86 |
+
("Muhanga", "Nyamagabe"),
|
87 |
+
("Nyamagabe", "Rusizi"),
|
88 |
+
("Rusizi", "Rubavu"),
|
89 |
+
("Rubavu", "Nyamasheke"),
|
90 |
+
("Nyamasheke", "Karongi"),
|
91 |
+
("Karongi", "Rutsiro"),
|
92 |
+
("Rutsiro", "Ngororero"),
|
93 |
+
("Ngororero", "Gakenke"),
|
94 |
+
("Gakenke", "Burera")
|
95 |
]
|
96 |
|
97 |
for edge in edges:
|
|
|
100 |
distance = haversine(coord1, coord2)
|
101 |
G.add_edge(edge[0], edge[1], weight=distance)
|
102 |
|
103 |
+
# Heuristic function yerekana intera hagati y’uturere mu graph
|
104 |
def heuristic(u, v):
|
105 |
return haversine(places[u], places[v])
|
106 |
|
|
|
112 |
if start == end:
|
113 |
return "Hitamo aho utangiriye n’aho ugiye bitandukanye.", m._repr_html_()
|
114 |
|
115 |
+
if not nx.has_path(G, start, end):
|
116 |
+
return f"Nta nzira ibaho hagati ya {start} na {end}.", m._repr_html_()
|
117 |
+
|
118 |
try:
|
119 |
path = nx.astar_path(G, start, end, heuristic=heuristic, weight='weight')
|
120 |
coords = [places[p] for p in path]
|
|
|
133 |
gr.Textbox(label="Ubutumwa"),
|
134 |
gr.HTML(label="Ikarita")
|
135 |
],
|
136 |
+
title="🗺️ Rwanda Smart Route Planner"
|
137 |
)
|
138 |
|
139 |
iface.launch()
|
140 |
|
141 |
+
|