|
| 1 | +from functools import cache |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from huggingface_hub import snapshot_download |
| 5 | + |
| 6 | + |
| 7 | +@cache |
| 8 | +def _get_videos_dir() -> Path: |
| 9 | + dataset_root = snapshot_download(repo_id="raivn/VideoNet", repo_type="dataset", allow_patterns=["videos/*.mp4"]) |
| 10 | + return Path(dataset_root) / "videos" |
| 11 | + |
| 12 | + |
| 13 | +def _get_video_path(video_fname) -> str: |
| 14 | + return str(_get_videos_dir() / video_fname) |
| 15 | + |
| 16 | + |
| 17 | +def videonet_binary_doc_to_visual(doc): |
| 18 | + question = doc["question"] |
| 19 | + video_fnames = [entry["video"] for entry in question if entry["type"] == "video"] |
| 20 | + video_paths = [_get_video_path(video_fname) for video_fname in video_fnames] |
| 21 | + return video_paths |
| 22 | + |
| 23 | + |
| 24 | +def videonet_binary_doc_to_text(doc, lmms_eval_specific_kwargs=None): |
| 25 | + question = doc["question"] |
| 26 | + texts = [entry["text"] for entry in question if entry["type"] == "text"] |
| 27 | + text = "\n".join(texts) |
| 28 | + if lmms_eval_specific_kwargs: |
| 29 | + pre_prompt = lmms_eval_specific_kwargs.get("pre_prompt", "") |
| 30 | + post_prompt = lmms_eval_specific_kwargs.get("post_prompt", "") |
| 31 | + text = pre_prompt + text + post_prompt |
| 32 | + return text |
| 33 | + |
| 34 | + |
| 35 | +def _process_video_entry(entry: dict) -> dict: |
| 36 | + video_fname = entry["video"] |
| 37 | + video_path = _get_video_path(video_fname) |
| 38 | + return {"type": "video", "url": video_path} |
| 39 | + |
| 40 | + |
| 41 | +def _process_text_entry(entry: dict, lmms_eval_specific_kwargs=None) -> dict: |
| 42 | + text = entry["text"] |
| 43 | + if lmms_eval_specific_kwargs: |
| 44 | + pre_prompt = lmms_eval_specific_kwargs.get("pre_prompt", "") |
| 45 | + post_prompt = lmms_eval_specific_kwargs.get("post_prompt", "") |
| 46 | + text = pre_prompt + text + post_prompt |
| 47 | + return {"type": "text", "text": text} |
| 48 | + |
| 49 | + |
| 50 | +def _question_to_content(question: list[dict], lmms_eval_specific_kwargs=None) -> list[dict]: |
| 51 | + content = [] |
| 52 | + if lmms_eval_specific_kwargs and (pre_prompt := lmms_eval_specific_kwargs.get("pre_prompt")): |
| 53 | + content.append({"type": "text", "text": pre_prompt}) |
| 54 | + for entry in question: |
| 55 | + if entry["type"] == "text": |
| 56 | + content.append(entry) |
| 57 | + elif entry["type"] == "video": |
| 58 | + content.append(_process_video_entry(entry)) |
| 59 | + else: |
| 60 | + raise Exception("Your copy of the benchmark is corrupted. Please re-download the `benchmarks/` folder from HuggingFace.") |
| 61 | + if lmms_eval_specific_kwargs and (post_prompt := lmms_eval_specific_kwargs.get("post_prompt")): |
| 62 | + content.append({"type": "text", "text": post_prompt}) |
| 63 | + return content |
| 64 | + |
| 65 | + |
| 66 | +def videonet_binary_doc_to_messages(doc, lmms_eval_specific_kwargs=None): |
| 67 | + question = doc["question"] |
| 68 | + content = _question_to_content(question, lmms_eval_specific_kwargs) |
| 69 | + return [{"role": "user", "content": content}] |
| 70 | + |
| 71 | + |
| 72 | +def _extract_binary_prediction(text: str) -> str: |
| 73 | + text = text.splitlines()[-1].strip().lower() |
| 74 | + pred = text.replace("*", "").replace("#", "").replace(",", "").replace(".", "").replace(":", "") |
| 75 | + |
| 76 | + if pred == "yes" or pred == "no": |
| 77 | + return pred |
| 78 | + parts = pred.split(" ") |
| 79 | + if len(parts) > 1: |
| 80 | + first, last = parts[0], parts[-1] |
| 81 | + if first == "yes" or first == "no": |
| 82 | + return first |
| 83 | + elif last == "yes" or last == "no": |
| 84 | + return last |
| 85 | + if "boxed{yes}" in pred: |
| 86 | + return "yes" |
| 87 | + if "boxed{no}" in pred: |
| 88 | + return "no" |
| 89 | + if "the answer is yes" in pred: |
| 90 | + return "yes" |
| 91 | + if "the answer is no" in pred: |
| 92 | + return "no" |
| 93 | + if 'is "yes"' in pred: |
| 94 | + return "yes" |
| 95 | + if "is 'yes'" in pred: |
| 96 | + return "yes" |
| 97 | + if 'is "no"' in pred: |
| 98 | + return "no" |
| 99 | + if "is 'no'" in pred: |
| 100 | + return "no" |
| 101 | + if "does not show" in pred or "does not depict" in pred: |
| 102 | + return "no" |
| 103 | + return pred |
| 104 | + |
| 105 | + |
| 106 | +def videonet_binary_process_results(doc, results): |
| 107 | + model_output = results[0] if results else "" |
| 108 | + ground_truth = doc["answer"] |
| 109 | + |
| 110 | + pred = _extract_binary_prediction(model_output) |
| 111 | + correct = 1.0 if pred == ground_truth else 0.0 |
| 112 | + return { |
| 113 | + "binary_acc": { |
| 114 | + "question_key": doc["key"], |
| 115 | + "correct": correct, |
| 116 | + "ground_truth": ground_truth, |
| 117 | + "model_prediction": pred, |
| 118 | + "model_output": model_output, |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + |
| 123 | +def videonet_binary_aggregate_results(results): |
| 124 | + if not results: |
| 125 | + return 0.0 |
| 126 | + num_correct = sum(r["correct"] for r in results) |
| 127 | + total = len(results) |
| 128 | + return num_correct / total |
0 commit comments