{ "cells": [ { "cell_type": "code", "execution_count": 34, "id": "f82c6d32", "metadata": {}, "outputs": [], "source": [ " #|default_exp app" ] }, { "cell_type": "markdown", "id": "5f27b4cb", "metadata": {}, "source": [ "# Imports" ] }, { "cell_type": "code", "execution_count": 26, "id": "60a8d84c", "metadata": {}, "outputs": [], "source": [ "#|export\n", "import PIL\n", "import gradio as gr\n", "from fastai.vision.all import *\n", "import librosa\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import shutil\n", "import os" ] }, { "cell_type": "markdown", "id": "2fd3a17d", "metadata": {}, "source": [ "# Load Model" ] }, { "cell_type": "code", "execution_count": 20, "id": "a7311575", "metadata": {}, "outputs": [], "source": [ "#|export\n", "learn = load_learner('./model.pkl')" ] }, { "cell_type": "markdown", "id": "a7647cf2", "metadata": {}, "source": [ "# Logic" ] }, { "cell_type": "code", "execution_count": 29, "id": "8fe7e353", "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAEsAAABLCAYAAAA4TnrqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjcuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/bCgiHAAAACXBIWXMAAA9hAAAPYQGoP6dpAAAKmElEQVR4nO1cW28b1xE+u+SSXFKUSNGyLpajxnbjJpHrugkKtK9FfkJ/SP9F/0PR5/6Aoi/NQwy0fWiAQI5lx7asuyhS4v22N+6tzzPfAD5rgCgKcN7OYLg659u5z1kZaZqmaklaZP6vN/D/REuwMtASrAy0BCsDLcHKQEuwMtASrAy0BCsDLcHKQHldweT9X5BZrZKlcXOLMkcX+KzrMV1PQ5BxT7GwGHbLZD1ySyDz43ANeO9nObL+vu+AzD+GfwIeJ22wjKsWMmurdH2GMhwYpZQKmz5ZOy3chucWgMfBeTOugkx3jsbSDyjwYzUDGR3SBksNhT9QZAeq4Js2G2Xg5b2IrO0gApnpBJ81DS2ytkzUvoLgWKr0Z2olxT3pkD5Yj/eA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plain": [ "
" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#|export\n", "def create_spectogram(path):\n", " samples, sample_rate = librosa.load(path, duration=5.0)\n", " S = librosa.feature.melspectrogram(y=samples, sr=sample_rate)\n", " return S\n", "\n", "def spectogram_to_image(spec):\n", " fig = plt.figure(figsize=[0.72, 0.72])\n", " ax = fig.add_subplot(111)\n", " ax.axes.get_xaxis().set_visible(False)\n", " ax.axes.get_yaxis().set_visible(False)\n", " ax.set_frame_on(False)\n", " librosa.display.specshow(librosa.power_to_db(spec, ref=np.max))\n", " fig.canvas.draw()\n", " return PIL.Image.frombytes('RGB',fig.canvas.get_width_height(),fig.canvas.tostring_rgb())\n", "\n", "def predict(audio_file_path):\n", " spec = create_spectogram(audio_file_path)\n", " spec_image = spectogram_to_image(spec)\n", " prediction,other,probs = learn.predict(spec_image)\n", " return prediction" ] }, { "cell_type": "markdown", "id": "0714b237", "metadata": {}, "source": [ "# UI" ] }, { "cell_type": "code", "execution_count": 30, "id": "a723c8c6", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7866\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stderr", "output_type": "stream", "text": [ "/Users/gabriel/Code/python/drum-sample-categorization/.venv/lib/python3.9/site-packages/gradio/processing_utils.py:180: UserWarning: Trying to convert audio automatically from int32 to 16-bit int format.\n", " warnings.warn(warning.format(data.dtype))\n", "/Users/gabriel/Code/python/drum-sample-categorization/.venv/lib/python3.9/site-packages/librosa/core/spectrum.py:256: UserWarning: n_fft=2048 is too large for input signal of length=1247\n", " warnings.warn(\n" ] }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "#|export\n", "iface = gr.Interface(fn=predict, inputs=[\n", " gr.components.Audio(type='filepath')], outputs=\"text\")\n", "iface.launch()" ] }, { "cell_type": "code", "execution_count": 35, "id": "a46000c8", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import nbdev\n", "nbdev.export.nb_export('drum_sample_categorization.ipynb', './')\n" ] }, { "cell_type": "code", "execution_count": null, "id": "0a38795f", "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.9.14" } }, "nbformat": 4, "nbformat_minor": 5 }