jee-neet-benchmark / jee_neet_benchmark_dataset.py
Reja1's picture
First working version
de60742
raw
history blame
5.73 kB
import json
import os
import datasets
_CITATION = """\
@misc{jee-neet-benchmark,
title={JEE/NEET LLM Benchmark},
author={Md Rejaullah},
year={2025},
howpublished={\\url{https://huggingface.co/datasets/Reja1/jee-neet-benchmark}},
}
"""
_DESCRIPTION = """\
A benchmark dataset for evaluating Large Language Models (LLMs) on Joint Entrance Examination (JEE)
and National Eligibility cum Entrance Test (NEET) questions from India. Questions are provided as
images, and metadata includes exam details, subject, and correct answers.
"""
_HOMEPAGE = "https://huggingface.co/datasets/Reja1/jee-neet-benchmark"
_LICENSE = "MIT License"
class JeeNeetBenchmarkConfig(datasets.BuilderConfig):
"""BuilderConfig for JeeNeetBenchmark."""
def __init__(self, **kwargs):
"""BuilderConfig for JeeNeetBenchmark.
Args:
**kwargs: keyword arguments forwarded to super.
"""
super(JeeNeetBenchmarkConfig, self).__init__(**kwargs)
class JeeNeetBenchmark(datasets.GeneratorBasedBuilder):
"""JEE/NEET LLM Benchmark Dataset."""
VERSION = datasets.Version("1.0.0") # Start with version 1.0.0
BUILDER_CONFIGS = [
JeeNeetBenchmarkConfig(
name="default",
version=VERSION,
description="Default config for JEE/NEET Benchmark",
),
]
DEFAULT_CONFIG_NAME = "default"
def _info(self):
features = datasets.Features(
{
"image": datasets.Image(),
"question_id": datasets.Value("string"),
"exam_name": datasets.Value("string"),
"exam_year": datasets.Value("int32"),
"exam_code": datasets.Value("string"),
"subject": datasets.Value("string"),
"question_type": datasets.Value("string"),
"correct_answer": datasets.Sequence(datasets.Value("int32")), # List of integers
}
)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=features,
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
"""Returns SplitGenerators."""
# dl_manager is useful if downloading/extracting files, but here we use local paths
# Determine the base directory for data files
# Use data_dir if provided (for local loading), otherwise use the script's directory
base_dir = self.config.data_dir if self.config.data_dir is not None else os.path.dirname(__file__)
metadata_path = os.path.join(base_dir, "data", "metadata.jsonl")
image_dir = os.path.join(base_dir, "images")
# Check if metadata file exists
if not os.path.exists(metadata_path):
raise FileNotFoundError(
f"Metadata file not found at {metadata_path}. "
f"Make sure 'data/metadata.jsonl' exists in your dataset repository. "
f"If running locally, you might need to specify the path using --data_dir argument "
f"or ensure the script is run from the project root."
)
return [
datasets.SplitGenerator(
name=datasets.Split.TEST, # Using TEST split as it's standard for evaluation-only data
# Or use name="evaluate" if you prefer that specific name
gen_kwargs={
"metadata_filepath": metadata_path,
"image_base_dir": image_dir, # Pass the base image directory
},
),
]
def _generate_examples(self, metadata_filepath, image_base_dir):
"""Yields examples."""
with open(metadata_filepath, "r", encoding="utf-8") as f:
for idx, line in enumerate(f):
try:
row = json.loads(line)
except json.JSONDecodeError as e:
print(f"Error decoding JSON on line {idx+1}: {e}")
continue # Skip malformed lines
image_path_relative = row.get("image_path")
if not image_path_relative:
print(f"Warning: Missing 'image_path' on line {idx+1}. Skipping.")
continue
# Construct the full path relative to the dataset root
image_path_full = os.path.join(image_base_dir, os.path.relpath(image_path_relative, start="images"))
# Alternative if image_path is already relative to root:
# image_path_full = os.path.join(image_base_dir, image_path_relative)
if not os.path.exists(image_path_full):
print(f"Warning: Image file not found at {image_path_full} referenced on line {idx+1}. Skipping.")
# Yielding with None image might cause issues later, better to skip or handle
# image_data = None
continue
# else:
# Let datasets.Image() handle the loading by passing the path
# image_data = image_path_full
yield idx, {
"image": image_path_full, # Pass the full path to the image feature
"question_id": row.get("question_id", ""),
"exam_name": row.get("exam_name", ""),
"exam_year": row.get("exam_year", -1), # Use a default if missing
"exam_code": row.get("exam_code", ""),
"subject": row.get("subject", ""),
"question_type": row.get("question_type", ""),
"correct_answer": row.get("correct_answer", []),
}