File size: 2,516 Bytes
3dc35ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a9c14507-a23e-4bd8-990e-d45a7460339d",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'MNIST_PATH' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[9], line 2\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mfastai\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mvision\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mall\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;241m*\u001b[39m\n\u001b[1;32m----> 2\u001b[0m path \u001b[38;5;241m=\u001b[39m untar_data(MNIST_PATH)\n\u001b[0;32m      3\u001b[0m data \u001b[38;5;241m=\u001b[39m image_data_from_folder(path)\n\u001b[0;32m      4\u001b[0m learn \u001b[38;5;241m=\u001b[39m cnn_learner(data, models\u001b[38;5;241m.\u001b[39mresnet18, metrics\u001b[38;5;241m=\u001b[39maccuracy)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'MNIST_PATH' is not defined"
     ]
    }
   ],
   "source": [
    "from fastai.vision.all import *\n",
    "import gradio as gr\n",
    "def is_cat(x):\n",
    "    return x[0].isupper()\n",
    "\n",
    "learn = load_learner('model.pkl')\n",
    "\n",
    "categories = ('Dog', 'Cat')\n",
    "\n",
    "def classify_image(img):\n",
    "    pred,idx,probs = learn.predict(img)\n",
    "    return dict(zip(categories), map(float,probs))\n",
    "\n",
    "image = gr.inputs.Image(shape=(192,192))\n",
    "label = gr.outputs.Label()\n",
    "\n",
    "intf = gr.Interface(fn=classify_image, inputs=image, outputs=label)\n",
    "intf.launch(inline=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c353f37d-7f82-4179-bf86-7cfa96d3d0b0",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}