"""Download multiple datasets from Hugging Face and COCO.""" import argparse import os import sys from huggingface_hub import snapshot_download COCO_VAL_URL = "http://images.cocodataset.org/zips/val2014.zip" def download_huggingface_dataset(repo_id: str, output_dir: str, use_auth_token: bool) -> str: os.makedirs(output_dir, exist_ok=True) print(f"Downloading Hugging Face dataset {repo_id} to '{output_dir}'...") local_dir = snapshot_download( repo_id=repo_id, repo_type="dataset", local_dir=output_dir, use_auth_token=use_auth_token, ) print(f"Finished: {repo_id} -> {local_dir}\n") return local_dir def download_file(url: str, output_path: str) -> None: try: import requests except ImportError: import urllib.request print("requests not found, falling back to urllib.request") urllib.request.urlretrieve(url, output_path) return print(f"Downloading file from {url} to '{output_path}'...") with requests.get(url, stream=True) as response: response.raise_for_status() total_size = int(response.headers.get("content-length", 0)) downloaded = 0 chunk_size = 8192 with open(output_path, "wb") as f: for chunk in response.iter_content(chunk_size=chunk_size): if chunk: f.write(chunk) downloaded += len(chunk) if total_size: percent = downloaded * 100 / total_size print(f"\rDownloaded {downloaded}/{total_size} bytes ({percent:.1f}%)", end="") if total_size: print() print(f"Finished: {output_path}\n") def main(): parser = argparse.ArgumentParser(description="Download VGMED, Medical Multimodal Evaluation Data, and COCO 2014 val images.") parser.add_argument( "--output-dir", default="/home/jinhua/ExplainableVQA/benchmarks", help="Base output directory for all downloads. Default: downloads", ) parser.add_argument( "--use-auth-token", action="store_true", help="Use HF authentication token from HUGGINGFACE_HUB_TOKEN environment variable if required.", ) parser.add_argument( "--skip-vgmed", action="store_true", help="Skip downloading guimeng-liu/VGMED.", ) parser.add_argument( "--skip-med-eval", action="store_true", help="Skip downloading FreedomIntelligence/Medical_Multimodal_Evaluation_Data.", ) parser.add_argument( "--skip-coco", action="store_true", help="Skip downloading COCO 2014 validation images.", ) parser.add_argument( "--coco-url", default=COCO_VAL_URL, help="COCO 2014 val images download URL. Default is the official val2014 zip URL.", ) args = parser.parse_args() base_dir = os.path.abspath(args.output_dir) os.makedirs(base_dir, exist_ok=True) if not args.skip_vgmed: download_huggingface_dataset( repo_id="guimeng-liu/VGMED", output_dir=os.path.join(base_dir, "VGMED"), use_auth_token=args.use_auth_token, ) if not args.skip_med_eval: download_huggingface_dataset( repo_id="FreedomIntelligence/Medical_Multimodal_Evaluation_Data", output_dir=os.path.join(base_dir, "Medical_Multimodal_Evaluation_Data"), use_auth_token=args.use_auth_token, ) if not args.skip_coco: coco_target = os.path.join(base_dir, "val2014.zip") download_file(args.coco_url, coco_target) print("All requested downloads completed.") if __name__ == "__main__": main()