CoolFace
Apppublic

HuggingDavid/simple-mnist

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
Untitled.ipynb201 linesDownload Raw Back to root
1{2 "cells": [3  {4   "cell_type": "code",5   "execution_count": 2,6   "id": "dd03eb44",7   "metadata": {},8   "outputs": [9    {10     "name": "stdout",11     "output_type": "stream",12     "text": [13      "token\n",14      "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\n"15     ]16    },17    {18     "name": "stderr",19     "output_type": "stream",20     "text": [21      "/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",22      "  warnings.warn(\n",23      "Cloning https://huggingface.co/datasets/HuggingDavid/simple-mnist-flagging into local empty directory.\n"24     ]25    },26    {27     "data": {28      "application/vnd.jupyter.widget-view+json": {29       "model_id": "38d85f20bb7d48f8934048f520b5125f",30       "version_major": 2,31       "version_minor": 032      },33      "text/plain": [34       "Download file img/tmp7qxdqjtl.png:  46%|####5     | 8.28k/18.1k [00:00<?, ?B/s]"35      ]36     },37     "metadata": {},38     "output_type": "display_data"39    },40    {41     "data": {42      "application/vnd.jupyter.widget-view+json": {43       "model_id": "ea599ef7307c42c7b1f3db8e453aadc9",44       "version_major": 2,45       "version_minor": 046      },47      "text/plain": [48       "Clean file img/tmp7qxdqjtl.png:   6%|5         | 1.00k/18.1k [00:00<?, ?B/s]"49      ]50     },51     "metadata": {},52     "output_type": "display_data"53    },54    {55     "data": {56      "application/vnd.jupyter.widget-view+json": {57       "model_id": "4badbc924d4a4d04b5469682a6837c9a",58       "version_major": 2,59       "version_minor": 060      },61      "text/plain": [62       "Download file img/tmpb9pmlzsj.png: 100%|##########| 15.4k/15.4k [00:00<?, ?B/s]"63      ]64     },65     "metadata": {},66     "output_type": "display_data"67    },68    {69     "data": {70      "application/vnd.jupyter.widget-view+json": {71       "model_id": "665fb3a23cc843cba87cdaad930b645a",72       "version_major": 2,73       "version_minor": 074      },75      "text/plain": [76       "Clean file img/tmpb9pmlzsj.png:   7%|6         | 1.00k/15.4k [00:00<?, ?B/s]"77      ]78     },79     "metadata": {},80     "output_type": "display_data"81    },82    {83     "name": "stdout",84     "output_type": "stream",85     "text": [86      "Running on local URL:  http://127.0.0.1:7880\n",87      "\n",88      "To create a public link, set `share=True` in `launch()`.\n"89     ]90    },91    {92     "data": {93      "text/html": [94       "<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>"95      ],96      "text/plain": [97       "<IPython.core.display.HTML object>"98      ]99     },100     "metadata": {},101     "output_type": "display_data"102    },103    {104     "data": {105      "text/plain": [106       "(<gradio.routes.App at 0x162231e40>, 'http://127.0.0.1:7880/', None)"107      ]108     },109     "execution_count": 2,110     "metadata": {},111     "output_type": "execute_result"112    },113    {114     "data": {115      "application/vnd.jupyter.widget-view+json": {116       "model_id": "2ecf20840bb14b4f96671ee323d83734",117       "version_major": 2,118       "version_minor": 0119      },120      "text/plain": [121       "Upload file img/tmpjuysmmri.png: 100%|##########| 17.6k/17.6k [00:00<?, ?B/s]"122      ]123     },124     "metadata": {},125     "output_type": "display_data"126    },127    {128     "name": "stderr",129     "output_type": "stream",130     "text": [131      "remote: Scanning LFS files for validity, may be slow...        \n",132      "remote: LFS file scan complete.        \n",133      "To https://huggingface.co/datasets/HuggingDavid/simple-mnist-flagging\n",134      "   4b19b7d..458cf22  main -> main\n",135      "\n"136     ]137    }138   ],139   "source": [140    "import torch\n",141    "import gradio as gr\n",142    "from torchvision import transforms\n",143    "from PIL import ImageOps\n",144    "import os\n",145    "from dotenv import load_dotenv\n",146    "\n",147    "load_dotenv()\n",148    "\n",149    "hf_writer = gr.HuggingFaceDatasetSaver(os.getenv('HF_TOKEN'), \"simple-mnist-flagging\")\n",150    "\n",151    "def load_model():\n",152    "    model_dict = torch.load('linear_model.pt')\n",153    "    return model_dict\n",154    "\n",155    "model = load_model()\n",156    "convert_tensor = transforms.ToTensor()\n",157    "\n",158    "def predict(img):\n",159    "    img =  ImageOps.grayscale(img).resize((28,28))\n",160    "    image_tensor = convert_tensor(img).view(28*28)\n",161    "    res = image_tensor @ model['weights'] + model['bias']\n",162    "    res = res.sigmoid()\n",163    "    return {\"It's 3\": float(res), \"It's 7\": float(1-res)}\n",164    "\n",165    "title = \"Is it 7 or 3\"\n",166    "description = '<p><center>Write a number, 7 or 3, in the middle.</center></p>'\n",167    "\n",168    "gr.Interface(fn=predict, \n",169    "             inputs=gr.Paint(type=\"pil\", invert_colors=True),\n",170    "             outputs=gr.Label(num_top_classes=2),\n",171    "             title=title,\n",172    "             flagging_options=[\"incorrect\",\"ambiguous\"],\n",173    "             flagging_callback=hf_writer,\n",174    "             description=description,\n",175    "             allow_flagging='manual').launch()"176   ]177  }178 ],179 "metadata": {180  "kernelspec": {181   "display_name": "Python 3 (ipykernel)",182   "language": "python",183   "name": "python3"184  },185  "language_info": {186   "codemirror_mode": {187    "name": "ipython",188    "version": 3189   },190   "file_extension": ".py",191   "mimetype": "text/x-python",192   "name": "python",193   "nbconvert_exporter": "python",194   "pygments_lexer": "ipython3",195   "version": "3.10.6"196  }197 },198 "nbformat": 4,199 "nbformat_minor": 5200}201