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() # 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
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 |
-
"
|
| 8 |
-
"
|
| 9 |
-
"
|
| 10 |
-
"
|
| 11 |
-
"
|
| 12 |
-
"
|
| 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 |
-
"
|
| 20 |
-
"
|
| 21 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
}
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
G = nx.Graph()
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
def generate_map(start, end):
|
| 43 |
-
m = Map(location=[-
|
| 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=
|
| 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
|
| 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="
|
| 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 |
+
|