DawnC commited on
Commit
418e83b
·
1 Parent(s): a0e14ea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -23
app.py CHANGED
@@ -303,7 +303,7 @@ def _detect_multiple_dogs(image, conf_threshold):
303
 
304
  async def predict(image):
305
  if image is None:
306
- return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False)
307
 
308
  try:
309
  if isinstance(image, np.ndarray):
@@ -315,14 +315,14 @@ async def predict(image):
315
  # 單狗情境
316
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
317
  if top1_prob < 0.2:
318
- return "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False)
319
 
320
  breed = topk_breeds[0]
321
  description = get_dog_description(breed)
322
 
323
  if top1_prob >= 0.5:
324
  formatted_description = format_description(description, breed)
325
- return formatted_description, image, gr.update(visible=False), gr.update(visible=False)
326
  else:
327
  explanation = (
328
  f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
@@ -331,13 +331,11 @@ async def predict(image):
331
  f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
332
  "Click on a button to view more information about the breed."
333
  )
334
- choices = [f"More about {breed}" for breed in topk_breeds[:3]]
335
- return explanation, image, gr.update(visible=True, choices=choices), gr.update(visible=False)
336
 
337
  # 多狗情境
338
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
339
  explanations = []
340
- choices = []
341
  annotated_image = image.copy()
342
  draw = ImageDraw.Draw(annotated_image)
343
  font = ImageFont.load_default()
@@ -358,25 +356,21 @@ async def predict(image):
358
  f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
359
  f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
360
  f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)")
361
- choices.extend([f"Dog {i+1}: {breed}" for breed in topk_breeds[:3]])
362
  else:
363
  explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
364
 
365
  final_explanation = "\n\n".join(explanations)
366
- return final_explanation, annotated_image, gr.update(visible=len(choices) > 0, choices=choices), gr.update(visible=False)
367
 
368
  except Exception as e:
369
- return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False)
370
 
371
- async def show_details(choice):
372
  if not choice:
373
  return "Please select a breed to view details."
374
 
375
  try:
376
- if "Dog" in choice:
377
- _, breed = choice.split(": ", 1)
378
- else:
379
- _, breed = choice.split("More about ", 1)
380
  description = get_dog_description(breed)
381
  return format_description(description, breed)
382
  except Exception as e:
@@ -391,20 +385,21 @@ with gr.Blocks() as iface:
391
  output_image = gr.Image(label="Annotated Image")
392
 
393
  output = gr.Markdown(label="Prediction Results")
394
- breed_buttons = gr.Radio([], label="Select breed for more details", visible=False)
395
- breed_details = gr.Markdown(label="Breed Details")
 
 
 
396
 
397
  input_image.change(
398
  predict,
399
  inputs=input_image,
400
- outputs=[output, output_image, breed_buttons, breed_details]
401
  )
402
 
403
- breed_buttons.select(
404
- show_details,
405
- inputs=breed_buttons,
406
- outputs=breed_details
407
- )
408
 
409
  gr.Examples(
410
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
@@ -415,4 +410,3 @@ with gr.Blocks() as iface:
415
 
416
  if __name__ == "__main__":
417
  iface.launch()
418
-
 
303
 
304
  async def predict(image):
305
  if image is None:
306
+ return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
307
 
308
  try:
309
  if isinstance(image, np.ndarray):
 
315
  # 單狗情境
316
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
317
  if top1_prob < 0.2:
318
+ return "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
319
 
320
  breed = topk_breeds[0]
321
  description = get_dog_description(breed)
322
 
323
  if top1_prob >= 0.5:
324
  formatted_description = format_description(description, breed)
325
+ return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
326
  else:
327
  explanation = (
328
  f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
 
331
  f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
332
  "Click on a button to view more information about the breed."
333
  )
334
+ return explanation, image, gr.update(visible=True, value=f"More about {topk_breeds[0]}"), gr.update(visible=True, value=f"More about {topk_breeds[1]}"), gr.update(visible=True, value=f"More about {topk_breeds[2]}")
 
335
 
336
  # 多狗情境
337
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
338
  explanations = []
 
339
  annotated_image = image.copy()
340
  draw = ImageDraw.Draw(annotated_image)
341
  font = ImageFont.load_default()
 
356
  f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
357
  f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
358
  f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)")
 
359
  else:
360
  explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
361
 
362
  final_explanation = "\n\n".join(explanations)
363
+ return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
364
 
365
  except Exception as e:
366
+ return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
367
 
368
+ def show_details(choice):
369
  if not choice:
370
  return "Please select a breed to view details."
371
 
372
  try:
373
+ breed = choice.split("More about ")[-1]
 
 
 
374
  description = get_dog_description(breed)
375
  return format_description(description, breed)
376
  except Exception as e:
 
385
  output_image = gr.Image(label="Annotated Image")
386
 
387
  output = gr.Markdown(label="Prediction Results")
388
+
389
+ with gr.Row():
390
+ btn1 = gr.Button("View More 1", visible=False)
391
+ btn2 = gr.Button("View More 2", visible=False)
392
+ btn3 = gr.Button("View More 3", visible=False)
393
 
394
  input_image.change(
395
  predict,
396
  inputs=input_image,
397
+ outputs=[output, output_image, btn1, btn2, btn3]
398
  )
399
 
400
+ btn1.click(show_details, inputs=btn1, outputs=output)
401
+ btn2.click(show_details, inputs=btn2, outputs=output)
402
+ btn3.click(show_details, inputs=btn3, outputs=output)
 
 
403
 
404
  gr.Examples(
405
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
 
410
 
411
  if __name__ == "__main__":
412
  iface.launch()