File size: 3,568 Bytes
6620552
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "c:\\Users\\iitsi\\anaconda3\\lib\\site-packages\\gradio\\blocks.py:503: UserWarning: Cannot load huggingface. Caught Exception: The space huggingface does not exist\n",
      "  warnings.warn(f\"Cannot load {theme}. Caught Exception: {str(e)}\")\n",
      "c:\\Users\\iitsi\\anaconda3\\lib\\site-packages\\gradio\\deprecation.py:40: UserWarning: `layout` parameter is deprecated, and it has no effect\n",
      "  warnings.warn(value)\n",
      "ERROR:    [Errno 10048] error while attempting to bind on address ('127.0.0.1', 7864): only one usage of each socket address (protocol/network address/port) is normally permitted\n"
     ]
    }
   ],
   "source": [
    "import gradio as gr\n",
    "from transformers import pipeline\n",
    "\n",
    "model_names = [\n",
    "    \"apple/mobilevit-small\",\n",
    "    \"facebook/deit-base-patch16-224\",\n",
    "    \"facebook/convnext-base-224\",\n",
    "    \"google/vit-base-patch16-224\",\n",
    "    \"google/mobilenet_v2_1.4_224\",\n",
    "    \"microsoft/resnet-50\",\n",
    "    \"microsoft/swin-base-patch4-window7-224\",\n",
    "    \"microsoft/beit-base-patch16-224\",\n",
    "    \"nvidia/mit-b0\",\n",
    "    \"shi-labs/nat-base-in1k-224\",\n",
    "    \"shi-labs/dinat-base-in1k-224\",\n",
    "]\n",
    "\n",
    "\n",
    "def process(image_file, top_k):\n",
    "    labels = []\n",
    "    for m in model_names:\n",
    "        p = pipeline(\"image-classification\", model=m)\n",
    "        pred = p(image_file)\n",
    "        labels.append({x[\"label\"]: x[\"score\"] for x in pred[:top_k]})\n",
    "    return labels\n",
    "\n",
    "\n",
    "# Inputs\n",
    "image = gr.Image(type=\"filepath\", label=\"Upload an image\")\n",
    "top_k = gr.Slider(minimum=1, maximum=5, step=1, value=5, label=\"Top k classes\")\n",
    "\n",
    "# Output\n",
    "labels = [gr.Label(label=m) for m in model_names]\n",
    "\n",
    "description = \"This Space lets you quickly compare the most popular image classifiers available on the hub, including the recent NAT and DINAT models. All of them have been fine-tuned on the ImageNet-1k dataset. Anecdotally, the three sample images have been generated with a Stable Diffusion model :)\"\n",
    "\n",
    "iface = gr.Interface(\n",
    "    theme=\"huggingface\",\n",
    "    description=description,\n",
    "    layout=\"horizontal\",\n",
    "    fn=process,\n",
    "    inputs=[image, top_k],\n",
    "    outputs=labels,\n",
    "    examples=[\n",
    "        [\"bike.jpg\", 5],\n",
    "        [\"car.jpg\", 5],\n",
    "        [\"food.jpg\", 5],\n",
    "    ],\n",
    "    allow_flagging=\"never\",\n",
    ")\n",
    "\n",
    "iface.launch()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.9.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "a10896378c7586fe060a2352f1f55c7416a5095f8b10e25e98ff567d7ae9c79d"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}