RubaAnam1 commited on
Commit
18b1587
ยท
verified ยท
1 Parent(s): f803cd6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -71
app.py CHANGED
@@ -8,10 +8,10 @@ from urllib.parse import quote
8
 
9
  st.set_page_config(page_title="Biodiversity GIS Explorer", layout="wide")
10
  st.title("๐ŸŒ Biodiversity GIS Explorer")
11
- st.write("Explore biodiversity species using GBIF + AI analysis")
12
 
13
  # =====================================================
14
- # COUNTRY
15
  # =====================================================
16
 
17
  country_codes = {
@@ -19,11 +19,25 @@ country_codes = {
19
  "ุงู„ุณูˆุฏุงู†": "SD",
20
  "egypt": "EG",
21
  "kenya": "KE",
22
- "ethiopia": "ET"
 
23
  }
24
 
25
  # =====================================================
26
- # LINKS FUNCTION
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  # =====================================================
28
 
29
  def get_links(species, country):
@@ -31,26 +45,24 @@ def get_links(species, country):
31
  q = quote(species)
32
  c = quote(country)
33
 
34
- links = [
35
  f"๐Ÿ”— [GBIF](https://www.gbif.org/species/match?name={q})",
36
  f"๐Ÿ“˜ [Wikipedia](https://en.wikipedia.org/wiki/{species.replace(' ','_')})",
37
  f"๐Ÿ“ธ [iNaturalist](https://www.inaturalist.org/search?q={q})",
38
  f"๐Ÿ“š [Google Scholar](https://scholar.google.com/scholar?q={q}+{c})"
39
  ]
40
 
41
- return links
42
-
43
-
44
  # =====================================================
45
- # GBIF GROUPED DATA
46
  # =====================================================
47
 
48
- def get_species_grouped(country_code):
49
 
50
  url = (
51
  "https://api.gbif.org/v1/occurrence/search?"
52
  f"country={country_code}"
53
- "&limit=300"
 
54
  "&has_coordinate=true"
55
  "&occurrence_status=PRESENT"
56
  )
@@ -60,14 +72,7 @@ def get_species_grouped(country_code):
60
  data = r.json()
61
  results = data.get("results", [])
62
 
63
- grouped = {
64
- "Birds": [],
65
- "Reptiles": [],
66
- "Mammals": [],
67
- "Insects": [],
68
- "Amphibians": [],
69
- "Other": []
70
- }
71
 
72
  bad_words = ["sp.", "cf.", "aff.", "unknown", "unclassified"]
73
 
@@ -89,34 +94,28 @@ def get_species_grouped(country_code):
89
  if kingdom != "Animalia":
90
  continue
91
 
92
- # =========================
93
- # CLASSIFY
94
- # =========================
95
-
96
- if cls == "Aves":
97
- grouped["Birds"].append(sp)
98
-
99
- elif cls == "Reptilia":
100
- grouped["Reptiles"].append(sp)
101
-
102
- elif cls == "Mammalia":
103
- grouped["Mammals"].append(sp)
104
-
105
- elif cls == "Insecta":
106
- grouped["Insects"].append(sp)
107
-
108
- elif cls == "Amphibia":
109
- grouped["Amphibians"].append(sp)
110
 
111
- else:
112
- grouped["Other"].append(sp)
113
 
114
- return grouped
115
 
116
  except Exception as e:
117
  st.error(f"GBIF Error: {e}")
118
- return None
119
-
120
 
121
  # =====================================================
122
  # UI
@@ -124,6 +123,15 @@ def get_species_grouped(country_code):
124
 
125
  country = st.text_input("Enter Country")
126
 
 
 
 
 
 
 
 
 
 
127
  if st.button("Search Biodiversity"):
128
 
129
  country_code = country_codes.get(country.strip().lower())
@@ -132,39 +140,21 @@ if st.button("Search Biodiversity"):
132
  st.error("Country not supported")
133
  st.stop()
134
 
135
- grouped = get_species_grouped(country_code)
136
 
137
- if not grouped:
138
- st.warning("No data found")
139
- st.stop()
140
 
141
- # =================================================
142
- # DISPLAY
143
- # =================================================
144
-
145
- for group, species_list in grouped.items():
146
-
147
- if not species_list:
148
- continue
149
-
150
- st.subheader(f"๐Ÿพ {group}")
151
-
152
- for sp in species_list[:10]:
153
-
154
- st.markdown(f"### ๐Ÿ”ฌ {sp}")
155
-
156
- links = get_links(sp, country)
157
 
158
- for l in links:
159
- st.markdown(l)
160
 
161
- st.markdown("---")
162
 
163
- # =================================================
164
- # SUMMARY
165
- # =================================================
166
 
167
- st.subheader("๐Ÿ“Š Summary")
 
168
 
169
- for k, v in grouped.items():
170
- st.write(f"{k}: {len(v)} species")
 
8
 
9
  st.set_page_config(page_title="Biodiversity GIS Explorer", layout="wide")
10
  st.title("๐ŸŒ Biodiversity GIS Explorer")
11
+ st.write("Search biodiversity by country and species type")
12
 
13
  # =====================================================
14
+ # COUNTRY MAP
15
  # =====================================================
16
 
17
  country_codes = {
 
19
  "ุงู„ุณูˆุฏุงู†": "SD",
20
  "egypt": "EG",
21
  "kenya": "KE",
22
+ "ethiopia": "ET",
23
+ "usa": "US"
24
  }
25
 
26
  # =====================================================
27
+ # TYPE FILTER
28
+ # =====================================================
29
+
30
+ species_filters = {
31
+ "Birds": "class=Aves",
32
+ "Reptiles": "class=Reptilia",
33
+ "Mammals": "class=Mammalia",
34
+ "Insects": "class=Insecta",
35
+ "Amphibians": "class=Amphibia",
36
+ "Fish": "class=Actinopterygii"
37
+ }
38
+
39
+ # =====================================================
40
+ # LINKS
41
  # =====================================================
42
 
43
  def get_links(species, country):
 
45
  q = quote(species)
46
  c = quote(country)
47
 
48
+ return [
49
  f"๐Ÿ”— [GBIF](https://www.gbif.org/species/match?name={q})",
50
  f"๐Ÿ“˜ [Wikipedia](https://en.wikipedia.org/wiki/{species.replace(' ','_')})",
51
  f"๐Ÿ“ธ [iNaturalist](https://www.inaturalist.org/search?q={q})",
52
  f"๐Ÿ“š [Google Scholar](https://scholar.google.com/scholar?q={q}+{c})"
53
  ]
54
 
 
 
 
55
  # =====================================================
56
+ # FETCH FUNCTION (FILTERED)
57
  # =====================================================
58
 
59
+ def get_species(country_code, filter_value):
60
 
61
  url = (
62
  "https://api.gbif.org/v1/occurrence/search?"
63
  f"country={country_code}"
64
+ f"&{filter_value}"
65
+ "&limit=50"
66
  "&has_coordinate=true"
67
  "&occurrence_status=PRESENT"
68
  )
 
72
  data = r.json()
73
  results = data.get("results", [])
74
 
75
+ species = []
 
 
 
 
 
 
 
76
 
77
  bad_words = ["sp.", "cf.", "aff.", "unknown", "unclassified"]
78
 
 
94
  if kingdom != "Animalia":
95
  continue
96
 
97
+ # strict match with user selection
98
+ if filter_value == "class=Aves" and cls != "Aves":
99
+ continue
100
+ if filter_value == "class=Reptilia" and cls != "Reptilia":
101
+ continue
102
+ if filter_value == "class=Mammalia" and cls != "Mammalia":
103
+ continue
104
+ if filter_value == "class=Insecta" and cls != "Insecta":
105
+ continue
106
+ if filter_value == "class=Amphibia" and cls != "Amphibia":
107
+ continue
108
+ if filter_value == "class=Actinopterygii" and cls != "Actinopterygii":
109
+ continue
 
 
 
 
 
110
 
111
+ if sp not in species:
112
+ species.append(sp)
113
 
114
+ return species[:10]
115
 
116
  except Exception as e:
117
  st.error(f"GBIF Error: {e}")
118
+ return []
 
119
 
120
  # =====================================================
121
  # UI
 
123
 
124
  country = st.text_input("Enter Country")
125
 
126
+ species_type = st.selectbox(
127
+ "Select Species Type",
128
+ ["Birds", "Reptiles", "Mammals", "Insects", "Amphibians", "Fish"]
129
+ )
130
+
131
+ # =====================================================
132
+ # RUN
133
+ # =====================================================
134
+
135
  if st.button("Search Biodiversity"):
136
 
137
  country_code = country_codes.get(country.strip().lower())
 
140
  st.error("Country not supported")
141
  st.stop()
142
 
143
+ filter_value = species_filters[species_type]
144
 
145
+ species_list = get_species(country_code, filter_value)
 
 
146
 
147
+ if not species_list:
148
+ st.warning("No species found for this selection")
149
+ st.stop()
 
 
 
 
 
 
 
 
 
 
 
 
 
150
 
151
+ st.subheader(f"๐Ÿพ {species_type} in {country}")
 
152
 
153
+ for sp in species_list:
154
 
155
+ st.markdown(f"### ๐Ÿ”ฌ {sp}")
 
 
156
 
157
+ for link in get_links(sp, country):
158
+ st.markdown(link)
159
 
160
+ st.markdown("---")