Spaces:
Sleeping
Sleeping
Fix: use Folium with preset locations, add sklearn and pin hopsworks
Browse files- app.py +106 -32
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -7,6 +7,7 @@ location, time, weather, and holidays.
|
|
| 7 |
|
| 8 |
import os
|
| 9 |
import gradio as gr
|
|
|
|
| 10 |
from datetime import datetime, timedelta
|
| 11 |
|
| 12 |
# Import prediction and data fetching modules
|
|
@@ -33,6 +34,7 @@ _predict_fn = predict_occupancy_mock if USE_MOCK else predict_occupancy
|
|
| 33 |
# Default map center: Linköping
|
| 34 |
DEFAULT_LAT = 58.4108
|
| 35 |
DEFAULT_LON = 15.6214
|
|
|
|
| 36 |
|
| 37 |
# Östergötland bounds (roughly)
|
| 38 |
BOUNDS = {
|
|
@@ -42,37 +44,56 @@ BOUNDS = {
|
|
| 42 |
"max_lon": 16.8
|
| 43 |
}
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
|
| 61 |
-
return
|
| 62 |
|
| 63 |
|
| 64 |
-
def make_prediction(
|
| 65 |
"""
|
| 66 |
Make occupancy prediction for given inputs.
|
| 67 |
|
| 68 |
Returns formatted result HTML.
|
| 69 |
"""
|
| 70 |
-
lat, lon = extract_coordinates(map_data)
|
| 71 |
-
|
| 72 |
if lat is None or lon is None:
|
| 73 |
return create_result_card(
|
| 74 |
"Please select a location",
|
| 75 |
-
"
|
| 76 |
"gray",
|
| 77 |
None
|
| 78 |
)
|
|
@@ -118,7 +139,7 @@ def make_prediction(map_data, date_choice, hour):
|
|
| 118 |
|
| 119 |
# Build context string
|
| 120 |
day_name = selected_date.strftime("%A")
|
| 121 |
-
day_type = "Holiday" if holidays.get("is_red_day") else ("Work-free day" if holidays.get("is_work_free") else "Regular
|
| 122 |
temp = weather.get("temperature_2m", "?")
|
| 123 |
|
| 124 |
context = f"{temp:.0f}°C • {day_name} • {day_type}"
|
|
@@ -196,6 +217,9 @@ CUSTOM_CSS = """
|
|
| 196 |
margin-top: 5px;
|
| 197 |
margin-bottom: 20px;
|
| 198 |
}
|
|
|
|
|
|
|
|
|
|
| 199 |
"""
|
| 200 |
|
| 201 |
# Build Gradio interface
|
|
@@ -210,20 +234,43 @@ with gr.Blocks(
|
|
| 210 |
gr.Markdown("*How packed are buses in Östergötland?*", elem_classes=["subtitle"])
|
| 211 |
|
| 212 |
with gr.Row():
|
| 213 |
-
# Left column: Map
|
| 214 |
with gr.Column(scale=2):
|
| 215 |
gr.Markdown("### Select Location")
|
| 216 |
-
gr.Markdown("Click on the map to place a marker:")
|
| 217 |
-
|
| 218 |
-
map_input = gr.Map(
|
| 219 |
-
center=(DEFAULT_LAT, DEFAULT_LON),
|
| 220 |
-
zoom=10,
|
| 221 |
-
height=400,
|
| 222 |
-
label="",
|
| 223 |
-
show_label=False,
|
| 224 |
-
)
|
| 225 |
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
with gr.Column(scale=1):
|
| 228 |
gr.Markdown("### When?")
|
| 229 |
|
|
@@ -242,7 +289,6 @@ with gr.Blocks(
|
|
| 242 |
info="Select time of day (24h format)"
|
| 243 |
)
|
| 244 |
|
| 245 |
-
# Show selected time
|
| 246 |
time_display = gr.Markdown("**Selected: 08:00**")
|
| 247 |
|
| 248 |
predict_btn = gr.Button("Predict Crowding", variant="primary", size="lg")
|
|
@@ -287,15 +333,43 @@ with gr.Blocks(
|
|
| 287 |
def update_time_display(hour):
|
| 288 |
return f"**Selected: {int(hour):02d}:00**"
|
| 289 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
hour_slider.change(
|
| 291 |
fn=update_time_display,
|
| 292 |
inputs=[hour_slider],
|
| 293 |
outputs=[time_display]
|
| 294 |
)
|
| 295 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
predict_btn.click(
|
| 297 |
fn=make_prediction,
|
| 298 |
-
inputs=[
|
| 299 |
outputs=[result_display]
|
| 300 |
)
|
| 301 |
|
|
|
|
| 7 |
|
| 8 |
import os
|
| 9 |
import gradio as gr
|
| 10 |
+
import folium
|
| 11 |
from datetime import datetime, timedelta
|
| 12 |
|
| 13 |
# Import prediction and data fetching modules
|
|
|
|
| 34 |
# Default map center: Linköping
|
| 35 |
DEFAULT_LAT = 58.4108
|
| 36 |
DEFAULT_LON = 15.6214
|
| 37 |
+
DEFAULT_ZOOM = 11
|
| 38 |
|
| 39 |
# Östergötland bounds (roughly)
|
| 40 |
BOUNDS = {
|
|
|
|
| 44 |
"max_lon": 16.8
|
| 45 |
}
|
| 46 |
|
| 47 |
+
# Preset locations for quick selection
|
| 48 |
+
PRESET_LOCATIONS = {
|
| 49 |
+
"Linköping Central": (58.4158, 15.6253),
|
| 50 |
+
"Norrköping Central": (58.5942, 16.1826),
|
| 51 |
+
"Linköping University": (58.3980, 15.5762),
|
| 52 |
+
"Mjärdevi Science Park": (58.4027, 15.5672),
|
| 53 |
+
"Motala": (58.5375, 15.0364),
|
| 54 |
+
"Finspång": (58.7050, 15.7700),
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def create_map(lat=DEFAULT_LAT, lon=DEFAULT_LON):
|
| 59 |
+
"""Create a Folium map centered on location with marker."""
|
| 60 |
+
m = folium.Map(
|
| 61 |
+
location=[lat, lon],
|
| 62 |
+
zoom_start=DEFAULT_ZOOM,
|
| 63 |
+
tiles="CartoDB positron"
|
| 64 |
+
)
|
| 65 |
|
| 66 |
+
# Add marker at selected location
|
| 67 |
+
folium.Marker(
|
| 68 |
+
[lat, lon],
|
| 69 |
+
popup=f"Selected: {lat:.4f}, {lon:.4f}",
|
| 70 |
+
icon=folium.Icon(color="blue", icon="bus", prefix="fa")
|
| 71 |
+
).add_to(m)
|
| 72 |
|
| 73 |
+
# Add a rectangle showing the coverage area
|
| 74 |
+
folium.Rectangle(
|
| 75 |
+
bounds=[[BOUNDS["min_lat"], BOUNDS["min_lon"]],
|
| 76 |
+
[BOUNDS["max_lat"], BOUNDS["max_lon"]]],
|
| 77 |
+
color="#3388ff",
|
| 78 |
+
fill=False,
|
| 79 |
+
weight=1,
|
| 80 |
+
opacity=0.3,
|
| 81 |
+
popup="Coverage area"
|
| 82 |
+
).add_to(m)
|
| 83 |
|
| 84 |
+
return m._repr_html_()
|
| 85 |
|
| 86 |
|
| 87 |
+
def make_prediction(lat, lon, date_choice, hour):
|
| 88 |
"""
|
| 89 |
Make occupancy prediction for given inputs.
|
| 90 |
|
| 91 |
Returns formatted result HTML.
|
| 92 |
"""
|
|
|
|
|
|
|
| 93 |
if lat is None or lon is None:
|
| 94 |
return create_result_card(
|
| 95 |
"Please select a location",
|
| 96 |
+
"Use the preset buttons or enter coordinates.",
|
| 97 |
"gray",
|
| 98 |
None
|
| 99 |
)
|
|
|
|
| 139 |
|
| 140 |
# Build context string
|
| 141 |
day_name = selected_date.strftime("%A")
|
| 142 |
+
day_type = "Holiday" if holidays.get("is_red_day") else ("Work-free day" if holidays.get("is_work_free") else "Regular day")
|
| 143 |
temp = weather.get("temperature_2m", "?")
|
| 144 |
|
| 145 |
context = f"{temp:.0f}°C • {day_name} • {day_type}"
|
|
|
|
| 217 |
margin-top: 5px;
|
| 218 |
margin-bottom: 20px;
|
| 219 |
}
|
| 220 |
+
.location-btn {
|
| 221 |
+
margin: 2px !important;
|
| 222 |
+
}
|
| 223 |
"""
|
| 224 |
|
| 225 |
# Build Gradio interface
|
|
|
|
| 234 |
gr.Markdown("*How packed are buses in Östergötland?*", elem_classes=["subtitle"])
|
| 235 |
|
| 236 |
with gr.Row():
|
| 237 |
+
# Left column: Map and location
|
| 238 |
with gr.Column(scale=2):
|
| 239 |
gr.Markdown("### Select Location")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
+
# Quick location buttons
|
| 242 |
+
gr.Markdown("**Quick select:**")
|
| 243 |
+
with gr.Row():
|
| 244 |
+
location_buttons = []
|
| 245 |
+
for name in list(PRESET_LOCATIONS.keys())[:3]:
|
| 246 |
+
btn = gr.Button(name, size="sm", elem_classes=["location-btn"])
|
| 247 |
+
location_buttons.append((name, btn))
|
| 248 |
+
with gr.Row():
|
| 249 |
+
for name in list(PRESET_LOCATIONS.keys())[3:]:
|
| 250 |
+
btn = gr.Button(name, size="sm", elem_classes=["location-btn"])
|
| 251 |
+
location_buttons.append((name, btn))
|
| 252 |
+
|
| 253 |
+
# Coordinate inputs
|
| 254 |
+
with gr.Row():
|
| 255 |
+
lat_input = gr.Number(
|
| 256 |
+
label="Latitude",
|
| 257 |
+
value=DEFAULT_LAT,
|
| 258 |
+
precision=4,
|
| 259 |
+
minimum=BOUNDS["min_lat"],
|
| 260 |
+
maximum=BOUNDS["max_lat"]
|
| 261 |
+
)
|
| 262 |
+
lon_input = gr.Number(
|
| 263 |
+
label="Longitude",
|
| 264 |
+
value=DEFAULT_LON,
|
| 265 |
+
precision=4,
|
| 266 |
+
minimum=BOUNDS["min_lon"],
|
| 267 |
+
maximum=BOUNDS["max_lon"]
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
# Map display
|
| 271 |
+
map_display = gr.HTML(value=create_map())
|
| 272 |
+
|
| 273 |
+
# Right column: Time and predict
|
| 274 |
with gr.Column(scale=1):
|
| 275 |
gr.Markdown("### When?")
|
| 276 |
|
|
|
|
| 289 |
info="Select time of day (24h format)"
|
| 290 |
)
|
| 291 |
|
|
|
|
| 292 |
time_display = gr.Markdown("**Selected: 08:00**")
|
| 293 |
|
| 294 |
predict_btn = gr.Button("Predict Crowding", variant="primary", size="lg")
|
|
|
|
| 333 |
def update_time_display(hour):
|
| 334 |
return f"**Selected: {int(hour):02d}:00**"
|
| 335 |
|
| 336 |
+
def update_location(name):
|
| 337 |
+
lat, lon = PRESET_LOCATIONS[name]
|
| 338 |
+
return lat, lon, create_map(lat, lon)
|
| 339 |
+
|
| 340 |
+
def update_map_from_coords(lat, lon):
|
| 341 |
+
if lat is not None and lon is not None:
|
| 342 |
+
return create_map(lat, lon)
|
| 343 |
+
return create_map()
|
| 344 |
+
|
| 345 |
hour_slider.change(
|
| 346 |
fn=update_time_display,
|
| 347 |
inputs=[hour_slider],
|
| 348 |
outputs=[time_display]
|
| 349 |
)
|
| 350 |
|
| 351 |
+
# Connect location buttons
|
| 352 |
+
for name, btn in location_buttons:
|
| 353 |
+
btn.click(
|
| 354 |
+
fn=lambda n=name: update_location(n),
|
| 355 |
+
outputs=[lat_input, lon_input, map_display]
|
| 356 |
+
)
|
| 357 |
+
|
| 358 |
+
# Update map when coordinates change
|
| 359 |
+
lat_input.change(
|
| 360 |
+
fn=update_map_from_coords,
|
| 361 |
+
inputs=[lat_input, lon_input],
|
| 362 |
+
outputs=[map_display]
|
| 363 |
+
)
|
| 364 |
+
lon_input.change(
|
| 365 |
+
fn=update_map_from_coords,
|
| 366 |
+
inputs=[lat_input, lon_input],
|
| 367 |
+
outputs=[map_display]
|
| 368 |
+
)
|
| 369 |
+
|
| 370 |
predict_btn.click(
|
| 371 |
fn=make_prediction,
|
| 372 |
+
inputs=[lat_input, lon_input, date_choice, hour_slider],
|
| 373 |
outputs=[result_display]
|
| 374 |
)
|
| 375 |
|
requirements.txt
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
gradio==4.44.0
|
| 2 |
huggingface_hub>=0.24.0,<1.0.0
|
| 3 |
-
hopsworks
|
| 4 |
xgboost>=2.0.0
|
|
|
|
| 5 |
pandas
|
| 6 |
numpy
|
| 7 |
requests
|
| 8 |
python-dotenv
|
|
|
|
|
|
| 1 |
gradio==4.44.0
|
| 2 |
huggingface_hub>=0.24.0,<1.0.0
|
| 3 |
+
hopsworks==4.2.*
|
| 4 |
xgboost>=2.0.0
|
| 5 |
+
scikit-learn
|
| 6 |
pandas
|
| 7 |
numpy
|
| 8 |
requests
|
| 9 |
python-dotenv
|
| 10 |
+
folium>=0.15.0
|