Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -6,7 +6,7 @@ from math import radians, cos, sin, sqrt, atan2
|
|
6 |
from branca.element import MacroElement
|
7 |
from jinja2 import Template
|
8 |
|
9 |
-
# 1. Districts
|
10 |
places = {
|
11 |
"Nyarugenge": (-1.9577, 30.0619), "Gasabo": (-1.9400, 30.0861), "Kicukiro": (-1.9781, 30.0597),
|
12 |
"Burera": (-1.4800, 29.7300), "Gakenke": (-1.5700, 29.7561), "Rulindo": (-1.8333, 30.0833),
|
@@ -20,7 +20,7 @@ places = {
|
|
20 |
"Nyamasheke": (-2.4700, 29.3222), "Ngororero": (-1.8733, 29.5811)
|
21 |
}
|
22 |
|
23 |
-
# 2.
|
24 |
def haversine(coord1, coord2):
|
25 |
R = 6371
|
26 |
lat1, lon1 = coord1
|
@@ -31,7 +31,7 @@ def haversine(coord1, coord2):
|
|
31 |
c = 2 * atan2(sqrt(a), sqrt(1 - a))
|
32 |
return R * c
|
33 |
|
34 |
-
# 3.
|
35 |
edges = [
|
36 |
("Nyarugenge", "Gasabo"), ("Gasabo", "Kicukiro"), ("Kicukiro", "Bugesera"), ("Bugesera", "Rwamagana"),
|
37 |
("Rwamagana", "Kayonza"), ("Kayonza", "Kirehe"), ("Kirehe", "Ngoma"), ("Ngoma", "Gatsibo"),
|
@@ -44,17 +44,12 @@ edges = [
|
|
44 |
("Gicumbi", "Gasabo"), ("Bugesera", "Ngoma")
|
45 |
]
|
46 |
|
47 |
-
# 4.
|
48 |
G = nx.Graph()
|
49 |
for u, v in edges:
|
50 |
-
|
51 |
-
G.add_edge(u, v, weight=dist)
|
52 |
|
53 |
-
# 5.
|
54 |
-
def heuristic(u, v):
|
55 |
-
return haversine(places[u], places[v])
|
56 |
-
|
57 |
-
# 6. Folium animation class
|
58 |
class AnimateMarker(MacroElement):
|
59 |
_template = Template("""
|
60 |
{% macro script(this, kwargs) %}
|
@@ -77,7 +72,7 @@ class AnimateMarker(MacroElement):
|
|
77 |
self._name = "AnimateMarker"
|
78 |
self.locations = locations
|
79 |
|
80 |
-
#
|
81 |
def generate_map(start, end):
|
82 |
if start == end:
|
83 |
return "Hitamo aho utangiriye n’aho ugiye bitandukanye.", ""
|
@@ -86,7 +81,7 @@ def generate_map(start, end):
|
|
86 |
return f"Nta nzira ibaho hagati ya {start} na {end}.", ""
|
87 |
|
88 |
try:
|
89 |
-
path = nx.astar_path(G, start, end, heuristic=
|
90 |
coords = [places[p] for p in path]
|
91 |
|
92 |
m = Map(location=[-2.0, 30.0], zoom_start=8)
|
@@ -100,7 +95,7 @@ def generate_map(start, end):
|
|
100 |
except Exception as e:
|
101 |
return f"Ntibishoboka kubona inzira: {str(e)}", ""
|
102 |
|
103 |
-
#
|
104 |
iface = gr.Interface(
|
105 |
fn=generate_map,
|
106 |
inputs=[
|
@@ -111,7 +106,9 @@ iface = gr.Interface(
|
|
111 |
gr.Textbox(label="Ubutumwa"),
|
112 |
gr.HTML(label="Ikarita")
|
113 |
],
|
114 |
-
title="🗺️ VIATEUR AI Rwanda Smart Route Planner
|
|
|
115 |
)
|
116 |
|
117 |
iface.launch()
|
|
|
|
6 |
from branca.element import MacroElement
|
7 |
from jinja2 import Template
|
8 |
|
9 |
+
# 1. Districts and Coordinates
|
10 |
places = {
|
11 |
"Nyarugenge": (-1.9577, 30.0619), "Gasabo": (-1.9400, 30.0861), "Kicukiro": (-1.9781, 30.0597),
|
12 |
"Burera": (-1.4800, 29.7300), "Gakenke": (-1.5700, 29.7561), "Rulindo": (-1.8333, 30.0833),
|
|
|
20 |
"Nyamasheke": (-2.4700, 29.3222), "Ngororero": (-1.8733, 29.5811)
|
21 |
}
|
22 |
|
23 |
+
# 2. Distance calculator
|
24 |
def haversine(coord1, coord2):
|
25 |
R = 6371
|
26 |
lat1, lon1 = coord1
|
|
|
31 |
c = 2 * atan2(sqrt(a), sqrt(1 - a))
|
32 |
return R * c
|
33 |
|
34 |
+
# 3. District Connections
|
35 |
edges = [
|
36 |
("Nyarugenge", "Gasabo"), ("Gasabo", "Kicukiro"), ("Kicukiro", "Bugesera"), ("Bugesera", "Rwamagana"),
|
37 |
("Rwamagana", "Kayonza"), ("Kayonza", "Kirehe"), ("Kirehe", "Ngoma"), ("Ngoma", "Gatsibo"),
|
|
|
44 |
("Gicumbi", "Gasabo"), ("Bugesera", "Ngoma")
|
45 |
]
|
46 |
|
47 |
+
# 4. Create Graph
|
48 |
G = nx.Graph()
|
49 |
for u, v in edges:
|
50 |
+
G.add_edge(u, v, weight=haversine(places[u], places[v]))
|
|
|
51 |
|
52 |
+
# 5. Animate Marker
|
|
|
|
|
|
|
|
|
53 |
class AnimateMarker(MacroElement):
|
54 |
_template = Template("""
|
55 |
{% macro script(this, kwargs) %}
|
|
|
72 |
self._name = "AnimateMarker"
|
73 |
self.locations = locations
|
74 |
|
75 |
+
# 6. Core route function
|
76 |
def generate_map(start, end):
|
77 |
if start == end:
|
78 |
return "Hitamo aho utangiriye n’aho ugiye bitandukanye.", ""
|
|
|
81 |
return f"Nta nzira ibaho hagati ya {start} na {end}.", ""
|
82 |
|
83 |
try:
|
84 |
+
path = nx.astar_path(G, start, end, heuristic=lambda u, v: haversine(places[u], places[v]), weight='weight')
|
85 |
coords = [places[p] for p in path]
|
86 |
|
87 |
m = Map(location=[-2.0, 30.0], zoom_start=8)
|
|
|
95 |
except Exception as e:
|
96 |
return f"Ntibishoboka kubona inzira: {str(e)}", ""
|
97 |
|
98 |
+
# 7. Gradio App
|
99 |
iface = gr.Interface(
|
100 |
fn=generate_map,
|
101 |
inputs=[
|
|
|
106 |
gr.Textbox(label="Ubutumwa"),
|
107 |
gr.HTML(label="Ikarita")
|
108 |
],
|
109 |
+
title="🗺️ VIATEUR AI - Rwanda Smart Route Planner",
|
110 |
+
theme="default"
|
111 |
)
|
112 |
|
113 |
iface.launch()
|
114 |
+
|