Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -209,69 +209,69 @@ def predict(image):
|
|
209 |
image_tensor = preprocess_image(image)
|
210 |
with torch.no_grad():
|
211 |
output = model(image_tensor)
|
212 |
-
if isinstance(output, tuple)
|
213 |
-
logits = output[0]
|
214 |
-
else:
|
215 |
-
logits = output
|
216 |
|
217 |
-
# 取得預測的top k結果
|
218 |
probabilities = F.softmax(logits, dim=1)
|
219 |
topk_probs, topk_indices = torch.topk(probabilities, k=3)
|
220 |
|
221 |
-
# 檢查最高的預測機率
|
222 |
top1_prob = topk_probs[0][0].item()
|
|
|
|
|
223 |
|
224 |
if top1_prob >= 0.5:
|
225 |
-
#
|
226 |
-
|
227 |
-
breed = dog_breeds[predicted.item()]
|
228 |
description = get_dog_description(breed)
|
229 |
-
akc_link = get_akc_breeds_link()
|
230 |
-
|
231 |
if isinstance(description, dict):
|
232 |
-
|
233 |
else:
|
234 |
-
|
235 |
|
236 |
-
|
237 |
-
|
238 |
|
239 |
-
# 添加免責聲明
|
240 |
disclaimer = ("\n\n*Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page. "
|
241 |
"You may need to search for the specific breed on that page. "
|
242 |
"I am not responsible for the content on external sites. "
|
243 |
"Please refer to the AKC's terms of use and privacy policy.*")
|
244 |
-
|
245 |
|
246 |
-
return
|
247 |
|
248 |
elif top1_prob < 0.1:
|
249 |
-
#
|
250 |
-
return "The
|
251 |
-
|
252 |
else:
|
253 |
-
#
|
254 |
-
|
255 |
-
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
256 |
-
|
257 |
-
# 用粗體返回品種和機率,並為每個品種添加點擊連結
|
258 |
-
topk_results = "\n\n".join([
|
259 |
-
f"**{i+1}. [Click here to view more about {breed}]()** ({prob} confidence)"
|
260 |
-
for i, (breed, prob) in enumerate(zip(topk_breeds, topk_probs_percent))
|
261 |
-
])
|
262 |
-
|
263 |
-
# 提供說明
|
264 |
explanation = (
|
265 |
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n{topk_results}\n\n"
|
266 |
-
"
|
267 |
-
"Please try uploading a clearer image or a different angle of the dog. "
|
268 |
-
"For more accurate results, ensure the dog is the main subject of the photo."
|
269 |
)
|
|
|
270 |
|
271 |
-
return explanation
|
272 |
except Exception as e:
|
273 |
return f"An error occurred: {e}"
|
274 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
275 |
iface = gr.Interface(
|
276 |
fn=predict,
|
277 |
inputs=gr.Image(label="Upload a dog image", type="numpy"),
|
|
|
209 |
image_tensor = preprocess_image(image)
|
210 |
with torch.no_grad():
|
211 |
output = model(image_tensor)
|
212 |
+
logits = output[0] if isinstance(output, tuple) else output
|
|
|
|
|
|
|
213 |
|
|
|
214 |
probabilities = F.softmax(logits, dim=1)
|
215 |
topk_probs, topk_indices = torch.topk(probabilities, k=3)
|
216 |
|
|
|
217 |
top1_prob = topk_probs[0][0].item()
|
218 |
+
topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
|
219 |
+
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
220 |
|
221 |
if top1_prob >= 0.5:
|
222 |
+
# High confidence prediction
|
223 |
+
breed = topk_breeds[0]
|
|
|
224 |
description = get_dog_description(breed)
|
|
|
|
|
225 |
if isinstance(description, dict):
|
226 |
+
formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
227 |
else:
|
228 |
+
formatted_description = description
|
229 |
|
230 |
+
akc_link = get_akc_breeds_link()
|
231 |
+
formatted_description += 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."
|
232 |
|
|
|
233 |
disclaimer = ("\n\n*Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page. "
|
234 |
"You may need to search for the specific breed on that page. "
|
235 |
"I am not responsible for the content on external sites. "
|
236 |
"Please refer to the AKC's terms of use and privacy policy.*")
|
237 |
+
formatted_description += disclaimer
|
238 |
|
239 |
+
return formatted_description
|
240 |
|
241 |
elif top1_prob < 0.1:
|
242 |
+
# Very low confidence prediction
|
243 |
+
return ("The model couldn't confidently identify the breed. The image might not be clear enough or might not contain a dog. "
|
244 |
+
"Please try uploading a clearer image of a dog.")
|
245 |
else:
|
246 |
+
# Medium confidence prediction
|
247 |
+
topk_results = "\n\n".join([f"**{i+1}. {breed}** (Confidence: {prob})" for i, (breed, prob) in enumerate(zip(topk_breeds, topk_probs_percent))])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
248 |
explanation = (
|
249 |
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n{topk_results}\n\n"
|
250 |
+
"Please select one of the breeds above to see more information:"
|
|
|
|
|
251 |
)
|
252 |
+
return explanation, gr.update(choices=topk_breeds, visible=True)
|
253 |
|
|
|
254 |
except Exception as e:
|
255 |
return f"An error occurred: {e}"
|
256 |
|
257 |
+
def show_breed_info(breed):
|
258 |
+
description = get_dog_description(breed)
|
259 |
+
if isinstance(description, dict):
|
260 |
+
formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
261 |
+
else:
|
262 |
+
formatted_description = description
|
263 |
+
|
264 |
+
akc_link = get_akc_breeds_link()
|
265 |
+
formatted_description += 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."
|
266 |
+
|
267 |
+
disclaimer = ("\n\n*Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page. "
|
268 |
+
"You may need to search for the specific breed on that page. "
|
269 |
+
"I am not responsible for the content on external sites. "
|
270 |
+
"Please refer to the AKC's terms of use and privacy policy.*")
|
271 |
+
formatted_description += disclaimer
|
272 |
+
|
273 |
+
return formatted_description
|
274 |
+
|
275 |
iface = gr.Interface(
|
276 |
fn=predict,
|
277 |
inputs=gr.Image(label="Upload a dog image", type="numpy"),
|