Wootang01 commited on
Commit
ad0f123
·
1 Parent(s): d852ad3

Create app.py

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