Jacksonnavigator7 commited on
Commit
1c7bf88
·
verified ·
1 Parent(s): a37f1bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -37
app.py CHANGED
@@ -45,8 +45,7 @@ translations = {
45
  "low_confidence": "Low confidence prediction. Results may not be accurate.",
46
  "not_a_bird": "The image may not contain a bird. Please upload a clear image of a bird.",
47
  "other_message": "This image is not in our trained dataset or the image may not be of a bird. Please try uploading a different image.",
48
- "example_images": "Example Images",
49
- "try_example": "Try Example"
50
  },
51
  "sw": {
52
  "app_title": "Mtafiti wa Ndege: Utambuzi wa Kiotomatiki kwa Watafiti",
@@ -70,8 +69,7 @@ translations = {
70
  "low_confidence": "Utabiri wa uhakika mdogo. Matokeo yanaweza kuwa si sahihi.",
71
  "not_a_bird": "Picha inaweza isiwe ya ndege. Tafadhali pakia picha wazi ya ndege.",
72
  "other_message": "Ndege huyu haipatikani katika hifadhidata yetu au picha inaweza isiwe ya ndege. Tafadhali jaribu kupakia picha nyingine.",
73
- "example_images": "Mifano ya Picha",
74
- "try_example": "Jaribu Mfano"
75
  }
76
  }
77
 
@@ -576,6 +574,14 @@ def follow_up_question(question, bird_name, language="en"):
576
  error_msg = "Hitilafu katika kupata taarifa" if language == "sw" else "Error fetching information"
577
  return f"{error_msg}: {str(e)}"
578
 
 
 
 
 
 
 
 
 
579
  # Create the Gradio interface
580
  with gr.Blocks(theme=gr.themes.Soft()) as app:
581
  # Current language and bird state
@@ -601,19 +607,24 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
601
  with gr.Column(scale=1):
602
  input_image = gr.Image(type="pil", label=translations['en']['upload_label'])
603
 
604
- # Example images section
605
  example_header = gr.Markdown(f"### {translations['en']['example_images']}")
606
 
607
- # Create example image buttons in a grid
608
- with gr.Row():
609
- example_buttons = []
610
- for i, example in enumerate(EXAMPLE_IMAGES):
611
- btn = gr.Button(
612
- f"{translations['en']['try_example']} {i+1}",
613
- variant="secondary",
614
- size="sm"
615
- )
616
- example_buttons.append(btn)
 
 
 
 
 
617
 
618
  submit_btn = gr.Button(translations['en']['identify_button'], variant="primary")
619
 
@@ -682,24 +693,17 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
682
  t['question_label'],
683
  t['question_placeholder'],
684
  t['submit_question'],
685
- t['clear_conversation'],
686
- # Update example button labels
687
- *[f"{t['try_example']} {i+1}" for i in range(len(EXAMPLE_IMAGES))]
688
  )
689
 
690
- # Functions to handle example image clicks
691
- def create_example_handlers():
692
- """Create handlers for example image buttons"""
693
- handlers = []
694
- for example in EXAMPLE_IMAGES:
695
- def make_handler(path):
696
- def handler():
697
- return load_example_image(path)
698
- return handler
699
- handlers.append(make_handler(example["path"]))
700
- return handlers
701
-
702
- example_handlers = create_example_handlers()
703
 
704
  # Set up event handlers
705
  language_selector.change(
@@ -718,15 +722,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
718
  follow_up_input,
719
  follow_up_input,
720
  follow_up_btn,
721
- clear_btn,
722
- *example_buttons
723
  ]
724
  )
725
 
726
- # Set up example image button handlers
727
- for i, (btn, handler) in enumerate(zip(example_buttons, example_handlers)):
728
- btn.click(
729
- handler,
730
  outputs=[input_image]
731
  )
732
 
 
45
  "low_confidence": "Low confidence prediction. Results may not be accurate.",
46
  "not_a_bird": "The image may not contain a bird. Please upload a clear image of a bird.",
47
  "other_message": "This image is not in our trained dataset or the image may not be of a bird. Please try uploading a different image.",
48
+ "example_images": "Example Images - Click on any image to use it"
 
49
  },
50
  "sw": {
51
  "app_title": "Mtafiti wa Ndege: Utambuzi wa Kiotomatiki kwa Watafiti",
 
69
  "low_confidence": "Utabiri wa uhakika mdogo. Matokeo yanaweza kuwa si sahihi.",
70
  "not_a_bird": "Picha inaweza isiwe ya ndege. Tafadhali pakia picha wazi ya ndege.",
71
  "other_message": "Ndege huyu haipatikani katika hifadhidata yetu au picha inaweza isiwe ya ndege. Tafadhali jaribu kupakia picha nyingine.",
72
+ "example_images": "Mifano ya Picha - Bofya picha yoyote kutumia"
 
73
  }
74
  }
75
 
 
574
  error_msg = "Hitilafu katika kupata taarifa" if language == "sw" else "Error fetching information"
575
  return f"{error_msg}: {str(e)}"
576
 
577
+ def get_available_examples():
578
+ """Get list of available example images that actually exist"""
579
+ available_examples = []
580
+ for example in EXAMPLE_IMAGES:
581
+ if os.path.exists(example["path"]):
582
+ available_examples.append(example)
583
+ return available_examples
584
+
585
  # Create the Gradio interface
586
  with gr.Blocks(theme=gr.themes.Soft()) as app:
587
  # Current language and bird state
 
607
  with gr.Column(scale=1):
608
  input_image = gr.Image(type="pil", label=translations['en']['upload_label'])
609
 
610
+ # Example images section with actual image display
611
  example_header = gr.Markdown(f"### {translations['en']['example_images']}")
612
 
613
+ # Create example images gallery
614
+ available_examples = get_available_examples()
615
+ if available_examples:
616
+ example_images = gr.Gallery(
617
+ value=[(example["path"], example["name"]) for example in available_examples],
618
+ label="Click any image to use it",
619
+ show_label=False,
620
+ elem_id="example-gallery",
621
+ columns=2,
622
+ rows=2,
623
+ height="auto",
624
+ allow_preview=False
625
+ )
626
+ else:
627
+ example_images = gr.Markdown("*No example images available*")
628
 
629
  submit_btn = gr.Button(translations['en']['identify_button'], variant="primary")
630
 
 
693
  t['question_label'],
694
  t['question_placeholder'],
695
  t['submit_question'],
696
+ t['clear_conversation']
 
 
697
  )
698
 
699
+ def handle_example_select(evt: gr.SelectData):
700
+ """Handle selection of example images"""
701
+ if evt.index is not None:
702
+ available_examples = get_available_examples()
703
+ if evt.index < len(available_examples):
704
+ selected_example = available_examples[evt.index]
705
+ return load_example_image(selected_example["path"])
706
+ return None
 
 
 
 
 
707
 
708
  # Set up event handlers
709
  language_selector.change(
 
722
  follow_up_input,
723
  follow_up_input,
724
  follow_up_btn,
725
+ clear_btn
 
726
  ]
727
  )
728
 
729
+ # Handle example image selection
730
+ if available_examples:
731
+ example_images.select(
732
+ handle_example_select,
733
  outputs=[input_image]
734
  )
735