Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ from groq import Groq
|
|
7 |
from PIL import Image
|
8 |
import time
|
9 |
import json
|
|
|
10 |
from functools import lru_cache
|
11 |
|
12 |
# Load the trained model
|
@@ -71,6 +72,7 @@ translations = {
|
|
71 |
}
|
72 |
}
|
73 |
|
|
|
74 |
def clean_bird_name(name):
|
75 |
"""Clean bird name by removing numbers and special characters, and fix formatting"""
|
76 |
# Remove numbers and dots at the beginning
|
@@ -83,11 +85,13 @@ def clean_bird_name(name):
|
|
83 |
cleaned = ' '.join(cleaned.split())
|
84 |
return cleaned
|
85 |
|
|
|
86 |
def get_cache_path(function_name, key):
|
87 |
"""Generate a cache file path"""
|
88 |
safe_key = re.sub(r'[^\w]', '_', key)
|
89 |
return f"cache/{function_name}_{safe_key}.json"
|
90 |
|
|
|
91 |
def save_to_cache(function_name, key, data):
|
92 |
"""Save API response to cache"""
|
93 |
try:
|
@@ -97,6 +101,7 @@ def save_to_cache(function_name, key, data):
|
|
97 |
except Exception as e:
|
98 |
print(f"Error saving to cache: {e}")
|
99 |
|
|
|
100 |
def load_from_cache(function_name, key, max_age=86400): # Default max age: 1 day
|
101 |
"""Load API response from cache if it exists and is not too old"""
|
102 |
try:
|
@@ -110,6 +115,7 @@ def load_from_cache(function_name, key, max_age=86400): # Default max age: 1 da
|
|
110 |
print(f"Error loading from cache: {e}")
|
111 |
return None
|
112 |
|
|
|
113 |
def is_likely_bird_image(img):
|
114 |
"""Basic check to see if the image might contain a bird"""
|
115 |
try:
|
@@ -136,6 +142,7 @@ def is_likely_bird_image(img):
|
|
136 |
# If any error occurs during the check, assume it might be a bird
|
137 |
return True
|
138 |
|
|
|
139 |
def get_bird_habitat_map(bird_name, check_tanzania=True):
|
140 |
"""Get habitat map locations for the bird using Groq API with caching"""
|
141 |
clean_name = clean_bird_name(bird_name)
|
@@ -227,6 +234,7 @@ def get_bird_habitat_map(bird_name, check_tanzania=True):
|
|
227 |
return [{"name": "Error retrieving data", "lat": 0, "lon": 0,
|
228 |
"description": "Please try again or check your connection."}], False
|
229 |
|
|
|
230 |
def create_habitat_map(habitat_locations):
|
231 |
"""Create a folium map with the habitat locations"""
|
232 |
# Find center point based on valid coordinates
|
@@ -266,6 +274,7 @@ def create_habitat_map(habitat_locations):
|
|
266 |
map_html = m._repr_html_()
|
267 |
return map_html
|
268 |
|
|
|
269 |
def format_bird_info(raw_info, language="en"):
|
270 |
"""Improve the formatting of bird information"""
|
271 |
# Add proper line breaks between sections and ensure consistent heading levels
|
@@ -293,6 +302,7 @@ def format_bird_info(raw_info, language="en"):
|
|
293 |
|
294 |
return formatted
|
295 |
|
|
|
296 |
def get_bird_info(bird_name, language="en"):
|
297 |
"""Get detailed information about a bird using Groq API with caching"""
|
298 |
clean_name = clean_bird_name(bird_name)
|
@@ -339,14 +349,14 @@ def get_bird_info(bird_name, language="en"):
|
|
339 |
error_msg = "Hitilafu katika kupata taarifa" if language == "sw" else "Error fetching information"
|
340 |
return f"{error_msg}: {str(e)}"
|
341 |
|
|
|
342 |
def create_message_html(message, icon="π", language="en"):
|
343 |
"""Create a styled message container for notifications"""
|
344 |
custom_css = """
|
345 |
<style>
|
346 |
.message-container {
|
347 |
font-family: Arial, sans-serif;
|
348 |
-
padding:
|
349 |
-
20px;
|
350 |
background-color: #f8f9fa;
|
351 |
border-radius: 8px;
|
352 |
text-align: center;
|
@@ -372,6 +382,7 @@ def create_message_html(message, icon="π", language="en"):
|
|
372 |
"""
|
373 |
return html
|
374 |
|
|
|
375 |
def predict_and_get_info(img, language="en"):
|
376 |
"""Predict bird species and get detailed information"""
|
377 |
# Get translations
|
@@ -502,6 +513,7 @@ def predict_and_get_info(img, language="en"):
|
|
502 |
error_msg = "Hitilafu katika kuchakata picha" if language == "sw" else "Error processing image"
|
503 |
return None, create_message_html(f"{error_msg}: {str(e)}", "β οΈ", language), "", ""
|
504 |
|
|
|
505 |
def follow_up_question(question, bird_name, language="en"):
|
506 |
"""Allow researchers to ask follow-up questions about the identified bird"""
|
507 |
t = translations[language]
|
@@ -551,6 +563,7 @@ def follow_up_question(question, bird_name, language="en"):
|
|
551 |
error_msg = "Hitilafu katika kupata taarifa" if language == "sw" else "Error fetching information"
|
552 |
return f"{error_msg}: {str(e)}"
|
553 |
|
|
|
554 |
# Create the Gradio interface
|
555 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
556 |
# Current language and bird state
|
|
|
7 |
from PIL import Image
|
8 |
import time
|
9 |
import json
|
10 |
+
import numpy as np
|
11 |
from functools import lru_cache
|
12 |
|
13 |
# Load the trained model
|
|
|
72 |
}
|
73 |
}
|
74 |
|
75 |
+
|
76 |
def clean_bird_name(name):
|
77 |
"""Clean bird name by removing numbers and special characters, and fix formatting"""
|
78 |
# Remove numbers and dots at the beginning
|
|
|
85 |
cleaned = ' '.join(cleaned.split())
|
86 |
return cleaned
|
87 |
|
88 |
+
|
89 |
def get_cache_path(function_name, key):
|
90 |
"""Generate a cache file path"""
|
91 |
safe_key = re.sub(r'[^\w]', '_', key)
|
92 |
return f"cache/{function_name}_{safe_key}.json"
|
93 |
|
94 |
+
|
95 |
def save_to_cache(function_name, key, data):
|
96 |
"""Save API response to cache"""
|
97 |
try:
|
|
|
101 |
except Exception as e:
|
102 |
print(f"Error saving to cache: {e}")
|
103 |
|
104 |
+
|
105 |
def load_from_cache(function_name, key, max_age=86400): # Default max age: 1 day
|
106 |
"""Load API response from cache if it exists and is not too old"""
|
107 |
try:
|
|
|
115 |
print(f"Error loading from cache: {e}")
|
116 |
return None
|
117 |
|
118 |
+
|
119 |
def is_likely_bird_image(img):
|
120 |
"""Basic check to see if the image might contain a bird"""
|
121 |
try:
|
|
|
142 |
# If any error occurs during the check, assume it might be a bird
|
143 |
return True
|
144 |
|
145 |
+
|
146 |
def get_bird_habitat_map(bird_name, check_tanzania=True):
|
147 |
"""Get habitat map locations for the bird using Groq API with caching"""
|
148 |
clean_name = clean_bird_name(bird_name)
|
|
|
234 |
return [{"name": "Error retrieving data", "lat": 0, "lon": 0,
|
235 |
"description": "Please try again or check your connection."}], False
|
236 |
|
237 |
+
|
238 |
def create_habitat_map(habitat_locations):
|
239 |
"""Create a folium map with the habitat locations"""
|
240 |
# Find center point based on valid coordinates
|
|
|
274 |
map_html = m._repr_html_()
|
275 |
return map_html
|
276 |
|
277 |
+
|
278 |
def format_bird_info(raw_info, language="en"):
|
279 |
"""Improve the formatting of bird information"""
|
280 |
# Add proper line breaks between sections and ensure consistent heading levels
|
|
|
302 |
|
303 |
return formatted
|
304 |
|
305 |
+
|
306 |
def get_bird_info(bird_name, language="en"):
|
307 |
"""Get detailed information about a bird using Groq API with caching"""
|
308 |
clean_name = clean_bird_name(bird_name)
|
|
|
349 |
error_msg = "Hitilafu katika kupata taarifa" if language == "sw" else "Error fetching information"
|
350 |
return f"{error_msg}: {str(e)}"
|
351 |
|
352 |
+
|
353 |
def create_message_html(message, icon="π", language="en"):
|
354 |
"""Create a styled message container for notifications"""
|
355 |
custom_css = """
|
356 |
<style>
|
357 |
.message-container {
|
358 |
font-family: Arial, sans-serif;
|
359 |
+
padding: 20px;
|
|
|
360 |
background-color: #f8f9fa;
|
361 |
border-radius: 8px;
|
362 |
text-align: center;
|
|
|
382 |
"""
|
383 |
return html
|
384 |
|
385 |
+
|
386 |
def predict_and_get_info(img, language="en"):
|
387 |
"""Predict bird species and get detailed information"""
|
388 |
# Get translations
|
|
|
513 |
error_msg = "Hitilafu katika kuchakata picha" if language == "sw" else "Error processing image"
|
514 |
return None, create_message_html(f"{error_msg}: {str(e)}", "β οΈ", language), "", ""
|
515 |
|
516 |
+
|
517 |
def follow_up_question(question, bird_name, language="en"):
|
518 |
"""Allow researchers to ask follow-up questions about the identified bird"""
|
519 |
t = translations[language]
|
|
|
563 |
error_msg = "Hitilafu katika kupata taarifa" if language == "sw" else "Error fetching information"
|
564 |
return f"{error_msg}: {str(e)}"
|
565 |
|
566 |
+
|
567 |
# Create the Gradio interface
|
568 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
569 |
# Current language and bird state
|