Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -40,15 +40,15 @@ class_names = ['Abyssinian (阿比西尼亞貓)', 'American Bulldog (美國鬥
|
|
40 |
|
41 |
# 定義預測函數
|
42 |
def classify_image(image):
|
43 |
-
image = transform(image).unsqueeze(0).to(device)
|
44 |
with torch.no_grad():
|
45 |
outputs = model(image)
|
46 |
-
probabilities, indices = torch.topk(outputs, k=3)
|
47 |
-
probabilities = torch.nn.functional.softmax(probabilities, dim=1)
|
48 |
predictions = [(class_names[idx], prob.item()) for idx, prob in zip(indices[0], probabilities[0])]
|
49 |
-
return {class_name: prob for class_name, prob in predictions}
|
50 |
|
51 |
-
#
|
52 |
examples_path = './examples'
|
53 |
|
54 |
if os.path.exists(examples_path):
|
@@ -56,23 +56,21 @@ if os.path.exists(examples_path):
|
|
56 |
else:
|
57 |
print(f"[ERROR] Examples folder not found at {examples_path}")
|
58 |
|
59 |
-
#
|
60 |
examples = [[examples_path + "/" + img] for img in os.listdir(examples_path)]
|
61 |
|
62 |
-
#
|
63 |
-
|
64 |
-
|
65 |
-
demo = gr.Blocks()
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
gr.
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
|
78 |
demo.launch()
|
|
|
40 |
|
41 |
# 定義預測函數
|
42 |
def classify_image(image):
|
43 |
+
image = transform(image).unsqueeze(0).to(device)
|
44 |
with torch.no_grad():
|
45 |
outputs = model(image)
|
46 |
+
probabilities, indices = torch.topk(outputs, k=3)
|
47 |
+
probabilities = torch.nn.functional.softmax(probabilities, dim=1)
|
48 |
predictions = [(class_names[idx], prob.item()) for idx, prob in zip(indices[0], probabilities[0])]
|
49 |
+
return {class_name: f"{prob:.2f}" for class_name, prob in predictions}
|
50 |
|
51 |
+
# 設定 examples 路徑
|
52 |
examples_path = './examples'
|
53 |
|
54 |
if os.path.exists(examples_path):
|
|
|
56 |
else:
|
57 |
print(f"[ERROR] Examples folder not found at {examples_path}")
|
58 |
|
59 |
+
# 設定範例圖片
|
60 |
examples = [[examples_path + "/" + img] for img in os.listdir(examples_path)]
|
61 |
|
62 |
+
# 新增下拉選單,顯示所有品種
|
63 |
+
dropdown = gr.Dropdown(choices=class_names, label="Select a breed", type="value")
|
|
|
|
|
64 |
|
65 |
+
# Gradio 介面
|
66 |
+
demo = gr.Interface(
|
67 |
+
fn=classify_image,
|
68 |
+
inputs=[gr.Image(type="pil")], # 移除掉 dropdown 作為輸入
|
69 |
+
outputs=[gr.Label(num_top_classes=3, label="Top 3 Predictions")],
|
70 |
+
examples=examples,
|
71 |
+
title='Oxford Pet 🐈🐕',
|
72 |
+
description='A ResNet50-based model for classifying 37 different pet breeds.',
|
73 |
+
article='[Oxford Project](https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/The%20Oxford-IIIT%20Pet%20Project)'
|
74 |
+
)
|
75 |
|
76 |
demo.launch()
|