DawnC commited on
Commit
37d85f8
1 Parent(s): 3a0553e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -16
app.py CHANGED
@@ -349,7 +349,7 @@ async def process_single_dog(image):
349
 
350
  async def predict(image):
351
  if image is None:
352
- return "請上傳一張圖片開始。", None, gr.update(visible=False, choices=[]), None
353
 
354
  try:
355
  if isinstance(image, np.ndarray):
@@ -384,29 +384,22 @@ async def predict(image):
384
  dogs_info += f"<p>前3個可能的品種:</p><ul>"
385
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
386
  prob = float(prob.replace('%', ''))
387
- dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% 信心度)</li>"
388
- dogs_info += "</ul>"
389
-
390
- # 修改:直接在每隻狗的資訊下方添加按鈕
391
- dogs_info += '<div class="breed-buttons">'
392
- for breed in topk_breeds[:3]:
393
  button_id = f"狗狗 {i+1}: 更多關於 {breed}"
394
- dogs_info += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
395
  buttons.append(button_id)
396
- dogs_info += '</div>'
397
 
398
  else:
399
- dogs_info += "<p>圖像不清晰或該品種不在數據集中。請上傳一張更清晰的圖片。</p>"
400
 
401
  dogs_info += '</div>'
402
 
403
- # 修改:更新HTML輸出,包含內聯的按鈕和處理點擊的JavaScript
404
  html_output = f"""
405
  <style>
406
  .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); }}
407
  .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
408
- .breed-buttons {{ margin-top: 10px; }}
409
- .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
410
  </style>
411
  {dogs_info}
412
  <script>
@@ -415,7 +408,7 @@ async def predict(image):
415
  if (radio) {{
416
  radio.click();
417
  }} else {{
418
- console.error("找不到對應的單選按鈕:", button_id);
419
  }}
420
  }}
421
  </script>
@@ -429,13 +422,15 @@ async def predict(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=buttons), initial_state
433
 
434
  except Exception as e:
435
- error_msg = f"發生錯誤:{str(e)}\n\n追蹤信息:\n{traceback.format_exc()}"
436
  print(error_msg)
437
  return error_msg, None, gr.update(visible=False, choices=[]), None
438
 
 
 
439
 
440
  def show_details_html(choice, previous_output, initial_state):
441
  if not choice:
 
349
 
350
  async def predict(image):
351
  if image is None:
352
+ return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
353
 
354
  try:
355
  if isinstance(image, np.ndarray):
 
384
  dogs_info += f"<p>前3個可能的品種:</p><ul>"
385
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
386
  prob = float(prob.replace('%', ''))
387
+ dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% 信心度)"
 
 
 
 
 
388
  button_id = f"狗狗 {i+1}: 更多關於 {breed}"
389
+ dogs_info += f' <button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button></li>'
390
  buttons.append(button_id)
391
+ dogs_info += "</ul>"
392
 
393
  else:
394
+ dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
395
 
396
  dogs_info += '</div>'
397
 
 
398
  html_output = f"""
399
  <style>
400
  .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); }}
401
  .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
402
+ .breed-button {{ margin-left: 10px; padding: 2px 5px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
 
403
  </style>
404
  {dogs_info}
405
  <script>
 
408
  if (radio) {{
409
  radio.click();
410
  }} else {{
411
+ console.error("Radio button not found:", button_id);
412
  }}
413
  }}
414
  </script>
 
422
  "is_multi_dog": len(dogs) > 1,
423
  "html_output": html_output
424
  }
425
+ return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
426
 
427
  except Exception as e:
428
+ error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
429
  print(error_msg)
430
  return error_msg, None, gr.update(visible=False, choices=[]), None
431
 
432
+
433
+
434
 
435
  def show_details_html(choice, previous_output, initial_state):
436
  if not choice: