Spaces:
Running
Running
Sync DemoService 459408fb757e1f564a75cd41141e56b6d2d012d1
Browse files
api/ai-service/app/api/v1/endpoints/route_generation.py
CHANGED
|
@@ -3,6 +3,7 @@ from __future__ import annotations
|
|
| 3 |
import importlib.util
|
| 4 |
import logging
|
| 5 |
import sys
|
|
|
|
| 6 |
from pathlib import Path
|
| 7 |
from types import ModuleType
|
| 8 |
import math
|
|
@@ -320,7 +321,10 @@ def _run_fast_road_pipeline(
|
|
| 320 |
if not all(callable(item) for item in (build_graph, build_candidates, route_points, simplify_coords)):
|
| 321 |
raise RuntimeError("Fast road route helpers are unavailable.")
|
| 322 |
|
|
|
|
|
|
|
| 323 |
graph, location_name = graph_override if graph_override is not None else build_graph(place, center_lat, center_lon, radius_km)
|
|
|
|
| 324 |
# A small safety margin is enough for the live service. Generating a
|
| 325 |
# larger pool on the CPU-only Space makes a five-route request look like a
|
| 326 |
# failed request before it can return any usable road loop.
|
|
@@ -338,6 +342,7 @@ def _run_fast_road_pipeline(
|
|
| 338 |
selected_scales = None
|
| 339 |
if selected_label and callable(adaptive_scales):
|
| 340 |
selected_scales = adaptive_scales(graph, examples)
|
|
|
|
| 341 |
if not selected_scales:
|
| 342 |
selected_scales = [(distance, None) for distance in search_distances_km]
|
| 343 |
for search_distance_km, screened_templates in selected_scales:
|
|
@@ -358,6 +363,7 @@ def _run_fast_road_pipeline(
|
|
| 358 |
template_similarity_fn=lambda coords, template: best_gpx_shape_match(coords, [template])[0],
|
| 359 |
**template_kwargs,
|
| 360 |
))
|
|
|
|
| 361 |
else:
|
| 362 |
candidates = build_candidates(
|
| 363 |
graph=graph,
|
|
@@ -443,6 +449,7 @@ def _run_fast_road_pipeline(
|
|
| 443 |
)
|
| 444 |
|
| 445 |
ranking_mode = "gpx_similarity"
|
|
|
|
| 446 |
if examples:
|
| 447 |
routes.sort(key=lambda route: (
|
| 448 |
-route["route_quality"]["shape_similarity"],
|
|
@@ -470,12 +477,15 @@ def _run_fast_road_pipeline(
|
|
| 470 |
if not routes:
|
| 471 |
raise RuntimeError("Walkable road routes did not contain usable map coordinates.")
|
| 472 |
|
|
|
|
|
|
|
| 473 |
return {
|
| 474 |
"place": location_name,
|
| 475 |
"source": "point" if center_lat is not None and center_lon is not None else "place",
|
| 476 |
"candidate_count": len(candidates),
|
| 477 |
"requested_count": route_count,
|
| 478 |
"generated_count": len(routes),
|
|
|
|
| 479 |
"target_distance_km": 0.0 if shape_priority else target_distance_km,
|
| 480 |
"radius_km": radius_km,
|
| 481 |
"labels": [selected_label] if selected_label else [],
|
|
|
|
| 3 |
import importlib.util
|
| 4 |
import logging
|
| 5 |
import sys
|
| 6 |
+
import time
|
| 7 |
from pathlib import Path
|
| 8 |
from types import ModuleType
|
| 9 |
import math
|
|
|
|
| 321 |
if not all(callable(item) for item in (build_graph, build_candidates, route_points, simplify_coords)):
|
| 322 |
raise RuntimeError("Fast road route helpers are unavailable.")
|
| 323 |
|
| 324 |
+
started_at = time.perf_counter()
|
| 325 |
+
generation_timings_ms: dict[str, int] = {}
|
| 326 |
graph, location_name = graph_override if graph_override is not None else build_graph(place, center_lat, center_lon, radius_km)
|
| 327 |
+
generation_timings_ms["road_graph"] = round((time.perf_counter() - started_at) * 1000)
|
| 328 |
# A small safety margin is enough for the live service. Generating a
|
| 329 |
# larger pool on the CPU-only Space makes a five-route request look like a
|
| 330 |
# failed request before it can return any usable road loop.
|
|
|
|
| 342 |
selected_scales = None
|
| 343 |
if selected_label and callable(adaptive_scales):
|
| 344 |
selected_scales = adaptive_scales(graph, examples)
|
| 345 |
+
generation_timings_ms["scale_screening"] = round((time.perf_counter() - started_at) * 1000) - generation_timings_ms["road_graph"]
|
| 346 |
if not selected_scales:
|
| 347 |
selected_scales = [(distance, None) for distance in search_distances_km]
|
| 348 |
for search_distance_km, screened_templates in selected_scales:
|
|
|
|
| 363 |
template_similarity_fn=lambda coords, template: best_gpx_shape_match(coords, [template])[0],
|
| 364 |
**template_kwargs,
|
| 365 |
))
|
| 366 |
+
generation_timings_ms["candidate_routes"] = round((time.perf_counter() - started_at) * 1000) - generation_timings_ms["road_graph"] - generation_timings_ms["scale_screening"]
|
| 367 |
else:
|
| 368 |
candidates = build_candidates(
|
| 369 |
graph=graph,
|
|
|
|
| 449 |
)
|
| 450 |
|
| 451 |
ranking_mode = "gpx_similarity"
|
| 452 |
+
generation_timings_ms["route_evaluation"] = round((time.perf_counter() - started_at) * 1000) - sum(generation_timings_ms.values())
|
| 453 |
if examples:
|
| 454 |
routes.sort(key=lambda route: (
|
| 455 |
-route["route_quality"]["shape_similarity"],
|
|
|
|
| 477 |
if not routes:
|
| 478 |
raise RuntimeError("Walkable road routes did not contain usable map coordinates.")
|
| 479 |
|
| 480 |
+
generation_timings_ms["final_ranking"] = round((time.perf_counter() - started_at) * 1000) - sum(generation_timings_ms.values())
|
| 481 |
+
|
| 482 |
return {
|
| 483 |
"place": location_name,
|
| 484 |
"source": "point" if center_lat is not None and center_lon is not None else "place",
|
| 485 |
"candidate_count": len(candidates),
|
| 486 |
"requested_count": route_count,
|
| 487 |
"generated_count": len(routes),
|
| 488 |
+
"generation_timings_ms": generation_timings_ms,
|
| 489 |
"target_distance_km": 0.0 if shape_priority else target_distance_km,
|
| 490 |
"radius_km": radius_km,
|
| 491 |
"labels": [selected_label] if selected_label else [],
|