{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pkl_filepath = \"/home/tony/Data/Preference/up_v1/interesting_clips_up_u_1_20241201_full.pkl\"\n",
    "df = pd.read_pickle(pkl_filepath)\n",
    "print(len(df))\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# let's download all the mp3 files with positive preference\n",
    "# to get all the rows with positive preference \n",
    "positive_df = df[df[\"preference\"] == True]\n",
    "print(len(positive_df))\n",
    "\n",
    "# iterate and get list of s3 paths, then we will download with multiprocessing\n",
    "s3_ids = positive_df[\"id_x\"].tolist()\n",
    "print(len(s3_ids))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# first download audios in parallel to local from s3\n",
    "base_dir = \"/app/suno/christian/data\"\n",
    "out_dir = \"interesting_clips_up_u_1_20241201_positive\"\n",
    "os.makedirs(os.path.join(base_dir, out_dir), exist_ok=True)\n",
    "\n",
    "def download_npz(song_id):\n",
    "    npz_filepath = f\"s3://suno-data-uploads/studio/uploads/{song_id}.npz\"\n",
    "    out_filepath = os.path.join(base_dir, out_dir, f\"{song_id}.npz\")\n",
    "    # surpress output\n",
    "    if not os.path.exists(out_filepath):\n",
    "        os.system(f\"aws s3 cp {npz_filepath} {out_filepath} > /dev/null 2>&1\")\n",
    "\n",
    "# use joblib for parallel downloads with progress bar\n",
    "from joblib import Parallel, delayed\n",
    "from tqdm import tqdm\n",
    "\n",
    "results = Parallel(n_jobs=96, backend=\"loky\")(\n",
    "    delayed(download_npz)(song_id) for song_id in tqdm(s3_ids, desc=\"Downloading npz files\")\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# now we will need to collect npz files and we also need to create the metas with the prompt (text and tags) this might be tricky since i need to follow the packing used in gpt memmaps"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "suno_gpt45",
   "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.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
