DawnC commited on
Commit
1f2f07c
1 Parent(s): 86a506d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -235
app.py CHANGED
@@ -243,217 +243,9 @@ async def process_single_dog(image):
243
  return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
244
 
245
 
246
- # async def predict(image):
247
- # if image is None:
248
- # return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
249
-
250
- # try:
251
- # if isinstance(image, np.ndarray):
252
- # image = Image.fromarray(image)
253
-
254
- # dogs = await detect_multiple_dogs(image)
255
-
256
- # color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
257
- # buttons = []
258
- # annotated_image = image.copy()
259
- # draw = ImageDraw.Draw(annotated_image)
260
- # font = ImageFont.load_default()
261
-
262
- # dogs_info = ""
263
-
264
- # for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
265
- # buttons_html = ""
266
- # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
267
- # color = color_list[i % len(color_list)]
268
- # draw.rectangle(box, outline=color,, width=3)
269
- # draw.text((box[0] + 5, box[1] + 5) f"Dog {i+1}", fill=color, font=font)
270
-
271
- # combined_confidence = detection_confidence * top1_prob
272
- # dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
273
- # dogs_info += f'<h2>Dog {i+1}</h2>'
274
-
275
- # if top1_prob >= 0.45:
276
- # breed = topk_breeds[0]
277
- # description = get_dog_description(breed)
278
- # dogs_info += format_description_html(description, breed)
279
-
280
- # elif combined_confidence >= 0.15:
281
- # dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
282
- # for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
283
- # prob = float(prob.replace('%', ''))
284
- # dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
285
- # dogs_info += "</ul>"
286
-
287
- # for breed in topk_breeds[:3]:
288
- # button_id = f"Dog {i+1}: More about {breed}"
289
- # buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
290
- # buttons.append(button_id)
291
-
292
- # else:
293
- # dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
294
-
295
- # dogs_info += '</div>'
296
-
297
-
298
- # buttons_html = ""
299
-
300
- # html_output = f"""
301
- # <style>
302
- # .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); }}
303
- # .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
304
- # .breed-buttons {{ margin-top: 10px; }}
305
- # .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
306
- # </style>
307
- # {dogs_info}
308
- # """
309
-
310
- # if buttons:
311
- # html_output += """
312
- # <script>
313
- # function handle_button_click(button_id) {
314
- # const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
315
- # if (radio) {
316
- # radio.click();
317
- # } else {
318
- # console.error("Radio button not found:", button_id);
319
- # }
320
- # }
321
- # </script>
322
- # """
323
- # initial_state = {
324
- # "dogs_info": dogs_info,
325
- # "buttons": buttons,
326
- # "show_back": True,
327
- # "image": annotated_image,
328
- # "is_multi_dog": len(dogs) > 1,
329
- # "html_output": html_output
330
- # }
331
- # return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
332
- # else:
333
- # initial_state = {
334
- # "dogs_info": dogs_info,
335
- # "buttons": [],
336
- # "show_back": False,
337
- # "image": annotated_image,
338
- # "is_multi_dog": len(dogs) > 1,
339
- # "html_output": html_output
340
- # }
341
- # return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
342
-
343
-
344
- # except Exception as e:
345
- # error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
346
- # print(error_msg)
347
- # return error_msg, None, gr.update(visible=False, choices=[]), None
348
-
349
-
350
- # def show_details_html(choice, previous_output, initial_state):
351
- # if not choice:
352
- # return previous_output, gr.update(visible=True), initial_state
353
-
354
- # try:
355
- # breed = choice.split("More about ")[-1]
356
- # description = get_dog_description(breed)
357
- # formatted_description = format_description_html(description, breed)
358
-
359
- # html_output = f"""
360
- # <div class="dog-info">
361
- # <h2>{breed}</h2>
362
- # {formatted_description}
363
- # </div>
364
- # """
365
-
366
- # initial_state["current_description"] = html_output
367
- # initial_state["original_buttons"] = initial_state.get("buttons", [])
368
-
369
- # return html_output, gr.update(visible=True), initial_state
370
- # except Exception as e:
371
- # error_msg = f"An error occurred while showing details: {e}"
372
- # print(error_msg)
373
- # return f"<p style='color: red;'>{error_msg}</p>", gr.update(visible=True), initial_state
374
-
375
-
376
- # def format_description_html(description, breed):
377
- # html = "<ul style='list-style-type: none; padding-left: 0;'>"
378
- # if isinstance(description, dict):
379
- # for key, value in description.items():
380
- # html += f"<li style='margin-bottom: 10px;'><strong>{key}:</strong> {value}</li>"
381
- # elif isinstance(description, str):
382
- # html += f"<li>{description}</li>"
383
- # else:
384
- # html += f"<li>No description available for {breed}</li>"
385
- # html += "</ul>"
386
- # akc_link = get_akc_breeds_link()
387
- # html += f'<p><a href="{akc_link}" target="_blank">Learn more about {breed} on the AKC website</a></p>'
388
- # return html
389
-
390
-
391
- # def go_back(state):
392
- # buttons = state.get("buttons", [])
393
- # return (
394
- # state["html_output"],
395
- # state["image"],
396
- # gr.update(visible=True, choices=buttons),
397
- # gr.update(visible=False),
398
- # state
399
- # )
400
-
401
-
402
- # with gr.Blocks() as iface:
403
- # gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
404
- # gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>")
405
-
406
- # with gr.Row():
407
- # input_image = gr.Image(label="Upload a dog image", type="pil")
408
- # output_image = gr.Image(label="Annotated Image")
409
-
410
- # output = gr.HTML(label="Prediction Results")
411
-
412
- # breed_buttons = gr.Radio(choices=[], label="More Information", visible=False)
413
-
414
- # back_button = gr.Button("Back", visible=False)
415
-
416
- # initial_state = gr.State()
417
-
418
- # input_image.change(
419
- # predict,
420
- # inputs=input_image,
421
- # outputs=[output, output_image, breed_buttons, initial_state]
422
- # )
423
-
424
- # breed_buttons.change(
425
- # show_details_html,
426
- # inputs=[breed_buttons, output, initial_state],
427
- # outputs=[output, back_button, initial_state]
428
- # )
429
-
430
- # back_button.click(
431
- # go_back,
432
- # inputs=[initial_state],
433
- # outputs=[output, output_image, breed_buttons, back_button, initial_state]
434
- # )
435
-
436
- # gr.Examples(
437
- # examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
438
- # inputs=input_image
439
- # )
440
-
441
- # gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
442
-
443
-
444
-
445
- # if __name__ == "__main__":
446
- # iface.launch()
447
-
448
-
449
-
450
-
451
-
452
-
453
-
454
  async def predict(image):
455
  if image is None:
456
- return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False)
457
 
458
  try:
459
  if isinstance(image, np.ndarray):
@@ -462,18 +254,19 @@ async def predict(image):
462
  dogs = await detect_multiple_dogs(image)
463
 
464
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
 
465
  annotated_image = image.copy()
466
  draw = ImageDraw.Draw(annotated_image)
467
  font = ImageFont.load_default()
468
 
469
  dogs_info = ""
470
- buttons = []
471
 
472
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
 
473
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
474
  color = color_list[i % len(color_list)]
475
- draw.rectangle(box, outline=color, width=3)
476
- draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
477
 
478
  combined_confidence = detection_confidence * top1_prob
479
  dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
@@ -483,38 +276,80 @@ async def predict(image):
483
  breed = topk_breeds[0]
484
  description = get_dog_description(breed)
485
  dogs_info += format_description_html(description, breed)
 
486
  elif combined_confidence >= 0.15:
487
  dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
488
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
489
  prob = float(prob.replace('%', ''))
490
- button_id = f"Dog {i+1}: More about {breed}"
491
- buttons.append(button_id)
492
  dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
493
  dogs_info += "</ul>"
 
 
 
 
 
 
494
  else:
495
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
496
 
497
- dogs_info += '</div>'
498
 
 
 
 
499
  html_output = f"""
500
  <style>
501
  .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); }}
502
  .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
 
 
503
  </style>
504
-
505
  {dogs_info}
506
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
507
 
508
- return html_output, annotated_image, gr.update(visible=True, choices=buttons), gr.update(visible=False)
509
 
510
  except Exception as e:
511
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
512
  print(error_msg)
513
- return error_msg, None, gr.update(visible=False), gr.update(visible=False)
514
 
515
- def show_breed_info(choice):
 
516
  if not choice:
517
- return "", gr.update(visible=False)
518
 
519
  try:
520
  breed = choice.split("More about ")[-1]
@@ -528,13 +363,42 @@ def show_breed_info(choice):
528
  </div>
529
  """
530
 
531
- return html_output, gr.update(visible=True)
 
 
 
532
  except Exception as e:
533
  error_msg = f"An error occurred while showing details: {e}"
534
  print(error_msg)
535
- return f"<p style='color: red;'>{error_msg}</p>", gr.update(visible=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
536
 
537
- # Main Gradio interface
538
  with gr.Blocks() as iface:
539
  gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
540
  gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>")
@@ -543,29 +407,30 @@ with gr.Blocks() as iface:
543
  input_image = gr.Image(label="Upload a dog image", type="pil")
544
  output_image = gr.Image(label="Annotated Image")
545
 
546
- output = gr.HTML(label="Prediction Results")
547
- breed_info = gr.HTML(label="Breed Information")
548
 
549
  breed_buttons = gr.Radio(choices=[], label="More Information", visible=False)
550
 
551
- back_button = gr.Button("Back to Results", visible=False)
 
 
552
 
553
  input_image.change(
554
  predict,
555
- inputs=[input_image],
556
- outputs=[output, output_image, breed_buttons, back_button]
557
  )
558
 
559
  breed_buttons.change(
560
- show_breed_info,
561
- inputs=[breed_buttons],
562
- outputs=[breed_info, back_button]
563
  )
564
 
565
  back_button.click(
566
- lambda: (gr.update(visible=True), gr.update(visible=True), gr.update(visible=True),
567
- gr.update(visible=False), gr.update(visible=False)),
568
- outputs=[output, output_image, breed_buttons, breed_info, back_button]
569
  )
570
 
571
  gr.Examples(
@@ -575,5 +440,7 @@ with gr.Blocks() as iface:
575
 
576
  gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
577
 
 
 
578
  if __name__ == "__main__":
579
  iface.launch()
 
243
  return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
244
 
245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
246
  async def predict(image):
247
  if image is None:
248
+ return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
249
 
250
  try:
251
  if isinstance(image, np.ndarray):
 
254
  dogs = await detect_multiple_dogs(image)
255
 
256
  color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
257
+ buttons = []
258
  annotated_image = image.copy()
259
  draw = ImageDraw.Draw(annotated_image)
260
  font = ImageFont.load_default()
261
 
262
  dogs_info = ""
 
263
 
264
  for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
265
+ buttons_html = ""
266
  top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
267
  color = color_list[i % len(color_list)]
268
+ draw.rectangle(box, outline=color,, width=3)
269
+ draw.text((box[0] + 5, box[1] + 5) f"Dog {i+1}", fill=color, font=font)
270
 
271
  combined_confidence = detection_confidence * top1_prob
272
  dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
 
276
  breed = topk_breeds[0]
277
  description = get_dog_description(breed)
278
  dogs_info += format_description_html(description, breed)
279
+
280
  elif combined_confidence >= 0.15:
281
  dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
282
  for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
283
  prob = float(prob.replace('%', ''))
 
 
284
  dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
285
  dogs_info += "</ul>"
286
+
287
+ for breed in topk_breeds[:3]:
288
+ button_id = f"Dog {i+1}: More about {breed}"
289
+ buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
290
+ buttons.append(button_id)
291
+
292
  else:
293
  dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
294
 
295
+ dogs_info += '</div>'
296
 
297
+
298
+ buttons_html = ""
299
+
300
  html_output = f"""
301
  <style>
302
  .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); }}
303
  .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
304
+ .breed-buttons {{ margin-top: 10px; }}
305
+ .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
306
  </style>
 
307
  {dogs_info}
308
+ """
309
+
310
+ if buttons:
311
+ html_output += """
312
+ <script>
313
+ function handle_button_click(button_id) {
314
+ const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
315
+ if (radio) {
316
+ radio.click();
317
+ } else {
318
+ console.error("Radio button not found:", button_id);
319
+ }
320
+ }
321
+ </script>
322
+ """
323
+ initial_state = {
324
+ "dogs_info": dogs_info,
325
+ "buttons": buttons,
326
+ "show_back": True,
327
+ "image": annotated_image,
328
+ "is_multi_dog": len(dogs) > 1,
329
+ "html_output": html_output
330
+ }
331
+ return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
332
+ else:
333
+ initial_state = {
334
+ "dogs_info": dogs_info,
335
+ "buttons": [],
336
+ "show_back": False,
337
+ "image": annotated_image,
338
+ "is_multi_dog": len(dogs) > 1,
339
+ "html_output": html_output
340
+ }
341
+ return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
342
 
 
343
 
344
  except Exception as e:
345
  error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
346
  print(error_msg)
347
+ return error_msg, None, gr.update(visible=False, choices=[]), None
348
 
349
+
350
+ def show_details_html(choice, previous_output, initial_state):
351
  if not choice:
352
+ return previous_output, gr.update(visible=True), initial_state
353
 
354
  try:
355
  breed = choice.split("More about ")[-1]
 
363
  </div>
364
  """
365
 
366
+ initial_state["current_description"] = html_output
367
+ initial_state["original_buttons"] = initial_state.get("buttons", [])
368
+
369
+ return html_output, gr.update(visible=True), initial_state
370
  except Exception as e:
371
  error_msg = f"An error occurred while showing details: {e}"
372
  print(error_msg)
373
+ return f"<p style='color: red;'>{error_msg}</p>", gr.update(visible=True), initial_state
374
+
375
+
376
+ def format_description_html(description, breed):
377
+ html = "<ul style='list-style-type: none; padding-left: 0;'>"
378
+ if isinstance(description, dict):
379
+ for key, value in description.items():
380
+ html += f"<li style='margin-bottom: 10px;'><strong>{key}:</strong> {value}</li>"
381
+ elif isinstance(description, str):
382
+ html += f"<li>{description}</li>"
383
+ else:
384
+ html += f"<li>No description available for {breed}</li>"
385
+ html += "</ul>"
386
+ akc_link = get_akc_breeds_link()
387
+ html += f'<p><a href="{akc_link}" target="_blank">Learn more about {breed} on the AKC website</a></p>'
388
+ return html
389
+
390
+
391
+ def go_back(state):
392
+ buttons = state.get("buttons", [])
393
+ return (
394
+ state["html_output"],
395
+ state["image"],
396
+ gr.update(visible=True, choices=buttons),
397
+ gr.update(visible=False),
398
+ state
399
+ )
400
+
401
 
 
402
  with gr.Blocks() as iface:
403
  gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
404
  gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>")
 
407
  input_image = gr.Image(label="Upload a dog image", type="pil")
408
  output_image = gr.Image(label="Annotated Image")
409
 
410
+ output = gr.HTML(label="Prediction Results")
 
411
 
412
  breed_buttons = gr.Radio(choices=[], label="More Information", visible=False)
413
 
414
+ back_button = gr.Button("Back", visible=False)
415
+
416
+ initial_state = gr.State()
417
 
418
  input_image.change(
419
  predict,
420
+ inputs=input_image,
421
+ outputs=[output, output_image, breed_buttons, initial_state]
422
  )
423
 
424
  breed_buttons.change(
425
+ show_details_html,
426
+ inputs=[breed_buttons, output, initial_state],
427
+ outputs=[output, back_button, initial_state]
428
  )
429
 
430
  back_button.click(
431
+ go_back,
432
+ inputs=[initial_state],
433
+ outputs=[output, output_image, breed_buttons, back_button, initial_state]
434
  )
435
 
436
  gr.Examples(
 
440
 
441
  gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog_Breed_Classifier">Dog Breed Classifier</a>')
442
 
443
+
444
+
445
  if __name__ == "__main__":
446
  iface.launch()