Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -468,7 +468,7 @@ async def predict(image):
|
|
468 |
|
469 |
dogs_info = ""
|
470 |
buttons = []
|
471 |
-
|
472 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
473 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
474 |
color = color_list[i % len(color_list)]
|
@@ -488,20 +488,19 @@ async def predict(image):
|
|
488 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
489 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
490 |
prob = float(prob.replace('%', ''))
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
dogs_info += "</ul>"
|
498 |
|
499 |
else:
|
500 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
501 |
|
502 |
dogs_info += '</div>'
|
503 |
-
|
504 |
-
# ็ๆ JavaScript ่็ๆ้้ปๆไบไปถ
|
505 |
html_output = f"""
|
506 |
<style>
|
507 |
.dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }}
|
@@ -513,29 +512,26 @@ async def predict(image):
|
|
513 |
{dogs_info}
|
514 |
|
515 |
<script>
|
516 |
-
|
517 |
-
document.querySelectorAll('button[id^="Dog"]').forEach(button => {{
|
518 |
button.addEventListener('click', () => {{
|
519 |
-
const
|
520 |
-
const
|
521 |
-
if (
|
522 |
-
|
523 |
-
console.log('Button clicked: ' + button_id);
|
524 |
-
}} else {{
|
525 |
-
console.error("Radio button not found:", button_id);
|
526 |
}}
|
527 |
}});
|
528 |
}});
|
529 |
</script>
|
530 |
"""
|
531 |
-
|
532 |
initial_state = {
|
533 |
"dogs_info": dogs_info,
|
534 |
"buttons": buttons,
|
535 |
"image": annotated_image
|
536 |
}
|
537 |
|
538 |
-
return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
|
|
539 |
|
540 |
except Exception as e:
|
541 |
error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
@@ -604,7 +600,11 @@ with gr.Blocks() as iface:
|
|
604 |
|
605 |
output = gr.HTML(label="Prediction Results")
|
606 |
|
607 |
-
breed_buttons = gr.Radio(
|
|
|
|
|
|
|
|
|
608 |
|
609 |
back_button = gr.Button("Back", visible=False)
|
610 |
|
@@ -635,5 +635,6 @@ with gr.Blocks() as iface:
|
|
635 |
|
636 |
gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
|
637 |
|
|
|
638 |
if __name__ == "__main__":
|
639 |
-
iface.launch()
|
|
|
468 |
|
469 |
dogs_info = ""
|
470 |
buttons = []
|
471 |
+
|
472 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
473 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
474 |
color = color_list[i % len(color_list)]
|
|
|
488 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
489 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
490 |
prob = float(prob.replace('%', ''))
|
491 |
+
button_id = f"learn-more-{i}-{j}"
|
492 |
+
buttons.append({"id": button_id, "text": f"Dog {i+1}: More about {breed}"})
|
493 |
+
dogs_info += f"""
|
494 |
+
<li><strong>{breed}</strong> ({prob:.2f}% confidence)
|
495 |
+
<button class="breed-button" id="{button_id}">Learn More</button></li>
|
496 |
+
"""
|
497 |
dogs_info += "</ul>"
|
498 |
|
499 |
else:
|
500 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
501 |
|
502 |
dogs_info += '</div>'
|
503 |
+
|
|
|
504 |
html_output = f"""
|
505 |
<style>
|
506 |
.dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }}
|
|
|
512 |
{dogs_info}
|
513 |
|
514 |
<script>
|
515 |
+
document.querySelectorAll('.breed-button').forEach(button => {{
|
|
|
516 |
button.addEventListener('click', () => {{
|
517 |
+
const buttonId = button.id;
|
518 |
+
const radioButton = document.querySelector(`input[type="radio"][data-button-id="${{buttonId}}"]`);
|
519 |
+
if (radioButton) {{
|
520 |
+
radioButton.click();
|
|
|
|
|
|
|
521 |
}}
|
522 |
}});
|
523 |
}});
|
524 |
</script>
|
525 |
"""
|
526 |
+
|
527 |
initial_state = {
|
528 |
"dogs_info": dogs_info,
|
529 |
"buttons": buttons,
|
530 |
"image": annotated_image
|
531 |
}
|
532 |
|
533 |
+
return html_output, annotated_image, gr.update(visible=True, choices=[button["text"] for button in buttons]), initial_state
|
534 |
+
|
535 |
|
536 |
except Exception as e:
|
537 |
error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
|
|
600 |
|
601 |
output = gr.HTML(label="Prediction Results")
|
602 |
|
603 |
+
breed_buttons = gr.Radio(
|
604 |
+
choices=[],
|
605 |
+
label="More Information",
|
606 |
+
visible=False
|
607 |
+
)
|
608 |
|
609 |
back_button = gr.Button("Back", visible=False)
|
610 |
|
|
|
635 |
|
636 |
gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
|
637 |
|
638 |
+
# Launch the interface
|
639 |
if __name__ == "__main__":
|
640 |
+
iface.launch()
|