Spaces:
Running
Running
Sync DemoService 6d301586e2a13a00c630826f7afcccbdc7744922
Browse files
api/ai-service/app/api/v1/endpoints/route_generation.py
CHANGED
|
@@ -419,6 +419,8 @@ def _run_fast_road_pipeline(
|
|
| 419 |
selected_label: str | None,
|
| 420 |
shape_priority: bool = False,
|
| 421 |
experimental_candidate_ranker: bool = False,
|
|
|
|
|
|
|
| 422 |
) -> dict[str, Any]:
|
| 423 |
"""Create real walkable-road routes without per-route CLIP inference.
|
| 424 |
|
|
@@ -436,7 +438,7 @@ def _run_fast_road_pipeline(
|
|
| 436 |
if not all(callable(item) for item in (build_graph, build_candidates, route_points, simplify_coords)):
|
| 437 |
raise RuntimeError("Fast road route helpers are unavailable.")
|
| 438 |
|
| 439 |
-
graph, location_name = build_graph(place, center_lat, center_lon, radius_km)
|
| 440 |
# A small safety margin is enough for the live service. Generating a
|
| 441 |
# larger pool on the CPU-only Space makes a five-route request look like a
|
| 442 |
# failed request before it can return any usable road loop.
|
|
@@ -450,7 +452,7 @@ def _run_fast_road_pipeline(
|
|
| 450 |
# not request a distance; these are search scales, not length targets.
|
| 451 |
candidates = []
|
| 452 |
rank_projections = getattr(module, "rank_road_template_projections", None)
|
| 453 |
-
for search_distance_km in
|
| 454 |
template_kwargs = {}
|
| 455 |
if selected_label in {"symbol_star", "dog", "heart", "candle", "christmas_tree_trunk"} and callable(rank_projections):
|
| 456 |
# Screen every supplied animal/shape outline against this road graph;
|
|
@@ -986,6 +988,25 @@ def generate_routes(payload: RouteGenerateRequest) -> dict[str, Any]:
|
|
| 986 |
)
|
| 987 |
except Exception:
|
| 988 |
logger.exception("fallback-centre walk-road generation failed")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 989 |
# Provider outages must reach this branch too. Previously the
|
| 990 |
# early skip above also skipped recovery and returned HTTP 503.
|
| 991 |
template_fn = getattr(module, "guided_waypoints_for_label", None)
|
|
|
|
| 419 |
selected_label: str | None,
|
| 420 |
shape_priority: bool = False,
|
| 421 |
experimental_candidate_ranker: bool = False,
|
| 422 |
+
graph_override: tuple[Any, str] | None = None,
|
| 423 |
+
search_distances_km: tuple[float, ...] = (6.0, 11.0, 18.0),
|
| 424 |
) -> dict[str, Any]:
|
| 425 |
"""Create real walkable-road routes without per-route CLIP inference.
|
| 426 |
|
|
|
|
| 438 |
if not all(callable(item) for item in (build_graph, build_candidates, route_points, simplify_coords)):
|
| 439 |
raise RuntimeError("Fast road route helpers are unavailable.")
|
| 440 |
|
| 441 |
+
graph, location_name = graph_override if graph_override is not None else build_graph(place, center_lat, center_lon, radius_km)
|
| 442 |
# A small safety margin is enough for the live service. Generating a
|
| 443 |
# larger pool on the CPU-only Space makes a five-route request look like a
|
| 444 |
# failed request before it can return any usable road loop.
|
|
|
|
| 452 |
# not request a distance; these are search scales, not length targets.
|
| 453 |
candidates = []
|
| 454 |
rank_projections = getattr(module, "rank_road_template_projections", None)
|
| 455 |
+
for search_distance_km in search_distances_km:
|
| 456 |
template_kwargs = {}
|
| 457 |
if selected_label in {"symbol_star", "dog", "heart", "candle", "christmas_tree_trunk"} and callable(rank_projections):
|
| 458 |
# Screen every supplied animal/shape outline against this road graph;
|
|
|
|
| 988 |
)
|
| 989 |
except Exception:
|
| 990 |
logger.exception("fallback-centre walk-road generation failed")
|
| 991 |
+
if module is not None and shape_priority and selected_label and overpass_unavailable:
|
| 992 |
+
try:
|
| 993 |
+
import networkx as nx
|
| 994 |
+
from experimental_next_edge_service import fetch_small_walk_graph
|
| 995 |
+
|
| 996 |
+
nearby_graph = fetch_small_walk_graph(fallback_lat, fallback_lon, radius_km=2.0)
|
| 997 |
+
component = max(nx.weakly_connected_components(nearby_graph), key=len)
|
| 998 |
+
nearby_graph = nearby_graph.subgraph(component).copy()
|
| 999 |
+
return _run_fast_road_pipeline(
|
| 1000 |
+
module, place=None, center_lat=fallback_lat,
|
| 1001 |
+
center_lon=fallback_lon, radius_km=2.0,
|
| 1002 |
+
route_count=route_count, target_distance_km=target_distance_km,
|
| 1003 |
+
selected_label=selected_label, shape_priority=True,
|
| 1004 |
+
experimental_candidate_ranker=payload.experimentalCandidateRanker,
|
| 1005 |
+
graph_override=(nearby_graph, f"{fallback_lat:.6f},{fallback_lon:.6f}"),
|
| 1006 |
+
search_distances_km=(4.0, 7.0, 10.0),
|
| 1007 |
+
)
|
| 1008 |
+
except Exception:
|
| 1009 |
+
logger.exception("cached map road generation failed; using independent walking provider")
|
| 1010 |
# Provider outages must reach this branch too. Previously the
|
| 1011 |
# early skip above also skipped recovery and returned HTTP 503.
|
| 1012 |
template_fn = getattr(module, "guided_waypoints_for_label", None)
|
api/ai-service/tests/test_route_generation_recovery.py
CHANGED
|
@@ -91,6 +91,26 @@ def test_generation_identity_reaches_walking_provider(pipeline):
|
|
| 91 |
assert walking.call_args.kwargs["generation_id"] == "new-search"
|
| 92 |
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
def test_resolved_district_preferred_to_provincial_center(pipeline):
|
| 95 |
error = RuntimeError("All Overpass endpoints failed")
|
| 96 |
error.center = (35.1631, 129.1635)
|
|
|
|
| 91 |
assert walking.call_args.kwargs["generation_id"] == "new-search"
|
| 92 |
|
| 93 |
|
| 94 |
+
def test_shape_search_recovers_with_cached_osm_roads_before_independent_provider(pipeline):
|
| 95 |
+
graph = nx.MultiDiGraph()
|
| 96 |
+
graph.add_edge(1, 2, length=100)
|
| 97 |
+
graph.add_edge(2, 1, length=100)
|
| 98 |
+
fetch = Mock(return_value=graph)
|
| 99 |
+
pipeline.setitem(sys.modules, "experimental_next_edge_service",
|
| 100 |
+
SimpleNamespace(fetch_small_walk_graph=fetch))
|
| 101 |
+
def road_attempt(_module, **kwargs):
|
| 102 |
+
if kwargs.get("graph_override") is None:
|
| 103 |
+
raise RuntimeError("All Overpass endpoints failed")
|
| 104 |
+
assert kwargs["search_distances_km"] == (4.0, 7.0, 10.0)
|
| 105 |
+
return {"generation_mode": "fast_walk_roads", "routes": ["shape road"]}
|
| 106 |
+
pipeline.setattr(routes, "_run_fast_road_pipeline", road_attempt)
|
| 107 |
+
result = routes.generate_routes(routes.RouteGenerateRequest(
|
| 108 |
+
source="current", latitude=37.516, longitude=127.049,
|
| 109 |
+
labels=["dog"], selectedLabel="dog", shapePriority=True))
|
| 110 |
+
assert result["generation_mode"] == "fast_walk_roads"
|
| 111 |
+
fetch.assert_called_once_with(37.516, 127.049, radius_km=2.0)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
def test_resolved_district_preferred_to_provincial_center(pipeline):
|
| 115 |
error = RuntimeError("All Overpass endpoints failed")
|
| 116 |
error.center = (35.1631, 129.1635)
|
docs/SHAPE_AWARE_ROAD_CONNECTION.md
CHANGED
|
@@ -10,6 +10,13 @@ graph. The complete path is ranked against its source GPX outline; the
|
|
| 10 |
shortest route remains a candidate and can win. GATv2 is no longer offered
|
| 11 |
in the normal route-search UI.
|
| 12 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
Local checks on a Seoul road graph (6,021 nodes) with 10 fixed placements
|
| 14 |
across five labels: the best corridor option improved the existing GPX
|
| 15 |
similarity in 8 cases and decreased it in 2; the mean raw difference was
|
|
|
|
| 10 |
shortest route remains a candidate and can win. GATv2 is no longer offered
|
| 11 |
in the normal route-search UI.
|
| 12 |
|
| 13 |
+
If the usual Overpass graph provider is unavailable, shape-priority search
|
| 14 |
+
tries the same cached 2 km OSM walking graph used for map-point discovery
|
| 15 |
+
before falling back to the independent route provider. This recovery uses
|
| 16 |
+
4, 7, and 10 km search scales so the smaller graph can still produce a
|
| 17 |
+
shape-aware candidate. It keeps the independent provider as the last real-road
|
| 18 |
+
recovery path when OSM map data cannot be fetched either.
|
| 19 |
+
|
| 20 |
Local checks on a Seoul road graph (6,021 nodes) with 10 fixed placements
|
| 21 |
across five labels: the best corridor option improved the existing GPX
|
| 22 |
similarity in 8 cases and decreased it in 2; the mean raw difference was
|