RubaAnam1 commited on
Commit
2386180
·
verified ·
1 Parent(s): 0769c59

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -40
app.py CHANGED
@@ -4,7 +4,7 @@ import os
4
  from urllib.parse import quote
5
 
6
  # =====================================================
7
- # PAGE
8
  # =====================================================
9
 
10
  st.set_page_config(page_title="Biodiversity GIS Explorer", layout="wide")
@@ -41,44 +41,56 @@ def call_gemini(prompt, api_key):
41
 
42
 
43
  # =====================================================
44
- # COUNTRY CLEAN INPUT
45
  # =====================================================
46
 
47
  country_codes = {
48
- "sudan": "SD", "السودان": "SD",
49
- "egypt": "EG", "مصر": "EG",
50
- "kenya": "KE", "كينيا": "KE",
51
- "ethiopia": "ET", "إثيوبيا": "ET",
52
- "saudi arabia": "SA", "السعودية": "SA",
53
- "usa": "US", "united states": "US", "america": "US", "امريكا": "US",
54
- "canada": "CA", "كندا": "CA"
 
 
 
 
 
 
 
 
 
55
  }
56
 
 
57
  # =====================================================
58
- # TAXON FILTERS (FIXED APPROACH)
59
  # =====================================================
60
 
61
  species_filters = {
62
- "Birds": "kingdom=Animalia&class=Aves",
63
  "Plants": "kingdom=Plantae",
64
- "Mammals": "kingdom=Animalia&class=Mammalia",
65
- "Reptiles": "kingdom=Animalia&class=Reptilia",
66
- "Fish": "kingdom=Animalia&class=Actinopterygii",
67
- "Insects": "kingdom=Animalia&class=Insecta"
68
  }
69
 
 
70
  # =====================================================
71
- # GET CLEAN SPECIES (IMPORTANT FIX)
72
  # =====================================================
73
 
74
  def get_species(country_code, filter_value):
75
 
76
  url = (
77
- "https://api.gbif.org/v1/species/search?"
78
  f"country={country_code}"
79
  f"&{filter_value}"
80
- "&rank=SPECIES"
81
- "&limit=50"
 
82
  )
83
 
84
  try:
@@ -90,34 +102,26 @@ def get_species(country_code, filter_value):
90
 
91
  bad_words = [
92
  "unclassified", "sp.", "cf.", "aff.",
93
- "unknown", "incertae"
94
  ]
95
 
96
  for item in results:
97
 
98
- sp = item.get("scientificName")
99
- kingdom = item.get("kingdom")
100
- cls = item.get("class")
101
 
102
  if not sp:
103
  continue
104
 
105
  sp_low = sp.lower()
106
 
 
107
  if any(w in sp_low for w in bad_words):
108
  continue
109
 
 
110
  if len(sp.split()) < 2:
111
  continue
112
 
113
- # strict filtering
114
- if species_type == "Birds" and cls != "Aves":
115
- continue
116
- if species_type == "Plants" and kingdom != "Plantae":
117
- continue
118
- if species_type != "Plants" and kingdom != "Animalia":
119
- continue
120
-
121
  if sp not in species:
122
  species.append(sp)
123
 
@@ -140,6 +144,7 @@ def get_species_links(species_name, country):
140
  q = quote(clean)
141
  c = quote(country)
142
 
 
143
  try:
144
  r = requests.get(
145
  f"https://api.gbif.org/v1/species/match?name={q}"
@@ -152,14 +157,17 @@ def get_species_links(species_name, country):
152
  except:
153
  pass
154
 
 
155
  links.append(
156
  f"- [Wikipedia](https://en.wikipedia.org/wiki/{clean.replace(' ','_')})"
157
  )
158
 
 
159
  links.append(
160
  f"- [iNaturalist](https://www.inaturalist.org/search?q={q})"
161
  )
162
 
 
163
  links.append(
164
  f"- [Google Scholar](https://scholar.google.com/scholar?q={q}+{c})"
165
  )
@@ -179,30 +187,30 @@ species_type = st.selectbox(
179
  )
180
 
181
  # =====================================================
182
- # SEARCH
183
  # =====================================================
184
 
185
  if st.button("Search Biodiversity"):
186
 
187
- # clean country input
188
  country_clean = country.strip().lower()
189
  country_code = country_codes.get(country_clean)
190
 
191
  if not country:
192
- st.warning("Enter country")
193
  st.stop()
194
 
195
  if not country_code:
196
- st.error(f"Country '{country}' not supported")
197
  st.stop()
198
 
199
  filter_value = species_filters[species_type]
200
 
201
- with st.spinner("Fetching clean GBIF species..."):
202
  species_list = get_species(country_code, filter_value)
203
 
204
  if not species_list:
205
- st.warning("No clean species found")
206
  st.stop()
207
 
208
  # =================================================
@@ -215,7 +223,7 @@ if st.button("Search Biodiversity"):
215
  st.write("•", sp)
216
 
217
  # =================================================
218
- # GEMINI
219
  # =================================================
220
 
221
  prompt = f"""
@@ -224,10 +232,10 @@ You are a biodiversity scientist.
224
  Country: {country}
225
  Group: {species_type}
226
 
227
- Species list:
228
  {chr(10).join(species_list)}
229
 
230
- Only analyze real species. No hallucination.
231
  """
232
 
233
  st.subheader("🤖 AI Analysis")
 
4
  from urllib.parse import quote
5
 
6
  # =====================================================
7
+ # APP
8
  # =====================================================
9
 
10
  st.set_page_config(page_title="Biodiversity GIS Explorer", layout="wide")
 
41
 
42
 
43
  # =====================================================
44
+ # COUNTRY NORMALIZATION
45
  # =====================================================
46
 
47
  country_codes = {
48
+ "sudan": "SD",
49
+ "السودان": "SD",
50
+ "egypt": "EG",
51
+ "مصر": "EG",
52
+ "kenya": "KE",
53
+ "كينيا": "KE",
54
+ "ethiopia": "ET",
55
+ "إثيوبيا": "ET",
56
+ "saudi arabia": "SA",
57
+ "السعودية": "SA",
58
+ "usa": "US",
59
+ "united states": "US",
60
+ "america": "US",
61
+ "امريكا": "US",
62
+ "canada": "CA",
63
+ "كندا": "CA"
64
  }
65
 
66
+
67
  # =====================================================
68
+ # FILTERS (FIXED)
69
  # =====================================================
70
 
71
  species_filters = {
72
+ "Birds": "class=Aves",
73
  "Plants": "kingdom=Plantae",
74
+ "Mammals": "class=Mammalia",
75
+ "Reptiles": "class=Reptilia",
76
+ "Fish": "class=Actinopterygii",
77
+ "Insects": "class=Insecta"
78
  }
79
 
80
+
81
  # =====================================================
82
+ # GET SPECIES (WORKING VERSION)
83
  # =====================================================
84
 
85
  def get_species(country_code, filter_value):
86
 
87
  url = (
88
+ "https://api.gbif.org/v1/occurrence/search?"
89
  f"country={country_code}"
90
  f"&{filter_value}"
91
+ "&limit=200"
92
+ "&has_coordinate=true"
93
+ "&occurrence_status=PRESENT"
94
  )
95
 
96
  try:
 
102
 
103
  bad_words = [
104
  "unclassified", "sp.", "cf.", "aff.",
105
+ "unknown", "incertae", "cf", "sp"
106
  ]
107
 
108
  for item in results:
109
 
110
+ sp = item.get("species")
 
 
111
 
112
  if not sp:
113
  continue
114
 
115
  sp_low = sp.lower()
116
 
117
+ # clean junk names
118
  if any(w in sp_low for w in bad_words):
119
  continue
120
 
121
+ # must be binomial name
122
  if len(sp.split()) < 2:
123
  continue
124
 
 
 
 
 
 
 
 
 
125
  if sp not in species:
126
  species.append(sp)
127
 
 
144
  q = quote(clean)
145
  c = quote(country)
146
 
147
+ # GBIF
148
  try:
149
  r = requests.get(
150
  f"https://api.gbif.org/v1/species/match?name={q}"
 
157
  except:
158
  pass
159
 
160
+ # Wikipedia
161
  links.append(
162
  f"- [Wikipedia](https://en.wikipedia.org/wiki/{clean.replace(' ','_')})"
163
  )
164
 
165
+ # iNaturalist
166
  links.append(
167
  f"- [iNaturalist](https://www.inaturalist.org/search?q={q})"
168
  )
169
 
170
+ # Google Scholar
171
  links.append(
172
  f"- [Google Scholar](https://scholar.google.com/scholar?q={q}+{c})"
173
  )
 
187
  )
188
 
189
  # =====================================================
190
+ # MAIN BUTTON
191
  # =====================================================
192
 
193
  if st.button("Search Biodiversity"):
194
 
195
+ # normalize input
196
  country_clean = country.strip().lower()
197
  country_code = country_codes.get(country_clean)
198
 
199
  if not country:
200
+ st.warning("Please enter a country")
201
  st.stop()
202
 
203
  if not country_code:
204
+ st.error(f"Country '{country}' not supported yet")
205
  st.stop()
206
 
207
  filter_value = species_filters[species_type]
208
 
209
+ with st.spinner("Fetching biodiversity data..."):
210
  species_list = get_species(country_code, filter_value)
211
 
212
  if not species_list:
213
+ st.warning("No species found (try another filter or country)")
214
  st.stop()
215
 
216
  # =================================================
 
223
  st.write("•", sp)
224
 
225
  # =================================================
226
+ # AI
227
  # =================================================
228
 
229
  prompt = f"""
 
232
  Country: {country}
233
  Group: {species_type}
234
 
235
+ Species:
236
  {chr(10).join(species_list)}
237
 
238
+ Give ecological and GIS analysis only for real species.
239
  """
240
 
241
  st.subheader("🤖 AI Analysis")