DawnC commited on
Commit
7b60fa3
·
1 Parent(s): 6bc0bd8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +6 -33
app.py CHANGED
@@ -359,7 +359,7 @@ async def predict(image):
359
  dogs = await detect_multiple_dogs(image)
360
 
361
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
362
- buttons = []
363
  annotated_image = image.copy()
364
  draw = ImageDraw.Draw(annotated_image)
365
  font = ImageFont.load_default()
@@ -383,6 +383,7 @@ async def predict(image):
383
 
384
  elif combined_confidence >= 0.15:
385
  dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
 
386
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
387
  prob = float(prob.replace('%', ''))
388
  button_id = f"Dog {i+1}: More about {breed}"
@@ -392,8 +393,9 @@ async def predict(image):
392
  <button class="breed-button" onclick="handle_button_click('{button_id}')">{breed}</button>
393
  </li>
394
  """
395
- buttons.append(button_id)
396
  dogs_info += "</ul>"
 
397
 
398
  else:
399
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
@@ -421,13 +423,13 @@ async def predict(image):
421
 
422
  initial_state = {
423
  "dogs_info": dogs_info,
424
- "buttons": buttons,
425
  "show_back": True,
426
  "image": annotated_image,
427
  "is_multi_dog": len(dogs) > 1,
428
  "html_output": html_output
429
  }
430
- return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
431
 
432
  except Exception as e:
433
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
@@ -440,7 +442,6 @@ def show_details_html(choice, previous_output, initial_state):
440
  return previous_output, gr.update(visible=True), initial_state
441
 
442
  try:
443
- # Extract the breed name from the choice string
444
  breed = choice.split("More about ")[-1]
445
  description = get_dog_description(breed)
446
  formatted_description = format_description_html(description, breed)
@@ -461,34 +462,6 @@ def show_details_html(choice, previous_output, initial_state):
461
  print(error_msg)
462
  return f"<p style='color: red;'>{error_msg}</p>", gr.update(visible=True), initial_state
463
 
464
-
465
-
466
-
467
- # def show_details_html(choice, previous_output, initial_state):
468
- # if not choice:
469
- # return previous_output, gr.update(visible=True), initial_state
470
-
471
- # try:
472
- # breed = choice.split("More about ")[-1]
473
- # description = get_dog_description(breed)
474
- # formatted_description = format_description_html(description, breed)
475
-
476
- # html_output = f"""
477
- # <div class="dog-info">
478
- # <h2>{breed}</h2>
479
- # {formatted_description}
480
- # </div>
481
- # """
482
-
483
- # initial_state["current_description"] = html_output
484
- # initial_state["original_buttons"] = initial_state.get("buttons", [])
485
-
486
- # return html_output, gr.update(visible=True), initial_state
487
- # except Exception as e:
488
- # error_msg = f"An error occurred while showing details: {e}"
489
- # print(error_msg)
490
- # return f"<p style='color: red;'>{error_msg}</p>", gr.update(visible=True), initial_state
491
-
492
 
493
  def format_description_html(description, breed):
494
  html = "<ul style='list-style-type: none; padding-left: 0;'>"
 
359
  dogs = await detect_multiple_dogs(image)
360
 
361
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
362
+ all_buttons = []
363
  annotated_image = image.copy()
364
  draw = ImageDraw.Draw(annotated_image)
365
  font = ImageFont.load_default()
 
383
 
384
  elif combined_confidence >= 0.15:
385
  dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
386
+ dog_buttons = [] # Reset buttons for each dog
387
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
388
  prob = float(prob.replace('%', ''))
389
  button_id = f"Dog {i+1}: More about {breed}"
 
393
  <button class="breed-button" onclick="handle_button_click('{button_id}')">{breed}</button>
394
  </li>
395
  """
396
+ dog_buttons.append(button_id)
397
  dogs_info += "</ul>"
398
+ all_buttons.extend(dog_buttons) # Add this dog's buttons to the overall list
399
 
400
  else:
401
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
 
423
 
424
  initial_state = {
425
  "dogs_info": dogs_info,
426
+ "buttons": all_buttons,
427
  "show_back": True,
428
  "image": annotated_image,
429
  "is_multi_dog": len(dogs) > 1,
430
  "html_output": html_output
431
  }
432
+ return html_output, annotated_image, gr.update(visible=True, choices=all_buttons), initial_state
433
 
434
  except Exception as e:
435
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
 
442
  return previous_output, gr.update(visible=True), initial_state
443
 
444
  try:
 
445
  breed = choice.split("More about ")[-1]
446
  description = get_dog_description(breed)
447
  formatted_description = format_description_html(description, breed)
 
462
  print(error_msg)
463
  return f"<p style='color: red;'>{error_msg}</p>", gr.update(visible=True), initial_state
464
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
465
 
466
  def format_description_html(description, breed):
467
  html = "<ul style='list-style-type: none; padding-left: 0;'>"