Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,17 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
-
# =========================================
|
| 6 |
# GEMINI API
|
| 7 |
-
# =========================================
|
| 8 |
|
| 9 |
def call_gemini(prompt, api_key):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-flash-lite:generateContent?key={api_key}"
|
| 11 |
|
| 12 |
payload = {
|
|
@@ -17,108 +22,372 @@ def call_gemini(prompt, api_key):
|
|
| 17 |
]
|
| 18 |
}
|
| 19 |
|
| 20 |
-
response = requests.post(url, json=payload)
|
| 21 |
-
result = response.json()
|
| 22 |
-
|
| 23 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
return result["candidates"][0]["content"]["parts"][0]["text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
except:
|
| 26 |
-
return str(result)
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
|
| 30 |
-
# SCIENTIFIC LINKS
|
| 31 |
-
# =========================================
|
| 32 |
|
| 33 |
-
def get_species_links(species_name):
|
| 34 |
links = []
|
| 35 |
-
|
| 36 |
-
# تنظيف الاسم: أخذ أول كلمتين فقط لتجنب الأخطاء
|
| 37 |
-
clean_name = " ".join(species_name.split()[:2]).replace(",", "").replace("(", "").replace(")", "")
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
try:
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
if "usageKey" in r:
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
except:
|
| 47 |
pass
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
wiki_url = f"https://en.wikipedia.org/wiki/{wiki_name}"
|
| 52 |
-
links.append(f"* [Wikipedia Article]({wiki_url})")
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
return links
|
| 59 |
|
| 60 |
|
| 61 |
-
# =========================================
|
| 62 |
-
# STREAMLIT
|
| 63 |
-
# =========================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
st.set_page_config(page_title="Biodiversity GIS Explorer", layout="wide")
|
| 66 |
st.title("🌍 Biodiversity GIS Explorer")
|
| 67 |
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
species_type = st.selectbox(
|
| 71 |
"Select Species Type",
|
| 72 |
-
[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
)
|
| 74 |
|
| 75 |
-
# =========================================
|
| 76 |
-
# MAIN BUTTON
|
| 77 |
-
# =========================================
|
| 78 |
|
| 79 |
if st.button("Search Biodiversity"):
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
data = response.json()
|
| 85 |
|
| 86 |
-
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
]
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
st.subheader("🐾 Species Found")
|
| 97 |
-
st.text(species_text)
|
| 98 |
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
prompt = f"""
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
| 103 |
|
| 104 |
-
Species
|
|
|
|
|
|
|
|
|
|
| 105 |
{species_text}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
"""
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
st.write(ai_text)
|
| 110 |
|
| 111 |
-
#
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
for sp in species_list:
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
+
from urllib.parse import quote
|
| 5 |
|
| 6 |
+
# =====================================================
|
| 7 |
# GEMINI API
|
| 8 |
+
# =====================================================
|
| 9 |
|
| 10 |
def call_gemini(prompt, api_key):
|
| 11 |
+
|
| 12 |
+
if not api_key:
|
| 13 |
+
return "⚠️ GEMINI_API_KEY not found."
|
| 14 |
+
|
| 15 |
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-flash-lite:generateContent?key={api_key}"
|
| 16 |
|
| 17 |
payload = {
|
|
|
|
| 22 |
]
|
| 23 |
}
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
try:
|
| 26 |
+
|
| 27 |
+
response = requests.post(url, json=payload)
|
| 28 |
+
|
| 29 |
+
result = response.json()
|
| 30 |
+
|
| 31 |
return result["candidates"][0]["content"]["parts"][0]["text"]
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
|
| 35 |
+
return f"Gemini Error: {e}"
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# =====================================================
|
| 39 |
+
# COUNTRY CODES
|
| 40 |
+
# =====================================================
|
| 41 |
+
|
| 42 |
+
country_codes = {
|
| 43 |
+
"Sudan": "SD",
|
| 44 |
+
"Egypt": "EG",
|
| 45 |
+
"Kenya": "KE",
|
| 46 |
+
"Ethiopia": "ET",
|
| 47 |
+
"Saudi Arabia": "SA",
|
| 48 |
+
"India": "IN",
|
| 49 |
+
"USA": "US",
|
| 50 |
+
"Canada": "CA"
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# =====================================================
|
| 55 |
+
# SPECIES FILTERS
|
| 56 |
+
# =====================================================
|
| 57 |
+
|
| 58 |
+
species_filters = {
|
| 59 |
+
"Birds": "class=Aves",
|
| 60 |
+
"Plants": "kingdom=Plantae",
|
| 61 |
+
"Mammals": "class=Mammalia",
|
| 62 |
+
"Reptiles": "class=Reptilia",
|
| 63 |
+
"Fish": "class=Actinopterygii",
|
| 64 |
+
"Insects": "class=Insecta"
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# =====================================================
|
| 69 |
+
# GET SPECIES FROM GBIF
|
| 70 |
+
# =====================================================
|
| 71 |
+
|
| 72 |
+
def get_species(country_code, filter_value):
|
| 73 |
+
|
| 74 |
+
url = f"https://api.gbif.org/v1/occurrence/search?country={country_code}&{filter_value}&limit=10"
|
| 75 |
+
|
| 76 |
+
try:
|
| 77 |
+
|
| 78 |
+
response = requests.get(url)
|
| 79 |
+
|
| 80 |
+
data = response.json()
|
| 81 |
+
|
| 82 |
+
results = data.get("results", [])
|
| 83 |
+
|
| 84 |
+
species = []
|
| 85 |
+
|
| 86 |
+
for item in results:
|
| 87 |
+
|
| 88 |
+
sp = item.get("species")
|
| 89 |
+
|
| 90 |
+
if sp and sp not in species:
|
| 91 |
+
species.append(sp)
|
| 92 |
+
|
| 93 |
+
return species
|
| 94 |
+
|
| 95 |
except:
|
|
|
|
| 96 |
|
| 97 |
+
return []
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
# =====================================================
|
| 101 |
+
# GET LINKS
|
| 102 |
+
# =====================================================
|
| 103 |
|
| 104 |
+
def get_species_links(species_name, country):
|
|
|
|
|
|
|
| 105 |
|
|
|
|
| 106 |
links = []
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
clean_name = " ".join(species_name.split()[:2])
|
| 109 |
+
|
| 110 |
+
encoded_species = quote(clean_name)
|
| 111 |
+
|
| 112 |
+
encoded_country = quote(country)
|
| 113 |
+
|
| 114 |
+
# -------------------------------------------------
|
| 115 |
+
# GBIF
|
| 116 |
+
# -------------------------------------------------
|
| 117 |
+
|
| 118 |
+
gbif_api = f"https://api.gbif.org/v1/species/match?name={encoded_species}"
|
| 119 |
+
|
| 120 |
try:
|
| 121 |
+
|
| 122 |
+
r = requests.get(gbif_api).json()
|
| 123 |
+
|
| 124 |
if "usageKey" in r:
|
| 125 |
+
|
| 126 |
+
gbif_link = f"https://www.gbif.org/species/{r['usageKey']}"
|
| 127 |
+
|
| 128 |
+
links.append(
|
| 129 |
+
f"- [GBIF Species Page]({gbif_link})"
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
except:
|
| 133 |
pass
|
| 134 |
|
| 135 |
+
# -------------------------------------------------
|
| 136 |
+
# Wikipedia
|
| 137 |
+
# -------------------------------------------------
|
| 138 |
+
|
| 139 |
+
wiki_name = clean_name.replace(" ", "_")
|
| 140 |
+
|
| 141 |
wiki_url = f"https://en.wikipedia.org/wiki/{wiki_name}"
|
|
|
|
| 142 |
|
| 143 |
+
links.append(
|
| 144 |
+
f"- [Wikipedia Article]({wiki_url})"
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# -------------------------------------------------
|
| 148 |
+
# Google Scholar
|
| 149 |
+
# -------------------------------------------------
|
| 150 |
+
|
| 151 |
+
scholar_url = f"https://scholar.google.com/scholar?q={encoded_species}+{encoded_country}"
|
| 152 |
+
|
| 153 |
+
links.append(
|
| 154 |
+
f"- [Google Scholar ({country})]({scholar_url})"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
# -------------------------------------------------
|
| 158 |
+
# iNaturalist
|
| 159 |
+
# -------------------------------------------------
|
| 160 |
+
|
| 161 |
+
inaturalist = f"https://www.inaturalist.org/search?q={encoded_species}"
|
| 162 |
+
|
| 163 |
+
links.append(
|
| 164 |
+
f"- [iNaturalist Observations]({inaturalist})"
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
# -------------------------------------------------
|
| 168 |
+
# Encyclopedia of Life
|
| 169 |
+
# -------------------------------------------------
|
| 170 |
+
|
| 171 |
+
eol = f"https://eol.org/search?q={encoded_species}"
|
| 172 |
+
|
| 173 |
+
links.append(
|
| 174 |
+
f"- [Encyclopedia of Life]({eol})"
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# -------------------------------------------------
|
| 178 |
+
# Biodiversity Heritage Library
|
| 179 |
+
# -------------------------------------------------
|
| 180 |
+
|
| 181 |
+
bhl = f"https://www.biodiversitylibrary.org/search?searchTerm={encoded_species}"
|
| 182 |
+
|
| 183 |
+
links.append(
|
| 184 |
+
f"- [Biodiversity Heritage Library]({bhl})"
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
# -------------------------------------------------
|
| 188 |
+
# Google Search
|
| 189 |
+
# -------------------------------------------------
|
| 190 |
+
|
| 191 |
+
google_search = f"https://www.google.com/search?q={encoded_species}+in+{encoded_country}"
|
| 192 |
+
|
| 193 |
+
links.append(
|
| 194 |
+
f"- [Google Search ({country})]({google_search})"
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
# -------------------------------------------------
|
| 198 |
+
# Images Search
|
| 199 |
+
# -------------------------------------------------
|
| 200 |
+
|
| 201 |
+
image_search = f"https://www.google.com/search?tbm=isch&q={encoded_species}+{encoded_country}"
|
| 202 |
+
|
| 203 |
+
links.append(
|
| 204 |
+
f"- [Images Search]({image_search})"
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
# -------------------------------------------------
|
| 208 |
+
# Research Papers
|
| 209 |
+
# -------------------------------------------------
|
| 210 |
+
|
| 211 |
+
research = f"https://www.google.com/search?q={encoded_species}+{encoded_country}+research+paper"
|
| 212 |
+
|
| 213 |
+
links.append(
|
| 214 |
+
f"- [Research Papers]({research})"
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
# -------------------------------------------------
|
| 218 |
+
# Conservation Information
|
| 219 |
+
# -------------------------------------------------
|
| 220 |
+
|
| 221 |
+
conservation = f"https://www.google.com/search?q={encoded_species}+{encoded_country}+conservation"
|
| 222 |
+
|
| 223 |
+
links.append(
|
| 224 |
+
f"- [Conservation Information]({conservation})"
|
| 225 |
+
)
|
| 226 |
|
| 227 |
return links
|
| 228 |
|
| 229 |
|
| 230 |
+
# =====================================================
|
| 231 |
+
# STREAMLIT PAGE
|
| 232 |
+
# =====================================================
|
| 233 |
+
|
| 234 |
+
st.set_page_config(
|
| 235 |
+
page_title="Biodiversity GIS Explorer",
|
| 236 |
+
layout="wide"
|
| 237 |
+
)
|
| 238 |
|
|
|
|
| 239 |
st.title("🌍 Biodiversity GIS Explorer")
|
| 240 |
|
| 241 |
+
st.write(
|
| 242 |
+
"Explore biodiversity species using GBIF + AI analysis."
|
| 243 |
+
)
|
| 244 |
+
|
| 245 |
+
# =====================================================
|
| 246 |
+
# USER INPUTS
|
| 247 |
+
# =====================================================
|
| 248 |
+
|
| 249 |
+
country = st.text_input(
|
| 250 |
+
"Enter Country"
|
| 251 |
+
)
|
| 252 |
|
| 253 |
species_type = st.selectbox(
|
| 254 |
"Select Species Type",
|
| 255 |
+
[
|
| 256 |
+
"Birds",
|
| 257 |
+
"Plants",
|
| 258 |
+
"Mammals",
|
| 259 |
+
"Reptiles",
|
| 260 |
+
"Fish",
|
| 261 |
+
"Insects"
|
| 262 |
+
]
|
| 263 |
)
|
| 264 |
|
| 265 |
+
# =====================================================
|
| 266 |
+
# MAIN SEARCH BUTTON
|
| 267 |
+
# =====================================================
|
| 268 |
|
| 269 |
if st.button("Search Biodiversity"):
|
| 270 |
|
| 271 |
+
# -------------------------------------------------
|
| 272 |
+
# VALIDATION
|
| 273 |
+
# -------------------------------------------------
|
|
|
|
| 274 |
|
| 275 |
+
if not country:
|
| 276 |
|
| 277 |
+
st.warning(
|
| 278 |
+
"Please enter a country."
|
| 279 |
+
)
|
|
|
|
| 280 |
|
| 281 |
+
st.stop()
|
| 282 |
+
|
| 283 |
+
country_code = country_codes.get(country)
|
| 284 |
+
|
| 285 |
+
if not country_code:
|
| 286 |
+
|
| 287 |
+
st.error(
|
| 288 |
+
"Country not supported yet."
|
| 289 |
+
)
|
| 290 |
+
|
| 291 |
+
st.stop()
|
| 292 |
+
|
| 293 |
+
filter_value = species_filters.get(
|
| 294 |
+
species_type
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
# -------------------------------------------------
|
| 298 |
+
# FETCH SPECIES
|
| 299 |
+
# -------------------------------------------------
|
| 300 |
+
|
| 301 |
+
with st.spinner(
|
| 302 |
+
"Fetching biodiversity data..."
|
| 303 |
+
):
|
| 304 |
+
|
| 305 |
+
species_list = get_species(
|
| 306 |
+
country_code,
|
| 307 |
+
filter_value
|
| 308 |
+
)
|
| 309 |
+
|
| 310 |
+
if not species_list:
|
| 311 |
+
|
| 312 |
+
st.warning(
|
| 313 |
+
"No species found."
|
| 314 |
+
)
|
| 315 |
+
|
| 316 |
+
st.stop()
|
| 317 |
+
|
| 318 |
+
# -------------------------------------------------
|
| 319 |
+
# DISPLAY SPECIES
|
| 320 |
+
# -------------------------------------------------
|
| 321 |
|
| 322 |
st.subheader("🐾 Species Found")
|
|
|
|
| 323 |
|
| 324 |
+
for sp in species_list:
|
| 325 |
+
|
| 326 |
+
st.write(f"• {sp}")
|
| 327 |
+
|
| 328 |
+
# -------------------------------------------------
|
| 329 |
+
# AI ANALYSIS
|
| 330 |
+
# -------------------------------------------------
|
| 331 |
+
|
| 332 |
+
species_text = "\n".join(species_list)
|
| 333 |
+
|
| 334 |
prompt = f"""
|
| 335 |
+
You are a biodiversity scientist.
|
| 336 |
+
|
| 337 |
+
Country:
|
| 338 |
+
{country}
|
| 339 |
|
| 340 |
+
Species Type:
|
| 341 |
+
{species_type}
|
| 342 |
+
|
| 343 |
+
Species Found:
|
| 344 |
{species_text}
|
| 345 |
+
|
| 346 |
+
Write:
|
| 347 |
+
1. Biodiversity summary
|
| 348 |
+
2. Ecological importance
|
| 349 |
+
3. Conservation challenges
|
| 350 |
+
4. GIS/environment insights
|
| 351 |
"""
|
| 352 |
|
| 353 |
+
with st.spinner(
|
| 354 |
+
"Generating AI analysis..."
|
| 355 |
+
):
|
| 356 |
+
|
| 357 |
+
ai_text = call_gemini(
|
| 358 |
+
prompt,
|
| 359 |
+
os.getenv("GEMINI_API_KEY")
|
| 360 |
+
)
|
| 361 |
+
|
| 362 |
+
st.subheader(
|
| 363 |
+
"🤖 AI Biodiversity Analysis"
|
| 364 |
+
)
|
| 365 |
+
|
| 366 |
st.write(ai_text)
|
| 367 |
|
| 368 |
+
# =================================================
|
| 369 |
+
# LINKS SECTION
|
| 370 |
+
# =================================================
|
| 371 |
+
|
| 372 |
+
st.subheader(
|
| 373 |
+
"🔗 Scientific & Country Related Links"
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
+
with st.spinner(
|
| 377 |
+
"Generating links..."
|
| 378 |
+
):
|
| 379 |
+
|
| 380 |
for sp in species_list:
|
| 381 |
+
|
| 382 |
+
st.markdown(f"## 🔬 {sp}")
|
| 383 |
+
|
| 384 |
+
links = get_species_links(
|
| 385 |
+
sp,
|
| 386 |
+
country
|
| 387 |
+
)
|
| 388 |
+
|
| 389 |
+
for link in links:
|
| 390 |
+
|
| 391 |
+
st.markdown(link)
|
| 392 |
+
|
| 393 |
+
st.markdown("---")
|