Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import re
|
|
|
|
| 4 |
from fastai.vision.all import *
|
| 5 |
from groq import Groq
|
| 6 |
from PIL import Image
|
|
@@ -26,6 +27,87 @@ def clean_bird_name(name):
|
|
| 26 |
cleaned = ' '.join(cleaned.split())
|
| 27 |
return cleaned
|
| 28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
def get_bird_info(bird_name):
|
| 30 |
"""Get detailed information about a bird using Groq API"""
|
| 31 |
clean_name = clean_bird_name(bird_name)
|
|
@@ -79,10 +161,25 @@ def predict_and_get_info(img):
|
|
| 79 |
# Also keep a clean version for display
|
| 80 |
clean_top_bird = clean_bird_name(top_bird)
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
# Get detailed information about the top predicted bird
|
| 83 |
bird_info = get_bird_info(top_bird)
|
| 84 |
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
def follow_up_question(question, bird_name):
|
| 88 |
"""Allow researchers to ask follow-up questions about the identified bird"""
|
|
@@ -132,7 +229,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
| 132 |
|
| 133 |
with gr.Column(scale=2):
|
| 134 |
prediction_output = gr.Label(label="Top 5 Predictions", num_top_classes=5)
|
| 135 |
-
bird_info_output = gr.
|
| 136 |
|
| 137 |
# Clear divider
|
| 138 |
gr.Markdown("---")
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import re
|
| 4 |
+
import folium
|
| 5 |
from fastai.vision.all import *
|
| 6 |
from groq import Groq
|
| 7 |
from PIL import Image
|
|
|
|
| 27 |
cleaned = ' '.join(cleaned.split())
|
| 28 |
return cleaned
|
| 29 |
|
| 30 |
+
def get_bird_habitat_map(bird_name):
|
| 31 |
+
"""Get habitat map locations for the bird using Groq API"""
|
| 32 |
+
clean_name = clean_bird_name(bird_name)
|
| 33 |
+
|
| 34 |
+
prompt = f"""
|
| 35 |
+
Provide a JSON array of the main habitat locations for the {clean_name} bird in the world.
|
| 36 |
+
Return ONLY a JSON array with 3-5 entries, each containing:
|
| 37 |
+
1. "name": Location name
|
| 38 |
+
2. "lat": Latitude (numeric value)
|
| 39 |
+
3. "lon": Longitude (numeric value)
|
| 40 |
+
4. "description": Brief description of why this is a key habitat (2-3 sentences)
|
| 41 |
+
|
| 42 |
+
Example format:
|
| 43 |
+
[
|
| 44 |
+
{{"name": "Example Location", "lat": 12.34, "lon": 56.78, "description": "Brief description"}},
|
| 45 |
+
...
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
If this bird is not commonly found in Tanzania, include at least one location in Tanzania with a note about its unusual presence there.
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
chat_completion = client.chat.completions.create(
|
| 53 |
+
messages=[
|
| 54 |
+
{
|
| 55 |
+
"role": "user",
|
| 56 |
+
"content": prompt,
|
| 57 |
+
}
|
| 58 |
+
],
|
| 59 |
+
model="llama-3.3-70b-versatile",
|
| 60 |
+
)
|
| 61 |
+
response = chat_completion.choices[0].message.content
|
| 62 |
+
|
| 63 |
+
# Extract JSON from response (in case there's additional text)
|
| 64 |
+
import json
|
| 65 |
+
import re
|
| 66 |
+
|
| 67 |
+
# Find JSON pattern in response
|
| 68 |
+
json_match = re.search(r'\[.*\]', response, re.DOTALL)
|
| 69 |
+
if json_match:
|
| 70 |
+
locations = json.loads(json_match.group())
|
| 71 |
+
else:
|
| 72 |
+
# Fallback if JSON extraction fails
|
| 73 |
+
locations = [
|
| 74 |
+
{"name": "Habitat information unavailable", "lat": 0, "lon": 0,
|
| 75 |
+
"description": "Could not retrieve habitat information for this bird."}
|
| 76 |
+
]
|
| 77 |
+
|
| 78 |
+
return locations
|
| 79 |
+
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return [{"name": f"Error retrieving habitat data: {str(e)}", "lat": 0, "lon": 0,
|
| 82 |
+
"description": "Please try again or check your connection."}]
|
| 83 |
+
|
| 84 |
+
def create_habitat_map(habitat_locations):
|
| 85 |
+
"""Create a folium map with the habitat locations"""
|
| 86 |
+
# Create a world-centered map
|
| 87 |
+
m = folium.Map(location=[20, 0], zoom_start=2)
|
| 88 |
+
|
| 89 |
+
# Add markers for each habitat location
|
| 90 |
+
for location in habitat_locations:
|
| 91 |
+
name = location.get("name", "Unknown")
|
| 92 |
+
lat = location.get("lat", 0)
|
| 93 |
+
lon = location.get("lon", 0)
|
| 94 |
+
description = location.get("description", "No description available")
|
| 95 |
+
|
| 96 |
+
# Skip invalid coordinates
|
| 97 |
+
if lat == 0 and lon == 0:
|
| 98 |
+
continue
|
| 99 |
+
|
| 100 |
+
# Add marker
|
| 101 |
+
folium.Marker(
|
| 102 |
+
location=[lat, lon],
|
| 103 |
+
popup=folium.Popup(f"<b>{name}</b><br>{description}", max_width=300),
|
| 104 |
+
tooltip=name
|
| 105 |
+
).add_to(m)
|
| 106 |
+
|
| 107 |
+
# Save map to HTML
|
| 108 |
+
map_html = m._repr_html_()
|
| 109 |
+
return map_html
|
| 110 |
+
|
| 111 |
def get_bird_info(bird_name):
|
| 112 |
"""Get detailed information about a bird using Groq API"""
|
| 113 |
clean_name = clean_bird_name(bird_name)
|
|
|
|
| 161 |
# Also keep a clean version for display
|
| 162 |
clean_top_bird = clean_bird_name(top_bird)
|
| 163 |
|
| 164 |
+
# Get habitat locations and create map
|
| 165 |
+
habitat_locations = get_bird_habitat_map(top_bird)
|
| 166 |
+
habitat_map_html = create_habitat_map(habitat_locations)
|
| 167 |
+
|
| 168 |
# Get detailed information about the top predicted bird
|
| 169 |
bird_info = get_bird_info(top_bird)
|
| 170 |
|
| 171 |
+
# Create combined info with map at the top
|
| 172 |
+
combined_info = f"""
|
| 173 |
+
## Natural Habitat Map for {clean_top_bird}
|
| 174 |
+
<div style="height: 400px; width: 100%; border: 1px solid #ddd; border-radius: 8px; overflow: hidden; margin-bottom: 20px;">
|
| 175 |
+
{habitat_map_html}
|
| 176 |
+
</div>
|
| 177 |
+
|
| 178 |
+
## Detailed Information
|
| 179 |
+
{bird_info}
|
| 180 |
+
"""
|
| 181 |
+
|
| 182 |
+
return prediction_results, combined_info, clean_top_bird
|
| 183 |
|
| 184 |
def follow_up_question(question, bird_name):
|
| 185 |
"""Allow researchers to ask follow-up questions about the identified bird"""
|
|
|
|
| 229 |
|
| 230 |
with gr.Column(scale=2):
|
| 231 |
prediction_output = gr.Label(label="Top 5 Predictions", num_top_classes=5)
|
| 232 |
+
bird_info_output = gr.HTML(label="Bird Information")
|
| 233 |
|
| 234 |
# Clear divider
|
| 235 |
gr.Markdown("---")
|