File size: 5,746 Bytes
ea81160
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dd03eb44",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "token\n",
      "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\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/david/Documents/python-env-test/venv/lib/python3.10/site-packages/huggingface_hub/hf_api.py:101: FutureWarning: `name` and `organization` input arguments are deprecated and will be removed in v0.10. Pass `repo_id` instead.\n",
      "  warnings.warn(\n",
      "Cloning https://huggingface.co/datasets/HuggingDavid/simple-mnist-flagging into local empty directory.\n"
     ]
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "38d85f20bb7d48f8934048f520b5125f",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Download file img/tmp7qxdqjtl.png:  46%|####5     | 8.28k/18.1k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ea599ef7307c42c7b1f3db8e453aadc9",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Clean file img/tmp7qxdqjtl.png:   6%|5         | 1.00k/18.1k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "4badbc924d4a4d04b5469682a6837c9a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Download file img/tmpb9pmlzsj.png: 100%|##########| 15.4k/15.4k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "665fb3a23cc843cba87cdaad930b645a",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Clean file img/tmpb9pmlzsj.png:   7%|6         | 1.00k/15.4k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running on local URL:  http://127.0.0.1:7880\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7880/\" width=\"900\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(<gradio.routes.App at 0x162231e40>, 'http://127.0.0.1:7880/', None)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2ecf20840bb14b4f96671ee323d83734",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "Upload file img/tmpjuysmmri.png: 100%|##########| 17.6k/17.6k [00:00<?, ?B/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "remote: Scanning LFS files for validity, may be slow...        \n",
      "remote: LFS file scan complete.        \n",
      "To https://huggingface.co/datasets/HuggingDavid/simple-mnist-flagging\n",
      "   4b19b7d..458cf22  main -> main\n",
      "\n"
     ]
    }
   ],
   "source": [
    "import torch\n",
    "import gradio as gr\n",
    "from torchvision import transforms\n",
    "from PIL import ImageOps\n",
    "import os\n",
    "from dotenv import load_dotenv\n",
    "\n",
    "load_dotenv()\n",
    "\n",
    "hf_writer = gr.HuggingFaceDatasetSaver(os.getenv('HF_TOKEN'), \"simple-mnist-flagging\")\n",
    "\n",
    "def load_model():\n",
    "    model_dict = torch.load('linear_model.pt')\n",
    "    return model_dict\n",
    "\n",
    "model = load_model()\n",
    "convert_tensor = transforms.ToTensor()\n",
    "\n",
    "def predict(img):\n",
    "    img =  ImageOps.grayscale(img).resize((28,28))\n",
    "    image_tensor = convert_tensor(img).view(28*28)\n",
    "    res = image_tensor @ model['weights'] + model['bias']\n",
    "    res = res.sigmoid()\n",
    "    return {\"It's 3\": float(res), \"It's 7\": float(1-res)}\n",
    "\n",
    "title = \"Is it 7 or 3\"\n",
    "description = '<p><center>Write a number, 7 or 3, in the middle.</center></p>'\n",
    "\n",
    "gr.Interface(fn=predict, \n",
    "             inputs=gr.Paint(type=\"pil\", invert_colors=True),\n",
    "             outputs=gr.Label(num_top_classes=2),\n",
    "             title=title,\n",
    "             flagging_options=[\"incorrect\",\"ambiguous\"],\n",
    "             flagging_callback=hf_writer,\n",
    "             description=description,\n",
    "             allow_flagging='manual').launch()"
   ]
  }
 ],
 "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}