Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -143,6 +143,67 @@ def preprocess_image(image):
|
|
143 |
def get_akc_breeds_link():
|
144 |
return "https://www.akc.org/dog-breeds/"
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
def predict(image):
|
147 |
try:
|
148 |
image_tensor = preprocess_image(image)
|
@@ -160,8 +221,16 @@ def predict(image):
|
|
160 |
# 檢查最高的預測機率
|
161 |
top1_prob = topk_probs[0][0].item()
|
162 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
if top1_prob >= 0.5:
|
164 |
-
# 正確辨識時,返回該品種資訊
|
165 |
predicted = topk_indices[0][0]
|
166 |
breed = dog_breeds[predicted.item()]
|
167 |
description = get_dog_description(breed)
|
@@ -185,12 +254,15 @@ def predict(image):
|
|
185 |
return description_str
|
186 |
|
187 |
else:
|
188 |
-
#
|
189 |
topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
|
190 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
191 |
|
192 |
-
#
|
193 |
-
topk_results = "\n\n".join(
|
|
|
|
|
|
|
194 |
|
195 |
# 提���說明
|
196 |
explanation = (
|
|
|
143 |
def get_akc_breeds_link():
|
144 |
return "https://www.akc.org/dog-breeds/"
|
145 |
|
146 |
+
# def predict(image):
|
147 |
+
# try:
|
148 |
+
# image_tensor = preprocess_image(image)
|
149 |
+
# with torch.no_grad():
|
150 |
+
# output = model(image_tensor)
|
151 |
+
# if isinstance(output, tuple):
|
152 |
+
# logits = output[0]
|
153 |
+
# else:
|
154 |
+
# logits = output
|
155 |
+
|
156 |
+
# # 取得預測的top k結果
|
157 |
+
# probabilities = F.softmax(logits, dim=1)
|
158 |
+
# topk_probs, topk_indices = torch.topk(probabilities, k=3)
|
159 |
+
|
160 |
+
# # 檢查最高的預測機率
|
161 |
+
# top1_prob = topk_probs[0][0].item()
|
162 |
+
|
163 |
+
# if top1_prob >= 0.5:
|
164 |
+
# # 正確辨識時,返回該品種資訊
|
165 |
+
# predicted = topk_indices[0][0]
|
166 |
+
# breed = dog_breeds[predicted.item()]
|
167 |
+
# description = get_dog_description(breed)
|
168 |
+
# akc_link = get_akc_breeds_link()
|
169 |
+
|
170 |
+
# if isinstance(description, dict):
|
171 |
+
# description_str = "\n\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
172 |
+
# else:
|
173 |
+
# description_str = description
|
174 |
+
|
175 |
+
# # 添加AKC連結
|
176 |
+
# description_str += f"\n\n**Want to learn more about dog breeds?** [Visit the AKC dog breeds page]({akc_link}) and search for {breed} to find detailed information."
|
177 |
+
|
178 |
+
# # 添加免責聲明
|
179 |
+
# disclaimer = ("\n\n*Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page. "
|
180 |
+
# "You may need to search for the specific breed on that page. "
|
181 |
+
# "I am not responsible for the content on external sites. "
|
182 |
+
# "Please refer to the AKC's terms of use and privacy policy.*")
|
183 |
+
# description_str += disclaimer
|
184 |
+
|
185 |
+
# return description_str
|
186 |
+
|
187 |
+
# else:
|
188 |
+
# # 不確定時,返回top 3的預測結果
|
189 |
+
# topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
|
190 |
+
# topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
191 |
+
|
192 |
+
# # 用粗體返回品種和機率
|
193 |
+
# topk_results = "\n\n".join([f"**{i+1}. {breed}** ({prob} confidence)" for i, (breed, prob) in enumerate(zip(topk_breeds, topk_probs_percent))])
|
194 |
+
|
195 |
+
# # 提供說明
|
196 |
+
# explanation = (
|
197 |
+
# f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n{topk_results}\n\n"
|
198 |
+
# "This can happen if the image quality is low or the breed is rare in the dataset. "
|
199 |
+
# "Please try uploading a clearer image or a different angle of the dog. "
|
200 |
+
# "For more accurate results, ensure the dog is the main subject of the photo."
|
201 |
+
# )
|
202 |
+
|
203 |
+
# return explanation
|
204 |
+
# except Exception as e:
|
205 |
+
# return f"An error occurred: {e}"
|
206 |
+
|
207 |
def predict(image):
|
208 |
try:
|
209 |
image_tensor = preprocess_image(image)
|
|
|
221 |
# 檢查最高的預測機率
|
222 |
top1_prob = topk_probs[0][0].item()
|
223 |
|
224 |
+
# 假設低於 20% 機率為非狗或不確定的圖片
|
225 |
+
if top1_prob < 0.2:
|
226 |
+
return (
|
227 |
+
"The model couldn't confidently identify a dog breed. "
|
228 |
+
"It seems like the image may not contain a dog, or the image quality is too low. "
|
229 |
+
"Please upload a clearer picture or ensure the subject is a dog."
|
230 |
+
)
|
231 |
+
|
232 |
+
# 當信心高於 50% 時,直接返回該品種資訊
|
233 |
if top1_prob >= 0.5:
|
|
|
234 |
predicted = topk_indices[0][0]
|
235 |
breed = dog_breeds[predicted.item()]
|
236 |
description = get_dog_description(breed)
|
|
|
254 |
return description_str
|
255 |
|
256 |
else:
|
257 |
+
# 信心不足50%,返回top 3的預測結果並附加連結
|
258 |
topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
|
259 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
260 |
|
261 |
+
# 構建每個品種的連結和預測機率
|
262 |
+
topk_results = "\n\n".join(
|
263 |
+
[f"**{i+1}. [{breed}](https://www.akc.org/dog-breeds/{quote(breed)})** ({prob} confidence)"
|
264 |
+
for i, (breed, prob) in enumerate(zip(topk_breeds, topk_probs_percent))]
|
265 |
+
)
|
266 |
|
267 |
# 提���說明
|
268 |
explanation = (
|