Process a single score test sample: call API, extract score, and calculate metrics.
(self, item: Dict, max_retries: int = 3)
| 358 | return None |
| 359 | |
| 360 | def process_score(self, item: Dict, max_retries: int = 3) -> Dict: |
| 361 | """Process a single score test sample: call API, extract score, and calculate metrics.""" |
| 362 | try: |
| 363 | # Convert image to base64 |
| 364 | image_path = os.path.join(self.image_root, item["image"]) if self.image_root else item["image"] |
| 365 | if not os.path.exists(image_path): |
| 366 | print(f"Debug - Score image path: {image_path} (exists: {os.path.exists(image_path)})") |
| 367 | base64_image_uri = image_to_base64_uri(image_path) |
| 368 | if not base64_image_uri: |
| 369 | return None |
| 370 | |
| 371 | messages = [ |
| 372 | {"role": "system", "content": self.system_prompt}, |
| 373 | { |
| 374 | "role": "user", |
| 375 | "content": [ |
| 376 | {"type": "text", "text": self.score_prompt}, |
| 377 | {"type": "image_url", "image_url": {"url": base64_image_uri}} |
| 378 | ] |
| 379 | } |
| 380 | ] |
| 381 | |
| 382 | # Call API with retries |
| 383 | generated_text = "" |
| 384 | for attempt in range(max_retries): |
| 385 | try: |
| 386 | completion = self.client.chat.completions.create( |
| 387 | model=self.test_model, |
| 388 | messages=messages, |
| 389 | temperature=1.0, |
| 390 | # top_p=0.95, |
| 391 | max_completion_tokens=1024, |
| 392 | ) |
| 393 | |
| 394 | generated_text = completion.choices[0].message.content or "" |
| 395 | break |
| 396 | |
| 397 | except Exception as e: |
| 398 | print(f"API call failed for slide {item.get('slide_id', 'unknown')} (attempt {attempt + 1}/{max_retries}): {e}") |
| 399 | if attempt < max_retries - 1: |
| 400 | time.sleep(2 ** attempt) # Exponential backoff |
| 401 | else: |
| 402 | print(f"Failed after {max_retries} attempts for slide {item.get('slide_id', 'unknown')}") |
| 403 | return None |
| 404 | |
| 405 | # Extract score from <answer> tags with improved validation |
| 406 | predicted_score, is_valid = self._extract_score_with_validation(generated_text) |
| 407 | |
| 408 | # Get ground truth score (overall if dict, otherwise direct value) |
| 409 | gt_score = item.get("score", None) or item.get("gt_score_norm", None) |
| 410 | if isinstance(gt_score, dict): |
| 411 | gt_score = gt_score.get("overall") |
| 412 | |
| 413 | # Calculate metrics |
| 414 | if predicted_score is not None and gt_score is not None and is_valid: |
| 415 | mae = abs(predicted_score - gt_score) |
| 416 | mse = (predicted_score - gt_score) ** 2 |
| 417 | # Threshold-based accuracy (within 0.35 as in training) |
no test coverage detected