Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -451,6 +451,9 @@ async def process_single_dog(image):
|
|
451 |
|
452 |
|
453 |
|
|
|
|
|
|
|
454 |
async def predict(image):
|
455 |
if image is None:
|
456 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
@@ -467,8 +470,8 @@ async def predict(image):
|
|
467 |
font = ImageFont.load_default()
|
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,19 +491,21 @@ 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 |
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,26 +517,29 @@ async def predict(image):
|
|
512 |
{dogs_info}
|
513 |
|
514 |
<script>
|
515 |
-
|
|
|
516 |
button.addEventListener('click', () => {{
|
517 |
-
const
|
518 |
-
const
|
519 |
-
if (
|
520 |
-
|
|
|
|
|
|
|
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=
|
534 |
-
|
535 |
|
536 |
except Exception as e:
|
537 |
error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
@@ -600,11 +608,7 @@ with gr.Blocks() as iface:
|
|
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,6 +639,5 @@ 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 |
-
# Launch the interface
|
639 |
if __name__ == "__main__":
|
640 |
-
iface.launch()
|
|
|
451 |
|
452 |
|
453 |
|
454 |
+
import gradio as gr
|
455 |
+
from PIL import Image, ImageDraw, ImageFont
|
456 |
+
|
457 |
async def predict(image):
|
458 |
if image is None:
|
459 |
return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
|
|
|
470 |
font = ImageFont.load_default()
|
471 |
|
472 |
dogs_info = ""
|
473 |
+
buttons = [] # ๅฒๅญๅฏไธ็ๆ้ ID
|
474 |
+
|
475 |
for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
|
476 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
477 |
color = color_list[i % len(color_list)]
|
|
|
491 |
dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
|
492 |
for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
|
493 |
prob = float(prob.replace('%', ''))
|
494 |
+
|
495 |
+
# ็บๆฏๅๅ็จฎ็ๆๅฏไธ็ button_id๏ผไธฆ้ๅ Learn More ๆ้
|
496 |
+
button_id = f"Dog {i+1}: More about {breed}"
|
497 |
+
if button_id not in buttons:
|
498 |
+
buttons.append(button_id) # ็ขบไฟไธ้่คๆทปๅ ๆ้
|
499 |
+
dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)"
|
500 |
+
dogs_info += f'<button class="breed-button" id="{button_id}" style="background-color: #4CAF50; color: white; border: none; padding: 5px 10px; border-radius: 3px; margin-left: 10px;" onclick="handle_button_click(\'{button_id}\')">Learn More</button></li>'
|
501 |
dogs_info += "</ul>"
|
502 |
|
503 |
else:
|
504 |
dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
|
505 |
|
506 |
dogs_info += '</div>'
|
507 |
+
|
508 |
+
# ็ๆ JavaScript ่็ๆ้้ปๆไบไปถ
|
509 |
html_output = f"""
|
510 |
<style>
|
511 |
.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); }}
|
|
|
517 |
{dogs_info}
|
518 |
|
519 |
<script>
|
520 |
+
// ็บๆๆ็ๆ็ๆ้้ๅ ไบไปถ็ฃ่ฝๅจ
|
521 |
+
document.querySelectorAll('button[id^="Dog"]').forEach(button => {{
|
522 |
button.addEventListener('click', () => {{
|
523 |
+
const button_id = button.id;
|
524 |
+
const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
|
525 |
+
if (radio) {{
|
526 |
+
radio.click(); // ่งธ็ผ Radio ๆ้้ธๆ
|
527 |
+
console.log('Button clicked: ' + button_id);
|
528 |
+
}} else {{
|
529 |
+
console.error("Radio button not found:", button_id);
|
530 |
}}
|
531 |
}});
|
532 |
}});
|
533 |
</script>
|
534 |
"""
|
535 |
+
|
536 |
initial_state = {
|
537 |
"dogs_info": dogs_info,
|
538 |
"buttons": buttons,
|
539 |
"image": annotated_image
|
540 |
}
|
541 |
|
542 |
+
return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
|
|
|
543 |
|
544 |
except Exception as e:
|
545 |
error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
|
|
608 |
|
609 |
output = gr.HTML(label="Prediction Results")
|
610 |
|
611 |
+
breed_buttons = gr.Radio(choices=[], label="More Information", visible=False)
|
|
|
|
|
|
|
|
|
612 |
|
613 |
back_button = gr.Button("Back", visible=False)
|
614 |
|
|
|
639 |
|
640 |
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>')
|
641 |
|
|
|
642 |
if __name__ == "__main__":
|
643 |
+
iface.launch()
|