Process a single comparison test sample: call API and calculate metrics.
(self, item: Dict, max_retries: int = 3)
| 491 | return None, False |
| 492 | |
| 493 | def process_compare(self, item: Dict, max_retries: int = 3) -> Dict: |
| 494 | """Process a single comparison test sample: call API and calculate metrics.""" |
| 495 | try: |
| 496 | # Convert images to base64 with constraints matching trainer |
| 497 | ref_image_path = os.path.join(self.image_root, item["ref_image"]) if self.image_root else item["ref_image"] |
| 498 | image_a_path = os.path.join(self.image_root, item["ImageA"]) if self.image_root else item["ImageA"] |
| 499 | image_b_path = os.path.join(self.image_root, item["ImageB"]) if self.image_root else item["ImageB"] |
| 500 | |
| 501 | # Debug: print the full paths being used (only if file doesn't exist) |
| 502 | if not os.path.exists(ref_image_path) or not os.path.exists(image_a_path) or not os.path.exists(image_b_path): |
| 503 | print(f"Debug - Image paths:") |
| 504 | print(f" ref_image: {ref_image_path} (exists: {os.path.exists(ref_image_path)})") |
| 505 | print(f" image_a: {image_a_path} (exists: {os.path.exists(image_a_path)})") |
| 506 | print(f" image_b: {image_b_path} (exists: {os.path.exists(image_b_path)})") |
| 507 | |
| 508 | ref_image_uri = process_image_to_base64(ref_image_path, max_long_side=720, min_side=28) |
| 509 | image_a_uri = process_image_to_base64(image_a_path, max_long_side=720, min_side=28) |
| 510 | image_b_uri = process_image_to_base64(image_b_path, max_long_side=720, min_side=28) |
| 511 | |
| 512 | if not all([ref_image_uri, image_a_uri, image_b_uri]): |
| 513 | return None |
| 514 | |
| 515 | messages = [ |
| 516 | {"role": "system", "content": [{"type": "text", "text": self.system_prompt}]}, |
| 517 | { |
| 518 | "role": "user", |
| 519 | "content": [ |
| 520 | {"type": "text", "text": "Given a low-quality reference slide and two enhanced outputs. Reference Slide:"}, |
| 521 | {"type": "image_url", "image_url": {"url": f"{ref_image_uri}"}}, |
| 522 | {"type": "text", "text": "Slide A:"}, |
| 523 | {"type": "image_url", "image_url": {"url": f"{image_a_uri}"}}, |
| 524 | {"type": "text", "text": "Slide B:"}, |
| 525 | {"type": "image_url", "image_url": {"url": f"{image_b_uri}"}}, |
| 526 | {"type": "text", "text": self.compare_prompt}, |
| 527 | ] |
| 528 | } |
| 529 | ] |
| 530 | |
| 531 | # Call API with retries |
| 532 | generated_text = "" |
| 533 | for attempt in range(max_retries): |
| 534 | try: |
| 535 | completion = self.client.chat.completions.create( |
| 536 | model=self.test_model, |
| 537 | messages=messages, |
| 538 | temperature=1.0, |
| 539 | # top_p=0.95, |
| 540 | max_completion_tokens=1024*2, |
| 541 | ) |
| 542 | |
| 543 | generated_text = completion.choices[0].message.content or "" |
| 544 | break |
| 545 | |
| 546 | except Exception as e: |
| 547 | print(f"API call failed for sample (attempt {attempt + 1}/{max_retries}): {e}") |
| 548 | if attempt < max_retries - 1: |
| 549 | time.sleep(2 ** attempt) # Exponential backoff |
| 550 | else: |
no test coverage detected