thliang01 commited on
Commit
df93a86
1 Parent(s): 715e599

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -0
app.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import gradio as gr
3
+ import torch
4
+ import requests
5
+ from PIL import Image
6
+ from torchvision import transforms
7
+
8
+ model = torch.hub.load('pytorch/vision', '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
+ 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(999)}
19
+ return confidences
20
+
21
+ # create gradio interface, with text input and dict output
22
+ gr.Interface(title="Image Classification in PyTorch",
23
+ fn=predict,
24
+ inputs=gr.Image(type="pil"),
25
+ outputs=gr.Label(num_top_classes=3),
26
+ examples=["lion.jpg", "cheetah.jpg"]).launch()
27
+
28
+ # run the app
29
+ gr.launch(server_port=7680, enable_queue=False, share=True)