Update app.py
Browse files
app.py
CHANGED
|
@@ -50,8 +50,14 @@ def predict_and_get_info(img):
|
|
| 50 |
# Get prediction
|
| 51 |
pred, pred_idx, probs = learn.predict(img)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Get top prediction
|
| 57 |
top_bird = str(pred)
|
|
@@ -59,10 +65,13 @@ def predict_and_get_info(img):
|
|
| 59 |
# Get detailed information about the top predicted bird
|
| 60 |
bird_info = get_bird_info(top_bird)
|
| 61 |
|
| 62 |
-
return prediction_results, bird_info
|
| 63 |
|
| 64 |
def follow_up_question(question, bird_name):
|
| 65 |
"""Allow researchers to ask follow-up questions about the identified bird"""
|
|
|
|
|
|
|
|
|
|
| 66 |
prompt = f"""
|
| 67 |
The researcher is asking about the {bird_name} bird: "{question}"
|
| 68 |
|
|
@@ -90,47 +99,89 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 90 |
gr.Markdown("# Bird Species Identification for Researchers")
|
| 91 |
gr.Markdown("Upload an image to identify bird species and get detailed information relevant to research in Tanzania and climate change studies.")
|
| 92 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
with gr.Row():
|
| 94 |
with gr.Column(scale=1):
|
| 95 |
input_image = gr.Image(type="pil", label="Upload Bird Image")
|
| 96 |
submit_btn = gr.Button("Identify Bird", variant="primary")
|
| 97 |
|
| 98 |
with gr.Column(scale=2):
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
# For follow-up questions
|
| 105 |
with gr.Row():
|
| 106 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
with gr.Row():
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
follow_up_input = gr.Textbox(label="Your Question", placeholder="Ask more about this bird species...")
|
| 112 |
-
follow_up_btn = gr.Button("Submit Question")
|
| 113 |
-
|
| 114 |
-
with gr.Column(scale=2):
|
| 115 |
-
follow_up_output = gr.Markdown(label="Answer")
|
| 116 |
|
| 117 |
# Set up event handlers
|
| 118 |
def process_image(img):
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
submit_btn.click(
|
| 125 |
process_image,
|
| 126 |
inputs=[input_image],
|
| 127 |
-
outputs=[prediction_output, bird_info_output, current_bird]
|
| 128 |
)
|
| 129 |
|
| 130 |
follow_up_btn.click(
|
| 131 |
-
|
| 132 |
-
inputs=[follow_up_input, current_bird],
|
| 133 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
)
|
| 135 |
|
| 136 |
# Launch the app
|
|
|
|
| 50 |
# Get prediction
|
| 51 |
pred, pred_idx, probs = learn.predict(img)
|
| 52 |
|
| 53 |
+
# Get top 5 predictions (or all if less than 5)
|
| 54 |
+
num_classes = min(5, len(labels))
|
| 55 |
+
top_indices = probs.argsort(descending=True)[:num_classes]
|
| 56 |
+
top_probs = probs[top_indices]
|
| 57 |
+
top_labels = [labels[i] for i in top_indices]
|
| 58 |
+
|
| 59 |
+
# Format as dictionary for display
|
| 60 |
+
prediction_results = {top_labels[i]: float(top_probs[i]) for i in range(num_classes)}
|
| 61 |
|
| 62 |
# Get top prediction
|
| 63 |
top_bird = str(pred)
|
|
|
|
| 65 |
# Get detailed information about the top predicted bird
|
| 66 |
bird_info = get_bird_info(top_bird)
|
| 67 |
|
| 68 |
+
return prediction_results, bird_info, top_bird
|
| 69 |
|
| 70 |
def follow_up_question(question, bird_name):
|
| 71 |
"""Allow researchers to ask follow-up questions about the identified bird"""
|
| 72 |
+
if not question.strip() or not bird_name:
|
| 73 |
+
return "Please identify a bird first and ask a specific question about it."
|
| 74 |
+
|
| 75 |
prompt = f"""
|
| 76 |
The researcher is asking about the {bird_name} bird: "{question}"
|
| 77 |
|
|
|
|
| 99 |
gr.Markdown("# Bird Species Identification for Researchers")
|
| 100 |
gr.Markdown("Upload an image to identify bird species and get detailed information relevant to research in Tanzania and climate change studies.")
|
| 101 |
|
| 102 |
+
# Store the current bird for context
|
| 103 |
+
current_bird = gr.State("")
|
| 104 |
+
|
| 105 |
+
# Main identification section
|
| 106 |
with gr.Row():
|
| 107 |
with gr.Column(scale=1):
|
| 108 |
input_image = gr.Image(type="pil", label="Upload Bird Image")
|
| 109 |
submit_btn = gr.Button("Identify Bird", variant="primary")
|
| 110 |
|
| 111 |
with gr.Column(scale=2):
|
| 112 |
+
prediction_output = gr.Label(label="Top 5 Predictions", num_top_classes=5)
|
| 113 |
+
bird_info_output = gr.Markdown(label="Bird Information")
|
| 114 |
+
|
| 115 |
+
# Clear divider
|
| 116 |
+
gr.Markdown("---")
|
| 117 |
+
|
| 118 |
+
# Follow-up question section with improved UI
|
| 119 |
+
gr.Markdown("## Research Questions")
|
| 120 |
+
|
| 121 |
+
conversation_history = gr.Markdown("")
|
| 122 |
|
|
|
|
| 123 |
with gr.Row():
|
| 124 |
+
follow_up_input = gr.Textbox(
|
| 125 |
+
label="Ask a question about this bird",
|
| 126 |
+
placeholder="Example: How has climate change affected this bird's migration pattern?",
|
| 127 |
+
lines=2
|
| 128 |
+
)
|
| 129 |
|
| 130 |
with gr.Row():
|
| 131 |
+
follow_up_btn = gr.Button("Submit Question", variant="primary")
|
| 132 |
+
clear_btn = gr.Button("Clear Conversation")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
# Set up event handlers
|
| 135 |
def process_image(img):
|
| 136 |
+
if img is None:
|
| 137 |
+
return None, "Please upload an image", "", ""
|
| 138 |
+
|
| 139 |
+
try:
|
| 140 |
+
pred_results, info, bird_name = predict_and_get_info(img)
|
| 141 |
+
return pred_results, info, bird_name, ""
|
| 142 |
+
except Exception as e:
|
| 143 |
+
return None, f"Error processing image: {str(e)}", "", ""
|
| 144 |
+
|
| 145 |
+
def update_conversation(question, bird_name, history):
|
| 146 |
+
if not question.strip():
|
| 147 |
+
return history
|
| 148 |
+
|
| 149 |
+
answer = follow_up_question(question, bird_name)
|
| 150 |
+
|
| 151 |
+
# Format the conversation with clear separation
|
| 152 |
+
new_exchange = f"""
|
| 153 |
+
### Question:
|
| 154 |
+
{question}
|
| 155 |
+
|
| 156 |
+
### Answer:
|
| 157 |
+
{answer}
|
| 158 |
+
|
| 159 |
+
---
|
| 160 |
+
"""
|
| 161 |
+
updated_history = new_exchange + history
|
| 162 |
+
return updated_history
|
| 163 |
+
|
| 164 |
+
def clear_conversation_history():
|
| 165 |
+
return ""
|
| 166 |
|
| 167 |
submit_btn.click(
|
| 168 |
process_image,
|
| 169 |
inputs=[input_image],
|
| 170 |
+
outputs=[prediction_output, bird_info_output, current_bird, conversation_history]
|
| 171 |
)
|
| 172 |
|
| 173 |
follow_up_btn.click(
|
| 174 |
+
update_conversation,
|
| 175 |
+
inputs=[follow_up_input, current_bird, conversation_history],
|
| 176 |
+
outputs=[conversation_history]
|
| 177 |
+
).then(
|
| 178 |
+
lambda: "",
|
| 179 |
+
outputs=follow_up_input
|
| 180 |
+
)
|
| 181 |
+
|
| 182 |
+
clear_btn.click(
|
| 183 |
+
clear_conversation_history,
|
| 184 |
+
outputs=[conversation_history]
|
| 185 |
)
|
| 186 |
|
| 187 |
# Launch the app
|