Jacksonnavigator7 commited on
Commit
4955cfc
·
verified ·
1 Parent(s): 09adb2a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -37
app.py CHANGED
@@ -45,7 +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 - Click on any image to use it"
49
  },
50
  "sw": {
51
  "app_title": "Mtafiti wa Ndege: Utambuzi wa Kiotomatiki kwa Watafiti",
@@ -69,7 +69,7 @@ translations = {
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
 
@@ -606,32 +606,38 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
606
  with gr.Row():
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
 
631
  with gr.Column(scale=2):
632
  prediction_output = gr.Label(label=translations['en']['predictions_label'], num_top_classes=5)
633
  bird_info_output = gr.HTML(label=translations['en']['bird_info_label'])
634
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
635
  # Clear divider
636
  gr.Markdown("---")
637
 
@@ -685,10 +691,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
685
  f"# {t['app_title']}",
686
  f"{t['app_description']}",
687
  t['upload_label'],
688
- f"### {t['example_images']}",
689
  t['identify_button'],
690
  t['predictions_label'],
691
  t['bird_info_label'],
 
692
  f"## {t['research_questions']}",
693
  t['question_label'],
694
  t['question_placeholder'],
@@ -696,14 +702,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
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(
@@ -714,10 +715,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
714
  title_md,
715
  description_md,
716
  input_image,
717
- example_header,
718
  submit_btn,
719
  prediction_output,
720
  bird_info_output,
 
721
  questions_header,
722
  follow_up_input,
723
  follow_up_input,
@@ -726,12 +727,14 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
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
 
736
  # Add loading state for better UX
737
  submit_btn.click(
 
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
+ "examples_title": "Examples"
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
+ "examples_title": "Mifano"
73
  }
74
  }
75
 
 
606
  with gr.Row():
607
  with gr.Column(scale=1):
608
  input_image = gr.Image(type="pil", label=translations['en']['upload_label'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
609
  submit_btn = gr.Button(translations['en']['identify_button'], variant="primary")
610
 
611
  with gr.Column(scale=2):
612
  prediction_output = gr.Label(label=translations['en']['predictions_label'], num_top_classes=5)
613
  bird_info_output = gr.HTML(label=translations['en']['bird_info_label'])
614
 
615
+ # Examples section - horizontal layout below main content
616
+ examples_header = gr.Markdown(f"### {translations['en']['examples_title']}")
617
+
618
+ # Create horizontal row of example images
619
+ available_examples = get_available_examples()
620
+ if available_examples:
621
+ with gr.Row():
622
+ example_buttons = []
623
+ for example in available_examples:
624
+ with gr.Column(scale=1):
625
+ example_image = gr.Image(
626
+ value=example["path"],
627
+ label=example["name"],
628
+ show_label=True,
629
+ interactive=False,
630
+ height=150
631
+ )
632
+ example_btn = gr.Button(
633
+ f"Use {example['name']}",
634
+ size="sm",
635
+ variant="secondary"
636
+ )
637
+ example_buttons.append((example_btn, example["path"]))
638
+ else:
639
+ gr.Markdown("*No example images available*")
640
+
641
  # Clear divider
642
  gr.Markdown("---")
643
 
 
691
  f"# {t['app_title']}",
692
  f"{t['app_description']}",
693
  t['upload_label'],
 
694
  t['identify_button'],
695
  t['predictions_label'],
696
  t['bird_info_label'],
697
+ f"### {t['examples_title']}",
698
  f"## {t['research_questions']}",
699
  t['question_label'],
700
  t['question_placeholder'],
 
702
  t['clear_conversation']
703
  )
704
 
705
+ def use_example_image(image_path):
706
+ """Load and return example image"""
707
+ return load_example_image(image_path)
 
 
 
 
 
708
 
709
  # Set up event handlers
710
  language_selector.change(
 
715
  title_md,
716
  description_md,
717
  input_image,
 
718
  submit_btn,
719
  prediction_output,
720
  bird_info_output,
721
+ examples_header,
722
  questions_header,
723
  follow_up_input,
724
  follow_up_input,
 
727
  ]
728
  )
729
 
730
+ # Set up example button event handlers
731
  if available_examples:
732
+ for example_btn, image_path in example_buttons:
733
+ example_btn.click(
734
+ use_example_image,
735
+ inputs=[gr.State(image_path)],
736
+ outputs=[input_image]
737
+ )
738
 
739
  # Add loading state for better UX
740
  submit_btn.click(