JuanB commited on
Commit
afd8893
·
1 Parent(s): 32bf6a8

Create app.py

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