DawnC commited on
Commit
432a157
·
1 Parent(s): 08313a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -19
app.py CHANGED
@@ -263,8 +263,8 @@ async def predict(image):
263
  draw = ImageDraw.Draw(annotated_image)
264
  font = ImageFont.load_default()
265
 
266
- dogs_info = ""
267
- buttons_html = ""
268
 
269
  # for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
270
  # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
@@ -302,6 +302,54 @@ async def predict(image):
302
  # dogs_info += buttons_html
303
 
304
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
306
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
307
  color = color_list[i % len(color_list)]
@@ -332,27 +380,14 @@ async def predict(image):
332
  button_id = f"Dog {i+1}: More about {breed}"
333
  temp_buttons += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
334
  buttons.append(button_id)
335
- temp_buttons += '</div>'
336
-
337
- # 將 buttons_html 只生成按鈕
338
- buttons_html += temp_buttons
339
 
340
  else:
341
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
342
 
343
  dogs_info += '</div>' # 結束該狗的描述區塊
344
 
345
- # 確保按鈕獨立於 dogs_info 區塊內,避免重複品種名
346
- if buttons_html:
347
- dogs_info += buttons_html # 確保按鈕獨立並附加到最後輸出
348
-
349
-
350
-
351
- # 避免將按鈕與 dogs_info 同步顯示
352
- if buttons_html:
353
- dogs_info += buttons_html # 確保按鈕獨立並附加到最後輸出
354
-
355
-
356
  html_output = f"""
357
  <style>
358
  .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); }}
@@ -361,9 +396,12 @@ async def predict(image):
361
  .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
362
  </style>
363
  {dogs_info}
364
-
365
  """
366
-
 
 
 
 
367
  if buttons:
368
  html_output += """
369
  <script>
@@ -397,6 +435,7 @@ async def predict(image):
397
  }
398
  return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
399
 
 
400
  except Exception as e:
401
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
402
  print(error_msg)
 
263
  draw = ImageDraw.Draw(annotated_image)
264
  font = ImageFont.load_default()
265
 
266
+ # dogs_info = ""
267
+ # buttons_html = ""
268
 
269
  # for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
270
  # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
 
302
  # dogs_info += buttons_html
303
 
304
 
305
+ # html_output = f"""
306
+ # <style>
307
+ # .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); }}
308
+ # .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
309
+ # .breed-buttons {{ margin-top: 10px; }}
310
+ # .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
311
+ # </style>
312
+ # {dogs_info}
313
+
314
+ # """
315
+
316
+ # if buttons:
317
+ # html_output += """
318
+ # <script>
319
+ # function handle_button_click(button_id) {
320
+ # const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
321
+ # if (radio) {
322
+ # radio.click();
323
+ # } else {
324
+ # console.error("Radio button not found:", button_id);
325
+ # }
326
+ # }
327
+ # </script>
328
+ # """
329
+ # initial_state = {
330
+ # "dogs_info": dogs_info,
331
+ # "buttons": buttons,
332
+ # "show_back": True,
333
+ # "image": annotated_image,
334
+ # "is_multi_dog": len(dogs) > 1,
335
+ # "html_output": html_output # 儲存完整的 HTML 輸出
336
+ # }
337
+ # return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
338
+ # else:
339
+ # initial_state = {
340
+ # "dogs_info": dogs_info,
341
+ # "buttons": [],
342
+ # "show_back": False,
343
+ # "image": annotated_image,
344
+ # "is_multi_dog": len(dogs) > 1,
345
+ # "html_output": html_output # 儲存完整的 HTML 輸出
346
+ # }
347
+ # return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
348
+
349
+
350
+ dogs_info = "" # 初始化 dogs_info
351
+ buttons_html = "" # 初始化 buttons_html
352
+
353
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
354
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
355
  color = color_list[i % len(color_list)]
 
380
  button_id = f"Dog {i+1}: More about {breed}"
381
  temp_buttons += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
382
  buttons.append(button_id)
383
+ buttons_html += temp_buttons + '</div>' # 加入到 buttons_html
 
 
 
384
 
385
  else:
386
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
387
 
388
  dogs_info += '</div>' # 結束該狗的描述區塊
389
 
390
+ # 按鈕不再每個迴圈加入,而是放到整個迴圈結束後統一加入
 
 
 
 
 
 
 
 
 
 
391
  html_output = f"""
392
  <style>
393
  .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); }}
 
396
  .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
397
  </style>
398
  {dogs_info}
 
399
  """
400
+
401
+ # 確保按鈕獨立顯示,只加一次
402
+ if buttons_html:
403
+ html_output += buttons_html
404
+
405
  if buttons:
406
  html_output += """
407
  <script>
 
435
  }
436
  return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
437
 
438
+
439
  except Exception as e:
440
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
441
  print(error_msg)