Create download.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546import requestsimport osimport zipfilefrom tqdm import tqdmdef download_file(url, save_path):"""Downloads a file only if it does not already exist."""if not os.path.exists(save_path):print(f"Downloading {os.path.basename(save_path)}...")response = requests.get(url, stream=True)total_size = int(response.headers.get('content-length', 0))with open(save_path, "wb") as f, tqdm(desc=os.path.basename(save_path), total=total_size, unit='B', unit_scale=True) as pbar:for chunk in response.iter_content(chunk_size=1024):if chunk:f.write(chunk)pbar.update(len(chunk))return 1else:print(f"{os.path.basename(save_path)} already exists. Skipping download.")return 0def extract_zip(zip_path, extract_to):"""Extracts a ZIP file only if the extracted directory does not already exist."""extracted_dir = os.path.join(extract_to, os.path.splitext(os.path.basename(zip_path))[0])if not os.path.exists(extracted_dir):print(f"Extracting {os.path.basename(zip_path)}...")with zipfile.ZipFile(zip_path, "r") as zip_ref:zip_ref.extractall(extract_to)else:print(f"{os.path.basename(zip_path)} already extracted. Skipping extraction.")def download_flickr8k_dataset():"""Downloads and extracts Flickr8k dataset if not already present."""try:image_url = "https://github.com/jbrownlee/Datasets/releases/download/Flickr8k/Flickr8k_Dataset.zip"os.makedirs("flickr8k", exist_ok=True)image_zip_path = "flickr8k/Flickr8k_Dataset.zip"res = download_file(image_url, image_zip_path)if res:extract_zip(image_zip_path, "flickr8k")return Trueexcept Exception as e:print(f"β Failed to download dataset: {e}")return False1234567891011121314151617181920212223242526import osimport zipfileimport gdowndef download_flickr8k_dataset(dest_folder="flickr8k_dataset"):"""Downloads and extracts the Flickr8k dataset ZIP from Google Drive.If already downloaded, skips re-download."""dataset_url = "https://drive.google.com/uc?id=1pzu2sV_qD8eS9GlZ6H02vjY7tRB25qV4"zip_path = os.path.join(dest_folder, "flickr8k.zip")os.makedirs(dest_folder, exist_ok=True)if not os.path.exists(os.path.join(dest_folder, "Flicker8k_Dataset")):print("π₯ Downloading Flickr8k dataset...")gdown.download(dataset_url, zip_path, quiet=False, fuzzy=True)print("π¦ Extracting...")with zipfile.ZipFile(zip_path, 'r') as zip_ref:zip_ref.extractall(dest_folder)os.remove(zip_path)print("β Dataset ready at:", os.path.join(dest_folder, "Flicker8k_Dataset"))else:print("π Flickr8k dataset already exists. Skipping download.")
download.py+26-46