{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0",
   "metadata": {},
   "outputs": [],
   "source": [
    "from suno_utils.utils.text import read_jsonl, write_json, read_json\n",
    "import random\n",
    "from suno_utils.audio.conversion import Audio\n",
    "import statistics\n",
    "from profanity_check import predict\n",
    "from langdetect import detect\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [],
   "source": [
    "id_to_views = read_json(\"id_to_views.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {},
   "outputs": [],
   "source": [
    "genuis = read_jsonl(\n",
    "    \"/app/suno/data/chirp_v4_genius_hq_filtered/metadata/genius_hq_v6.jsonl\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [],
   "source": [
    "candidates = []\n",
    "views = []\n",
    "for g in genuis:\n",
    "    # print(g.keys())\n",
    "    if g[\"lang\"] == \"en\" and detect(g[\"text\"]) == \"en\":\n",
    "        views.append(g[\"views\"])\n",
    "        if (\n",
    "            int(g[\"views\"]) > 300000\n",
    "            and int(g[\"views\"]) < 600000\n",
    "            and len(g[\"tags\"]) > 3\n",
    "            and len(g[\"text\"]) > 200\n",
    "        ):\n",
    "            if (\n",
    "                \"Disney\" not in g[\"tags\"]\n",
    "                and \"Cover\" not in g[\"tags\"]\n",
    "                and \"En Español\" not in g[\"tags\"]\n",
    "                and \"Remix\" not in g[\"tags\"]\n",
    "                and \"Worship\" not in g[\"tags\"]\n",
    "                and \"Soundtrack\" not in g[\"tags\"]\n",
    "            ):\n",
    "                profanity = predict([g[\"text\"]])\n",
    "                if profanity[0] < 0.9:\n",
    "                    # print(g['tags'], g['s3_filepath'], g['views'])\n",
    "                    candidates.append(\n",
    "                        {\n",
    "                            \"s3_id\": g[\"s3_filepath\"],\n",
    "                            \"tags\": \", \".join(g[\"tags\"]),\n",
    "                            \"lyrics\": g[\"text\"],\n",
    "                        }\n",
    "                    )\n",
    "print(len(candidates), statistics.median(views))\n",
    "random.shuffle(candidates)\n",
    "final_candidates = candidates[:300]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {},
   "outputs": [],
   "source": [
    "output_sources = {\"all_genres\": final_candidates}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5",
   "metadata": {},
   "outputs": [],
   "source": [
    "write_json(output_sources, \"./suno_utils/task_eval/genius_sources_mediocre.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {},
   "outputs": [],
   "source": [
    "adjust = read_json(\"./suno_utils/task_eval/genius_sources_mediocre.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7",
   "metadata": {},
   "outputs": [],
   "source": [
    "for a in adjust[\"all_genres\"]:\n",
    "    og_path = a[\"s3_id\"]\n",
    "    id_only = os.path.splitext(os.path.basename(og_path))[0]\n",
    "    a[\"s3_id\"] = f\"s3://suno-data/sara/labelbox_ab_train/{id_only}.mp3\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {},
   "outputs": [],
   "source": [
    "write_json(adjust, \"./suno_utils/task_eval/genius_sources_mediocre.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {},
   "outputs": [],
   "source": [
    "for c in final_candidates:\n",
    "    print(c[\"tags\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "10",
   "metadata": {},
   "outputs": [],
   "source": [
    "for c in candidates[:4]:\n",
    "    tags, s3_fp, lyrics = c\n",
    "    a = Audio.from_s3(s3_fp, sample_rate=44_100, n_channels=2).normalize_volume()\n",
    "    print(tags)\n",
    "    print(lyrics[:100])\n",
    "    a.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "deezer_data = read_jsonl(\"/home/minz/llm_tagging/data/deezer_llm_metas.jsonl\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12",
   "metadata": {},
   "outputs": [],
   "source": [
    "deezer_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "suno_clean",
   "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
