{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fetch the npz from s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:36:56.065152Z",
     "start_time": "2024-05-26T00:36:54.886147Z"
    },
    "execution": {
     "iopub.execute_input": "2025-11-20T16:55:49.332148Z",
     "iopub.status.busy": "2025-11-20T16:55:49.331970Z",
     "iopub.status.idle": "2025-11-20T16:55:51.344460Z",
     "shell.execute_reply": "2025-11-20T16:55:51.343917Z",
     "shell.execute_reply.started": "2025-11-20T16:55:49.332132Z"
    }
   },
   "outputs": [],
   "source": [
    "import ast\n",
    "import os\n",
    "import sys\n",
    "from collections import defaultdict\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from suno_utils.utils.s3 import download_s3_files\n",
    "from suno_utils.utils.text import read_json, read_jsonl, write_json, write_jsonl\n",
    "from tqdm import tqdm\n",
    "\n",
    "sys.path.insert(0, \"/home/tony/Work/neon/sunoGPT/scripts/\")\n",
    "\n",
    "import numpy as np\n",
    "from data_preparation_7b import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:36:56.068073Z",
     "start_time": "2024-05-26T00:36:56.066469Z"
    },
    "execution": {
     "iopub.execute_input": "2025-11-20T16:55:51.345219Z",
     "iopub.status.busy": "2025-11-20T16:55:51.345027Z",
     "iopub.status.idle": "2025-11-20T16:55:51.347384Z",
     "shell.execute_reply": "2025-11-20T16:55:51.346982Z",
     "shell.execute_reply.started": "2025-11-20T16:55:51.345203Z"
    }
   },
   "outputs": [],
   "source": [
    "NPZ_DIR = \"/app2/suno/data/dpo/diff2_v2_d5\"\n",
    "JSON_DIR = \"/app2/suno/data/dpo/up_v2_d5_json/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-20T16:55:51.347977Z",
     "iopub.status.busy": "2025-11-20T16:55:51.347847Z",
     "iopub.status.idle": "2025-11-20T16:55:51.350022Z",
     "shell.execute_reply": "2025-11-20T16:55:51.349622Z",
     "shell.execute_reply.started": "2025-11-20T16:55:51.347964Z"
    }
   },
   "outputs": [],
   "source": [
    "data_csv_path = \"/home/tony/Data/Preference/up_v2_d5/interesting_clips_ahi_d5_20251120.pkl\"\n",
    "# data_csv_path = \"/home/tony/Data/Preference/up_v2_d4/fully_merged_up_v2_d4.pkl\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:36:59.932066Z",
     "start_time": "2024-05-26T00:36:56.069087Z"
    },
    "execution": {
     "iopub.execute_input": "2025-11-20T16:55:51.350598Z",
     "iopub.status.busy": "2025-11-20T16:55:51.350468Z",
     "iopub.status.idle": "2025-11-20T16:55:55.345997Z",
     "shell.execute_reply": "2025-11-20T16:55:55.345520Z",
     "shell.execute_reply.started": "2025-11-20T16:55:51.350585Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(228470, 94)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# df = pd.read_csv(data_csv_path)\n",
    "df = pd.read_pickle(data_csv_path)\n",
    "df.shape\n",
    "# v2: (2341508, 31)\n",
    "# v3: (2609830, 31)\n",
    "# v4: (1926120, 35)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-20T16:55:55.347590Z",
     "iopub.status.busy": "2025-11-20T16:55:55.347433Z",
     "iopub.status.idle": "2025-11-20T16:55:55.706275Z",
     "shell.execute_reply": "2025-11-20T16:55:55.705727Z",
     "shell.execute_reply.started": "2025-11-20T16:55:55.347575Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(228470, 94)\n",
      "model_name\n",
      "chirp-carp-up-1        219834\n",
      "chirp-bass-up-3          8492\n",
      "chirp-v4-up-u-d-2-5       144\n",
      "Name: count, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "df = df[df[\"model_name\"].str.contains(\"up\")]\n",
    "print(df.shape)\n",
    "# for extend, also fetch their parents\n",
    "print(df[\"model_name\"].value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-20T16:55:55.706960Z",
     "iopub.status.busy": "2025-11-20T16:55:55.706800Z",
     "iopub.status.idle": "2025-11-20T16:55:55.709148Z",
     "shell.execute_reply": "2025-11-20T16:55:55.708741Z",
     "shell.execute_reply.started": "2025-11-20T16:55:55.706945Z"
    }
   },
   "outputs": [],
   "source": [
    "# if \"extend\" in data_csv_path:\n",
    "# test_slice = df[\"metadata\"].apply(lambda x: ast.literal_eval(x))\n",
    "# test_slice_series = test_slice.apply(pd.Series)\n",
    "# df = pd.concat([df, test_slice_series], axis=1, join=\"inner\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-20T16:55:55.709731Z",
     "iopub.status.busy": "2025-11-20T16:55:55.709598Z",
     "iopub.status.idle": "2025-11-20T16:55:55.931794Z",
     "shell.execute_reply": "2025-11-20T16:55:55.931260Z",
     "shell.execute_reply.started": "2025-11-20T16:55:55.709718Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "all 228470\n",
      "unique 110382\n"
     ]
    }
   ],
   "source": [
    "edit_clip_ids = df[\"metadata\"].apply(lambda x: x.get(\"upsample_clip_id\", \"\"))\n",
    "s3_ids = [s3_id for s3_id in edit_clip_ids if s3_id and len(s3_id) > 0]\n",
    "print(\"all\", len(s3_ids))\n",
    "unique_s3_ids = sorted(set(s3_ids))\n",
    "print(\"unique\", len(unique_s3_ids))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:36:59.940064Z",
     "start_time": "2024-05-26T00:36:59.933947Z"
    },
    "execution": {
     "iopub.execute_input": "2025-11-20T16:55:55.932647Z",
     "iopub.status.busy": "2025-11-20T16:55:55.932503Z",
     "iopub.status.idle": "2025-11-20T16:55:55.971483Z",
     "shell.execute_reply": "2025-11-20T16:55:55.970991Z",
     "shell.execute_reply.started": "2025-11-20T16:55:55.932632Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "110382\n",
      "full 110382\n"
     ]
    }
   ],
   "source": [
    "# download all clips; 883k\n",
    "from suno_utils.utils.s3 import download_s3_files\n",
    "\n",
    "s3_ids = unique_s3_ids.copy()\n",
    "print(len(s3_ids))\n",
    "# s3_ids = set(s3_ids).union(set(df[\"audio_prompt_id\"].values))\n",
    "print(\"full\", len(s3_ids))\n",
    "s3_paths = [f\"s3://suno-data-uploads/studio/uploads/{s3_id}.npz\" for s3_id in s3_ids]\n",
    "local_paths = [f\"{NPZ_DIR}/{s3_id}.npz\" for s3_id in s3_ids]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:37:00.153602Z",
     "start_time": "2024-05-26T00:36:59.941125Z"
    },
    "execution": {
     "iopub.execute_input": "2025-11-20T16:55:55.972123Z",
     "iopub.status.busy": "2025-11-20T16:55:55.971981Z",
     "iopub.status.idle": "2025-11-20T16:56:17.702328Z",
     "shell.execute_reply": "2025-11-20T16:56:17.701760Z",
     "shell.execute_reply.started": "2025-11-20T16:55:55.972109Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 110220 110220\n"
     ]
    }
   ],
   "source": [
    "finished_paths = os.listdir(NPZ_DIR)\n",
    "finished_paths_set = set(finished_paths)\n",
    "unfinished_s3_paths = [\n",
    "    path for path in s3_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_paths = [\n",
    "    path for path in local_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_ids_set = set([os.path.basename(path).replace(\".npz\", \"\") for path in unfinished_paths])\n",
    "unfinished_ids = [i for i in s3_ids if i in unfinished_ids_set]\n",
    "print(\"jobs to be done\", len(unfinished_paths), len(unfinished_s3_paths))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:49:33.275556Z",
     "start_time": "2024-05-26T00:37:00.154648Z"
    },
    "execution": {
     "iopub.execute_input": "2025-11-20T16:56:17.703027Z",
     "iopub.status.busy": "2025-11-20T16:56:17.702881Z",
     "iopub.status.idle": "2025-11-20T18:30:42.698406Z",
     "shell.execute_reply": "2025-11-20T18:30:42.697715Z",
     "shell.execute_reply.started": "2025-11-20T16:56:17.703012Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████| 111/111 [1:34:24<00:00, 51.03s/it]\n"
     ]
    }
   ],
   "source": [
    "_ = download_s3_files(unfinished_s3_paths, unfinished_paths, n_cores=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:59:42.420025Z",
     "start_time": "2024-05-26T00:49:33.276845Z"
    },
    "execution": {
     "iopub.execute_input": "2025-11-20T18:30:42.699372Z",
     "iopub.status.busy": "2025-11-20T18:30:42.699121Z",
     "iopub.status.idle": "2025-11-20T18:32:04.079158Z",
     "shell.execute_reply": "2025-11-20T18:32:04.078628Z",
     "shell.execute_reply.started": "2025-11-20T18:30:42.699356Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 1106 1106\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:57<00:00, 28.70s/it]\n"
     ]
    }
   ],
   "source": [
    "# # deleted files are at: deleted; try to get them as well\n",
    "finished_paths = os.listdir(NPZ_DIR)\n",
    "finished_paths_set = set(finished_paths)\n",
    "unfinished_s3_paths = [\n",
    "    path for path in s3_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_paths = [\n",
    "    path for path in local_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "print(\"jobs to be done\", len(unfinished_paths), len(unfinished_s3_paths))\n",
    "unfinished_deleted_s3_paths = [\n",
    "    path.replace(\"/uploads/\", \"/deleted/\") for path in unfinished_s3_paths\n",
    "]\n",
    "_ = download_s3_files(unfinished_deleted_s3_paths, unfinished_paths, n_cores=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-05-26T00:59:42.423220Z",
     "start_time": "2024-05-26T00:59:42.421231Z"
    },
    "execution": {
     "iopub.execute_input": "2025-11-20T18:32:04.079961Z",
     "iopub.status.busy": "2025-11-20T18:32:04.079789Z",
     "iopub.status.idle": "2025-11-20T18:32:04.082542Z",
     "shell.execute_reply": "2025-11-20T18:32:04.082139Z",
     "shell.execute_reply.started": "2025-11-20T18:32:04.079945Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Finish downloads\n"
     ]
    }
   ],
   "source": [
    "print(\"Finish downloads\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# VAE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-20T18:32:04.083161Z",
     "iopub.status.busy": "2025-11-20T18:32:04.083024Z",
     "iopub.status.idle": "2025-11-20T18:32:04.191518Z",
     "shell.execute_reply": "2025-11-20T18:32:04.191036Z",
     "shell.execute_reply.started": "2025-11-20T18:32:04.083148Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "228470\n",
      "full 228470\n"
     ]
    }
   ],
   "source": [
    "# download all clips; 883k\n",
    "from suno_utils.utils.s3 import download_s3_files\n",
    "\n",
    "# for vae...we actually need the original s3 id\n",
    "s3_ids = df[\"s3_id\"].unique()\n",
    "print(len(s3_ids))\n",
    "# s3_ids = set(s3_ids).union(set(df[\"audio_prompt_id\"].values))\n",
    "print(\"full\", len(s3_ids))\n",
    "s3_paths = [f\"s3://suno-data-uploads/studio/uploads/{s3_id}_vae.npz\" for s3_id in s3_ids]\n",
    "local_paths = [f\"{NPZ_DIR}/{s3_id}_vae.npz\" for s3_id in s3_ids]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-20T18:32:04.192251Z",
     "iopub.status.busy": "2025-11-20T18:32:04.192105Z",
     "iopub.status.idle": "2025-11-20T18:32:25.066958Z",
     "shell.execute_reply": "2025-11-20T18:32:25.066428Z",
     "shell.execute_reply.started": "2025-11-20T18:32:04.192237Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 228470 228470\n"
     ]
    }
   ],
   "source": [
    "finished_paths = os.listdir(NPZ_DIR)\n",
    "finished_paths_set = set(finished_paths)\n",
    "unfinished_s3_paths = [\n",
    "    path for path in s3_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_paths = [\n",
    "    path for path in local_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_ids_set = set([os.path.basename(path).replace(\".npz\", \"\") for path in unfinished_paths])\n",
    "unfinished_ids = [i for i in s3_ids if i in unfinished_ids_set]\n",
    "print(\"jobs to be done\", len(unfinished_paths), len(unfinished_s3_paths))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-20T18:32:25.067657Z",
     "iopub.status.busy": "2025-11-20T18:32:25.067515Z",
     "iopub.status.idle": "2025-11-20T22:13:00.651867Z",
     "shell.execute_reply": "2025-11-20T22:13:00.651095Z",
     "shell.execute_reply.started": "2025-11-20T18:32:25.067643Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████| 229/229 [3:40:34<00:00, 57.79s/it]\n"
     ]
    }
   ],
   "source": [
    "_ = download_s3_files(unfinished_s3_paths, unfinished_paths, n_cores=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-20T22:13:00.652691Z",
     "iopub.status.busy": "2025-11-20T22:13:00.652541Z",
     "iopub.status.idle": "2025-11-20T22:13:30.737412Z",
     "shell.execute_reply": "2025-11-20T22:13:30.736819Z",
     "shell.execute_reply.started": "2025-11-20T22:13:00.652676Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 0 0\n"
     ]
    }
   ],
   "source": [
    "# # deleted files are at: deleted; try to get them as well\n",
    "finished_paths = os.listdir(NPZ_DIR)\n",
    "finished_paths_set = set(finished_paths)\n",
    "unfinished_s3_paths = [\n",
    "    path for path in s3_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_paths = [\n",
    "    path for path in local_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "print(\"jobs to be done\", len(unfinished_paths), len(unfinished_s3_paths))\n",
    "unfinished_deleted_s3_paths = [\n",
    "    path.replace(\"/uploads/\", \"/deleted/\") for path in unfinished_s3_paths\n",
    "]\n",
    "_ = download_s3_files(unfinished_deleted_s3_paths, unfinished_paths, n_cores=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-20T22:13:30.738214Z",
     "iopub.status.busy": "2025-11-20T22:13:30.738055Z",
     "iopub.status.idle": "2025-11-20T22:13:30.741150Z",
     "shell.execute_reply": "2025-11-20T22:13:30.740691Z",
     "shell.execute_reply.started": "2025-11-20T22:13:30.738198Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "DONE with vaes\n"
     ]
    }
   ],
   "source": [
    "print(\"DONE with vaes\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-20T22:13:30.741784Z",
     "iopub.status.busy": "2025-11-20T22:13:30.741648Z",
     "iopub.status.idle": "2025-11-20T22:13:30.743757Z",
     "shell.execute_reply": "2025-11-20T22:13:30.743368Z",
     "shell.execute_reply.started": "2025-11-20T22:13:30.741771Z"
    }
   },
   "outputs": [],
   "source": [
    "# BREAK"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Hoot"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-20T22:13:30.744339Z",
     "iopub.status.busy": "2025-11-20T22:13:30.744210Z",
     "iopub.status.idle": "2025-11-20T22:13:30.849994Z",
     "shell.execute_reply": "2025-11-20T22:13:30.849262Z",
     "shell.execute_reply.started": "2025-11-20T22:13:30.744327Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "228470\n"
     ]
    }
   ],
   "source": [
    "s3_ids = df[\"s3_id\"].unique()\n",
    "print(len(s3_ids))\n",
    "s3_paths = [f\"s3://suno-data-uploads/studio/uploads/{s3_id}_hoot.json\" for s3_id in s3_ids]\n",
    "local_paths = [f\"{JSON_DIR}/{s3_id}_hoot.json\" for s3_id in s3_ids]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-20T22:13:30.852406Z",
     "iopub.status.busy": "2025-11-20T22:13:30.852215Z",
     "iopub.status.idle": "2025-11-20T22:13:47.474313Z",
     "shell.execute_reply": "2025-11-20T22:13:47.473642Z",
     "shell.execute_reply.started": "2025-11-20T22:13:30.852388Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 228470 228470\n"
     ]
    }
   ],
   "source": [
    "finished_paths = os.listdir(JSON_DIR)\n",
    "finished_paths_set = set(finished_paths)\n",
    "unfinished_s3_paths = [\n",
    "    path for path in s3_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_paths = [\n",
    "    path for path in local_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "print(\"jobs to be done\", len(unfinished_paths), len(unfinished_s3_paths))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-20T22:13:47.478118Z",
     "iopub.status.busy": "2025-11-20T22:13:47.477988Z",
     "iopub.status.idle": "2025-11-21T01:47:44.702875Z",
     "shell.execute_reply": "2025-11-21T01:47:44.702279Z",
     "shell.execute_reply.started": "2025-11-20T22:13:47.478102Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████| 229/229 [3:33:55<00:00, 56.05s/it]\n"
     ]
    }
   ],
   "source": [
    "_ = download_s3_files(unfinished_s3_paths, unfinished_paths, n_cores=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-21T01:47:44.703668Z",
     "iopub.status.busy": "2025-11-21T01:47:44.703503Z",
     "iopub.status.idle": "2025-11-21T01:48:14.190776Z",
     "shell.execute_reply": "2025-11-21T01:48:14.190217Z",
     "shell.execute_reply.started": "2025-11-21T01:47:44.703651Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jobs to be done 183 183\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1/1 [00:09<00:00,  9.93s/it]\n"
     ]
    }
   ],
   "source": [
    "# # deleted files are at: deleted; try to get them as well\n",
    "finished_paths = os.listdir(JSON_DIR)\n",
    "finished_paths_set = set(finished_paths)\n",
    "unfinished_s3_paths = [\n",
    "    path for path in s3_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "unfinished_paths = [\n",
    "    path for path in local_paths if os.path.basename(path) not in finished_paths_set\n",
    "]\n",
    "print(\"jobs to be done\", len(unfinished_paths), len(unfinished_s3_paths))\n",
    "unfinished_deleted_s3_paths = [\n",
    "    path.replace(\"/uploads/\", \"/deleted/\") for path in unfinished_s3_paths\n",
    "]\n",
    "_ = download_s3_files(unfinished_deleted_s3_paths, unfinished_paths, n_cores=32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-11-21T01:48:14.191567Z",
     "iopub.status.busy": "2025-11-21T01:48:14.191403Z",
     "iopub.status.idle": "2025-11-21T01:48:14.194475Z",
     "shell.execute_reply": "2025-11-21T01:48:14.194058Z",
     "shell.execute_reply.started": "2025-11-21T01:48:14.191551Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Finish downloads hoot!\n"
     ]
    }
   ],
   "source": [
    "print(\"Finish downloads hoot!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.15"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
