DawnC commited on
Commit
dfeffc9
·
1 Parent(s): 0972466

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -7
app.py CHANGED
@@ -364,8 +364,8 @@ async def predict(image):
364
  font = ImageFont.load_default()
365
 
366
  dogs_info = []
367
- buttons = []
368
-
369
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
370
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
371
  color = color_list[i % len(color_list)]
@@ -390,11 +390,12 @@ async def predict(image):
390
  dog_info.append(f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>")
391
  dog_info.append("</ul>")
392
 
 
393
  dog_info.append('<div class="breed-buttons">')
394
  for breed in topk_breeds[:3]:
395
  button_id = f"Dog {i+1}: More about {breed}"
396
  dog_info.append(f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>')
397
- buttons.append(button_id)
398
  dog_info.append('</div>')
399
 
400
  else:
@@ -403,6 +404,8 @@ async def predict(image):
403
  dog_info.append('</div>')
404
  dogs_info.append(''.join(dog_info))
405
 
 
 
406
  html_output = f"""
407
  <style>
408
  .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); }}
@@ -413,7 +416,7 @@ async def predict(image):
413
  {''.join(dogs_info)}
414
  """
415
 
416
- if buttons:
417
  html_output += """
418
  <script>
419
  function handle_button_click(button_id) {
@@ -427,14 +430,25 @@ async def predict(image):
427
  </script>
428
  """
429
  initial_state = {
430
- "dogs_info": dogs_info,
431
- "buttons": buttons,
432
  "show_back": True,
433
  "image": annotated_image,
434
  "is_multi_dog": len(dogs) > 1,
435
  "html_output": html_output
436
  }
437
- return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
 
 
 
 
 
 
 
 
 
 
 
438
  else:
439
  initial_state = {
440
  "dogs_info": dogs_info,
 
364
  font = ImageFont.load_default()
365
 
366
  dogs_info = []
367
+ all_buttons = []
368
+
369
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
370
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
371
  color = color_list[i % len(color_list)]
 
390
  dog_info.append(f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>")
391
  dog_info.append("</ul>")
392
 
393
+ # 將按鈕直接添加到每個狗狗的資訊中
394
  dog_info.append('<div class="breed-buttons">')
395
  for breed in topk_breeds[:3]:
396
  button_id = f"Dog {i+1}: More about {breed}"
397
  dog_info.append(f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>')
398
+ all_buttons.append(button_id)
399
  dog_info.append('</div>')
400
 
401
  else:
 
404
  dog_info.append('</div>')
405
  dogs_info.append(''.join(dog_info))
406
 
407
+ # 刪除:移除了在這裡生成重複品種名稱的代碼
408
+
409
  html_output = f"""
410
  <style>
411
  .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); }}
 
416
  {''.join(dogs_info)}
417
  """
418
 
419
+ if all_buttons:
420
  html_output += """
421
  <script>
422
  function handle_button_click(button_id) {
 
430
  </script>
431
  """
432
  initial_state = {
433
+ "dogs_info": ''.join(dogs_info),
434
+ "buttons": all_buttons,
435
  "show_back": True,
436
  "image": annotated_image,
437
  "is_multi_dog": len(dogs) > 1,
438
  "html_output": html_output
439
  }
440
+ return html_output, annotated_image, gr.update(visible=True, choices=all_buttons), initial_state
441
+ else:
442
+ initial_state = {
443
+ "dogs_info": ''.join(dogs_info),
444
+ "buttons": [],
445
+ "show_back": False,
446
+ "image": annotated_image,
447
+ "is_multi_dog": len(dogs) > 1,
448
+ "html_output": html_output
449
+ }
450
+ return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
451
+
452
  else:
453
  initial_state = {
454
  "dogs_info": dogs_info,