dmusingu commited on
Commit
bd32f71
·
verified ·
1 Parent(s): 8275d19

Update app.py

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