RubaAnam1 commited on
Commit
e7d1aa2
Β·
verified Β·
1 Parent(s): 5d2d4b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -22
app.py CHANGED
@@ -9,9 +9,10 @@ from urllib.parse import quote
9
 
10
  st.set_page_config(page_title="Biodiversity GIS Explorer", layout="wide")
11
  st.title("🌍 Biodiversity GIS Explorer")
 
12
 
13
  # =====================================================
14
- # GEMINI (FIXED SAFE PARSING)
15
  # =====================================================
16
 
17
  def call_gemini(prompt, api_key):
@@ -33,7 +34,6 @@ def call_gemini(prompt, api_key):
33
  r = requests.post(url, json=payload)
34
  data = r.json()
35
 
36
- # SAFE CHECK
37
  if "candidates" not in data:
38
  return f"Gemini Error: {data}"
39
 
@@ -55,12 +55,12 @@ country_codes = {
55
  "kenya": "KE",
56
  "ethiopia": "ET",
57
  "usa": "US",
58
- "america": "US"
 
59
  }
60
 
61
-
62
  # =====================================================
63
- # FIXED FILTERS (IMPORTANT)
64
  # =====================================================
65
 
66
  species_filters = {
@@ -72,9 +72,8 @@ species_filters = {
72
  "Insects": "class=Insecta"
73
  }
74
 
75
-
76
  # =====================================================
77
- # CLEAN GBIF (STRICT FILTERING)
78
  # =====================================================
79
 
80
  def get_species(country_code, filter_value, species_type):
@@ -84,6 +83,7 @@ def get_species(country_code, filter_value, species_type):
84
  f"country={country_code}"
85
  f"&{filter_value}"
86
  "&limit=200"
 
87
  "&occurrence_status=PRESENT"
88
  )
89
 
@@ -94,34 +94,47 @@ def get_species(country_code, filter_value, species_type):
94
 
95
  species = []
96
 
97
- bad = ["sp.", "cf.", "aff.", "unclassified", "unknown"]
 
 
 
98
 
99
  for item in results:
100
 
101
  sp = item.get("species")
102
  kingdom = item.get("kingdom")
103
  cls = item.get("class")
 
104
 
105
  if not sp:
106
  continue
107
 
108
  sp_low = sp.lower()
109
 
110
- if any(b in sp_low for b in bad):
 
111
  continue
112
 
 
113
  if len(sp.split()) < 2:
114
  continue
115
 
116
- # STRICT FILTER (FIX FOR YOUR PROBLEM)
117
- if species_type == "Birds" and cls != "Aves":
118
- continue
 
119
 
120
- if species_type == "Plants" and kingdom != "Plantae":
121
- continue
 
122
 
123
- if species_type != "Plants" and kingdom != "Animalia":
124
- continue
 
 
 
 
 
125
 
126
  if sp not in species:
127
  species.append(sp)
@@ -157,9 +170,17 @@ def get_species_links(species_name, country):
157
  except:
158
  pass
159
 
160
- links.append(f"- [Wikipedia](https://en.wikipedia.org/wiki/{clean.replace(' ','_')})")
161
- links.append(f"- [iNaturalist](https://www.inaturalist.org/search?q={q})")
162
- links.append(f"- [Google Scholar](https://scholar.google.com/scholar?q={q}+{c})")
 
 
 
 
 
 
 
 
163
 
164
  return links
165
 
@@ -184,6 +205,10 @@ if st.button("Search Biodiversity"):
184
  country_clean = country.strip().lower()
185
  country_code = country_codes.get(country_clean)
186
 
 
 
 
 
187
  if not country_code:
188
  st.error("Country not supported")
189
  st.stop()
@@ -203,7 +228,7 @@ if st.button("Search Biodiversity"):
203
  st.write("β€’", sp)
204
 
205
  # =================================================
206
- # AI FIXED
207
  # =================================================
208
 
209
  prompt = f"""
@@ -215,12 +240,14 @@ Group: {species_type}
215
  Species:
216
  {chr(10).join(species_list)}
217
 
218
- Only use real species. No hallucination.
219
  """
220
 
221
  st.subheader("πŸ€– AI Analysis")
222
 
223
- st.write(call_gemini(prompt, os.getenv("GEMINI_API_KEY")))
 
 
224
 
225
  # =================================================
226
  # LINKS
@@ -229,7 +256,10 @@ Only use real species. No hallucination.
229
  st.subheader("πŸ”— Links")
230
 
231
  for sp in species_list:
 
232
  st.markdown(f"### πŸ”¬ {sp}")
 
233
  for link in get_species_links(sp, country):
234
  st.markdown(link)
 
235
  st.markdown("---")
 
9
 
10
  st.set_page_config(page_title="Biodiversity GIS Explorer", layout="wide")
11
  st.title("🌍 Biodiversity GIS Explorer")
12
+ st.write("Explore biodiversity species using GBIF + AI analysis")
13
 
14
  # =====================================================
15
+ # GEMINI (SAFE)
16
  # =====================================================
17
 
18
  def call_gemini(prompt, api_key):
 
34
  r = requests.post(url, json=payload)
35
  data = r.json()
36
 
 
37
  if "candidates" not in data:
38
  return f"Gemini Error: {data}"
39
 
 
55
  "kenya": "KE",
56
  "ethiopia": "ET",
57
  "usa": "US",
58
+ "america": "US",
59
+ "canada": "CA"
60
  }
61
 
 
62
  # =====================================================
63
+ # FILTERS
64
  # =====================================================
65
 
66
  species_filters = {
 
72
  "Insects": "class=Insecta"
73
  }
74
 
 
75
  # =====================================================
76
+ # GET SPECIES (STRICT + CLEAN + NATIVE FILTER)
77
  # =====================================================
78
 
79
  def get_species(country_code, filter_value, species_type):
 
83
  f"country={country_code}"
84
  f"&{filter_value}"
85
  "&limit=200"
86
+ "&has_coordinate=true"
87
  "&occurrence_status=PRESENT"
88
  )
89
 
 
94
 
95
  species = []
96
 
97
+ bad_words = [
98
+ "unclassified", "sp.", "cf.", "aff.",
99
+ "unknown", "incertae", "sp", "cf"
100
+ ]
101
 
102
  for item in results:
103
 
104
  sp = item.get("species")
105
  kingdom = item.get("kingdom")
106
  cls = item.get("class")
107
+ status = item.get("establishmentMeans")
108
 
109
  if not sp:
110
  continue
111
 
112
  sp_low = sp.lower()
113
 
114
+ # remove junk names
115
+ if any(b in sp_low for b in bad_words):
116
  continue
117
 
118
+ # must be binomial name
119
  if len(sp.split()) < 2:
120
  continue
121
 
122
+ # STRICT TAXON FILTER
123
+ if species_type == "Birds":
124
+ if cls != "Aves":
125
+ continue
126
 
127
+ # remove invasive / introduced species
128
+ if status in ["INTRODUCED", "ASSISTED COLONIZATION"]:
129
+ continue
130
 
131
+ elif species_type == "Plants":
132
+ if kingdom != "Plantae":
133
+ continue
134
+
135
+ else:
136
+ if kingdom != "Animalia":
137
+ continue
138
 
139
  if sp not in species:
140
  species.append(sp)
 
170
  except:
171
  pass
172
 
173
+ links.append(
174
+ f"- [Wikipedia](https://en.wikipedia.org/wiki/{clean.replace(' ','_')})"
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
 
 
205
  country_clean = country.strip().lower()
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")
214
  st.stop()
 
228
  st.write("β€’", sp)
229
 
230
  # =================================================
231
+ # AI
232
  # =================================================
233
 
234
  prompt = f"""
 
240
  Species:
241
  {chr(10).join(species_list)}
242
 
243
+ Analyze only real native biodiversity. Avoid hallucination.
244
  """
245
 
246
  st.subheader("πŸ€– AI Analysis")
247
 
248
+ st.write(
249
+ call_gemini(prompt, os.getenv("GEMINI_API_KEY"))
250
+ )
251
 
252
  # =================================================
253
  # LINKS
 
256
  st.subheader("πŸ”— Links")
257
 
258
  for sp in species_list:
259
+
260
  st.markdown(f"### πŸ”¬ {sp}")
261
+
262
  for link in get_species_links(sp, country):
263
  st.markdown(link)
264
+
265
  st.markdown("---")