Aseem Behl commited on
Commit
24ada56
·
1 Parent(s): 1648a29
Files changed (2) hide show
  1. app.py +26 -3
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,8 +1,31 @@
1
  import gradio as gr
2
  import torch
 
 
3
 
4
- def greet(name):
5
- return "Hello " + name + "!!"
6
 
7
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  iface.launch()
 
1
  import gradio as gr
2
  import torch
3
+ from torchvision import datasets, models, transforms
4
+ from PIL import Image
5
 
6
+ LABELS = ['VW Up!', 'Fiat 500']
 
7
 
8
+ model = models.resnet18(pretrained=True)
9
+ num_ftrs = model.fc.in_features
10
+
11
+ model.fc = torch.nn.Linear(num_ftrs, 2)
12
+
13
+ state_dict = torch.load('up500Model.pt', map_location='cpu')
14
+ model.load_state_dict(state_dict, strict=False)
15
+ model.eval()
16
+
17
+ imgTransforms = transforms.Compose([
18
+ transforms.Resize(256),
19
+ transforms.CenterCrop(224),
20
+ transforms.ToTensor(),
21
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
22
+
23
+ def predict(im):
24
+ inp = Image.fromarray(inp.astype('uint8'), 'RGB')
25
+ inp = imgTransforms(inp).unsqueeze(0)
26
+ with torch.no_grad():
27
+ prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
28
+ return {LABELS[i]: float(prediction[i]) for i in range(2)}
29
+
30
+ iface = gr.Interface(predict, inputs=gr.inputs.Image(), outputs="label")
31
  iface.launch()
requirements.txt CHANGED
@@ -1 +1,2 @@
1
- torch
 
 
1
+ torch
2
+ torchvision