Spaces:
Runtime error
Runtime error
Download app/services/explanation.py from devmurtaza/RastaAI: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/spaces/devmurtaza/RastaAI/resolve/main/app/services/explanation.py
- Command line
-
hf download hf://spaces/devmurtaza/RastaAI/app/services/explanation.py
-
curl -L -o explanation.py https://huggingface.co/spaces/devmurtaza/RastaAI/resolve/main/app/services/explanation.py
2.56 kB
| """Human-readable explanation for the chosen route. | |
| Currently template-based. Swap for an LLM later if needed (the input dict | |
| already has all the structured numbers an LLM would summarise). | |
| """ | |
| from __future__ import annotations | |
| from typing import Dict, List, Tuple | |
| def _strengths_and_tradeoffs(c: Dict, peers: List[Dict]) -> Tuple[List[str], List[str]]: | |
| s, t = [], [] | |
| is_fastest = c["eta_min"] == min(p["eta_min"] for p in peers) | |
| is_cheapest = c["fuel_l"] == min(p["fuel_l"] for p in peers) | |
| is_calmest = c["traffic_risk"] == min(p["traffic_risk"] for p in peers) | |
| is_smoothest = c["features"]["road_quality"] == max(p["features"]["road_quality"] for p in peers) | |
| if is_fastest: | |
| s.append(f"Fastest option (~{c['eta_min']:.0f} min)") | |
| if is_cheapest: | |
| s.append(f"Most fuel-efficient (~{c['fuel_l']:.1f} L)") | |
| if is_calmest: | |
| s.append("Lightest traffic right now") | |
| if is_smoothest: | |
| s.append("Best road quality") | |
| if c["confidence"] > 0.8: | |
| s.append(f"High confidence ({int(c['confidence']*100)}%)") | |
| if not is_fastest: | |
| diff = c["eta_min"] - min(p["eta_min"] for p in peers) | |
| t.append(f"~{diff:.0f} min slower than the express option") | |
| if not is_cheapest: | |
| diff = c["fuel_l"] - min(p["fuel_l"] for p in peers) | |
| t.append(f"~{diff:.1f} L more fuel than the cheapest option") | |
| if c["traffic_risk"] > 0.6: | |
| t.append("Heavy traffic expected") | |
| if c["features"]["weather_severity"] > 0.5: | |
| t.append("Poor weather along the way") | |
| if not s: | |
| s.append("Well-balanced trade-off across speed, fuel and comfort") | |
| return s, t | |
| def explain(best: Dict, peers: List[Dict], preference: str) -> str: | |
| """One-paragraph rationale for why `best` won.""" | |
| pref_phrase = { | |
| "fastest": "speed", | |
| "fuel_saving": "fuel economy", | |
| "balanced": "an overall balance of speed, fuel and comfort", | |
| "safer_route": "road quality and safety", | |
| }.get(preference, "an overall balance") | |
| eta = best["eta_min"] | |
| fuel = best["fuel_l"] | |
| traffic = int(best["traffic_risk"] * 100) | |
| conf = int(best["confidence"] * 100) | |
| parts = [ | |
| f"Picked because you asked to optimise for {pref_phrase}.", | |
| f"ETA ~{eta:.0f} min, fuel ~{fuel:.1f} L, traffic {traffic}% intensity, confidence {conf}%.", | |
| ] | |
| if any(p["score"] > best["score"] * 0.97 and p is not best for p in peers): | |
| parts.append("Alternatives are close - check them if you have other preferences.") | |
| return " ".join(parts) | |