{ "cells": [ { "cell_type": "code", "execution_count": 4, "id": "a99e3288", "metadata": {}, "outputs": [], "source": [ "import gradio as gr\n", "from fastai.vision.all import *\n", "import pathlib\n", "from sklearn.metrics import roc_auc_score" ] }, { "cell_type": "code", "execution_count": 5, "id": "252a5439", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "pathlib.PosixPath" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pathlib.PosixPath" ] }, { "cell_type": "code", "execution_count": 6, "id": "177d0534", "metadata": {}, "outputs": [], "source": [ "def get_x(row): return Path(str(path/f\"{row['rootname']}_small\"/f\"{row['ID']}\") + \".png\")\n", "def get_y(row): return row[\"LABEL\"]\n", "\n", "def auroc_score(input, target):\n", " input, target = input.cpu().numpy()[:,1], target.cpu().numpy()\n", " return roc_auc_score(target, input)" ] }, { "cell_type": "code", "execution_count": 7, "id": "7ab6178b", "metadata": {}, "outputs": [], "source": [ "posix_backup = pathlib.PosixPath\n", "\n", "try:\n", " pathlib.PosixPath = pathlib.WindowsPath\n", " learn = load_learner(\"export.pkl\")\n", "finally:\n", " pathlib.PosixPath = posix_backup" ] }, { "cell_type": "code", "execution_count": 8, "id": "59333bd1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "learn.dls.vocab" ] }, { "cell_type": "code", "execution_count": 9, "id": "ba409ec0", "metadata": {}, "outputs": [], "source": [ "labels = [\"Negative\", \"Positive\"]\n", "def predict(img):\n", " img = PILImage.create(img)\n", " pred, idx, probs = learn.predict(img)\n", " return {labels[i]: float(probs[i]) for i in range(len(labels))}" ] }, { "cell_type": "code", "execution_count": 10, "id": "f899f197", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "{'Negative': 0.0016864568460732698, 'Positive': 0.9983135461807251}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "predict(\"patient1.png\")" ] }, { "cell_type": "code", "execution_count": 39, "id": "8d4bf857", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Closing server running on port: 7860\n", "Closing server running on port: 7860\n", "Closing server running on port: 7860\n" ] } ], "source": [ "gr.close_all()" ] }, { "cell_type": "code", "execution_count": 12, "id": "7874b68b", "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7860\n", "Running on public URL: https://28603.gradio.app\n", "\n", "This share link expires in 72 hours. For free permanent hosting, check out Spaces: https://huggingface.co/spaces\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [ "(,\n", " 'http://127.0.0.1:7860/',\n", " 'https://28603.gradio.app')" ] }, "execution_count": 12, "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" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "title = \"Classifying x-ray scans as either positive or negative Tuberculosis(TB)\"\n", "description = \"\"\"Catching TB early can be life altering.\n", "Among various techniques used for detecting Tuberculosis, image scanning can be aided with computer vision.\n", "This dataset was obtained from Zindi with link below. \n", "Note: This model and classification is mainly for educational purpose and any user uses it at their own discretion\"\"\"\n", "article=\"

Link to Zindi competition

\"\n", "examples = ['patient1.png', 'patient2.png', 'patient3.png']\n", "enable_queue=True\n", "\n", "gr.Interface(fn=predict,inputs=gr.inputs.Image(shape=(800, 800)),\n", " outputs=gr.outputs.Label(num_top_classes=1),\n", " title=title,\n", " description=description,article=article,\n", " examples=examples,\n", " enable_queue=enable_queue).launch(share=True, )" ] }, { "cell_type": "code", "execution_count": 33, "id": "655faace", "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Closing server running on port: 7860\n" ] } ], "source": [ "gr.close_all()" ] } ], "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.5" } }, "nbformat": 4, "nbformat_minor": 5 }