{ "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 }