Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 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 |
from branca.element import MacroElement
|
| 7 |
from jinja2 import Template
|
|
@@ -40,7 +40,7 @@ places = {
|
|
| 40 |
"Ngororero": (-1.8733, 29.5811)
|
| 41 |
}
|
| 42 |
|
| 43 |
-
# 2. Place images URLs
|
| 44 |
place_images = {
|
| 45 |
"Kigali": "https://upload.wikimedia.org/wikipedia/commons/8/89/Kigali_city_view.jpg",
|
| 46 |
"Nyarugenge": "https://upload.wikimedia.org/wikipedia/commons/f/f8/Nyarugenge_district.jpg",
|
|
@@ -96,7 +96,6 @@ edges = [
|
|
| 96 |
("Ruhango", "Nyanza"), ("Nyanza", "Huye"), ("Huye", "Gisagara"), ("Gisagara", "Nyaruguru"),
|
| 97 |
("Nyaruguru", "Nyamagabe"), ("Nyamagabe", "Karongi"), ("Ngororero", "Ruhango"),
|
| 98 |
("Gicumbi", "Gasabo"), ("Bugesera", "Ngoma"),
|
| 99 |
-
# Kigali connections
|
| 100 |
("Kigali", "Nyarugenge"), ("Kigali", "Gasabo"), ("Kigali", "Kicukiro")
|
| 101 |
]
|
| 102 |
|
|
@@ -105,7 +104,7 @@ G = nx.Graph()
|
|
| 105 |
for u, v in edges:
|
| 106 |
G.add_edge(u, v, weight=haversine(places[u], places[v]))
|
| 107 |
|
| 108 |
-
# 6. Animation class for marker movement
|
| 109 |
class AnimateMarker(MacroElement):
|
| 110 |
_template = Template("""
|
| 111 |
{% macro script(this, kwargs) %}
|
|
@@ -128,7 +127,7 @@ class AnimateMarker(MacroElement):
|
|
| 128 |
self._name = "AnimateMarker"
|
| 129 |
self.locations = locations
|
| 130 |
|
| 131 |
-
# 7. Routing function with images
|
| 132 |
def generate_map(start, end):
|
| 133 |
if start == end:
|
| 134 |
return "Hitamo aho utangiriye n’aho ugiye bitandukanye.", "", ""
|
|
@@ -141,50 +140,41 @@ def generate_map(start, end):
|
|
| 141 |
coords = [places[p] for p in path]
|
| 142 |
|
| 143 |
m = Map(location=[-1.9441, 30.0619], zoom_start=9)
|
| 144 |
-
for name, coord in places.items():
|
| 145 |
-
Marker(location=coord, popup=name).add_to(m)
|
| 146 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
PolyLine(coords, color="blue", weight=5).add_to(m)
|
| 148 |
m.add_child(AnimateMarker(coords))
|
| 149 |
|
| 150 |
-
|
| 151 |
-
start_img = place_images.get(start, "https://via.placeholder.com/300?text=No+Image")
|
| 152 |
-
end_img = place_images.get(end, "https://via.placeholder.com/300?text=No+Image")
|
| 153 |
-
|
| 154 |
-
images_html = f"""
|
| 155 |
-
<div style='display:flex; justify-content:space-around; margin-top: 10px;'>
|
| 156 |
-
<div style='text-align:center;'>
|
| 157 |
-
<h4>{start}</h4>
|
| 158 |
-
<img src="{start_img}" alt="{start}" width="300" style="border-radius: 8px;"/>
|
| 159 |
-
</div>
|
| 160 |
-
<div style='text-align:center;'>
|
| 161 |
-
<h4>{end}</h4>
|
| 162 |
-
<img src="{end_img}" alt="{end}" width="300" style="border-radius: 8px;"/>
|
| 163 |
-
</div>
|
| 164 |
-
</div>
|
| 165 |
-
"""
|
| 166 |
-
|
| 167 |
-
return "Inzira ngufi ni: " + " ➔ ".join(path), m._repr_html_(), images_html
|
| 168 |
|
| 169 |
except Exception as e:
|
| 170 |
return f"Ntibishoboka kubona inzira: {str(e)}", "", ""
|
| 171 |
|
| 172 |
-
# 8. Gradio
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
title="🗺️ VIATEUR AI - Rwanda Smart Route Planner",
|
| 185 |
-
theme="default"
|
| 186 |
-
)
|
| 187 |
-
|
| 188 |
-
if __name__ == "__main__":
|
| 189 |
-
iface.launch()
|
| 190 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import folium
|
| 3 |
import networkx as nx
|
| 4 |
+
from folium import Map, Marker, PolyLine, Popup, IFrame
|
| 5 |
from math import radians, cos, sin, sqrt, atan2
|
| 6 |
from branca.element import MacroElement
|
| 7 |
from jinja2 import Template
|
|
|
|
| 40 |
"Ngororero": (-1.8733, 29.5811)
|
| 41 |
}
|
| 42 |
|
| 43 |
+
# 2. Place images URLs
|
| 44 |
place_images = {
|
| 45 |
"Kigali": "https://upload.wikimedia.org/wikipedia/commons/8/89/Kigali_city_view.jpg",
|
| 46 |
"Nyarugenge": "https://upload.wikimedia.org/wikipedia/commons/f/f8/Nyarugenge_district.jpg",
|
|
|
|
| 96 |
("Ruhango", "Nyanza"), ("Nyanza", "Huye"), ("Huye", "Gisagara"), ("Gisagara", "Nyaruguru"),
|
| 97 |
("Nyaruguru", "Nyamagabe"), ("Nyamagabe", "Karongi"), ("Ngororero", "Ruhango"),
|
| 98 |
("Gicumbi", "Gasabo"), ("Bugesera", "Ngoma"),
|
|
|
|
| 99 |
("Kigali", "Nyarugenge"), ("Kigali", "Gasabo"), ("Kigali", "Kicukiro")
|
| 100 |
]
|
| 101 |
|
|
|
|
| 104 |
for u, v in edges:
|
| 105 |
G.add_edge(u, v, weight=haversine(places[u], places[v]))
|
| 106 |
|
| 107 |
+
# 6. Animation class for marker movement
|
| 108 |
class AnimateMarker(MacroElement):
|
| 109 |
_template = Template("""
|
| 110 |
{% macro script(this, kwargs) %}
|
|
|
|
| 127 |
self._name = "AnimateMarker"
|
| 128 |
self.locations = locations
|
| 129 |
|
| 130 |
+
# 7. Routing function with popup images on map markers
|
| 131 |
def generate_map(start, end):
|
| 132 |
if start == end:
|
| 133 |
return "Hitamo aho utangiriye n’aho ugiye bitandukanye.", "", ""
|
|
|
|
| 140 |
coords = [places[p] for p in path]
|
| 141 |
|
| 142 |
m = Map(location=[-1.9441, 30.0619], zoom_start=9)
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
# Add markers for all places (normal popup with name)
|
| 145 |
+
for name, coord in places.items():
|
| 146 |
+
if name == start or name == end:
|
| 147 |
+
# For start and end: popup with image + name
|
| 148 |
+
img_url = place_images.get(name, "https://via.placeholder.com/300?text=No+Image")
|
| 149 |
+
html = f"""
|
| 150 |
+
<h4>{name}</h4>
|
| 151 |
+
<img src="{img_url}" width="300" style="border-radius:8px;"/>
|
| 152 |
+
"""
|
| 153 |
+
iframe = IFrame(html, width=320, height=320)
|
| 154 |
+
popup = Popup(iframe, max_width=320)
|
| 155 |
+
Marker(location=coord, popup=popup, icon=folium.Icon(color='green' if name == start else 'red')).add_to(m)
|
| 156 |
+
else:
|
| 157 |
+
Marker(location=coord, popup=name).add_to(m)
|
| 158 |
+
|
| 159 |
+
# Draw the route line
|
| 160 |
PolyLine(coords, color="blue", weight=5).add_to(m)
|
| 161 |
m.add_child(AnimateMarker(coords))
|
| 162 |
|
| 163 |
+
return "Inzira ngufi ni: " + " ➔ ".join(path), m._repr_html_(), ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
except Exception as e:
|
| 166 |
return f"Ntibishoboka kubona inzira: {str(e)}", "", ""
|
| 167 |
|
| 168 |
+
# 8. Gradio interface
|
| 169 |
+
with gr.Blocks() as demo:
|
| 170 |
+
gr.Markdown("## Shaka inzira ngufi hagati y’uturere twa Rwanda")
|
| 171 |
+
start = gr.Dropdown(list(places.keys()), label="Aho utangirira")
|
| 172 |
+
end = gr.Dropdown(list(places.keys()), label="Aho ugiye")
|
| 173 |
+
btn = gr.Button("Shaka inzira")
|
| 174 |
+
output_text = gr.Textbox(label="Inzira", interactive=False)
|
| 175 |
+
output_map = gr.HTML()
|
| 176 |
+
|
| 177 |
+
btn.click(fn=generate_map, inputs=[start, end], outputs=[output_text, output_map, gr.Textbox(visible=False)])
|
| 178 |
+
|
| 179 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|