| import ast |
| from datasets import load_dataset |
| from transformers import AutoModelForCausalLM, AutoTokenizer |
| import torch |
|
|
| print("1. Loading LogistikaBench from Hugging Face...") |
| |
| dataset = load_dataset("berdymurad/LogistikaBench", data_files="logistikabench.csv") |
| test_data = dataset["train"] |
| print(f"Loaded {len(test_data)} evaluation items successfully!") |
|
|
| print("\n2. Loading test model (Qwen2.5-1.5B-Instruct)...") |
| model_id = "Qwen/Qwen2-7B-Instruct" |
| tokenizer = AutoTokenizer.from_pretrained(model_id) |
|
|
| try: |
| model = AutoModelForCausalLM.from_pretrained( |
| model_id, |
| torch_dtype=torch.float16, |
| device_map="auto" |
| ) |
| except Exception as e: |
| print(f"Error loading model: {e}") |
| raise |
|
|
| correct_predictions = 0 |
| total_evaluated = 0 |
|
|
| print("\n3. Starting benchmark loop...") |
| for i, item in enumerate(test_data): |
| question = item.get('question', '') |
|
|
| |
| raw_choices = item.get('choices', '[]') |
| if isinstance(raw_choices, str): |
| choices_list = ast.literal_eval(raw_choices) |
| else: |
| choices_list = raw_choices |
|
|
| raw_answer = item.get('answer', '[]') |
| if isinstance(raw_answer, str): |
| true_indices = ast.literal_eval(raw_answer) |
| else: |
| true_indices = raw_answer |
|
|
| |
| letter_mapping = {idx: chr(65 + idx) for idx in range(len(choices_list))} |
| true_letters = set([letter_mapping[idx] for idx in true_indices if idx in letter_mapping]) |
|
|
| |
| formatted_choices = "\n".join([f"{letter_mapping[idx]}: {choice}" for idx, choice in enumerate(choices_list)]) |
|
|
| |
| prompt = ( |
| f"Answer the following logistics and supply chain multiple-choice question. " |
| f"Note that there may be ONE or MORE correct answers. " |
| f"Provide the letters of all correct options together (e.g., A, or AB, or A, C).\n\n" |
| f"Question: {question}\n\n" |
| f"Choices:\n{formatted_choices}\n\n" |
| f"Answers (give only the letters):" |
| ) |
|
|
| inputs = tokenizer(prompt, return_tensors="pt").to(model.device) |
| outputs = model.generate( |
| **inputs, |
| max_new_tokens=15, |
| temperature=0.0, |
| do_sample=False |
| ) |
|
|
| response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True).upper().strip() |
|
|
| |
| valid_choice_letters = set(letter_mapping.values()) |
|
|
| |
| |
| predicted_letters_temp = set() |
| for char in response: |
| if char in valid_choice_letters: |
| predicted_letters_temp.add(char) |
| elif char.isspace() or char == ',': |
| |
| continue |
| else: |
| |
| break |
| predicted_letters = predicted_letters_temp |
|
|
| |
| if predicted_letters == true_letters: |
| correct_predictions += 1 |
|
|
| total_evaluated += 1 |
|
|
| |
|
|
| if (i + 1) % 50 == 0: |
| print(f"Processed {i + 1} / {len(test_data)} questions...") |
|
|
| |
| accuracy = (correct_predictions / total_evaluated) * 100 |
| print("\n================================") |
| print(f"🏁 BENCHMARK COMPLETE!") |
| print(f"Total Questions Evaluated: {total_evaluated}") |
| print(f"Strict Match Accuracy: {accuracy:.2f}%") |
| print("================================") |