Spaces:
Running on T4
Running on T4
github-actions[bot] commited on
Commit ·
413b142
1
Parent(s): e798a49
Automated Deploy: ec05ab3f96c5cf267b404fb78be94815fdb8cbea
Browse files
AI-coach-agent/src/ai_coach/video_review.py
CHANGED
|
@@ -154,7 +154,13 @@ def provider_failure(error: Exception) -> dict:
|
|
| 154 |
return result
|
| 155 |
|
| 156 |
|
| 157 |
-
def validate_note(note: VisualNote, frames: list[dict]) ->
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
by_id = {f["frame"]: f for f in frames}
|
| 159 |
for feature in note.features:
|
| 160 |
ids = feature.evidence_frame_ids
|
|
@@ -169,12 +175,20 @@ def validate_note(note: VisualNote, frames: list[dict]) -> None:
|
|
| 169 |
raise ValueError("Visual feedback cannot invent measured or medical claims")
|
| 170 |
if feature.rating and re.search(r"\d|degrees?|milliseconds?|\bms\b|\bmph\b|\bm/s\b|percent|validated|diagnos|injur|pain|\bscore\b|\bpoints?\b", feature.rating.explanation.lower()):
|
| 171 |
raise ValueError("Visual rating cannot invent measured or medical claims")
|
| 172 |
-
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
| 174 |
if (feature.key == "bat_path_contact" and re.search(r"\b(hit|hits|contact|connects|connected)\b", text)
|
| 175 |
and not feature.ball_interaction_visible
|
| 176 |
and not re.search(r"not|unclear|cannot|can't|missing|unseen|unavailable|no ", text)):
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
if note.priority:
|
| 179 |
p = note.priority
|
| 180 |
feature = next(f for f in note.features if f.key == p.feature)
|
|
@@ -182,6 +196,7 @@ def validate_note(note: VisualNote, frames: list[dict]) -> None:
|
|
| 182 |
raise ValueError("Priority needs observed evidence and a matching library drill")
|
| 183 |
if re.search(r"\d|degrees?|milliseconds?|\bms\b|\bmph\b|validated|diagnos|injur|pain", p.reason.lower()):
|
| 184 |
raise ValueError("Priority cannot invent measured or medical claims")
|
|
|
|
| 185 |
|
| 186 |
|
| 187 |
def visual_response_schema() -> dict:
|
|
@@ -206,8 +221,10 @@ def visual_response_schema() -> dict:
|
|
| 206 |
result[key] = value
|
| 207 |
return result
|
| 208 |
schema = supported(VisualNote.model_json_schema())
|
| 209 |
-
#
|
| 210 |
-
|
|
|
|
|
|
|
| 211 |
return schema
|
| 212 |
|
| 213 |
|
|
@@ -283,7 +300,7 @@ def review_video(frames: list[dict], facts: dict, settings: Settings,
|
|
| 283 |
raw, usage = (generator or gemini_generate)(settings, frames, facts)
|
| 284 |
outcome = "invalid_visual_response"
|
| 285 |
note = VisualNote.model_validate_json(raw)
|
| 286 |
-
validate_note(note, frames)
|
| 287 |
public = note.model_dump(mode="json")
|
| 288 |
score = build_video_score(public["features"], frames)
|
| 289 |
for feature in public["features"]:
|
|
@@ -297,7 +314,7 @@ def review_video(frames: list[dict], facts: dict, settings: Settings,
|
|
| 297 |
return {"status": "completed", "source": "poc_video_review", "model": settings.video_review_model,
|
| 298 |
"accuracy_validated": False, "judgment_eligible": False,
|
| 299 |
"notice": "Experimental video observations, visual rubric ratings and a practice suggestion; accuracy has not been validated.",
|
| 300 |
-
**public, "drill": drill, "score": score,
|
| 301 |
"latency_ms": int((time.monotonic()-started)*1000)}
|
| 302 |
except Exception as error:
|
| 303 |
# Provider errors can include credentials and uploaded content. Public
|
|
@@ -324,7 +341,6 @@ def review_video(frames: list[dict], facts: dict, settings: Settings,
|
|
| 324 |
"Observed movement needs multiple frames": "insufficient_evidence",
|
| 325 |
"Visual feedback cannot invent measured or medical claims": "unsupported_claim",
|
| 326 |
"Visual rating cannot invent measured or medical claims": "unsupported_rating_claim",
|
| 327 |
-
"Contact claim without visible interaction": "unsupported_contact",
|
| 328 |
"Priority needs observed evidence and a matching library drill": "unsupported_priority",
|
| 329 |
"Priority cannot invent measured or medical claims": "unsupported_priority_claim"}
|
| 330 |
result["validation_reason"] = checks.get(str(error), "invalid_response")
|
|
|
|
| 154 |
return result
|
| 155 |
|
| 156 |
|
| 157 |
+
def validate_note(note: VisualNote, frames: list[dict]) -> list[dict]:
|
| 158 |
+
"""Reject invalid evidence; discard unsupported contact prose locally.
|
| 159 |
+
|
| 160 |
+
A contradictory contact sentence must never reach the player, but it does
|
| 161 |
+
not invalidate independently supported preparation/turning observations.
|
| 162 |
+
Validate every reference and measured claim before downgrading that feature.
|
| 163 |
+
"""
|
| 164 |
by_id = {f["frame"]: f for f in frames}
|
| 165 |
for feature in note.features:
|
| 166 |
ids = feature.evidence_frame_ids
|
|
|
|
| 175 |
raise ValueError("Visual feedback cannot invent measured or medical claims")
|
| 176 |
if feature.rating and re.search(r"\d|degrees?|milliseconds?|\bms\b|\bmph\b|\bm/s\b|percent|validated|diagnos|injur|pain|\bscore\b|\bpoints?\b", feature.rating.explanation.lower()):
|
| 177 |
raise ValueError("Visual rating cannot invent measured or medical claims")
|
| 178 |
+
warnings = []
|
| 179 |
+
for feature in note.features:
|
| 180 |
+
# Missing interaction may be described; unsupported positive contact
|
| 181 |
+
# statements are removed rather than promoted to visible evidence.
|
| 182 |
+
text = feature.observation.lower()
|
| 183 |
if (feature.key == "bat_path_contact" and re.search(r"\b(hit|hits|contact|connects|connected)\b", text)
|
| 184 |
and not feature.ball_interaction_visible
|
| 185 |
and not re.search(r"not|unclear|cannot|can't|missing|unseen|unavailable|no ", text)):
|
| 186 |
+
feature.status = "unclear"
|
| 187 |
+
feature.observation = "Clear bat and ball interaction was not established in the sampled images; contact cannot be confirmed."
|
| 188 |
+
feature.rating = None
|
| 189 |
+
warnings.append({"feature": feature.key, "reason_code": "unverified_contact_statement"})
|
| 190 |
+
if note.priority and note.priority.feature == feature.key:
|
| 191 |
+
note.priority = None
|
| 192 |
if note.priority:
|
| 193 |
p = note.priority
|
| 194 |
feature = next(f for f in note.features if f.key == p.feature)
|
|
|
|
| 196 |
raise ValueError("Priority needs observed evidence and a matching library drill")
|
| 197 |
if re.search(r"\d|degrees?|milliseconds?|\bms\b|\bmph\b|validated|diagnos|injur|pain", p.reason.lower()):
|
| 198 |
raise ValueError("Priority cannot invent measured or medical claims")
|
| 199 |
+
return warnings
|
| 200 |
|
| 201 |
|
| 202 |
def visual_response_schema() -> dict:
|
|
|
|
| 221 |
result[key] = value
|
| 222 |
return result
|
| 223 |
schema = supported(VisualNote.model_json_schema())
|
| 224 |
+
# Legacy notes remain readable locally. New provider responses must explicitly
|
| 225 |
+
# report interaction visibility; omission must not silently default to false
|
| 226 |
+
# beside a positive contact sentence.
|
| 227 |
+
schema["$defs"]["FeatureReview"]["required"].extend(["rating", "ball_interaction_visible"])
|
| 228 |
return schema
|
| 229 |
|
| 230 |
|
|
|
|
| 300 |
raw, usage = (generator or gemini_generate)(settings, frames, facts)
|
| 301 |
outcome = "invalid_visual_response"
|
| 302 |
note = VisualNote.model_validate_json(raw)
|
| 303 |
+
guardrail_warnings = validate_note(note, frames)
|
| 304 |
public = note.model_dump(mode="json")
|
| 305 |
score = build_video_score(public["features"], frames)
|
| 306 |
for feature in public["features"]:
|
|
|
|
| 314 |
return {"status": "completed", "source": "poc_video_review", "model": settings.video_review_model,
|
| 315 |
"accuracy_validated": False, "judgment_eligible": False,
|
| 316 |
"notice": "Experimental video observations, visual rubric ratings and a practice suggestion; accuracy has not been validated.",
|
| 317 |
+
**public, "drill": drill, "score": score, "guardrail_warnings": guardrail_warnings,
|
| 318 |
"latency_ms": int((time.monotonic()-started)*1000)}
|
| 319 |
except Exception as error:
|
| 320 |
# Provider errors can include credentials and uploaded content. Public
|
|
|
|
| 341 |
"Observed movement needs multiple frames": "insufficient_evidence",
|
| 342 |
"Visual feedback cannot invent measured or medical claims": "unsupported_claim",
|
| 343 |
"Visual rating cannot invent measured or medical claims": "unsupported_rating_claim",
|
|
|
|
| 344 |
"Priority needs observed evidence and a matching library drill": "unsupported_priority",
|
| 345 |
"Priority cannot invent measured or medical claims": "unsupported_priority_claim"}
|
| 346 |
result["validation_reason"] = checks.get(str(error), "invalid_response")
|
docs/EXPERIMENTAL_VIDEO_SCORE.md
CHANGED
|
@@ -110,5 +110,12 @@ an unavailable score with `value: null`. Legacy notes without rating fields
|
|
| 110 |
remain readable but do not acquire invented grades. Original Mechanics scoring
|
| 111 |
and measurement eligibility rules are unchanged.
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
The authenticated `/v1/video-score-schema` endpoint and
|
| 114 |
[video-score.schema.json](video-score.schema.json) publish the full score schema.
|
|
|
|
| 110 |
remain readable but do not acquire invented grades. Original Mechanics scoring
|
| 111 |
and measurement eligibility rules are unchanged.
|
| 112 |
|
| 113 |
+
If the model describes contact without confirming visible ball interaction, the
|
| 114 |
+
server removes that statement and marks only bat/contact unclear. Valid body
|
| 115 |
+
observations remain available, and any priority based on that contact claim is
|
| 116 |
+
removed. `visual_review.guardrail_warnings` reports `unverified_contact_statement`
|
| 117 |
+
for that feature. Invalid frame references or invented measurements still reject
|
| 118 |
+
the review.
|
| 119 |
+
|
| 120 |
The authenticated `/v1/video-score-schema` endpoint and
|
| 121 |
[video-score.schema.json](video-score.schema.json) publish the full score schema.
|
tests/test_product.py
CHANGED
|
@@ -153,7 +153,7 @@ def test_visual_review_success_uses_library_drill_and_no_measurement_upgrade(tmp
|
|
| 153 |
assert result['accuracy_validated'] is False and result['judgment_eligible'] is False
|
| 154 |
|
| 155 |
|
| 156 |
-
@pytest.mark.parametrize('bad',['invented_frame','wrong_rep','duplicate','reversed','one_frame','angle','numeric_priority','
|
| 157 |
def test_visual_review_rejects_unsupported_responses(tmp_path,bad):
|
| 158 |
raw=note(); f=raw['features'][0]
|
| 159 |
if bad=='invented_frame': f['evidence_frame_ids']=[0,500]
|
|
@@ -163,7 +163,6 @@ def test_visual_review_rejects_unsupported_responses(tmp_path,bad):
|
|
| 163 |
if bad=='one_frame': f['evidence_frame_ids']=[0]
|
| 164 |
if bad=='angle': f['observation']='The hips rotate forty degrees.'
|
| 165 |
if bad=='numeric_priority': raw['priority']['reason']='You started 10 ms late.'
|
| 166 |
-
if bad=='contact': raw['features'][2].update(status='observed',observation='The bat hits the ball.',evidence_frame_ids=[0,1])
|
| 167 |
if bad=='wrong_drill': raw['priority']['drill_id']='hit_14'
|
| 168 |
if bad=='unclear_priority': f['status']='unclear'
|
| 169 |
if bad=='duplicate_feature': raw['features'][2]['key']='load_timing'
|
|
|
|
| 153 |
assert result['accuracy_validated'] is False and result['judgment_eligible'] is False
|
| 154 |
|
| 155 |
|
| 156 |
+
@pytest.mark.parametrize('bad',['invented_frame','wrong_rep','duplicate','reversed','one_frame','angle','numeric_priority','wrong_drill','unclear_priority','duplicate_feature'])
|
| 157 |
def test_visual_review_rejects_unsupported_responses(tmp_path,bad):
|
| 158 |
raw=note(); f=raw['features'][0]
|
| 159 |
if bad=='invented_frame': f['evidence_frame_ids']=[0,500]
|
|
|
|
| 163 |
if bad=='one_frame': f['evidence_frame_ids']=[0]
|
| 164 |
if bad=='angle': f['observation']='The hips rotate forty degrees.'
|
| 165 |
if bad=='numeric_priority': raw['priority']['reason']='You started 10 ms late.'
|
|
|
|
| 166 |
if bad=='wrong_drill': raw['priority']['drill_id']='hit_14'
|
| 167 |
if bad=='unclear_priority': f['status']='unclear'
|
| 168 |
if bad=='duplicate_feature': raw['features'][2]['key']='load_timing'
|
tests/test_visual_scoring.py
CHANGED
|
@@ -88,6 +88,7 @@ def test_legacy_notes_remain_readable_but_do_not_gain_invented_ratings(tmp_path)
|
|
| 88 |
assert result['score']['rep_scores'][0]['breakdown'][0]['reason_code']=='rating_not_supplied'
|
| 89 |
schema=visual_response_schema()
|
| 90 |
assert 'rating' in schema['$defs']['FeatureReview']['required']
|
|
|
|
| 91 |
|
| 92 |
|
| 93 |
def test_positive_visibility_flag_cannot_override_explicitly_unclear_interaction(tmp_path):
|
|
@@ -99,6 +100,36 @@ def test_positive_visibility_flag_cannot_override_explicitly_unclear_interaction
|
|
| 99 |
assert result['score']['rep_scores'][0]['breakdown'][2]['reason_code']=='ball_interaction_unclear'
|
| 100 |
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
@pytest.mark.parametrize('bad', ['numeric_score','invented_frame','rating_measurement','invalid_grade'])
|
| 103 |
def test_model_cannot_choose_a_numeric_score_or_bypass_evidence_guards(tmp_path,bad):
|
| 104 |
data=graded_note()
|
|
|
|
| 88 |
assert result['score']['rep_scores'][0]['breakdown'][0]['reason_code']=='rating_not_supplied'
|
| 89 |
schema=visual_response_schema()
|
| 90 |
assert 'rating' in schema['$defs']['FeatureReview']['required']
|
| 91 |
+
assert 'ball_interaction_visible' in schema['$defs']['FeatureReview']['required']
|
| 92 |
|
| 93 |
|
| 94 |
def test_positive_visibility_flag_cannot_override_explicitly_unclear_interaction(tmp_path):
|
|
|
|
| 100 |
assert result['score']['rep_scores'][0]['breakdown'][2]['reason_code']=='ball_interaction_unclear'
|
| 101 |
|
| 102 |
|
| 103 |
+
@pytest.mark.parametrize('priority_on_contact', [False, True])
|
| 104 |
+
def test_unverified_contact_is_removed_without_discarding_valid_body_feedback(tmp_path,priority_on_contact):
|
| 105 |
+
data=graded_note(('clear','mixed','clear'))
|
| 106 |
+
data['features'][2].update(observation='The bat hits the ball.',ball_interaction_visible=False)
|
| 107 |
+
data['priority']={'feature':'bat_path_contact' if priority_on_contact else 'load_timing',
|
| 108 |
+
'reason':'Practice the visible movement sequence.',
|
| 109 |
+
'drill_id':'hit_08' if priority_on_contact else 'hit_04'}
|
| 110 |
+
original=deepcopy(data)
|
| 111 |
+
result=review(data,tmp_path)
|
| 112 |
+
assert data==original
|
| 113 |
+
assert result['status']=='completed' and result['score']['value']==75
|
| 114 |
+
assert result['score']['rep_scores'][0]['features_scored']==2
|
| 115 |
+
contact=result['features'][2]
|
| 116 |
+
assert contact['status']=='unclear' and contact['rating'] is None
|
| 117 |
+
assert 'contact cannot be confirmed' in contact['observation']
|
| 118 |
+
assert result['guardrail_warnings']==[{'feature':'bat_path_contact','reason_code':'unverified_contact_statement'}]
|
| 119 |
+
if priority_on_contact:
|
| 120 |
+
assert result['priority'] is None and result['drill'] is None
|
| 121 |
+
else:
|
| 122 |
+
assert result['priority']['feature']=='load_timing' and result['drill']
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def test_contact_downgrade_does_not_hide_invalid_evidence(tmp_path):
|
| 126 |
+
data=graded_note(('clear','mixed','clear'))
|
| 127 |
+
data['features'][2].update(observation='The bat hits the ball.',
|
| 128 |
+
ball_interaction_visible=False,evidence_frame_ids=[0,999])
|
| 129 |
+
result=review(data,tmp_path)
|
| 130 |
+
assert result['status']=='unavailable' and result['validation_reason']=='evidence_reference'
|
| 131 |
+
|
| 132 |
+
|
| 133 |
@pytest.mark.parametrize('bad', ['numeric_score','invented_frame','rating_measurement','invalid_grade'])
|
| 134 |
def test_model_cannot_choose_a_numeric_score_or_bypass_evidence_guards(tmp_path,bad):
|
| 135 |
data=graded_note()
|