{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d28807ed",
   "metadata": {},
   "source": [
    "## Get rev annotation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "bba2c769",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "import os\n",
    "import uuid\n",
    "\n",
    "import numpy as np\n",
    "\n",
    "from suno_utils.audio import Audio\n",
    "from suno_utils.utils.numbers import safe_round\n",
    "from suno_utils.web.harvest import get_filename\n",
    "\n",
    "BASE_DIR = \"/home/georg/notebooks/tasks/s2t\"\n",
    "AUDIO_DIR = os.path.join(BASE_DIR, \"sample_data\", \"audio_16k\")\n",
    "REV_DIR = os.path.join(BASE_DIR, \"sample_data\", \"rev_data\")\n",
    "\n",
    "AUDIO_FILEPATHS = [\n",
    "    os.path.join(AUDIO_DIR, \"russia.wav\"),\n",
    "    os.path.join(AUDIO_DIR, \"georgia.wav\"),\n",
    "    os.path.join(AUDIO_DIR, \"spgi_2spk.wav\"),\n",
    "    os.path.join(AUDIO_DIR, \"spgi_hard_1.wav\"),\n",
    "    os.path.join(AUDIO_DIR, \"spgi_hard_2.wav\"),\n",
    "    os.path.join(AUDIO_DIR, \"spgi_standup.wav\"),\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "50ac40c1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def stitch_audios(audios, indicator_audio_dir, min_duration_s=60*60, sample_rate=16_000, byte_width=2):\n",
    "    if len(audios) == 0:\n",
    "        return [], []\n",
    "    # assemble stitched audios\n",
    "    audio_metas = []\n",
    "    stitched_audios = []\n",
    "    tmp_audio_metas = []\n",
    "    tmp_stitched_audio = Audio.empty(sample_rate, byte_width, 1)\n",
    "    tmp_n = 0\n",
    "    for audio in audios:\n",
    "        audio = audio.convert(sample_rate, byte_width, 1)\n",
    "        offset_s = tmp_stitched_audio.duration_s\n",
    "        indicator_str = str(tmp_n).zfill(2)\n",
    "        for c in indicator_str:\n",
    "            indicator_audio = Audio.from_file(\n",
    "                os.path.join(indicator_audio_dir, f\"{c}_fast.wav\"), \n",
    "                sample_rate=sample_rate, byte_width=byte_width,\n",
    "            )\n",
    "            tmp_stitched_audio = tmp_stitched_audio.append(indicator_audio)\n",
    "        tmp_audio_metas.append({\n",
    "            \"n_segment\": tmp_n,\n",
    "            \"type\": \"indicator\",\n",
    "            \"offset_s\": safe_round(offset_s),\n",
    "            \"indicator_str\": indicator_str,\n",
    "        })\n",
    "        offset_s = tmp_stitched_audio.duration_s\n",
    "        tmp_stitched_audio = tmp_stitched_audio.append(audio)\n",
    "        tmp_audio_metas.append({\n",
    "            \"n_segment\": tmp_n,\n",
    "            \"type\": \"speech\",\n",
    "            \"offset_s\": safe_round(offset_s),\n",
    "            \"orginial_audio_offset_s\": 0,\n",
    "            \"orginial_audio_uid\": audio.metadata[\"id\"],\n",
    "        })\n",
    "        tmp_n += 1\n",
    "        if tmp_stitched_audio.duration_s >= min_duration_s:\n",
    "            audio_metas.append(tmp_audio_metas)\n",
    "            stitched_audios.append(tmp_stitched_audio)\n",
    "            tmp_audio_metas = []\n",
    "            tmp_stitched_audio = Audio.empty(sample_rate, byte_width, 1)\n",
    "            tmp_n = 0\n",
    "    if tmp_stitched_audio.duration_s > 0:\n",
    "        audio_metas.append(tmp_audio_metas)\n",
    "        stitched_audios.append(tmp_stitched_audio)\n",
    "    return audio_metas, stitched_audios\n",
    "\n",
    "INDICATOR_AUDIO_DIR = \"/mnt/data-ssd-1/data/custom/segment_indicators\"\n",
    "audios = [Audio.from_file(fp, sample_rate=16_000, metadata={\"id\": get_filename(fp)}) for fp in AUDIO_FILEPATHS]\n",
    "audio_metas, stitched_audios = stitch_audios(audios, INDICATOR_AUDIO_DIR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "bf5c88cb",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(os.path.join(REV_DIR, \"stitched_audio_meta.json\"), \"w\") as f:\n",
    "    json.dump(audio_metas[0], f)\n",
    "stitched_audios[0].to_mp3(os.path.join(REV_DIR, \"stitched_audio.mp3\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88a9e9cc",
   "metadata": {},
   "source": [
    "##### Send to rev"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "05fbe672",
   "metadata": {},
   "outputs": [],
   "source": [
    "from suno_utils.web.rev import get_auth_string, upload_file, make_order, make_cc_order\n",
    "\n",
    "with open(\"/home/georg/.secrets/secrets.json\") as f:\n",
    "    secrets = json.load(f)\n",
    "    \n",
    "CLIENT_API_KEY = secrets[\"REV_CLIENT_API_KEY\"]\n",
    "USER_API_KEY = secrets[\"REV_USER_API_KEY\"]\n",
    "AUTH_STR = get_auth_string(CLIENT_API_KEY, USER_API_KEY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "2493d8f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "AUDIO_FILEPATH = os.path.join(REV_DIR, \"stitched_audio.mp3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "6f4ea68c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TC0772282748 submitted.\n"
     ]
    }
   ],
   "source": [
    "media_loc = upload_file(AUTH_STR, AUDIO_FILEPATH, file_ref_str=\"stitched_audio\")\n",
    "order_items = [\n",
    "    {\n",
    "        \"media_loc\": media_loc,\n",
    "        \"hotwords\": [],\n",
    "        \"duration_s\": int(np.ceil(Audio.get_details(AUDIO_FILEPATH)[\"duration_s\"])),\n",
    "        \"uuid\": \"stitched_audio\",\n",
    "    }\n",
    "]\n",
    "order_number = make_order(AUTH_STR, order_items, order_ref_str=\"asr_samples\")\n",
    "print(order_number, \"submitted.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "2c72ac88",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CP0909854329 submitted.\n"
     ]
    }
   ],
   "source": [
    "media_loc = upload_file(AUTH_STR, AUDIO_FILEPATH, file_ref_str=\"stitched_audio\")\n",
    "order_items = [\n",
    "    {\n",
    "        \"media_loc\": media_loc,\n",
    "        \"hotwords\": [],\n",
    "        \"duration_s\": int(np.ceil(Audio.get_details(AUDIO_FILEPATH)[\"duration_s\"])),\n",
    "        \"uuid\": \"stitched_audio\",\n",
    "    }\n",
    "]\n",
    "order_number_2 = make_cc_order(AUTH_STR, order_items, order_ref_str=\"asr_samples\")\n",
    "print(order_number_2, \"submitted.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "eebf9950",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CP1025570327 submitted.\n"
     ]
    }
   ],
   "source": [
    "# media_loc = upload_file(AUTH_STR, AUDIO_FILEPATH, file_ref_str=\"stitched_audio\")\n",
    "# order_items = [\n",
    "#     {\n",
    "#         \"media_loc\": media_loc,\n",
    "#         \"hotwords\": [],\n",
    "#         \"duration_s\": int(np.ceil(Audio.get_details(AUDIO_FILEPATH)[\"duration_s\"])),\n",
    "#         \"uuid\": \"stitched_audio\",\n",
    "#     }\n",
    "# ]\n",
    "# order_number_3 = make_cc_order(AUTH_STR, order_items, order_ref_str=\"asr_samples\", add_premium=True, add_rush=True)\n",
    "order_number_3 = \"CP1025570327\"\n",
    "print(order_number_3, \"submitted.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "ba3f7811",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(os.path.join(REV_DIR, \"order_numer.txt\"), \"w\") as f:\n",
    "    f.write(order_number)\n",
    "with open(os.path.join(REV_DIR, \"order_numer_2.txt\"), \"w\") as f:\n",
    "    f.write(order_number_2)\n",
    "with open(os.path.join(REV_DIR, \"order_numer_3.txt\"), \"w\") as f:\n",
    "    f.write(order_number_3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76501ee9",
   "metadata": {},
   "source": [
    "##### Get from rev           "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "ecafa34f",
   "metadata": {},
   "outputs": [],
   "source": [
    "from suno_utils.web.rev import get_auth_string, get_finished_order, get_transcript\n",
    "\n",
    "with open(\"/home/georg/.secrets/secrets.json\") as f:\n",
    "    secrets = json.load(f)\n",
    "    \n",
    "CLIENT_API_KEY = secrets[\"REV_CLIENT_API_KEY\"]\n",
    "USER_API_KEY = secrets[\"REV_USER_API_KEY\"]\n",
    "AUTH_STR = get_auth_string(CLIENT_API_KEY, USER_API_KEY)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "be440a7f",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(os.path.join(REV_DIR, \"order_numer.txt\")) as f:\n",
    "    order_number = f.read()\n",
    "# with open(os.path.join(REV_DIR, \"order_numer_2.txt\")) as f:\n",
    "#     order_number_2 = f.read()\n",
    "with open(os.path.join(REV_DIR, \"order_numer_3.txt\")) as f:\n",
    "    order_number_3 = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "74af9dba",
   "metadata": {},
   "outputs": [],
   "source": [
    "transcript_metas = get_finished_order(AUTH_STR, order_number)\n",
    "transcript_json = get_transcript(AUTH_STR, transcript_metas[0])\n",
    "\n",
    "# transcript_metas = get_finished_order(AUTH_STR, order_number_2)\n",
    "# transcript_srt = get_transcript(AUTH_STR, transcript_metas[0])\n",
    "\n",
    "transcript_metas = get_finished_order(AUTH_STR, order_number_3)\n",
    "transcript_premium_srt = get_transcript(AUTH_STR, transcript_metas[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "5a47700b",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(os.path.join(REV_DIR, \"stitched_audio_transcript.json\"), \"w\") as f:\n",
    "    json.dump(transcript_json, f)\n",
    "    \n",
    "# with open(os.path.join(REV_DIR, \"stitched_audio_captions.srt\"), \"w\") as f:\n",
    "#     f.write(transcript_srt)\n",
    "    \n",
    "with open(os.path.join(REV_DIR, \"stitched_audio_captions_prem.srt\"), \"w\") as f:\n",
    "    f.write(transcript_premium_srt)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51f41b60",
   "metadata": {},
   "source": [
    "##### make combined human groundtruth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "e802f9b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "AUDIO_DATA = [\n",
    "    {\n",
    "        \"id\": \"a85b9df3-46a4-4461-88ab-c4a52b9fdd96\",\n",
    "        \"filepath\": os.path.join(AUDIO_DIR, \"russia.wav\"),\n",
    "    },\n",
    "    {\n",
    "        \"id\": \"fdeb8ce0-2bca-4b57-ac8b-70c1c8f7202d\",\n",
    "        \"filepath\": os.path.join(AUDIO_DIR, \"georgia.wav\"),\n",
    "    },\n",
    "    {\n",
    "        \"id\": \"a67d8ccc-9bdd-4b1d-be29-88feb1373a4b\",\n",
    "        \"filepath\": os.path.join(AUDIO_DIR, \"spgi_2spk.wav\"),\n",
    "    },\n",
    "    {\n",
    "        \"id\": \"4ac0be6e-d159-4f17-afe3-b1be4e56ef50\",\n",
    "        \"filepath\": os.path.join(AUDIO_DIR, \"spgi_hard_1.wav\"),\n",
    "    },\n",
    "    {\n",
    "        \"id\": \"1bfa8675-ec09-4cf1-a8b6-338c5a6f997d\",\n",
    "        \"filepath\": os.path.join(AUDIO_DIR, \"spgi_hard_2.wav\"),\n",
    "    },\n",
    "    {\n",
    "        \"id\": \"cec77f58-192b-4e56-9fd1-ad5454a23122\",\n",
    "        \"filepath\": os.path.join(AUDIO_DIR, \"spgi_standup.wav\"),\n",
    "    },\n",
    "]\n",
    "\n",
    "TRANSCRIPT_CONTAINER = {\n",
    "    \"a85b9df3-46a4-4461-88ab-c4a52b9fdd96\": {\n",
    "        \"true\": \"President Joe Biden, backed by the full symbolic power of the Western Alliance, is locked in a showdown with Russian President Vladimir Putin, who is using Ukraine as a hostage to try to force the U.S. to renegotiate the settled outcome of the Cold War.\",\n",
    "        \"true_norm\": \"president joe biden backed by the full symbolic power of the western alliance is locked in a showdown with russian president vladimir putin who is using ukraine as a hostage to try to force the u s to renegotiate the settled outcome of the cold war\",\n",
    "    },\n",
    "    \"fdeb8ce0-2bca-4b57-ac8b-70c1c8f7202d\": {\n",
    "        \"true\": \"Pine tree. Uh -- And our neighbor, it was -- it was on a Wednesday. The Saturday before my neighbor had -- our neighbor had said, \\\"And we're getting that tree out, Cath. Because I don't know if you noticed.\\\" [laughter] There it went. There it went. Yeah.\",\n",
    "        \"true_norm\": \"pine tree uh and our neighbor it was it was on a wednesday the saturday before my neighbor had our neighbor had said and we're getting that tree out cath because i don't know if you noticed there it went there it went yeah\",\n",
    "    },\n",
    "    \"a67d8ccc-9bdd-4b1d-be29-88feb1373a4b\": {\n",
    "        \"true\": \"The -- the domestic side of the pork, uh, outlook seems exceedingly strong. H -- how do I look into that? Uh, you, you hit the nail on the head, Ken. Uh, the -- Our -- Uh, the on -- you know we feel very good about pork, there is gonna be, you know --\",\n",
    "        \"true_norm\": \"the the domestic side of the pork uh outlook seems exceedingly strong h how do i look into that uh you you hit the nail on the head ken uh the our uh the on you know we feel very good about pork there is gonna be you know\",\n",
    "    },\n",
    "    \"4ac0be6e-d159-4f17-afe3-b1be4e56ef50\": {\n",
    "        \"true\": \"When you start excluding exceptionals from free cash flow investment in cargo, you make it a much less useful number for the equity investors.\",\n",
    "        \"true_norm\": \"when you start excluding exceptionals from free cash flow investment in cargo you make it a much less useful number for the equity investors\",\n",
    "    },\n",
    "    \"1bfa8675-ec09-4cf1-a8b6-338c5a6f997d\": {\n",
    "        \"true\": \"already. So our factories in Wujiang, Dongguan and even in Thailand, after we --\",\n",
    "        \"true_norm\": \"already so our factories in wujiang dongguan and even in thailand after we\",\n",
    "    },\n",
    "    \"cec77f58-192b-4e56-9fd1-ad5454a23122\": {\n",
    "        \"true\": \"we get some data from the MI clients. I use, uh, Kensho Company Score in type-ahead. Uh, doing competitor analysis for the NERD perspectives and sitting down with Georg to, uh, sync on ASR and R&D. Uh, New York? Uh, NERD evaluation and concepts research. I'm hooking up into the MI database for Companies House and discovering some bugs on their end. So that's fun. [laughter] Tysons? Uh, working on speeding up a giant query over Xpressfeed, so adding a bunch of indices and gonna add some views. Uh, it's fun. Uh, also, Sireesh, guess how many donuts I had this week? Uh, 12. No, only five, but good guess. [laughter] Do better next time. [laughter] I'll work on it. [laughter] I've been working on cross document co-referencing for the Visallo Hack Week. Um, yeah, I'm demoing, uh, uh, t-SNE based graph layouts to Visallo Hack Week today. Uh, I'm still digging out as, uh, email a little bit from, uh, going to OpsCon and, uh, finishing up the NERD patent application. Tysons out. Chris? He's -- he's out. Oh, sorry, I thought you meant Blockchain. [laughter] All NERD, all the time. Uh, oh, we don't have Chris Blockchain in here. There isn't a lifeline of succession. We need that -- We need that for after. Oh right. Go team! Bye, bye.\",\n",
    "        \"true_norm\": \"we get some data from the m i clients i use uh kensho company score in type ahead uh doing competitor analysis for the nerd perspectives and sitting down with georg to uh sync on a s r and r and d uh new york uh nerd evaluation and concepts research i'm hooking up into the mi database for companies house and discovering some bugs on their end so that's fun tysons uh working on speeding up a giant query over xpressfeed so adding a bunch of indices and gonna add some views uh it's fun uh also sireesh guess how many donuts i had this week uh twelve no only five but good guess do better next time i'll work on it i've been working on cross document co referencing for the visallo hack week um yeah i'm demoing uh uh t sne based graph layouts to visallo hack week today uh i'm still digging out as uh email a little bit from uh going to opscon and uh finishing up the nerd patent application tysons out chris he's he's out oh sorry i thought you meant blockchain all nerd all the time uh oh we don't have chris blockchain in here there isn't a lifeline of succession we need that we need that for after oh right go team bye bye\",\n",
    "    },\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "9edd2cda",
   "metadata": {},
   "outputs": [],
   "source": [
    "BASE_DIR = \"/home/georg/notebooks/tasks/s2t\"\n",
    "DATA_DIR = os.path.join(BASE_DIR, \"sample_data\")\n",
    "with open(os.path.join(DATA_DIR, \"transcripts.json\"), \"w\") as f:\n",
    "    json.dump(TRANSCRIPT_CONTAINER, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f32a63c",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b6c1421",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60455078",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "555da53f",
   "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.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
