Jyothish CHANDRASENAN commited on
Commit
3498d34
·
1 Parent(s): 511ab75

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -0
app.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+
3
+ import requests
4
+ from PIL import Image
5
+ from torchvision import transforms
6
+
7
+ import gradio as gr
8
+
9
+ # Download human-readable labels for ImageNet.
10
+ response = requests.get("https://git.io/JJkYN")
11
+ labels = response.text.split("\n")
12
+
13
+ model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval()
14
+
15
+
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
+
24
+ gr.Interface(fn=predict,
25
+ inputs=gr.Image(type="pil"),
26
+ outputs=gr.Label(num_top_classes=3),
27
+ css="footer {visibility: hidden} body}, .gradio-container {background-color: white}",
28
+ examples=["lion.jpg","cat.jpg"]).launch(share=False)