RubaAnam1 commited on
Commit
ba741e6
·
verified ·
1 Parent(s): 41b2aba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -26
app.py CHANGED
@@ -3,9 +3,8 @@ import requests
3
  from urllib.parse import quote
4
 
5
  st.set_page_config(page_title="Clean Biodiversity Explorer", layout="centered")
6
- st.title("🌍 Clean Biodiversity Explorer (بدون أخطاء)")
7
 
8
- # قاموس تحويل أسماء البلدان إلى كود GBIF
9
  country_to_code = {
10
  "sudan": "SD", "السودان": "SD",
11
  "egypt": "EG", "مصر": "EG",
@@ -16,12 +15,12 @@ country_to_code = {
16
  }
17
 
18
  search_types = {
19
- "🐦 Birds": {"class": "Aves"},
20
- "🐍 Reptiles": {"class": "Reptilia"},
21
- "🐘 Mammals": {"class": "Mammalia"},
22
- "🦋 Insects": {"class": "Insecta"},
23
- "🐸 Amphibians": {"class": "Amphibia"},
24
- "🌳 غابات (نباتات)": {"kingdom": "Plantae", "taxonKey": 7707728} # Tracheophyta
25
  }
26
 
27
  def get_country_code(user_input):
@@ -57,6 +56,16 @@ def get_species(country_code, search_key):
57
  bad = ["sp.", "cf.", "aff.", "unknown", "unclassified", "indet.", "hybrid"]
58
 
59
  for item in results:
 
 
 
 
 
 
 
 
 
 
60
  sp = item.get("species") or item.get("scientificName")
61
  key = item.get("speciesKey") or item.get("taxonKey")
62
  if not sp or not key or len(sp) < 5:
@@ -70,14 +79,7 @@ def get_species(country_code, search_key):
70
  break
71
  return species_list
72
 
73
- # -----------------------------
74
- # دالة ويكيبيديا بدون مكتبة إضافية (باستخدام requests)
75
- # -----------------------------
76
  def get_wikipedia_real_title(scientific_name):
77
- """
78
- تبحث عن أفضل صفحة ويكيبيديا للاسم العلمي.
79
- تستخدم API البحث لتعيد عنوان الصفحة الحقيقي أو None.
80
- """
81
  search_url = "https://en.wikipedia.org/w/api.php"
82
  params = {
83
  "action": "query",
@@ -99,24 +101,16 @@ def get_wikipedia_real_title(scientific_name):
99
 
100
  def make_links(species_name, species_key):
101
  links = {}
102
- # GBIF شغال دائماً
103
  links["GBIF"] = f"https://www.gbif.org/species/{species_key}"
104
-
105
- # بحث ويكيبيديا
106
  real_title = get_wikipedia_real_title(species_name)
107
  if real_title:
108
  links["Wikipedia"] = f"https://en.wikipedia.org/wiki/{real_title.replace(' ', '_')}"
109
  else:
110
- # رابط بحث بدل 404
111
  links["Wikipedia"] = f"https://en.wikipedia.org/w/index.php?search={quote(species_name)}&title=Special:Search"
112
-
113
  links["iNaturalist"] = f"https://www.inaturalist.org/search?q={quote(species_name)}"
114
  links["Scholar"] = f"https://scholar.google.com/scholar?q={quote(species_name)}"
115
  return links
116
 
117
- # -----------------------------
118
- # واجهة المستخدم
119
- # -----------------------------
120
  user_country = st.text_input("🇺🇳 اسم الدولة (مثال: Sudan, السودان, Egypt, مصر)")
121
  selected_type = st.selectbox("📂 اختر نوع البحث", list(search_types.keys()))
122
 
@@ -127,14 +121,14 @@ if st.button("🔍 بحث"):
127
 
128
  country_code = get_country_code(user_country)
129
  if not country_code:
130
- st.error(f"❌ رمز البلد لـ '{user_country}' غير معروف. استخدم: Sudan, Egypt, Saudi Arabia, UAE, USA أو العربية.")
131
  st.stop()
132
 
133
- with st.spinner(f"جلب البيانات من GBIF لـ {user_country} ..."):
134
  species_data = get_species(country_code, selected_type)
135
 
136
  if not species_data:
137
- st.warning(f"😕 لا توجد أنواع لـ {selected_type} في {user_country}.")
138
  st.stop()
139
 
140
  st.success(f"✅ تم العثور على {len(species_data)} نوعاً")
 
3
  from urllib.parse import quote
4
 
5
  st.set_page_config(page_title="Clean Biodiversity Explorer", layout="centered")
6
+ st.title("🌍 Clean Biodiversity Explorer (فلتر دقيق)")
7
 
 
8
  country_to_code = {
9
  "sudan": "SD", "السودان": "SD",
10
  "egypt": "EG", "مصر": "EG",
 
15
  }
16
 
17
  search_types = {
18
+ "🐦 Birds": {"class": "Aves", "rank": "CLASS"},
19
+ "🐍 Reptiles": {"class": "Reptilia", "rank": "CLASS"},
20
+ "🐘 Mammals": {"class": "Mammalia", "rank": "CLASS"},
21
+ "🦋 Insects": {"class": "Insecta", "rank": "CLASS"},
22
+ "🐸 Amphibians": {"class": "Amphibia", "rank": "CLASS"},
23
+ "🌳 غابات (نباتات)": {"kingdom": "Plantae", "taxonKey": 7707728, "rank": "KINGDOM"}
24
  }
25
 
26
  def get_country_code(user_input):
 
56
  bad = ["sp.", "cf.", "aff.", "unknown", "unclassified", "indet.", "hybrid"]
57
 
58
  for item in results:
59
+ # التصفية حسب الفئة المطلوبة (الصارمة)
60
+ if "class" in search_info:
61
+ returned_class = item.get("class")
62
+ if returned_class != search_info["class"]:
63
+ continue # تخطى إذا لم يكن نفس الكلاس
64
+ elif "kingdom" in search_info:
65
+ returned_kingdom = item.get("kingdom")
66
+ if returned_kingdom != search_info["kingdom"]:
67
+ continue
68
+
69
  sp = item.get("species") or item.get("scientificName")
70
  key = item.get("speciesKey") or item.get("taxonKey")
71
  if not sp or not key or len(sp) < 5:
 
79
  break
80
  return species_list
81
 
 
 
 
82
  def get_wikipedia_real_title(scientific_name):
 
 
 
 
83
  search_url = "https://en.wikipedia.org/w/api.php"
84
  params = {
85
  "action": "query",
 
101
 
102
  def make_links(species_name, species_key):
103
  links = {}
 
104
  links["GBIF"] = f"https://www.gbif.org/species/{species_key}"
 
 
105
  real_title = get_wikipedia_real_title(species_name)
106
  if real_title:
107
  links["Wikipedia"] = f"https://en.wikipedia.org/wiki/{real_title.replace(' ', '_')}"
108
  else:
 
109
  links["Wikipedia"] = f"https://en.wikipedia.org/w/index.php?search={quote(species_name)}&title=Special:Search"
 
110
  links["iNaturalist"] = f"https://www.inaturalist.org/search?q={quote(species_name)}"
111
  links["Scholar"] = f"https://scholar.google.com/scholar?q={quote(species_name)}"
112
  return links
113
 
 
 
 
114
  user_country = st.text_input("🇺🇳 اسم الدولة (مثال: Sudan, السودان, Egypt, مصر)")
115
  selected_type = st.selectbox("📂 اختر نوع البحث", list(search_types.keys()))
116
 
 
121
 
122
  country_code = get_country_code(user_country)
123
  if not country_code:
124
+ st.error(f"❌ رمز البلد لـ '{user_country}' غير معروف.")
125
  st.stop()
126
 
127
+ with st.spinner(f"جلب البيانات من GBIF ..."):
128
  species_data = get_species(country_code, selected_type)
129
 
130
  if not species_data:
131
+ st.warning(f"😕 لا توجد أنواع مطابقة لـ {selected_type} في {user_country}.")
132
  st.stop()
133
 
134
  st.success(f"✅ تم العثور على {len(species_data)} نوعاً")