{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#|default_exp app2"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"from fastai.vision.all import *\n",
"import gradio as gr\n",
"import io\n",
"from PIL import Image\n",
"\n",
"def pet_class(x): return x"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"learn = load_learner('model.pkl')"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"#|export\n",
"#categories = ('basketball ball','golf ball', 'rugby ball', 'soccer ball')\n",
"\n",
"def classify_image(img, top_k=5):\n",
" pred_class, pred_idx, probs = learn.predict(img)\n",
" categories = learn.dls.vocab\n",
" sorted_probs_indices = probs.argsort(descending=True)\n",
" top_categories = [categories[i] for i in sorted_probs_indices[:top_k]]\n",
" top_probs = probs[sorted_probs_indices[:top_k]]\n",
" return dict(zip(top_categories, map(float, top_probs)))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Running on local URL: http://127.0.0.1:7863\n",
"\n",
"To create a public link, set `share=True` in `launch()`.\n"
]
},
{
"data": {
"text/plain": []
},
"execution_count": 13,
"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"
}
],
"source": [
"#|export\n",
"image = gr.components.Image(shape=(192,192))\n",
"label = gr.components.Label()\n",
"#examples = ['basketball.png', 'golf_ball.jpg', 'rugby_ball.jpg', 'soccer_ball.jpg']\n",
"\n",
"intf = gr.Interface(fn=classify_image, inputs=image, outputs=label)\n",
"intf.launch(inline=False)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"import nbdev\n",
"nbdev.export.nb_export('app.ipynb', 'app2')"
]
},
{
"cell_type": "code",
"execution_count": null,
"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.10.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}