|
| 1 | +import io |
| 2 | +import re |
| 3 | +import zipfile |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +from huggingface_hub import snapshot_download |
| 7 | +from PIL import Image |
| 8 | + |
| 9 | +DATASET_REPO_ID = "VisuLogic/VisuLogic" |
| 10 | +OPTION_LETTERS = {"A", "B", "C", "D"} |
| 11 | + |
| 12 | +_DATASET_DIR = Path(snapshot_download(repo_id=DATASET_REPO_ID, repo_type="dataset", local_dir_use_symlinks=False)) |
| 13 | +_IMAGES_ARCHIVE_PATH = _DATASET_DIR / "images.zip" |
| 14 | +_ANSWER_PATTERNS = [ |
| 15 | + re.compile(r"<answer>\s*\(?([A-D])\)?\s*</answer>", re.IGNORECASE | re.DOTALL), |
| 16 | + re.compile(r"\\boxed\{\s*([A-D])\s*\}", re.IGNORECASE), |
| 17 | + re.compile(r"answer\s*(?:is|:|-)\s*\(?([A-D])\)?\b", re.IGNORECASE), |
| 18 | + re.compile(r"option\s*([A-D])\b", re.IGNORECASE), |
| 19 | + re.compile(r"\(([A-D])\)", re.IGNORECASE), |
| 20 | +] |
| 21 | +_IMAGES_ARCHIVE = None |
| 22 | + |
| 23 | + |
| 24 | +def _get_images_archive() -> zipfile.ZipFile: |
| 25 | + global _IMAGES_ARCHIVE |
| 26 | + if _IMAGES_ARCHIVE is None: |
| 27 | + _IMAGES_ARCHIVE = zipfile.ZipFile(_IMAGES_ARCHIVE_PATH, "r") |
| 28 | + return _IMAGES_ARCHIVE |
| 29 | + |
| 30 | + |
| 31 | +def _extract_option_letter(text: str) -> str: |
| 32 | + normalized = str(text).strip() |
| 33 | + if not normalized: |
| 34 | + return "" |
| 35 | + |
| 36 | + for pattern in _ANSWER_PATTERNS: |
| 37 | + matches = pattern.findall(normalized) |
| 38 | + if matches: |
| 39 | + return matches[-1].upper() |
| 40 | + |
| 41 | + if len(normalized) <= 3: |
| 42 | + first_char = normalized.upper()[0] |
| 43 | + if first_char in OPTION_LETTERS: |
| 44 | + return first_char |
| 45 | + |
| 46 | + return "" |
| 47 | + |
| 48 | + |
| 49 | +def visulogic_doc_to_visual(doc): |
| 50 | + image_path = str(doc.get("image_path", "")).strip().lstrip("./") |
| 51 | + if not image_path: |
| 52 | + return [] |
| 53 | + |
| 54 | + archive = _get_images_archive() |
| 55 | + try: |
| 56 | + with archive.open(image_path) as image_file: |
| 57 | + image_bytes = image_file.read() |
| 58 | + except KeyError as error: |
| 59 | + raise FileNotFoundError(f"Image not found in {DATASET_REPO_ID} archive: {image_path}") from error |
| 60 | + |
| 61 | + return [Image.open(io.BytesIO(image_bytes)).convert("RGB")] |
| 62 | + |
| 63 | + |
| 64 | +def visulogic_doc_to_text(doc, lmms_eval_specific_kwargs=None): |
| 65 | + kwargs = lmms_eval_specific_kwargs or {} |
| 66 | + pre_prompt = kwargs.get("pre_prompt", "") |
| 67 | + post_prompt = kwargs.get("post_prompt", "") |
| 68 | + |
| 69 | + question = str(doc.get("question", "")).strip() |
| 70 | + return f"{pre_prompt}{question}{post_prompt}" |
| 71 | + |
| 72 | + |
| 73 | +def visulogic_doc_to_target(doc): |
| 74 | + return str(doc.get("label", "")).strip().upper()[:1] |
| 75 | + |
| 76 | + |
| 77 | +def visulogic_process_results(doc, results): |
| 78 | + prediction = str(results[0]).strip() if results else "" |
| 79 | + predicted_letter = _extract_option_letter(prediction) |
| 80 | + target = visulogic_doc_to_target(doc) |
| 81 | + score = 1.0 if predicted_letter == target else 0.0 |
| 82 | + return {"visulogic_acc": score} |
| 83 | + |
| 84 | + |
| 85 | +def visulogic_aggregate_acc(items): |
| 86 | + if not items: |
| 87 | + return 0.0 |
| 88 | + return sum(float(item) for item in items) / len(items) |
0 commit comments