-
Notifications
You must be signed in to change notification settings - Fork 610
FEAT Add JailbreakV_28k dataset from HF #1098
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AdrGav941
wants to merge
17
commits into
Azure:main
Choose a base branch
from
AdrGav941:add__HF_jailbreakV_28K_dataset
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1ab7c61
Adding JailBreakV_28k dataset and tests
5d297d3
Fixes for handling splits
1afab68
pre-commit hooks
2b7b81f
removing unused params
8ec3baa
Adding image support handling for invalid image_path entries coming f…
115c1c2
Integration tests, ValueError for empty seed_prompts, comment cleanup
725418b
Adding threshold for prompts returned, associated tests and local ima…
625a16c
Merge branch 'main' into add__HF_jailbreakV_28K_dataset
AdrGav941 9d65116
Fixing sorting for dataset init imports
615b01d
Fixing merge conflicts
31f7c48
Removing integration tests with manual download approach
8a9fa3b
Cleaning up comments
8439969
Merge branch 'main' into add__HF_jailbreakV_28K_dataset
AdrGav941 5becda3
Adding excpetion for JailbrakV-28k dataset in integration presence ve…
c9b9dc3
Merge remote-tracking branch 'refs/remotes/adrgav941/add__HF_jailbrea…
c2d8540
Removing jailbreakV-28k from integration tests
5a9de48
Merge branch 'main' into add__HF_jailbreakV_28K_dataset
AdrGav941 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| import logging | ||
| import pathlib | ||
| import uuid | ||
| import zipfile | ||
| from typing import Dict, List, Literal, Optional | ||
|
|
||
| from datasets import load_dataset | ||
| from huggingface_hub import HfApi, hf_hub_download | ||
|
|
||
| from pyrit.models import SeedPrompt, SeedPromptDataset | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| HarmLiteral = Literal[ | ||
| "Unethical Behavior", | ||
| "Economic Harm", | ||
| "Hate Speech", | ||
| "Government Decision", | ||
| "Physical Harm", | ||
| "Fraud", | ||
| "Political Sensitivity", | ||
| "Malware", | ||
| "Illegal Activity", | ||
| "Bias", | ||
| "Violence", | ||
| "Animal Abuse", | ||
| "Tailored Unlicensed Advice", | ||
| "Privacy Violation", | ||
| "Health Consultation", | ||
| "Child Abuse Content", | ||
| ] | ||
|
|
||
|
|
||
| def fetch_jailbreakv_28k_dataset( | ||
| *, | ||
| data_home: Optional[str] = None, | ||
| zip_dir: Optional[str] = None, | ||
| split: Literal["JailBreakV_28K", "mini_JailBreakV_28K"] = "mini_JailBreakV_28K", | ||
| text_field: Literal["jailbreak_query", "redteam_query"] = "redteam_query", | ||
| harm_categories: Optional[List[HarmLiteral]] = None, | ||
| min_prompts: int = 50, | ||
| ) -> SeedPromptDataset: | ||
AdrGav941 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| Fetch examples from the JailBreakV 28k Dataset with optional filtering and create a SeedPromptDataset. | ||
| Many images are missing from the dataset in HF and the team host the full image file in Google Drive. | ||
AdrGav941 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Prioritizes the HF dataset and falls back to a cached download of Google Drive contents. | ||
AdrGav941 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| As of 10/2025 the HF dataset is missing most images, backup path should be provided. | ||
| Please download the zip file from the HF owners Google Drive at this share link: | ||
| https://drive.google.com/file/d/1ZrvSHklXiGYhpiVoxUH8FWc5k0fv2xVZ/view | ||
AdrGav941 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Note that the file is 15 GB compressed due to images not compressing well. | ||
| To reduce disk usage after extracting the first time it is fine to delete the zip. | ||
| Args: | ||
| data_home: Directory used as cache_dir in call to HF to store cached data. Defaults to None. | ||
| If None, the default cache directory will be used. | ||
| zip_dir (str): The directory containing the zip file. Defaults to None. | ||
| As of 10/2025 the HF dataset is missing most images, backup path should be provided. | ||
| split (str): The split of the dataset to fetch. Defaults to "mini_JailBreakV_28K". | ||
| Options are "JailBreakV_28K" and "mini_JailBreakV_28K". | ||
| text_field (str): The field to use as the prompt text. Defaults to "redteam_query". | ||
| Options are "jailbreak_query" and "redteam_query". | ||
| harm_categories: List of harm categories to filter the examples. | ||
| Defaults to None, which means all categories are included. | ||
| Otherwise, only prompts with at least one matching category are included. | ||
| min_prompts (int): The minimum number of prompts to return. Defaults to 50. | ||
| If the number of prompts after filtering is less than this value, an error is raised. | ||
| Returns: | ||
| SeedPromptDataset: A SeedPromptDataset containing the filtered examples. | ||
| Note: | ||
| For more information and access to the original dataset and related materials, visit: | ||
| https://huggingface.co/datasets/JailbreakV-28K/JailBreakV-28k/blob/main/README.md \n | ||
| Related paper: https://arxiv.org/abs/2404.03027 \n | ||
| The dataset license: MIT | ||
| Authors: Weidi Luo, Siyuan Ma, Xiaogeng Liu, Chaowei Xiao, Xiaoyu Guo | ||
| Warning: | ||
| Due to the nature of these prompts, it may be advisable to consult your relevant legal | ||
| department before testing them with LLMs to ensure compliance and reduce potential risks. | ||
| """ | ||
|
|
||
| source = "JailbreakV-28K/JailBreakV-28k" | ||
|
|
||
| # Unzip the file if it is not already extracted | ||
| zip_extracted_path = None | ||
| if zip_dir: | ||
| zip_file_path = pathlib.Path(zip_dir) / "JailBreakV_28K.zip" | ||
| zip_extracted_path = pathlib.Path(zip_dir) / "JailBreakV_28K" | ||
| # Check if the zip file exists | ||
| if not zip_file_path.exists(): | ||
| raise FileNotFoundError("No zip file provided for JailBreakV-28K dataset. Many images likely missing.") | ||
| # Only unzip if the target directory does not already exist | ||
| if not zip_extracted_path.exists(): | ||
| with zipfile.ZipFile(zip_file_path, "r") as zip_ref: | ||
| zip_ref.extractall(pathlib.Path(zip_dir)) | ||
| else: | ||
| logger.warning("No zip file provided for JailBreakV-28K dataset. Many images likely missing.") | ||
|
|
||
| try: | ||
| logger.info(f"Loading JailBreakV-28k dataset from {source}") | ||
|
|
||
| # Normalize the harm categories to match pyrit harm category conventions | ||
| harm_categories_normalized = ( | ||
| None if not harm_categories else [_normalize_policy(policy) for policy in harm_categories] | ||
| ) | ||
|
|
||
| # Load the dataset from HuggingFace | ||
| data = load_dataset(source, "JailBreakV_28K", cache_dir=data_home) | ||
|
|
||
| dataset_split = data[split] | ||
|
|
||
| per_call_cache: Dict[str, str] = {} | ||
|
|
||
| seed_prompts = [] | ||
|
|
||
| # Define common metadata that will be used across all seed prompts | ||
| common_metadata = { | ||
| "dataset_name": "JailbreakV-28K", | ||
| "authors": ["Weidi Luo", "Siyuan Ma", "Xiaogeng Liu", "Chaowei Xiao", "Xiaoyu Guo"], | ||
| "description": ( | ||
| "Benchmark for Assessing the Robustness of " | ||
| "Multimodal Large Language Models against Jailbreak Attacks. " | ||
| ), | ||
| "groups": ["The Ohio State University", "Peking University", "University of Wisconsin-Madison"], | ||
| "source": "https://huggingface.co/datasets/JailbreakV-28K/JailBreakV-28k", | ||
| "name": "JailBreakV-28K", | ||
| } | ||
|
|
||
| # tracker for items in the dataset where image_path does not match an image in the repo | ||
| missing_images = 0 | ||
|
|
||
| for item in dataset_split: | ||
| policy = _normalize_policy(item.get("policy", "")) | ||
| # Skip if user requested policy filter and items policy does not match | ||
| if not (harm_categories_normalized) or policy in harm_categories_normalized: | ||
| image_rel_path = item.get("image_path", "") | ||
| image_abs_path = "" | ||
| if image_rel_path: | ||
| image_abs_path = _resolve_image_path( | ||
| image_rel_path, | ||
| repo_id=source, | ||
| data_home=data_home, | ||
| backup_root=zip_extracted_path, | ||
| call_cache=per_call_cache, | ||
| ) | ||
| if not image_abs_path: | ||
| missing_images += 1 | ||
| continue | ||
|
|
||
| group_id = uuid.uuid4() | ||
| text_seed_prompt = SeedPrompt( | ||
| value=item.get(text_field, ""), | ||
| harm_categories=[policy], | ||
| prompt_group_id=group_id, | ||
| data_type="text", | ||
| **common_metadata, # type: ignore[arg-type] | ||
| ) | ||
| image_seed_prompt = SeedPrompt( | ||
| value=image_abs_path, | ||
| harm_categories=[policy], | ||
| prompt_group_id=group_id, | ||
| data_type="image_path", | ||
| **common_metadata, # type: ignore[arg-type] | ||
| ) | ||
| seed_prompts.append(text_seed_prompt) | ||
| seed_prompts.append(image_seed_prompt) | ||
|
|
||
| except Exception as e: | ||
| logger.error(f"Failed to load JailBreakV-28K dataset: {str(e)}") | ||
| raise Exception(f"Error loading JailBreakV-28K dataset: {str(e)}") | ||
| if len(seed_prompts) < min_prompts: | ||
| raise ValueError( | ||
| f"JailBreakV-28K fetch produced {missing_images} missing images. " | ||
| f"Only {len(seed_prompts)} multimodal prompts were produced. " | ||
| f"This is below the minimum required prompts of {min_prompts}. " | ||
| f"Please ensure the zip_dir parameter is provided with the full image set or " | ||
| f"check your backup image source." | ||
| ) | ||
| elif missing_images > 0: | ||
| logger.warning(f"Failed to resolve {missing_images} image paths in JailBreakV-28K dataset") | ||
| if not seed_prompts: | ||
| raise ValueError( | ||
| "JailBreakV-28K fetch produced 0 prompts. " | ||
| "Likely caused by all items returned after filtering having invalid image paths." | ||
| ) | ||
AdrGav941 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| seed_prompt_dataset = SeedPromptDataset(prompts=seed_prompts) | ||
| return seed_prompt_dataset | ||
|
|
||
|
|
||
| def _normalize_policy(policy: str) -> str: | ||
| """Create a machine-friendly variant alongside the human-readable policy.""" | ||
| return policy.strip().lower().replace(" ", "_").replace("-", "_") | ||
|
|
||
|
|
||
| def _resolve_image_path( | ||
| rel_path: str, | ||
| repo_id: str, | ||
| data_home: Optional[str], | ||
| backup_root: Optional[pathlib.Path] = None, | ||
| call_cache: Dict[str, str] = {}, | ||
| ) -> str: | ||
| """ | ||
| Resolve a repo-relative image path to a local absolute path using hf_hub_download. | ||
| Uses a cache (module-level by default) to avoid re-downloading the same file. | ||
| Args: | ||
| rel_path: path relative to the dataset repository root (e.g., "images/0001.png"). | ||
| repo_id: HF dataset repo id, e.g., "JailbreakV-28K/JailBreakV-28k". | ||
| data_home: optional cache directory. | ||
| backup_root: optional path to a directory containing the zip file. | ||
| If provided, will be used as a backup source for images not found in the HF dataset. | ||
| cache: optional dict to use instead of the module-level cache. | ||
| Returns: | ||
| Absolute local path if resolved, else None (and caches the miss). | ||
| """ | ||
| if not rel_path: | ||
| return "" | ||
|
|
||
| # check if image has already been cached | ||
| if rel_path in call_cache: | ||
| return call_cache[rel_path] | ||
| path_root = "JailBreakV_28K" | ||
| hf_path = f"{path_root}/{rel_path}" | ||
| backup_path = None if not backup_root else backup_root / hf_path | ||
| try: | ||
| # first check if the path exists using HFApi() | ||
| repo_file_list = HfApi().list_repo_files(repo_id=repo_id, repo_type="dataset") | ||
| if hf_path in repo_file_list: | ||
| # download the image | ||
| abs_path = hf_hub_download( | ||
| repo_id=repo_id, | ||
| repo_type="dataset", | ||
| filename=hf_path, | ||
| cache_dir=data_home, | ||
| ) | ||
| else: | ||
| logger.debug(f"File {hf_path} not found in dataset {repo_id}, trying backup path {backup_path}") | ||
| if backup_path and backup_path.exists(): | ||
| abs_path = str(backup_path) | ||
| else: | ||
| logger.debug(f"File {hf_path} not found in dataset {repo_id} or backup path {backup_path}") | ||
| abs_path = "" | ||
|
|
||
| call_cache[rel_path] = abs_path | ||
| return abs_path | ||
| except Exception as e: | ||
| logger.error(f"Failed to download image {rel_path}: {str(e)}") | ||
| call_cache[rel_path] = "" | ||
| return "" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.