Subha Nawer Pushpita commited on
Commit
e459d75
·
1 Parent(s): aebbe04

create app.py

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