DawnC commited on
Commit
a5dfa51
·
1 Parent(s): 319d08b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -5
app.py CHANGED
@@ -416,7 +416,14 @@ async def predict(image):
416
  dogs = await detect_multiple_dogs(image)
417
 
418
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
419
- html_output = "<style>.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);}.dog-info h2{background-color:#f0f0f0;padding:10px;margin:-15px -15px 15px -15px;border-radius:5px 5px 0 0;}.breed-buttons{margin-top:10px;}</style>"
 
 
 
 
 
 
 
420
  buttons = []
421
  annotated_image = image.copy()
422
  draw = ImageDraw.Draw(annotated_image)
@@ -436,6 +443,8 @@ async def predict(image):
436
  breed = topk_breeds[0]
437
  description = get_dog_description(breed)
438
  html_output += format_description_html(description, breed)
 
 
439
  elif combined_confidence >= 0.15:
440
  html_output += f"<p>Top 3 possible breeds:</p><ul>"
441
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
@@ -444,7 +453,7 @@ async def predict(image):
444
  html_output += '<div class="breed-buttons">'
445
  for breed in topk_breeds[:3]:
446
  button_id = f"Dog {i+1}: More about {breed}"
447
- html_output += f'<button onclick="handle_button_click(\'{button_id}\')">{button_id}</button>'
448
  buttons.append(button_id)
449
  html_output += '</div>'
450
  else:
@@ -453,7 +462,13 @@ async def predict(image):
453
  html_output += '</div>'
454
 
455
  if buttons:
456
- html_output += "<script>function handle_button_click(button_id) { document.querySelector('input[type=radio][value=\"' + button_id + '\"]').click(); }</script>"
 
 
 
 
 
 
457
  initial_state = {
458
  "explanation": html_output,
459
  "buttons": buttons,
@@ -506,8 +521,11 @@ def show_details_html(choice, previous_output, initial_state):
506
 
507
  def format_description_html(description, breed):
508
  html = "<ul>"
509
- for key, value in description.items():
510
- html += f"<li><strong>{key}:</strong> {value}</li>"
 
 
 
511
  html += "</ul>"
512
  akc_link = get_akc_breeds_link()
513
  html += f'<p><a href="{akc_link}" target="_blank">Learn more about {breed} on the AKC website</a></p>'
 
416
  dogs = await detect_multiple_dogs(image)
417
 
418
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
419
+ html_output = """
420
+ <style>
421
+ .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); }
422
+ .dog-info h2 { background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }
423
+ .breed-buttons { margin-top: 10px; }
424
+ .breed-button { margin-right: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }
425
+ </style>
426
+ """
427
  buttons = []
428
  annotated_image = image.copy()
429
  draw = ImageDraw.Draw(annotated_image)
 
443
  breed = topk_breeds[0]
444
  description = get_dog_description(breed)
445
  html_output += format_description_html(description, breed)
446
+ html_output += f'<div class="breed-buttons"><button class="breed-button" onclick="handle_button_click(\'Dog {i+1}: More about {breed}\')">More about {breed}</button></div>'
447
+ buttons.append(f"Dog {i+1}: More about {breed}")
448
  elif combined_confidence >= 0.15:
449
  html_output += f"<p>Top 3 possible breeds:</p><ul>"
450
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
 
453
  html_output += '<div class="breed-buttons">'
454
  for breed in topk_breeds[:3]:
455
  button_id = f"Dog {i+1}: More about {breed}"
456
+ html_output += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
457
  buttons.append(button_id)
458
  html_output += '</div>'
459
  else:
 
462
  html_output += '</div>'
463
 
464
  if buttons:
465
+ html_output += """
466
+ <script>
467
+ function handle_button_click(button_id) {
468
+ document.querySelector('input[type=radio][value="' + button_id + '"]').click();
469
+ }
470
+ </script>
471
+ """
472
  initial_state = {
473
  "explanation": html_output,
474
  "buttons": buttons,
 
521
 
522
  def format_description_html(description, breed):
523
  html = "<ul>"
524
+ if isinstance(description, dict):
525
+ for key, value in description.items():
526
+ html += f"<li><strong>{key}:</strong> {value}</li>"
527
+ else:
528
+ html += f"<li>{description}</li>"
529
  html += "</ul>"
530
  akc_link = get_akc_breeds_link()
531
  html += f'<p><a href="{akc_link}" target="_blank">Learn more about {breed} on the AKC website</a></p>'