{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b467bae1-eefa-426d-b7d8-f71db5fff8ac",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import boto3, json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "990ae599-b391-4a50-8932-c34685c53cc0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 6.14 ms, sys: 1.63 ms, total: 7.76 ms\n",
      "Wall time: 7.21 ms\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "endpoint_name='ramp-202303-staging'\n",
    "runtime = boto3.client(service_name=\"sagemaker-runtime\")\n",
    "\n",
    "paths2audio_files = f\"../1.building-component/data/preprocessing/an4/wav/an4test_clstk/fcaw/an406-fcaw-b.wav\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5d952dfb-d3af-4a3b-b0d3-bcf5b452da11",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "                \n",
       "              "
      ],
      "text/plain": [
       ""
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import librosa\n",
    "import IPython.display as ipd\n",
    "\n",
    "# Load and listen to the audio file\n",
    "audio, sample_rate = librosa.load(paths2audio_files)\n",
    "\n",
    "ipd.Audio(paths2audio_files, rate=sample_rate)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee6d9303-6453-48f6-865c-f820806f721f",
   "metadata": {},
   "source": [
    "## Prediction - SageMaker SDK"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3a3e1b4f-bc95-446f-9d5b-68e3121f8cd9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from sagemaker.predictor import Predictor\n",
    "predictor = Predictor(endpoint_name=endpoint_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "dc4744ba-023e-41bd-a51a-7e8a7bc17731",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from sagemaker.serializers import DataSerializer\n",
    "predictor.serializer = DataSerializer()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "faa335ae-b89c-42da-959d-5b829d46721b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"result\": [\" \"]}'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "predictor.predict(paths2audio_files)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7a0c64e-4f0e-4878-b81d-f986ac4e019a",
   "metadata": {},
   "source": [
    "## Prediction - boto3 SDK "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "246963bd-d5ed-45ec-8104-4207c37c0a1c",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "with open(paths2audio_files, \"rb\") as f:\n",
    "    body = f.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "6f6cedb6-6fd1-44de-acaf-c1adbc9898bc",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [' ']}\n"
     ]
    }
   ],
   "source": [
    "response = runtime.invoke_endpoint(\n",
    "    EndpointName=endpoint_name,\n",
    "    Body=body\n",
    ")\n",
    "\n",
    "print(json.loads(response['Body'].read().decode()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ec8fca8-dd3e-4db3-ae3c-c2b46d42ab3d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "instance_type": "ml.t3.medium",
  "kernelspec": {
   "display_name": "conda_pytorch_p39",
   "language": "python",
   "name": "conda_pytorch_p39"
  },
  "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.9.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}