HeenaPatel commited on
Commit
ff53d5a
·
1 Parent(s): 835a845
Files changed (1) hide show
  1. app.py +19 -15
app.py CHANGED
@@ -1,19 +1,23 @@
1
- from fastai.vision.all import *
2
  import gradio as gr
 
 
 
3
 
4
- def is_cat(x): return x[0].isupper()
5
- im = PILImage.create('/dog.jpg')
6
- im.thumbnail((192,192))
7
- learn =load_learner('/model.pkl')
8
- categories = ('dog','cat')
9
 
10
- def classify_image(img):
11
- pred,idx,probs = learn.predict(img)
12
- return dict(zip(categories,map(float,probs)))
 
 
 
13
 
14
- #gradio interface
15
- image = gr.inputs.Image(shape=(192,192))
16
- label=gr.outputs.Label()
17
- examples=['dog.jpg','cat.jpg','dunno.jpg']
18
- intf=gr.Interface(fn=classify_image,inputs=image,outputs=label,example=examples)
19
- intf.launch(inline=False)
 
 
 
1
  import gradio as gr
2
+ import torch
3
+ import requests
4
+ from torchvision import transforms
5
 
6
+ model = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=True).eval()
7
+ response = requests.get("https://git.io/JJkYN")
8
+ labels = response.text.split("\n")
 
 
9
 
10
+ def predict(inp):
11
+ inp = transforms.ToTensor()(inp).unsqueeze(0)
12
+ with torch.no_grad():
13
+ prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
14
+ confidences = {labels[i]: float(prediction[i]) for i in range(1000)}
15
+ return confidences
16
 
17
+ demo = gr.Interface(fn=predict,
18
+ inputs=gr.inputs.Image(type="pil"),
19
+ outputs=gr.outputs.Label(num_top_classes=3),
20
+ examples=[["dog.jpg"]],
21
+ )
22
+
23
+ demo.launch()