Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -267,6 +267,9 @@ def get_akc_breeds_link():
|
|
267 |
# iface.launch()
|
268 |
|
269 |
def predict(image):
|
|
|
|
|
|
|
270 |
try:
|
271 |
image_tensor = preprocess_image(image)
|
272 |
with torch.no_grad():
|
@@ -281,22 +284,19 @@ def predict(image):
|
|
281 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
282 |
|
283 |
if top1_prob >= 0.5:
|
284 |
-
# High confidence prediction
|
285 |
breed = topk_breeds[0]
|
286 |
description = get_dog_description(breed)
|
287 |
return format_description(description, breed), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
288 |
|
289 |
elif top1_prob < 0.1:
|
290 |
-
# Very low confidence prediction
|
291 |
return ("The image is too unclear or the dog breed is not in the dataset. Please upload a clearer image of the dog.",
|
292 |
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False))
|
293 |
else:
|
294 |
-
# Medium confidence prediction
|
295 |
explanation = (
|
296 |
-
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n"
|
297 |
-
f"1. {topk_breeds[0]} ({topk_probs_percent[0]} confidence)\n"
|
298 |
-
f"2. {topk_breeds[1]} ({topk_probs_percent[1]} confidence)\n"
|
299 |
-
f"3. {topk_breeds[2]} ({topk_probs_percent[2]} confidence)\n\n"
|
300 |
"Click on a button to view more information about the breed."
|
301 |
)
|
302 |
return explanation, gr.update(visible=True, value=f"More about {topk_breeds[0]}"), gr.update(visible=True, value=f"More about {topk_breeds[1]}"), gr.update(visible=True, value=f"More about {topk_breeds[2]}")
|
@@ -306,24 +306,25 @@ def predict(image):
|
|
306 |
|
307 |
def format_description(description, breed):
|
308 |
if isinstance(description, dict):
|
309 |
-
|
310 |
else:
|
311 |
-
|
312 |
|
313 |
akc_link = get_akc_breeds_link()
|
314 |
-
|
315 |
|
316 |
disclaimer = ("\n\n*Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page. "
|
317 |
"You may need to search for the specific breed on that page. "
|
318 |
"I am not responsible for the content on external sites. "
|
319 |
"Please refer to the AKC's terms of use and privacy policy.*")
|
320 |
-
|
321 |
|
322 |
-
return
|
323 |
|
324 |
def show_details(breed):
|
325 |
-
|
326 |
-
|
|
|
327 |
|
328 |
with gr.Blocks(css="""
|
329 |
.container {
|
|
|
267 |
# iface.launch()
|
268 |
|
269 |
def predict(image):
|
270 |
+
if image is None:
|
271 |
+
return "Please upload an image to get started.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
272 |
+
|
273 |
try:
|
274 |
image_tensor = preprocess_image(image)
|
275 |
with torch.no_grad():
|
|
|
284 |
topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
285 |
|
286 |
if top1_prob >= 0.5:
|
|
|
287 |
breed = topk_breeds[0]
|
288 |
description = get_dog_description(breed)
|
289 |
return format_description(description, breed), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
290 |
|
291 |
elif top1_prob < 0.1:
|
|
|
292 |
return ("The image is too unclear or the dog breed is not in the dataset. Please upload a clearer image of the dog.",
|
293 |
gr.update(visible=False), gr.update(visible=False), gr.update(visible=False))
|
294 |
else:
|
|
|
295 |
explanation = (
|
296 |
+
f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
297 |
+
f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
298 |
+
f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
299 |
+
f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
300 |
"Click on a button to view more information about the breed."
|
301 |
)
|
302 |
return explanation, gr.update(visible=True, value=f"More about {topk_breeds[0]}"), gr.update(visible=True, value=f"More about {topk_breeds[1]}"), gr.update(visible=True, value=f"More about {topk_breeds[2]}")
|
|
|
306 |
|
307 |
def format_description(description, breed):
|
308 |
if isinstance(description, dict):
|
309 |
+
formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
310 |
else:
|
311 |
+
formatted_description = description
|
312 |
|
313 |
akc_link = get_akc_breeds_link()
|
314 |
+
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."
|
315 |
|
316 |
disclaimer = ("\n\n*Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page. "
|
317 |
"You may need to search for the specific breed on that page. "
|
318 |
"I am not responsible for the content on external sites. "
|
319 |
"Please refer to the AKC's terms of use and privacy policy.*")
|
320 |
+
formatted_description += disclaimer
|
321 |
|
322 |
+
return formatted_description
|
323 |
|
324 |
def show_details(breed):
|
325 |
+
breed_name = breed.split("More about ")[-1]
|
326 |
+
description = get_dog_description(breed_name)
|
327 |
+
return format_description(description, breed_name)
|
328 |
|
329 |
with gr.Blocks(css="""
|
330 |
.container {
|