DawnC commited on
Commit
7f6e440
1 Parent(s): 434d875

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -30
app.py CHANGED
@@ -436,21 +436,73 @@ async def process_single_dog(image):
436
  }
437
  return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
438
 
439
- def show_details(choice, previous_output, initial_state):
440
- if not choice:
441
- return previous_output, gr.update(visible=True), initial_state
442
 
443
- try:
444
- breed = choice.split("More about ")[-1]
445
- description = get_dog_description(breed)
446
- formatted_description = format_description(description, breed)
447
- return formatted_description, gr.update(visible=True), initial_state
448
- except Exception as e:
449
- error_msg = f"An error occurred while showing details: {e}"
450
- print(error_msg) # 添加日誌輸出
451
- return error_msg, gr.update(visible=True), initial_state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
452
 
453
- # 介面部分
454
  with gr.Blocks() as iface:
455
  gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
456
  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>")
@@ -462,43 +514,50 @@ with gr.Blocks() as iface:
462
  output = gr.Markdown(label="Prediction Results")
463
 
464
  with gr.Row():
465
- btn1 = gr.Button("View More 1", visible=False)
466
- btn2 = gr.Button("View More 2", visible=False)
467
- btn3 = gr.Button("View More 3", visible=False)
468
 
469
  back_button = gr.Button("Back", visible=False)
470
 
471
  initial_state = gr.State()
472
 
 
 
 
 
 
 
 
 
 
 
 
 
 
473
  input_image.change(
474
  predict,
475
  inputs=input_image,
476
- outputs=[output, output_image, btn1, btn2, btn3, back_button, initial_state]
477
  )
478
-
479
- for btn in [btn1, btn2, btn3]:
480
  btn.click(
481
  show_details,
482
  inputs=[btn, output, initial_state],
483
  outputs=[output, back_button, initial_state]
484
  )
485
-
486
  back_button.click(
487
- lambda state: (state["explanation"],
488
- state["buttons"][0] if len(state["buttons"]) > 0 else gr.update(visible=False),
489
- state["buttons"][1] if len(state["buttons"]) > 1 else gr.update(visible=False),
490
- state["buttons"][2] if len(state["buttons"]) > 2 else gr.update(visible=False),
491
- gr.update(visible=state["show_back"])),
492
  inputs=[initial_state],
493
- outputs=[output, btn1, btn2, btn3, back_button]
494
  )
495
-
496
  gr.Examples(
497
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
498
  inputs=input_image
499
  )
500
-
501
  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>')
502
 
503
- if __name__ == "__main__":
504
- iface.launch()
 
436
  }
437
  return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
438
 
439
+ # def show_details(choice, previous_output, initial_state):
440
+ # if not choice:
441
+ # return previous_output, gr.update(visible=True), initial_state
442
 
443
+ # try:
444
+ # breed = choice.split("More about ")[-1]
445
+ # description = get_dog_description(breed)
446
+ # formatted_description = format_description(description, breed)
447
+ # return formatted_description, gr.update(visible=True), initial_state
448
+ # except Exception as e:
449
+ # error_msg = f"An error occurred while showing details: {e}"
450
+ # print(error_msg) # 添加日誌輸出
451
+ # return error_msg, gr.update(visible=True), initial_state
452
+
453
+ # # 介面部分
454
+ # with gr.Blocks() as iface:
455
+ # gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
456
+ # 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>")
457
+
458
+ # with gr.Row():
459
+ # input_image = gr.Image(label="Upload a dog image", type="pil")
460
+ # output_image = gr.Image(label="Annotated Image")
461
+
462
+ # output = gr.Markdown(label="Prediction Results")
463
+
464
+ # with gr.Row():
465
+ # btn1 = gr.Button("View More 1", visible=False)
466
+ # btn2 = gr.Button("View More 2", visible=False)
467
+ # btn3 = gr.Button("View More 3", visible=False)
468
+
469
+ # back_button = gr.Button("Back", visible=False)
470
+
471
+ # initial_state = gr.State()
472
+
473
+ # input_image.change(
474
+ # predict,
475
+ # inputs=input_image,
476
+ # outputs=[output, output_image, btn1, btn2, btn3, back_button, initial_state]
477
+ # )
478
+
479
+ # for btn in [btn1, btn2, btn3]:
480
+ # btn.click(
481
+ # show_details,
482
+ # inputs=[btn, output, initial_state],
483
+ # outputs=[output, back_button, initial_state]
484
+ # )
485
+
486
+ # back_button.click(
487
+ # lambda state: (state["explanation"],
488
+ # state["buttons"][0] if len(state["buttons"]) > 0 else gr.update(visible=False),
489
+ # state["buttons"][1] if len(state["buttons"]) > 1 else gr.update(visible=False),
490
+ # state["buttons"][2] if len(state["buttons"]) > 2 else gr.update(visible=False),
491
+ # gr.update(visible=state["show_back"])),
492
+ # inputs=[initial_state],
493
+ # outputs=[output, btn1, btn2, btn3, back_button]
494
+ # )
495
+
496
+ # gr.Examples(
497
+ # examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
498
+ # inputs=input_image
499
+ # )
500
+
501
+ # 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>')
502
+
503
+ # if __name__ == "__main__":
504
+ # iface.launch()
505
 
 
506
  with gr.Blocks() as iface:
507
  gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
508
  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>")
 
514
  output = gr.Markdown(label="Prediction Results")
515
 
516
  with gr.Row():
517
+ buttons = [gr.Button(f"View More {i+1}", visible=False) for i in range(9)] # 增加到9個按鈕
 
 
518
 
519
  back_button = gr.Button("Back", visible=False)
520
 
521
  initial_state = gr.State()
522
 
523
+ def show_details(button_text, current_output, state):
524
+ breed = button_text.split("More about ")[-1]
525
+ description = get_dog_description(breed)
526
+ formatted_description = format_description(description, breed)
527
+ state["current_breed"] = breed
528
+ return formatted_description, gr.update(visible=True), state
529
+
530
+ def go_back(state):
531
+ return (state["explanation"],
532
+ *[btn if i < len(state["buttons"]) else gr.update(visible=False)
533
+ for i, btn in enumerate(state["buttons"])],
534
+ gr.update(visible=state["show_back"]))
535
+
536
  input_image.change(
537
  predict,
538
  inputs=input_image,
539
+ outputs=[output, output_image] + buttons + [back_button, initial_state]
540
  )
541
+
542
+ for btn in buttons:
543
  btn.click(
544
  show_details,
545
  inputs=[btn, output, initial_state],
546
  outputs=[output, back_button, initial_state]
547
  )
548
+
549
  back_button.click(
550
+ go_back,
 
 
 
 
551
  inputs=[initial_state],
552
+ outputs=[output] + buttons + [back_button]
553
  )
554
+
555
  gr.Examples(
556
  examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
557
  inputs=input_image
558
  )
559
+
560
  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>')
561
 
562
+ if __name__ == "__main__":
563
+ iface.launch()