File size: 4,101 Bytes
3dd302d
 
 
 
 
 
 
 
0d5acf9
 
 
 
 
 
 
 
 
3dd302d
 
 
 
0d5acf9
3dd302d
 
 
 
 
 
 
 
 
 
 
 
 
 
0d5acf9
 
 
 
 
 
 
 
3dd302d
 
0d5acf9
3dd302d
0d5acf9
3dd302d
 
 
 
 
 
 
 
 
0d5acf9
3dd302d
0d5acf9
3dd302d
 
 
 
 
 
0d5acf9
 
 
 
 
 
 
 
3dd302d
 
6425640
3dd302d
 
 
 
 
 
6425640
 
 
3dd302d
 
 
 
 
 
 
 
6425640
3dd302d
 
 
 
 
 
 
 
0d5acf9
3dd302d
 
 
 
 
6425640
 
c5fed56
3dd905b
 
 
0d5acf9
3dd302d
3dd905b
 
 
 
 
6425640
3dd905b
 
0d5acf9
 
 
 
 
 
 
 
 
3dd302d
 
 
 
6425640
3dd302d
0d5acf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3dd302d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#|default_exp demo"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Basic setup, once again..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "#|export\n",
    "from fastai.vision.all import *\n",
    "import pathlib\n",
    "import gradio as gr\n",
    "plt = platform.system()\n",
    "if plt == 'Linux': pathlib.WindowsPath = pathlib.PosixPath\n",
    "\n",
    "def is_albani(path):\n",
    "  return parent_label(path) == \"albani\""
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Test with own images"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "albani = PILImage.create('albani2.jpg')\n",
    "\n",
    "albani.thumbnail((192, 192))\n",
    "albani"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "learn = load_learner(Path('./resnet18-albani.pkl'))\n",
    "\n",
    "learn.predict(albani)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Gradio demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Thanks for being a Gradio user! If you have questions or feedback, please join our Discord server and chat with us: https://discord.gg/feTf9x3ZSB\n",
      "Running on local URL:  http://127.0.0.1:7877\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/plain": []
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#|export\n",
    "\n",
    "learn = load_learner(Path('./resnet18-albani.pkl'))\n",
    "categories = ('Dårlig Øl', 'God Øl')\n",
    "\n",
    "def classify_image(img):\n",
    "  pred,idx,probs = learn.predict(img)\n",
    "  return dict(zip(categories, map(float, probs)))\n",
    "\n",
    "description = \"\"\"\n",
    "  ## Er du i tvivl om at den øl du sidder med i hånden lige, nu er god?\n",
    "  ### Tvivl ej 68 års invotation inden for machine learning skal nok fortælle dig om øllen er god eller ej\n",
    "\"\"\"\n",
    "image = gr.Image(shape=(192, 192))\n",
    "label = gr.Label()\n",
    "examples = ['albani.jpg', 'albani2.jpg', 'heineken.jpg', 'carlsberg.jpg']\n",
    "\n",
    "iface = gr.Interface(\n",
    "  fn=classify_image, \n",
    "  inputs=image, \n",
    "  outputs=label, \n",
    "  examples=examples, \n",
    "  description=description,\n",
    ")\n",
    "\n",
    "iface.launch(inline=False)\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Build for huggingface"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO: Successfully saved requirements file in ./requirements.txt\n"
     ]
    }
   ],
   "source": [
    "from nbdev.export import nb_export\n",
    "\n",
    "nb_export('model-test.ipynb', '.')\n",
    "! pipreqs . --force\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "fastai",
   "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.8"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "b977fc7f635931fb2de4a5e9c6503b6f91cf50b058799eb3f69da04d3aca2546"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}