{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "7e08d187", "metadata": {}, "outputs": [], "source": [ "#|default_exp app" ] }, { "cell_type": "code", "execution_count": 2, "id": "e449d107", "metadata": {}, "outputs": [], "source": [ "#|export\n", "# write this at the top of each cell which has to be exported to final script" ] }, { "cell_type": "code", "execution_count": 3, "id": "7fa3f38f", "metadata": {}, "outputs": [], "source": [ "#|export\n", "from fastai.vision.all import *" ] }, { "cell_type": "code", "execution_count": 6, "id": "ba04f1d8", "metadata": {}, "outputs": [], "source": [ "#|export\n", "# this identifies the type of classifier\n", "learn = load_learner('export.pkl')\n", "labels = learn.dls.vocab\n", "def predict(img):\n", " img = PILImage.create(img)\n", " pred,pred_idx,probs = learn.predict(img)\n", " return {labels[i]: float(probs[i]) for i in range(len(labels))}" ] }, { "cell_type": "code", "execution_count": 5, "id": "5c77d812", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/gradio/inputs.py:256: UserWarning: Usage of gradio.inputs is deprecated, and will not be supported in the future, please import your component from gradio.components\n", " warnings.warn(\n", "/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/gradio/deprecation.py:40: UserWarning: `optional` parameter is deprecated, and it has no effect\n", " warnings.warn(value)\n", "/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/gradio/outputs.py:196: UserWarning: Usage of gradio.outputs is deprecated, and will not be supported in the future, please import your components from gradio.components\n", " warnings.warn(\n", "/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/gradio/deprecation.py:40: UserWarning: The 'type' parameter has been deprecated. Use the Number component instead.\n", " warnings.warn(value)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7860\n", "Running on public URL: https://28738.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://28738.gradio.app')" ] }, "execution_count": 5, "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": [ "Traceback (most recent call last):\n", " File \"/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/PIL/Image.py\", line 3096, in open\n", " fp.seek(0)\n", "AttributeError: 'NoneType' object has no attribute 'seek'\n", "\n", "During handling of the above exception, another exception occurred:\n", "\n", "Traceback (most recent call last):\n", " File \"/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/gradio/routes.py\", line 268, in run_predict\n", " output = await app.blocks.process_api(\n", " File \"/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/gradio/blocks.py\", line 748, in process_api\n", " result = await self.call_function(fn_index, inputs, iterator)\n", " File \"/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/gradio/blocks.py\", line 627, in call_function\n", " prediction = await anyio.to_thread.run_sync(\n", " File \"/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/anyio/to_thread.py\", line 31, in run_sync\n", " return await get_asynclib().run_sync_in_worker_thread(\n", " File \"/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/anyio/_backends/_asyncio.py\", line 937, in run_sync_in_worker_thread\n", " return await future\n", " File \"/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/anyio/_backends/_asyncio.py\", line 867, in run\n", " result = context.run(func, *args)\n", " File \"\", line 5, in predict\n", " img = PILImage.create(img)\n", " File \"/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/fastai/vision/core.py\", line 123, in create\n", " return cls(load_image(fn, **merge(cls._open_args, kwargs)))\n", " File \"/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/fastai/vision/core.py\", line 98, in load_image\n", " im = Image.open(fn)\n", " File \"/home/gilopez/mambaforge/envs/fastai/lib/python3.9/site-packages/PIL/Image.py\", line 3098, in open\n", " fp = io.BytesIO(fp.read())\n", "AttributeError: 'NoneType' object has no attribute 'read'\n" ] }, { "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": [ "#|export\n", "import gradio as gr\n", "gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(512, 512)), outputs=gr.outputs.Label(num_top_classes=3)).launch(share=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "7d39cbf6", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "02a65a7b", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "ab54f129", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "00153d99", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "d3beb9d9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "384fdf33", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "301ee4c4", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "4ef0f13c", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "2712605e", "metadata": {}, "source": [ "## export notebook" ] }, { "cell_type": "code", "execution_count": 11, "id": "217ceef9", "metadata": {}, "outputs": [], "source": [ "# export notebook\n", "from nbdev.export import nb_export" ] }, { "cell_type": "code", "execution_count": 12, "id": "033dba80", "metadata": {}, "outputs": [], "source": [ "nb_export('app.ipynb')" ] }, { "cell_type": "code", "execution_count": null, "id": "24a6fd20", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "fastai", "language": "python", "name": "fastai" }, "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.13" } }, "nbformat": 4, "nbformat_minor": 5 }