aliabd HF staff commited on
Commit
b75a183
·
1 Parent(s): 63b2146

Create new file

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # URL: https://huggingface.co/spaces/abidlabs/pytorch-image-classifier
2
+ # imports
3
+ import gradio as gr
4
+ import torch
5
+ import requests
6
+ from torchvision import transforms
7
+
8
+ # load the model
9
+ model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval()
10
+
11
+ # download human-readable labels for ImageNet.
12
+ response = requests.get("https://git.io/JJkYN")
13
+ labels = response.text.split("\n")
14
+
15
+ # define core function
16
+ def predict(inp):
17
+ inp = transforms.ToTensor()(inp).unsqueeze(0)
18
+ with torch.no_grad():
19
+ prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
20
+ confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
21
+ return confidences
22
+
23
+ # define the interface
24
+ gr.Interface(fn=predict,
25
+ inputs=gr.inputs.Image(type="pil"),
26
+ outputs=gr.outputs.Label(num_top_classes=3),
27
+ examples=["lion.jpg", "cheetah.jpg"],
28
+ ).launch()