Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,35 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
import
|
5 |
import numpy as np
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
env_var = os.environ.get('env')
|
3 |
+
import torch
|
4 |
+
import time
|
5 |
import numpy as np
|
6 |
+
import gradio as gr
|
7 |
+
from PIL import Image
|
8 |
+
import torchvision
|
9 |
+
from torchvision import transforms
|
10 |
+
|
11 |
+
device = 'cpu'
|
12 |
+
model = torch.load('model.pkl').to(device).eval()
|
13 |
+
transform = transforms.Resize(size=500)
|
14 |
+
labels = ['Cat', 'Dog']
|
15 |
+
|
16 |
+
def predict(image):
|
17 |
+
start = time.time()
|
18 |
+
with torch.no_grad():
|
19 |
+
image = Image.fromarray(np.uint8(image)).convert('RGB')
|
20 |
+
image = transform(image)
|
21 |
+
image = np.array(image)
|
22 |
+
image = torch.from_numpy(image).permute(2,0,1).float()
|
23 |
+
image = image.unsqueeze(0)
|
24 |
+
prediction = model(image.to(device))
|
25 |
+
pred_idx = np.argmax(prediction.to(device))
|
26 |
+
pred_label = "Cat" if pred_idx == 0 else "Dog"
|
27 |
+
label = [l for l in labels if l!=pred_label]
|
28 |
+
confidences = {pred_label: float(prediction[0][pred_idx])/100, label[len(label)-1]: 1-(float(prediction[0][pred_idx]))/100 }
|
29 |
+
infer = time.time()-start
|
30 |
+
return confidences, infer
|
31 |
|
32 |
+
gr.Interface(fn=predict,
|
33 |
+
inputs=gr.inputs.Image(shape=(512, 512)),
|
34 |
+
outputs=[gr.outputs.Label(num_top_classes=3), gr.outputs.Textbox('infer',label='Inference Time')],
|
35 |
+
examples='1.jpg 2.jpg'.split(' ')).launch()
|