Spaces:
Running on T4
Running on T4
Download AI-coach-agent/src/ai_coach/fallback.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 7.7 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/src/ai_coach/fallback.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/AI-coach-agent/src/ai_coach/fallback.py
-
curl -L -o fallback.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/src/ai_coach/fallback.py
7.7 kB
| """Deterministic rule-based coaching note, built from the same evidence the agent sees. | |
| Used when there is no API key, the budget is spent, the model fails or keeps breaking the guardrails, or when there is nothing | |
| worth sending to the model. It names the software baseline's top-ranked supported problem, so it is also the reference that the | |
| agent's own judgement is compared against. Wording comes only from the reviewed knowledge files. | |
| """ | |
| from __future__ import annotations | |
| from .guardrails import BANNED, MAX_CAVEATS, MAX_SOLID_ITEMS, RECORD_HINT, evidence_keys | |
| from .knowledge import metric_info | |
| from .schemas import CoachInput, CoachNote, DrillNote, SolidItem, WorkOnItem | |
| CONFIDENCE_RANK = {"high": 0, "medium": 1, "est": 2} | |
| MIN_REPS_FOR_STABLE_RESULT = 3 | |
| MIN_FREQUENCY_TO_QUOTE = 0.3 | |
| # One short caveat per capture reason from the pipeline (keys match the capture-check reasons in docs/AGENT_INPUT_SPEC.md). | |
| REASON_CAVEATS = { | |
| "no_person": "We could not find you in the video. Please record again with you in the middle.", | |
| "multiple_people": "More than one person was in view. Please record again with only you in the picture.", | |
| "player_switched": "Someone else may have stepped into view. Please record again with only you in the picture.", | |
| "person_too_small": "You looked too small in the video. Please record again with the phone closer.", | |
| "partly_out_of_frame": "Part of your body was cut off. Please record again with your whole body in the frame.", | |
| "too_dark": "The video was too dark. Please record again with more light.", | |
| "a_bit_dark": "The video was a little dark, so numbers may be less accurate.", | |
| "too_blurry": "The video was too blurry. Please record again with a steady phone and more light.", | |
| "a_bit_blurry": "The video was a little blurry, so numbers may be less accurate.", | |
| "low_pose_confidence": "We could not see your body clearly. Please record again in clothes that stand out.", | |
| "pose_confidence_fair": "Parts of your body were hard to see, so some numbers may be less accurate.", | |
| "low_fps": "The video was not slow motion, so timing numbers are rough. Use 240 frames per second.", | |
| "very_low_fps": "The video was too slow to time your movement. Please use 240 frames per second slow motion.", | |
| "wrong_view": "The camera angle did not match the setup guide. Please record again from the angle shown.", | |
| "view_uncertain": "We are not sure the camera angle is right, so some numbers may be off.", | |
| } | |
| GENERIC_RECORD_AGAIN = "Please record again so we can coach you with confidence." | |
| FPS_CAVEAT = "The video was too slow to time your movement, so timing numbers are not available." | |
| ESTIMATE_CAVEAT = "Some numbers are only estimates from one camera, so we are not judging them." | |
| FEW_REPS_CAVEAT = "We saw only a few reps, so these results can change." | |
| DRAFT_RANGES_CAVEAT = "The typical ranges are drafts that your coach should confirm." | |
| DEFAULT_LEVEL_CAVEAT = "We did not know your level, so we used high school varsity ranges." | |
| def _movement(coach_input: CoachInput) -> str: | |
| return "swing" if coach_input.discipline == "hitting" else "delivery" | |
| def _solid_items(coach_input: CoachInput) -> list[SolidItem]: | |
| if not coach_input.capture.ok: | |
| return [] | |
| cited = evidence_keys(coach_input) # a metric a problem is built on is never praised | |
| candidates = [ | |
| m | |
| for m in coach_input.judgeable_metrics | |
| if m.zone == "in_range" and m.key not in cited and metric_info(coach_input.discipline, m.key).get("solid") | |
| ] | |
| candidates.sort(key=lambda m: CONFIDENCE_RANK[m.confidence]) # stable: input order within a level | |
| return [ | |
| SolidItem(metric_key=m.key, text=metric_info(coach_input.discipline, m.key)["solid"]) | |
| for m in candidates[:MAX_SOLID_ITEMS] | |
| ] | |
| def _work_on(coach_input: CoachInput) -> list[WorkOnItem]: | |
| if not coach_input.capture.ok or not coach_input.problems: | |
| return [] | |
| top = coach_input.problems[0] | |
| text = f"{top.what} {top.why}" | |
| if top.frequency >= MIN_FREQUENCY_TO_QUOTE: | |
| text += f" This showed up in {round(top.frequency * 100)}% of your reps." | |
| items = [WorkOnItem(fault_key=top.key, evidence=list(top.evidence), text=text)] | |
| if len(coach_input.problems) > 1: | |
| second = coach_input.problems[1] | |
| items.append(WorkOnItem(fault_key=second.key, evidence=list(second.evidence), text=second.what)) | |
| return items | |
| def arm_care_lines(coach_input: CoachInput) -> list[str]: | |
| """Arm-care facts repeated verbatim (trusted app text). Anything that would break the wording rules is skipped.""" | |
| arm = coach_input.arm_care | |
| if arm is None: | |
| return [] | |
| lines: list[str] = [] | |
| if arm.pitch_count_today is not None and arm.daily_max is not None: | |
| lines.append(f"You threw {arm.pitch_count_today} of {arm.daily_max} pitches today.") | |
| lines += [t for t in (*arm.alerts, arm.mechanics_drift) if t and not BANNED.search(t)] | |
| if arm.next_eligible_date: | |
| lines.append(f"You can pitch again on {arm.next_eligible_date}.") | |
| return lines | |
| def _caveats(coach_input: CoachInput) -> list[str]: | |
| """Most important first, then truncated: record-again always survives, then data limits, arm care, and the soft notes.""" | |
| reasons = [REASON_CAVEATS[r] for r in dict.fromkeys(coach_input.capture.reasons) if r in REASON_CAVEATS] | |
| record_again = [c for c in reasons if RECORD_HINT.search(c)] | |
| ordered: list[str] = [] | |
| if not coach_input.capture.ok: | |
| ordered.append(record_again[0] if record_again else GENERIC_RECORD_AGAIN) | |
| ordered += [c for c in reasons if c not in ordered and (coach_input.capture.ok or not RECORD_HINT.search(c))] | |
| if coach_input.fps_class == "low" and not any("slow" in c for c in ordered): | |
| ordered.append(FPS_CAVEAT) | |
| ordered += arm_care_lines(coach_input) | |
| if any(m.value is not None and m.confidence == "est" for m in coach_input.metrics): | |
| ordered.append(ESTIMATE_CAVEAT) | |
| if 0 < coach_input.reps_analyzed < MIN_REPS_FOR_STABLE_RESULT: | |
| ordered.append(FEW_REPS_CAVEAT) | |
| if coach_input.level_assumed and coach_input.has_anything_to_judge: | |
| ordered.append(DEFAULT_LEVEL_CAVEAT) | |
| if coach_input.ranges_provisional and coach_input.has_anything_to_judge: | |
| ordered.append(DRAFT_RANGES_CAVEAT) | |
| return list(dict.fromkeys(ordered))[:MAX_CAVEATS] | |
| def _headline(coach_input: CoachInput, solid: list[SolidItem], work_on: list[WorkOnItem]) -> str: | |
| move = _movement(coach_input) | |
| if not coach_input.capture.ok and not coach_input.has_anything_to_judge: | |
| return f"We could not read this video well enough to coach your {move} yet." | |
| if work_on and solid: | |
| return f"Parts of your {move} look solid. One change can help the most." | |
| if work_on: | |
| return f"Here is the one thing to work on in your {move}." | |
| if solid: | |
| return f"The parts of your {move} we could measure look solid." | |
| return f"We could not judge your {move} yet. Try a few more reps." | |
| def build_fallback_note(coach_input: CoachInput) -> CoachNote: | |
| """Always-available note built only from the input's evidence and reviewed copy.""" | |
| solid = _solid_items(coach_input) | |
| work_on = _work_on(coach_input) | |
| top = coach_input.problems[0] if work_on else None | |
| drill = None | |
| if top and top.drills: | |
| drill = DrillNote(name=top.drills[0].name, how=f"Do {top.drills[0].rep_scheme}. {top.drills[0].cue}") | |
| return CoachNote( | |
| headline=_headline(coach_input, solid, work_on), | |
| looks_solid=solid, | |
| work_on=work_on, | |
| cue=top.cue if top else None, | |
| drill=drill, | |
| caveats=_caveats(coach_input), | |
| ) | |