RubaAnam1 commited on
Commit
fdd188b
·
verified ·
1 Parent(s): 6920aed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +121 -42
app.py CHANGED
@@ -3,6 +3,21 @@ import requests
3
  import os
4
  from urllib.parse import quote
5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  # =====================================================
7
  # GEMINI API
8
  # =====================================================
@@ -40,14 +55,31 @@ def call_gemini(prompt, api_key):
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
 
@@ -56,22 +88,36 @@ country_codes = {
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
 
@@ -83,16 +129,43 @@ def get_species(country_code, filter_value):
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
 
@@ -148,7 +221,10 @@ def get_species_links(species_name, country):
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})"
@@ -158,7 +234,10 @@ def get_species_links(species_name, country):
158
  # iNaturalist
159
  # -------------------------------------------------
160
 
161
- inaturalist = f"https://www.inaturalist.org/search?q={encoded_species}"
 
 
 
162
 
163
  links.append(
164
  f"- [iNaturalist Observations]({inaturalist})"
@@ -178,7 +257,10 @@ def get_species_links(species_name, country):
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})"
@@ -188,7 +270,10 @@ def get_species_links(species_name, country):
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})"
@@ -198,7 +283,10 @@ def get_species_links(species_name, country):
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})"
@@ -208,7 +296,10 @@ def get_species_links(species_name, country):
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})"
@@ -218,7 +309,10 @@ def get_species_links(species_name, country):
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})"
@@ -227,21 +321,6 @@ def get_species_links(species_name, country):
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
  # =====================================================
@@ -263,7 +342,7 @@ species_type = st.selectbox(
263
  )
264
 
265
  # =====================================================
266
- # MAIN SEARCH BUTTON
267
  # =====================================================
268
 
269
  if st.button("Search Biodiversity"):
@@ -274,9 +353,7 @@ if st.button("Search Biodiversity"):
274
 
275
  if not country:
276
 
277
- st.warning(
278
- "Please enter a country."
279
- )
280
 
281
  st.stop()
282
 
@@ -284,9 +361,7 @@ if st.button("Search Biodiversity"):
284
 
285
  if not country_code:
286
 
287
- st.error(
288
- "Country not supported yet."
289
- )
290
 
291
  st.stop()
292
 
@@ -310,7 +385,7 @@ if st.button("Search Biodiversity"):
310
  if not species_list:
311
 
312
  st.warning(
313
- "No species found."
314
  )
315
 
316
  st.stop()
@@ -348,6 +423,10 @@ Write:
348
  2. Ecological importance
349
  3. Conservation challenges
350
  4. GIS/environment insights
 
 
 
 
351
  """
352
 
353
  with st.spinner(
 
3
  import os
4
  from urllib.parse import quote
5
 
6
+ # =====================================================
7
+ # PAGE CONFIG
8
+ # =====================================================
9
+
10
+ st.set_page_config(
11
+ page_title="Biodiversity GIS Explorer",
12
+ layout="wide"
13
+ )
14
+
15
+ st.title("🌍 Biodiversity GIS Explorer")
16
+
17
+ st.write(
18
+ "Explore biodiversity species using GBIF + AI analysis."
19
+ )
20
+
21
  # =====================================================
22
  # GEMINI API
23
  # =====================================================
 
55
  # =====================================================
56
 
57
  country_codes = {
58
+
59
  "Sudan": "SD",
60
+ "السودان": "SD",
61
+
62
  "Egypt": "EG",
63
+ "مصر": "EG",
64
+
65
  "Kenya": "KE",
66
+ "كينيا": "KE",
67
+
68
  "Ethiopia": "ET",
69
+ "إثيوبيا": "ET",
70
+
71
  "Saudi Arabia": "SA",
72
+ "السعودية": "SA",
73
+
74
  "India": "IN",
75
+ "الهند": "IN",
76
+
77
  "USA": "US",
78
+ "United States": "US",
79
+ "امريكا": "US",
80
+
81
+ "Canada": "CA",
82
+ "كندا": "CA"
83
  }
84
 
85
 
 
88
  # =====================================================
89
 
90
  species_filters = {
91
+
92
+ "Birds": "class=Aves&rank=SPECIES",
93
+
94
+ "Plants": "kingdom=Plantae&rank=SPECIES",
95
+
96
+ "Mammals": "class=Mammalia&rank=SPECIES",
97
+
98
+ "Reptiles": "class=Reptilia&rank=SPECIES",
99
+
100
+ "Fish": "class=Actinopterygii&rank=SPECIES",
101
+
102
+ "Insects": "class=Insecta&rank=SPECIES"
103
  }
104
 
105
 
106
  # =====================================================
107
+ # GET SPECIES
108
  # =====================================================
109
 
110
  def get_species(country_code, filter_value):
111
 
112
+ url = (
113
+ f"https://api.gbif.org/v1/occurrence/search?"
114
+ f"country={country_code}"
115
+ f"&{filter_value}"
116
+ f"&limit=200"
117
+ f"&has_coordinate=true"
118
+ f"&has_geospatial_issue=false"
119
+ f"&occurrence_status=PRESENT"
120
+ )
121
 
122
  try:
123
 
 
129
 
130
  species = []
131
 
132
+ bad_words = [
133
+ "unclassified",
134
+ "sp.",
135
+ "cf.",
136
+ "aff.",
137
+ "unknown",
138
+ "incertae",
139
+ "birds",
140
+ "plants"
141
+ ]
142
+
143
  for item in results:
144
 
145
  sp = item.get("species")
146
 
147
+ if not sp:
148
+ continue
149
+
150
+ sp_lower = sp.lower()
151
+
152
+ # تنظيف البيانات
153
+ if any(word in sp_lower for word in bad_words):
154
+ continue
155
+
156
+ # تجاهل الأسماء القصيرة
157
+ if len(sp.split()) < 2:
158
+ continue
159
+
160
+ # منع التكرار
161
+ if sp not in species:
162
  species.append(sp)
163
 
164
+ return species[:10]
165
 
166
+ except Exception as e:
167
+
168
+ st.error(f"GBIF Error: {e}")
169
 
170
  return []
171
 
 
221
  # Google Scholar
222
  # -------------------------------------------------
223
 
224
+ scholar_url = (
225
+ f"https://scholar.google.com/scholar?q="
226
+ f"{encoded_species}+{encoded_country}"
227
+ )
228
 
229
  links.append(
230
  f"- [Google Scholar ({country})]({scholar_url})"
 
234
  # iNaturalist
235
  # -------------------------------------------------
236
 
237
+ inaturalist = (
238
+ f"https://www.inaturalist.org/search?q="
239
+ f"{encoded_species}"
240
+ )
241
 
242
  links.append(
243
  f"- [iNaturalist Observations]({inaturalist})"
 
257
  # Biodiversity Heritage Library
258
  # -------------------------------------------------
259
 
260
+ bhl = (
261
+ f"https://www.biodiversitylibrary.org/search?"
262
+ f"searchTerm={encoded_species}"
263
+ )
264
 
265
  links.append(
266
  f"- [Biodiversity Heritage Library]({bhl})"
 
270
  # Google Search
271
  # -------------------------------------------------
272
 
273
+ google_search = (
274
+ f"https://www.google.com/search?q="
275
+ f"{encoded_species}+in+{encoded_country}"
276
+ )
277
 
278
  links.append(
279
  f"- [Google Search ({country})]({google_search})"
 
283
  # Images Search
284
  # -------------------------------------------------
285
 
286
+ image_search = (
287
+ f"https://www.google.com/search?"
288
+ f"tbm=isch&q={encoded_species}+{encoded_country}"
289
+ )
290
 
291
  links.append(
292
  f"- [Images Search]({image_search})"
 
296
  # Research Papers
297
  # -------------------------------------------------
298
 
299
+ research = (
300
+ f"https://www.google.com/search?q="
301
+ f"{encoded_species}+{encoded_country}+research+paper"
302
+ )
303
 
304
  links.append(
305
  f"- [Research Papers]({research})"
 
309
  # Conservation Information
310
  # -------------------------------------------------
311
 
312
+ conservation = (
313
+ f"https://www.google.com/search?q="
314
+ f"{encoded_species}+{encoded_country}+conservation"
315
+ )
316
 
317
  links.append(
318
  f"- [Conservation Information]({conservation})"
 
321
  return links
322
 
323
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
324
  # =====================================================
325
  # USER INPUTS
326
  # =====================================================
 
342
  )
343
 
344
  # =====================================================
345
+ # SEARCH BUTTON
346
  # =====================================================
347
 
348
  if st.button("Search Biodiversity"):
 
353
 
354
  if not country:
355
 
356
+ st.warning("Please enter a country.")
 
 
357
 
358
  st.stop()
359
 
 
361
 
362
  if not country_code:
363
 
364
+ st.error("Country not supported yet.")
 
 
365
 
366
  st.stop()
367
 
 
385
  if not species_list:
386
 
387
  st.warning(
388
+ "No clean biodiversity data found."
389
  )
390
 
391
  st.stop()
 
423
  2. Ecological importance
424
  3. Conservation challenges
425
  4. GIS/environment insights
426
+
427
+ IMPORTANT:
428
+ Only discuss species actually found in the country.
429
+ Avoid hallucinations.
430
  """
431
 
432
  with st.spinner(