Update app.py
Browse files
app.py
CHANGED
|
@@ -12,7 +12,7 @@ st.title("🌍 Biodiversity GIS Explorer")
|
|
| 12 |
st.write("Explore biodiversity species using GBIF + AI analysis")
|
| 13 |
|
| 14 |
# =====================================================
|
| 15 |
-
# GEMINI
|
| 16 |
# =====================================================
|
| 17 |
|
| 18 |
def call_gemini(prompt, api_key):
|
|
@@ -55,8 +55,7 @@ country_codes = {
|
|
| 55 |
"kenya": "KE",
|
| 56 |
"ethiopia": "ET",
|
| 57 |
"usa": "US",
|
| 58 |
-
"america": "US"
|
| 59 |
-
"canada": "CA"
|
| 60 |
}
|
| 61 |
|
| 62 |
# =====================================================
|
|
@@ -73,7 +72,7 @@ species_filters = {
|
|
| 73 |
}
|
| 74 |
|
| 75 |
# =====================================================
|
| 76 |
-
#
|
| 77 |
# =====================================================
|
| 78 |
|
| 79 |
def get_species(country_code, filter_value, species_type):
|
|
@@ -82,9 +81,9 @@ def get_species(country_code, filter_value, species_type):
|
|
| 82 |
"https://api.gbif.org/v1/occurrence/search?"
|
| 83 |
f"country={country_code}"
|
| 84 |
f"&{filter_value}"
|
| 85 |
-
"&limit=
|
| 86 |
-
"&has_coordinate=true"
|
| 87 |
"&occurrence_status=PRESENT"
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
try:
|
|
@@ -95,7 +94,7 @@ def get_species(country_code, filter_value, species_type):
|
|
| 95 |
species = []
|
| 96 |
|
| 97 |
bad_words = [
|
| 98 |
-
"
|
| 99 |
"unknown", "incertae", "sp", "cf"
|
| 100 |
]
|
| 101 |
|
|
@@ -105,6 +104,7 @@ def get_species(country_code, filter_value, species_type):
|
|
| 105 |
kingdom = item.get("kingdom")
|
| 106 |
cls = item.get("class")
|
| 107 |
status = item.get("establishmentMeans")
|
|
|
|
| 108 |
|
| 109 |
if not sp:
|
| 110 |
continue
|
|
@@ -115,23 +115,46 @@ def get_species(country_code, filter_value, species_type):
|
|
| 115 |
if any(b in sp_low for b in bad_words):
|
| 116 |
continue
|
| 117 |
|
| 118 |
-
# must be
|
| 119 |
if len(sp.split()) < 2:
|
| 120 |
continue
|
| 121 |
|
| 122 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
if species_type == "Birds":
|
|
|
|
| 124 |
if cls != "Aves":
|
| 125 |
continue
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
continue
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
elif species_type == "Plants":
|
| 132 |
if kingdom != "Plantae":
|
| 133 |
continue
|
| 134 |
|
|
|
|
| 135 |
else:
|
| 136 |
if kingdom != "Animalia":
|
| 137 |
continue
|
|
@@ -164,23 +187,13 @@ def get_species_links(species_name, country):
|
|
| 164 |
).json()
|
| 165 |
|
| 166 |
if "usageKey" in r:
|
| 167 |
-
links.append(
|
| 168 |
-
f"- [GBIF](https://www.gbif.org/species/{r['usageKey']})"
|
| 169 |
-
)
|
| 170 |
except:
|
| 171 |
pass
|
| 172 |
|
| 173 |
-
links.append(
|
| 174 |
-
|
| 175 |
-
)
|
| 176 |
-
|
| 177 |
-
links.append(
|
| 178 |
-
f"- [iNaturalist](https://www.inaturalist.org/search?q={q})"
|
| 179 |
-
)
|
| 180 |
-
|
| 181 |
-
links.append(
|
| 182 |
-
f"- [Google Scholar](https://scholar.google.com/scholar?q={q}+{c})"
|
| 183 |
-
)
|
| 184 |
|
| 185 |
return links
|
| 186 |
|
|
@@ -202,12 +215,7 @@ species_type = st.selectbox(
|
|
| 202 |
|
| 203 |
if st.button("Search Biodiversity"):
|
| 204 |
|
| 205 |
-
|
| 206 |
-
country_code = country_codes.get(country_clean)
|
| 207 |
-
|
| 208 |
-
if not country:
|
| 209 |
-
st.warning("Enter country")
|
| 210 |
-
st.stop()
|
| 211 |
|
| 212 |
if not country_code:
|
| 213 |
st.error("Country not supported")
|
|
@@ -228,7 +236,7 @@ if st.button("Search Biodiversity"):
|
|
| 228 |
st.write("•", sp)
|
| 229 |
|
| 230 |
# =================================================
|
| 231 |
-
# AI
|
| 232 |
# =================================================
|
| 233 |
|
| 234 |
prompt = f"""
|
|
@@ -240,7 +248,7 @@ Group: {species_type}
|
|
| 240 |
Species:
|
| 241 |
{chr(10).join(species_list)}
|
| 242 |
|
| 243 |
-
|
| 244 |
"""
|
| 245 |
|
| 246 |
st.subheader("🤖 AI Analysis")
|
|
|
|
| 12 |
st.write("Explore biodiversity species using GBIF + AI analysis")
|
| 13 |
|
| 14 |
# =====================================================
|
| 15 |
+
# GEMINI SAFE
|
| 16 |
# =====================================================
|
| 17 |
|
| 18 |
def call_gemini(prompt, api_key):
|
|
|
|
| 55 |
"kenya": "KE",
|
| 56 |
"ethiopia": "ET",
|
| 57 |
"usa": "US",
|
| 58 |
+
"america": "US"
|
|
|
|
| 59 |
}
|
| 60 |
|
| 61 |
# =====================================================
|
|
|
|
| 72 |
}
|
| 73 |
|
| 74 |
# =====================================================
|
| 75 |
+
# CLEAN GBIF (STRONG FILTER ENGINE)
|
| 76 |
# =====================================================
|
| 77 |
|
| 78 |
def get_species(country_code, filter_value, species_type):
|
|
|
|
| 81 |
"https://api.gbif.org/v1/occurrence/search?"
|
| 82 |
f"country={country_code}"
|
| 83 |
f"&{filter_value}"
|
| 84 |
+
"&limit=300"
|
|
|
|
| 85 |
"&occurrence_status=PRESENT"
|
| 86 |
+
"&has_coordinate=true"
|
| 87 |
)
|
| 88 |
|
| 89 |
try:
|
|
|
|
| 94 |
species = []
|
| 95 |
|
| 96 |
bad_words = [
|
| 97 |
+
"sp.", "cf.", "aff.", "unclassified",
|
| 98 |
"unknown", "incertae", "sp", "cf"
|
| 99 |
]
|
| 100 |
|
|
|
|
| 104 |
kingdom = item.get("kingdom")
|
| 105 |
cls = item.get("class")
|
| 106 |
status = item.get("establishmentMeans")
|
| 107 |
+
tax_status = item.get("taxonomicStatus")
|
| 108 |
|
| 109 |
if not sp:
|
| 110 |
continue
|
|
|
|
| 115 |
if any(b in sp_low for b in bad_words):
|
| 116 |
continue
|
| 117 |
|
| 118 |
+
# must be proper scientific name
|
| 119 |
if len(sp.split()) < 2:
|
| 120 |
continue
|
| 121 |
|
| 122 |
+
# =================================================
|
| 123 |
+
# STRONG FILTER ENGINE
|
| 124 |
+
# =================================================
|
| 125 |
+
|
| 126 |
+
if tax_status and tax_status != "ACCEPTED":
|
| 127 |
+
continue
|
| 128 |
+
|
| 129 |
+
# BIRDS ONLY CLEAN
|
| 130 |
if species_type == "Birds":
|
| 131 |
+
|
| 132 |
if cls != "Aves":
|
| 133 |
continue
|
| 134 |
|
| 135 |
+
if status in ["INTRODUCED", "ASSISTED COLONIZATION", "MANAGED"]:
|
| 136 |
+
continue
|
| 137 |
+
|
| 138 |
+
# remove urban-only birds (optional but important)
|
| 139 |
+
urban = ["Columba livia", "Passer domesticus", "Cairina moschata"]
|
| 140 |
+
if sp in urban:
|
| 141 |
+
continue
|
| 142 |
+
|
| 143 |
+
# REPTILES ONLY CLEAN
|
| 144 |
+
elif species_type == "Reptiles":
|
| 145 |
+
|
| 146 |
+
if cls != "Reptilia":
|
| 147 |
continue
|
| 148 |
|
| 149 |
+
if kingdom != "Animalia":
|
| 150 |
+
continue
|
| 151 |
+
|
| 152 |
+
# PLANTS
|
| 153 |
elif species_type == "Plants":
|
| 154 |
if kingdom != "Plantae":
|
| 155 |
continue
|
| 156 |
|
| 157 |
+
# OTHER ANIMALS
|
| 158 |
else:
|
| 159 |
if kingdom != "Animalia":
|
| 160 |
continue
|
|
|
|
| 187 |
).json()
|
| 188 |
|
| 189 |
if "usageKey" in r:
|
| 190 |
+
links.append(f"- [GBIF](https://www.gbif.org/species/{r['usageKey']})")
|
|
|
|
|
|
|
| 191 |
except:
|
| 192 |
pass
|
| 193 |
|
| 194 |
+
links.append(f"- [Wikipedia](https://en.wikipedia.org/wiki/{clean.replace(' ','_')})")
|
| 195 |
+
links.append(f"- [iNaturalist](https://www.inaturalist.org/search?q={q})")
|
| 196 |
+
links.append(f"- [Google Scholar](https://scholar.google.com/scholar?q={q}+{c})")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
|
| 198 |
return links
|
| 199 |
|
|
|
|
| 215 |
|
| 216 |
if st.button("Search Biodiversity"):
|
| 217 |
|
| 218 |
+
country_code = country_codes.get(country.strip().lower())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
if not country_code:
|
| 221 |
st.error("Country not supported")
|
|
|
|
| 236 |
st.write("•", sp)
|
| 237 |
|
| 238 |
# =================================================
|
| 239 |
+
# AI ANALYSIS
|
| 240 |
# =================================================
|
| 241 |
|
| 242 |
prompt = f"""
|
|
|
|
| 248 |
Species:
|
| 249 |
{chr(10).join(species_list)}
|
| 250 |
|
| 251 |
+
Only use verified scientific knowledge. No hallucination.
|
| 252 |
"""
|
| 253 |
|
| 254 |
st.subheader("🤖 AI Analysis")
|