abidlabs HF Staff commited on
Commit
82e31c2
·
verified ·
1 Parent(s): 34edf0a

Upload folder using huggingface_hub

Browse files
Files changed (6) hide show
  1. DESCRIPTION.md +1 -0
  2. README.md +2 -8
  3. cheetah.jpg +0 -0
  4. requirements.txt +2 -0
  5. run.ipynb +1 -0
  6. run.py +23 -0
DESCRIPTION.md ADDED
@@ -0,0 +1 @@
 
 
1
+ Simple image classification in Pytorch with Gradio's Image input and Label output.
README.md CHANGED
@@ -1,12 +1,6 @@
1
  ---
2
- title: Image Classification
3
- emoji:
4
- colorFrom: indigo
5
- colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 4.36.1
8
- app_file: app.py
9
- pinned: false
10
  ---
11
-
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: image_classification
3
+ app_file: run.py
 
 
4
  sdk: gradio
5
  sdk_version: 4.36.1
 
 
6
  ---
 
 
cheetah.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ torch
2
+ torchvision
run.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: image_classification\n", "### Simple image classification in Pytorch with Gradio's Image input and Label output.\n", " "]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio torch torchvision"]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["# Downloading files from the demo repo\n", "import os\n", "!wget -q https://github.com/gradio-app/gradio/raw/main/demo/image_classification/cheetah.jpg"]}, {"cell_type": "code", "execution_count": null, "id": "44380577570523278879349135829904343037", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import torch\n", "import requests\n", "from torchvision import transforms\n", "\n", "model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval()\n", "response = requests.get(\"https://git.io/JJkYN\")\n", "labels = response.text.split(\"\\n\")\n", "\n", "def predict(inp):\n", " inp = transforms.ToTensor()(inp).unsqueeze(0)\n", " with torch.no_grad():\n", " prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)\n", " confidences = {labels[i]: float(prediction[i]) for i in range(1000)} \n", " return confidences\n", "\n", "demo = gr.Interface(fn=predict, \n", " inputs=gr.Image(type=\"pil\"),\n", " outputs=gr.Label(num_top_classes=3),\n", " examples=[[\"cheetah.jpg\"]],\n", " )\n", " \n", "demo.launch()"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}
run.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import requests
4
+ from torchvision import transforms
5
+
6
+ model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval()
7
+ response = requests.get("https://git.io/JJkYN")
8
+ labels = response.text.split("\n")
9
+
10
+ def predict(inp):
11
+ inp = transforms.ToTensor()(inp).unsqueeze(0)
12
+ with torch.no_grad():
13
+ prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
14
+ confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
15
+ return confidences
16
+
17
+ demo = gr.Interface(fn=predict,
18
+ inputs=gr.Image(type="pil"),
19
+ outputs=gr.Label(num_top_classes=3),
20
+ examples=[["cheetah.jpg"]],
21
+ )
22
+
23
+ demo.launch()