(raw_text: str)
| 4 | |
| 5 | |
| 6 | def interpret_vlm_output(raw_text: str) -> Tuple[str, bool]: |
| 7 | if not raw_text: |
| 8 | return "", False |
| 9 | |
| 10 | raw_text = raw_text.strip() |
| 11 | candidates = [raw_text] |
| 12 | if "```" in raw_text: |
| 13 | for block in raw_text.split("```"): |
| 14 | block = block.strip() |
| 15 | if not block: |
| 16 | continue |
| 17 | if block.lower().startswith("json"): |
| 18 | block = block[4:].strip() |
| 19 | candidates.append(block) |
| 20 | |
| 21 | def to_bool(value: Any) -> bool: |
| 22 | if isinstance(value, bool): |
| 23 | return value |
| 24 | if isinstance(value, str): |
| 25 | lowered = value.strip().lower() |
| 26 | if lowered in {"true", "yes"}: |
| 27 | return True |
| 28 | if lowered in {"false", "no"}: |
| 29 | return False |
| 30 | if isinstance(value, (int, float)): |
| 31 | return bool(value) |
| 32 | return False |
| 33 | |
| 34 | for candidate in candidates: |
| 35 | if not candidate: |
| 36 | continue |
| 37 | try: |
| 38 | parsed = json.loads(candidate) |
| 39 | except Exception: |
| 40 | continue |
| 41 | analysis = parsed.get("analysis", raw_text) |
| 42 | matches = to_bool(parsed.get("matches")) |
| 43 | return analysis, matches |
| 44 | |
| 45 | lowered = raw_text.lower() |
| 46 | match = re.search(r'"matches"\\s*:\\s*(true|false)', lowered) |
| 47 | matches = match.group(1) == "true" if match else ("\"matches\"" in lowered and "true" in lowered) |
| 48 | return raw_text, matches |
no test coverage detected