DawnC commited on
Commit
1101544
·
1 Parent(s): 5d32c8a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -19
app.py CHANGED
@@ -366,8 +366,7 @@ async def predict(image):
366
  dogs_info = ""
367
 
368
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
369
- # 每個循環初始化 buttons_html,確保按鈕和品種不重複
370
- buttons_html = ""
371
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
372
  color = color_list[i % len(color_list)]
373
  draw.rectangle(box, outline=color, width=3)
@@ -382,37 +381,28 @@ async def predict(image):
382
  description = get_dog_description(breed)
383
  dogs_info += format_description_html(description, breed)
384
 
385
- # 放入於每個品種敘述區塊中,直接附加 buttons_html
386
  elif combined_confidence >= 0.15:
387
  dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
388
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
389
  prob = float(prob.replace('%', ''))
390
  dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
391
  dogs_info += "</ul>"
392
-
393
- # Append buttons directly into this dog's section
394
- buttons_html = ""
395
- for breed in topk_breeds[:3]:
396
- button_id = f"Dog {i+1}: More about {breed}"
397
- buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
398
- buttons.append(button_id)
399
-
400
- # 把 buttons_html 放入這個特定狗的區塊
401
- dogs_info += buttons_html
402
 
403
- # 在品種的描述下插入對應按鈕
 
404
  for breed in topk_breeds[:3]:
405
  button_id = f"Dog {i+1}: More about {breed}"
406
  buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
407
  buttons.append(button_id)
408
-
409
- dogs_info += buttons_html # 將按鈕直接插入當前區塊
410
-
411
  else:
412
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
413
 
414
- dogs_info += '</div>'
415
 
 
416
  html_output = f"""
417
  <style>
418
  .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,7 +412,7 @@ async def predict(image):
422
  </style>
423
  {dogs_info}
424
  """
425
-
426
  if buttons:
427
  html_output += """
428
  <script>
 
366
  dogs_info = ""
367
 
368
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
369
+ buttons_html = "" # 保證每個品種都單獨生成按鈕
 
370
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
371
  color = color_list[i % len(color_list)]
372
  draw.rectangle(box, outline=color, width=3)
 
381
  description = get_dog_description(breed)
382
  dogs_info += format_description_html(description, breed)
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
  dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
389
  dogs_info += "</ul>"
 
 
 
 
 
 
 
 
 
 
390
 
391
+ # 在該品種下生成按鈕
392
+ buttons_html = '<div class="breed-buttons">' # 新增按鈕容器
393
  for breed in topk_breeds[:3]:
394
  button_id = f"Dog {i+1}: More about {breed}"
395
  buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
396
  buttons.append(button_id)
397
+ buttons_html += '</div>' # 結束按鈕容器
398
+ dogs_info += buttons_html # 將按鈕添加到品種描述中
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>"
402
 
403
+ dogs_info += '</div>' # 結束該品種區塊
404
 
405
+ # 不要在此重新加上 buttons_html,確保按鈕在品種下方顯示
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); }}
 
412
  </style>
413
  {dogs_info}
414
  """
415
+
416
  if buttons:
417
  html_output += """
418
  <script>