Spaces:
Running on T4
Running on T4
github-actions[bot] commited on
Commit ·
6f63794
1
Parent(s): 7b016f3
Automated Deploy: 516a11a73fab7949b0b340b9de752485e86570c2
Browse files- AI-coach-agent/src/ai_coach/video_review.py +26 -1
- docs/SWING_COACH_PRODUCT.md +10 -0
- tests/test_product.py +21 -1
AI-coach-agent/src/ai_coach/video_review.py
CHANGED
|
@@ -170,6 +170,30 @@ def validate_note(note: VisualNote, frames: list[dict]) -> None:
|
|
| 170 |
raise ValueError("Priority cannot invent measured or medical claims")
|
| 171 |
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
def gemini_generate(settings: Settings, frames: list[dict], facts: dict):
|
| 174 |
from google import genai
|
| 175 |
from google.genai import types
|
|
@@ -186,7 +210,8 @@ def gemini_generate(settings: Settings, frames: list[dict], facts: dict):
|
|
| 186 |
retry_options=types.HttpRetryOptions(attempts=1))) as client:
|
| 187 |
response = client.models.generate_content(model=settings.video_review_model,
|
| 188 |
contents=contents, config=types.GenerateContentConfig(temperature=0,
|
| 189 |
-
max_output_tokens=2500, response_mime_type="application/json",
|
|
|
|
| 190 |
usage = response.usage_metadata
|
| 191 |
counts = Usage(input_tokens=(usage.prompt_token_count or 0) if usage else 0,
|
| 192 |
output_tokens=((usage.candidates_token_count or 0) + (usage.thoughts_token_count or 0)) if usage else 0,
|
|
|
|
| 170 |
raise ValueError("Priority cannot invent measured or medical claims")
|
| 171 |
|
| 172 |
|
| 173 |
+
def visual_response_schema() -> dict:
|
| 174 |
+
"""Send Gemini's JSON Schema subset, keeping full validation local.
|
| 175 |
+
|
| 176 |
+
The legacy response_schema conversion emits OpenAPI/protobuf fields that
|
| 177 |
+
Google rejects for this model. JSON Schema preserves refs and camel-case
|
| 178 |
+
keywords; string length and default constraints are not provider-supported.
|
| 179 |
+
Pydantic still enforces those constraints on every returned response.
|
| 180 |
+
"""
|
| 181 |
+
def supported(schema):
|
| 182 |
+
result = {}
|
| 183 |
+
for key, value in schema.items():
|
| 184 |
+
if key in {"default", "minLength", "maxLength"}:
|
| 185 |
+
continue
|
| 186 |
+
if key in {"properties", "$defs"}:
|
| 187 |
+
value = {name: supported(child) for name, child in value.items()}
|
| 188 |
+
elif key == "items":
|
| 189 |
+
value = supported(value)
|
| 190 |
+
elif key in {"anyOf", "oneOf", "prefixItems"}:
|
| 191 |
+
value = [supported(child) for child in value]
|
| 192 |
+
result[key] = value
|
| 193 |
+
return result
|
| 194 |
+
return supported(VisualNote.model_json_schema())
|
| 195 |
+
|
| 196 |
+
|
| 197 |
def gemini_generate(settings: Settings, frames: list[dict], facts: dict):
|
| 198 |
from google import genai
|
| 199 |
from google.genai import types
|
|
|
|
| 210 |
retry_options=types.HttpRetryOptions(attempts=1))) as client:
|
| 211 |
response = client.models.generate_content(model=settings.video_review_model,
|
| 212 |
contents=contents, config=types.GenerateContentConfig(temperature=0,
|
| 213 |
+
max_output_tokens=2500, response_mime_type="application/json",
|
| 214 |
+
response_json_schema=visual_response_schema()))
|
| 215 |
usage = response.usage_metadata
|
| 216 |
counts = Usage(input_tokens=(usage.prompt_token_count or 0) if usage else 0,
|
| 217 |
output_tokens=((usage.candidates_token_count or 0) + (usage.thoughts_token_count or 0)) if usage else 0,
|
docs/SWING_COACH_PRODUCT.md
CHANGED
|
@@ -138,6 +138,16 @@ provider messages, credentials, project identifiers and uploaded content are
|
|
| 138 |
never forwarded in error diagnostics. Model metadata availability alone does
|
| 139 |
not establish generation quota or that a video-review call will succeed.
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
Results and evidence follow the engine retention period (default 24 hours), are
|
| 142 |
checksum verified on retrieval, and require authentication. Original uploads
|
| 143 |
are removed after jobs finish; sampled evidence is retained until deletion or
|
|
|
|
| 138 |
never forwarded in error diagnostics. Model metadata availability alone does
|
| 139 |
not establish generation quota or that a video-review call will succeed.
|
| 140 |
|
| 141 |
+
The video request uses `response_json_schema`, with only Google's supported JSON
|
| 142 |
+
Schema constraints. Unsupported default and string-length keywords are removed
|
| 143 |
+
from the provider request; the full Pydantic length, evidence and content checks
|
| 144 |
+
still run locally before feedback is accepted. This avoids the rejected legacy
|
| 145 |
+
OpenAPI `response_schema` request format.
|
| 146 |
+
|
| 147 |
+
Space results need persistent server storage to survive rebuilds. On a Space
|
| 148 |
+
using temporary storage, save the product JSON and any needed evidence images
|
| 149 |
+
before restarting or deploying. A job ID cannot recover files erased by a rebuild.
|
| 150 |
+
|
| 151 |
Results and evidence follow the engine retention period (default 24 hours), are
|
| 152 |
checksum verified on retrieval, and require authentication. Original uploads
|
| 153 |
are removed after jobs finish; sampled evidence is retained until deletion or
|
tests/test_product.py
CHANGED
|
@@ -197,6 +197,8 @@ def test_actual_sdk_request_uses_images_schema_timeouts_and_billed_usage(tmp_pat
|
|
| 197 |
assert json.loads(raw)['features'][0]['key']=='load_timing'
|
| 198 |
assert usage.input_tokens==50 and usage.output_tokens==25 and usage.cached_tokens==10
|
| 199 |
assert calls[0]['config'].response_mime_type=='application/json'
|
|
|
|
|
|
|
| 200 |
assert len([p for p in calls[0]['contents'] if p.inline_data])==3
|
| 201 |
assert 'test-not-a-key' not in str(calls)
|
| 202 |
|
|
@@ -391,4 +393,22 @@ def test_real_google_sdk_serializes_our_schema_and_decodes_response(tmp_path,mon
|
|
| 391 |
raw,usage=gemini_generate(configured,frames(),{})
|
| 392 |
assert json.loads(raw)['features'][0]['key']=='load_timing'
|
| 393 |
assert usage.input_tokens==50 and usage.output_tokens==20
|
| 394 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
assert json.loads(raw)['features'][0]['key']=='load_timing'
|
| 198 |
assert usage.input_tokens==50 and usage.output_tokens==25 and usage.cached_tokens==10
|
| 199 |
assert calls[0]['config'].response_mime_type=='application/json'
|
| 200 |
+
assert calls[0]['config'].response_schema is None
|
| 201 |
+
assert calls[0]['config'].response_json_schema['properties']['features']['minItems']==3
|
| 202 |
assert len([p for p in calls[0]['contents'] if p.inline_data])==3
|
| 203 |
assert 'test-not-a-key' not in str(calls)
|
| 204 |
|
|
|
|
| 393 |
raw,usage=gemini_generate(configured,frames(),{})
|
| 394 |
assert json.loads(raw)['features'][0]['key']=='load_timing'
|
| 395 |
assert usage.input_tokens==50 and usage.output_tokens==20
|
| 396 |
+
config=requests[0]['generationConfig']
|
| 397 |
+
assert 'responseSchema' not in config
|
| 398 |
+
schema=config['responseJsonSchema']
|
| 399 |
+
assert schema['properties']['features']['minItems']==3
|
| 400 |
+
assert schema['additionalProperties'] is False
|
| 401 |
+
assert schema['$defs']['FeatureReview']['properties']['rep']['minimum']==1
|
| 402 |
+
assert schema['$defs']['FeatureReview']['properties']['key']['enum']==[
|
| 403 |
+
'load_timing','hip_shoulder_separation','bat_path_contact']
|
| 404 |
+
assert schema['properties']['priority']['anyOf'][1]=={'type':'null'}
|
| 405 |
+
for unsupported in ('minLength','maxLength','default','min_items','additional_properties'):
|
| 406 |
+
assert unsupported not in json.dumps(schema)
|
| 407 |
+
|
| 408 |
+
|
| 409 |
+
def test_provider_schema_subset_keeps_local_length_and_evidence_validation(tmp_path):
|
| 410 |
+
from ai_coach.video_review import review_video
|
| 411 |
+
too_long=note()
|
| 412 |
+
too_long['features'][0]['observation']='x'*551
|
| 413 |
+
result=review_video(frames(),{},coach_settings(tmp_path),generator=lambda *a:(json.dumps(too_long),Usage()))
|
| 414 |
+
assert result['reason_code']=='invalid_visual_response'
|