Spaces:
Sleeping
Sleeping
fix app.py to remove share=True
Browse files
app.py
CHANGED
@@ -1,48 +1,48 @@
|
|
1 |
-
|
2 |
-
import gradio as gr
|
3 |
-
import os
|
4 |
-
import torch
|
5 |
-
|
6 |
-
from model import create_effnetb2_model
|
7 |
-
from timeit import default_timer as timer
|
8 |
-
|
9 |
-
class_names = ["pizza", "steake", "sushi"]
|
10 |
-
|
11 |
-
effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=3)
|
12 |
-
|
13 |
-
effnetb2.load_state_dict(
|
14 |
-
torch.load(f"09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20.pth",
|
15 |
-
map_location=torch.device("cpu"))
|
16 |
-
)
|
17 |
-
|
18 |
-
def predict(img) -> tuple[dict, float]:
|
19 |
-
start_time = timer()
|
20 |
-
img = effnetb2_transforms(img).unsqueeze(0) # unsqueeze = add batch dimension on 0th dimension
|
21 |
-
effnetb2.eval()
|
22 |
-
with torch.inference_mode():
|
23 |
-
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
24 |
-
|
25 |
-
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
26 |
-
|
27 |
-
pred_time = round(timer() - start_time, 4)
|
28 |
-
|
29 |
-
return pred_labels_and_probs, pred_time
|
30 |
-
|
31 |
-
|
32 |
-
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
33 |
-
|
34 |
-
|
35 |
-
title = "FoodVision Mini😊"
|
36 |
-
description = "An [EffNetB2 feature extractor](https://pytorch.org/vision/main/models/generated/torchvision.models.efficientnet_b2.html) computer vision model to classify images as pizza, steak and sushi"
|
37 |
-
article = "Create at [09. PyTorch Model Deployment](http://keivanjamali.com)."
|
38 |
-
|
39 |
-
demo = gr.Interface(fn=predict,
|
40 |
-
inputs=gr.Image(type="pil"),
|
41 |
-
outputs=[gr.Label(num_top_classes=3, label="Predictions"),
|
42 |
-
gr.Number(label="Prediction Time (s)")],
|
43 |
-
examples=example_list,
|
44 |
-
title=title,
|
45 |
-
description=description,
|
46 |
-
article=article)
|
47 |
-
|
48 |
-
demo.launch(debug=False
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import os
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from model import create_effnetb2_model
|
7 |
+
from timeit import default_timer as timer
|
8 |
+
|
9 |
+
class_names = ["pizza", "steake", "sushi"]
|
10 |
+
|
11 |
+
effnetb2, effnetb2_transforms = create_effnetb2_model(num_classes=3)
|
12 |
+
|
13 |
+
effnetb2.load_state_dict(
|
14 |
+
torch.load(f"09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20.pth",
|
15 |
+
map_location=torch.device("cpu"))
|
16 |
+
)
|
17 |
+
|
18 |
+
def predict(img) -> tuple[dict, float]:
|
19 |
+
start_time = timer()
|
20 |
+
img = effnetb2_transforms(img).unsqueeze(0) # unsqueeze = add batch dimension on 0th dimension
|
21 |
+
effnetb2.eval()
|
22 |
+
with torch.inference_mode():
|
23 |
+
pred_probs = torch.softmax(effnetb2(img), dim=1)
|
24 |
+
|
25 |
+
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
26 |
+
|
27 |
+
pred_time = round(timer() - start_time, 4)
|
28 |
+
|
29 |
+
return pred_labels_and_probs, pred_time
|
30 |
+
|
31 |
+
|
32 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
33 |
+
|
34 |
+
|
35 |
+
title = "FoodVision Mini😊"
|
36 |
+
description = "An [EffNetB2 feature extractor](https://pytorch.org/vision/main/models/generated/torchvision.models.efficientnet_b2.html) computer vision model to classify images as pizza, steak and sushi"
|
37 |
+
article = "Create at [09. PyTorch Model Deployment](http://keivanjamali.com)."
|
38 |
+
|
39 |
+
demo = gr.Interface(fn=predict,
|
40 |
+
inputs=gr.Image(type="pil"),
|
41 |
+
outputs=[gr.Label(num_top_classes=3, label="Predictions"),
|
42 |
+
gr.Number(label="Prediction Time (s)")],
|
43 |
+
examples=example_list,
|
44 |
+
title=title,
|
45 |
+
description=description,
|
46 |
+
article=article)
|
47 |
+
|
48 |
+
demo.launch(debug=False) # Don't need share
|