Health-AI-Readiness / download.py
Kiwi321's picture
Rename download_and_decompress.py to download.py
616934a verified
Raw History Blame Contribute Delete
3.87 kB
#!/usr/bin/env python3
"""
Download and decompress data from Hugging Face dataset repository
Usage: python download_and_decompress.py --output_dir <path>
"""
import argparse
import os
import zipfile
from huggingface_hub import hf_hub_download
from pathlib import Path
from tqdm import tqdm
def download_and_extract(repo_id, filename, output_dir, repo_type="dataset"):
"""
Download a file from Hugging Face and extract it if it's a zip file.
Args:
repo_id: Repository ID (e.g., "Kiwi321/Health-AI-Readiness")
filename: Name of the file to download
output_dir: Directory where to extract the contents
repo_type: Type of repository (default: "dataset")
"""
print(f"\n{'='*60}")
print(f"πŸ“₯ Downloading: {filename}")
try:
# Download file from Hugging Face
downloaded_path = hf_hub_download(
repo_id=repo_id,
filename=filename,
repo_type=repo_type,
cache_dir=None # Use default cache
)
print(f"βœ… Downloaded to: {downloaded_path}")
# Extract if it's a zip file
if filename.endswith('.zip'):
extract_to = output_dir
print(f"πŸ“¦ Extracting to: {extract_to}")
with zipfile.ZipFile(downloaded_path, 'r') as zip_ref:
# Get list of files for progress bar
file_list = zip_ref.namelist()
# Create output directory if it doesn't exist
os.makedirs(extract_to, exist_ok=True)
# Extract with progress bar
for file in tqdm(file_list, desc="Extracting", unit="file"):
zip_ref.extract(file, extract_to)
print(f"βœ… Extracted successfully!")
# Optionally remove the downloaded zip file to save space
# os.remove(downloaded_path)
return True
except Exception as e:
print(f"❌ Error processing {filename}: {str(e)}")
return False
def main():
parser = argparse.ArgumentParser(
description="Download and decompress data from Hugging Face Health-AI-Readiness dataset"
)
parser.add_argument(
"--output_dir",
type=str,
default="./data",
help="Directory where to extract the data (default: ./data)"
)
parser.add_argument(
"--repo_id",
type=str,
default="Kiwi321/Health-AI-Readiness",
help="Hugging Face repository ID (default: Kiwi321/Health-AI-Readiness)"
)
parser.add_argument(
"--files",
nargs="+",
default=[
"JAMA.zip",
"NEJM_image_challenge.zip",
"OmniMedVQA.zip",
"PMC-VQA.zip",
"VQA_RAD.zip",
"test_unseen.zip"
],
help="List of files to download (default: all dataset files)"
)
args = parser.parse_args()
# Create output directory
output_dir = Path(args.output_dir)
output_dir.mkdir(parents=True, exist_ok=True)
print("πŸš€ Starting download and extraction process...")
print(f"πŸ“‚ Output directory: {output_dir.absolute()}")
print(f"πŸ“¦ Repository: {args.repo_id}")
print(f"πŸ“‹ Files to download: {len(args.files)}")
# Download and extract each file
success_count = 0
for filename in args.files:
if download_and_extract(args.repo_id, filename, str(output_dir)):
success_count += 1
# Summary
print(f"\n{'='*60}")
print(f"✨ Process completed!")
print(f"βœ… Successfully processed: {success_count}/{len(args.files)} files")
print(f"πŸ“‚ Data location: {output_dir.absolute()}")
print(f"{'='*60}\n")
if __name__ == "__main__":
main()