Process a single deficiency test sample: call API, classify deficiencies, and calculate metrics.
(self, item: Dict, max_retries: int = 3)
| 269 | self.compare_prompt = self._load_prompt_from_file("prompts/compare_question_prompt.txt") |
| 270 | |
| 271 | def process_deficiency(self, item: Dict, max_retries: int = 3) -> Dict: |
| 272 | """Process a single deficiency test sample: call API, classify deficiencies, and calculate metrics.""" |
| 273 | try: |
| 274 | # Convert image to base64 |
| 275 | image_path = os.path.join(self.image_root, item["image"]) if self.image_root else item["image"] |
| 276 | if not os.path.exists(image_path): |
| 277 | print(f"Debug - Deficiency image path: {image_path} (exists: {os.path.exists(image_path)})") |
| 278 | base64_image_uri = image_to_base64_uri(image_path) |
| 279 | if not base64_image_uri: |
| 280 | return None |
| 281 | |
| 282 | messages = [ |
| 283 | {"role": "system", "content": self.system_prompt}, |
| 284 | { |
| 285 | "role": "user", |
| 286 | "content": [ |
| 287 | {"type": "text", "text": self.deficiency_prompt}, |
| 288 | {"type": "image_url", "image_url": {"url": base64_image_uri}} |
| 289 | ] |
| 290 | } |
| 291 | ] |
| 292 | |
| 293 | # Call API with retries |
| 294 | generated_text = "" |
| 295 | for attempt in range(max_retries): |
| 296 | try: |
| 297 | completion = self.client.chat.completions.create( |
| 298 | model=self.test_model, |
| 299 | messages=messages, |
| 300 | temperature=1.0, |
| 301 | # top_p=0.95, |
| 302 | max_completion_tokens=1024*2, |
| 303 | ) |
| 304 | |
| 305 | generated_text = completion.choices[0].message.content or "" |
| 306 | break |
| 307 | |
| 308 | except Exception as e: |
| 309 | print(f"API call failed for slide {item.get('slide_id', 'unknown')} (attempt {attempt + 1}/{max_retries}): {e}") |
| 310 | if attempt < max_retries - 1: |
| 311 | time.sleep(2 ** attempt) # Exponential backoff |
| 312 | else: |
| 313 | print(f"Failed after {max_retries} attempts for slide {item.get('slide_id', 'unknown')}") |
| 314 | return None |
| 315 | |
| 316 | # Predict major categories directly via boolean classification |
| 317 | predicted_categories = set(classify_categories(generated_text)) |
| 318 | |
| 319 | # Get ground truth specific deficiencies and map to categories |
| 320 | ground_truth_deficiencies = [d["deficiency"] for d in item["deficiencies"]] |
| 321 | ground_truth_categories = { |
| 322 | DEFICIENCY_TO_CATEGORY_MAP[d] for d in ground_truth_deficiencies |
| 323 | if d in DEFICIENCY_TO_CATEGORY_MAP |
| 324 | } |
| 325 | |
| 326 | # Calculate metrics for CATEGORIES |
| 327 | cat_true_positives = len(predicted_categories.intersection(ground_truth_categories)) |
| 328 | cat_false_positives = len(predicted_categories - ground_truth_categories) |
no test coverage detected