Update app.py
Browse files
app.py
CHANGED
|
@@ -1,99 +1,165 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
from urllib.parse import quote
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
params = {
|
| 22 |
-
"
|
| 23 |
-
"limit":
|
| 24 |
-
"
|
| 25 |
}
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
results = data.get("results", [])
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
| 34 |
for item in results:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
if not
|
| 38 |
continue
|
| 39 |
-
|
| 40 |
-
# clean junk
|
| 41 |
-
if len(name.split()) < 2:
|
| 42 |
continue
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
#
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
st.stop()
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
if not
|
| 83 |
-
st.
|
| 84 |
st.stop()
|
| 85 |
-
|
| 86 |
-
st.
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
st.
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
st.markdown(f"
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
st.markdown("---")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
from urllib.parse import quote
|
| 4 |
+
import wikipediaapi
|
| 5 |
+
|
| 6 |
+
# -----------------------------
|
| 7 |
+
# إعدادات
|
| 8 |
+
# -----------------------------
|
| 9 |
+
st.set_page_config(page_title="Clean Biodiversity Explorer", layout="centered")
|
| 10 |
+
st.title("🌍 Clean Biodiversity Explorer (بدون أخطاء)")
|
| 11 |
+
|
| 12 |
+
# قاموس تحويل أسماء البلدان (عربي/إنجليزي) إلى كود GBIF (ISO2)
|
| 13 |
+
# أضف دولاً أخرى بنفس الصيغة
|
| 14 |
+
country_to_code = {
|
| 15 |
+
"sudan": "SD", "السودان": "SD",
|
| 16 |
+
"egypt": "EG", "مصر": "EG",
|
| 17 |
+
"saudi arabia": "SA", "السعودية": "SA",
|
| 18 |
+
"uae": "AE", "الامارات": "AE",
|
| 19 |
+
"usa": "US", "الولايات المتحدة": "US",
|
| 20 |
+
"uk": "GB", "المملكة المتحدة": "GB"
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
# أنواع البحث + معامل GBIF المناسبة
|
| 24 |
+
search_types = {
|
| 25 |
+
"🐦 Birds": {"class": "Aves"},
|
| 26 |
+
"🐍 Reptiles": {"class": "Reptilia"},
|
| 27 |
+
"🐘 Mammals": {"class": "Mammalia"},
|
| 28 |
+
"🦋 Insects": {"class": "Insecta"},
|
| 29 |
+
"🐸 Amphibians": {"class": "Amphibia"},
|
| 30 |
+
"🌳 غابات (نباتات)": {"kingdom": "Plantae", "limit_mode": "trees"} # نركز على النباتات
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# -----------------------------
|
| 34 |
+
# دالة تحويل اسم الدولة إلى كود
|
| 35 |
+
# -----------------------------
|
| 36 |
+
def get_country_code(user_input):
|
| 37 |
+
if not user_input:
|
| 38 |
+
return None
|
| 39 |
+
key = user_input.strip().lower()
|
| 40 |
+
return country_to_code.get(key, None)
|
| 41 |
+
|
| 42 |
+
# -----------------------------
|
| 43 |
+
# دالة الحصول على الأنواع من GBIF (بدون أخطاء)
|
| 44 |
+
# -----------------------------
|
| 45 |
+
def get_species(country_code, search_key):
|
| 46 |
+
url = "https://api.gbif.org/v1/occurrence/search"
|
| 47 |
params = {
|
| 48 |
+
"country": country_code.upper(),
|
| 49 |
+
"limit": 80,
|
| 50 |
+
"occurrence_status": "PRESENT"
|
| 51 |
}
|
| 52 |
+
# إضافة معاملات التصنيف حسب نوع البحث
|
| 53 |
+
if "class" in search_types[search_key]:
|
| 54 |
+
params["class"] = search_types[search_key]["class"]
|
| 55 |
+
elif "kingdom" in search_types[search_key]:
|
| 56 |
+
params["kingdom"] = search_types[search_key]["kingdom"]
|
| 57 |
+
# لإظهار نباتات بارزة، نطلب "highertaxonKey" للنباتات الوعائية (اختياري)
|
| 58 |
+
params["taxonKey"] = 7707728 # Tracheophyta (نباتات وعائية) للحصول على أشجار وشجيرات
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
r = requests.get(url, params=params, timeout=15)
|
| 62 |
+
r.raise_for_status()
|
| 63 |
+
data = r.json()
|
| 64 |
+
except Exception as e:
|
| 65 |
+
st.error(f"⚠️ فشل الاتصال بـ GBIF: {str(e)}")
|
| 66 |
+
return []
|
| 67 |
+
|
| 68 |
results = data.get("results", [])
|
| 69 |
+
species_set = set()
|
| 70 |
+
species_list = []
|
| 71 |
+
bad = ["sp.", "cf.", "aff.", "unknown", "unclassified", "indet.", "hybrid"]
|
| 72 |
+
|
| 73 |
for item in results:
|
| 74 |
+
sp = item.get("species") or item.get("scientificName")
|
| 75 |
+
key = item.get("speciesKey") or item.get("taxonKey")
|
| 76 |
+
if not sp or not key or len(sp) < 5:
|
| 77 |
continue
|
| 78 |
+
if any(b in sp.lower() for b in bad):
|
|
|
|
|
|
|
| 79 |
continue
|
| 80 |
+
if sp not in species_set:
|
| 81 |
+
species_set.add(sp)
|
| 82 |
+
species_list.append((sp, key))
|
| 83 |
+
if len(species_list) >= 12:
|
| 84 |
+
break
|
| 85 |
+
return species_list
|
| 86 |
+
|
| 87 |
+
# -----------------------------
|
| 88 |
+
# دالة إنشاء روابط شغالة (بدون 404)
|
| 89 |
+
# -----------------------------
|
| 90 |
+
@st.cache_data(ttl=3600)
|
| 91 |
+
def get_wikipedia_title(scientific_name):
|
| 92 |
+
"""تستخدم Wikipedia API للبحث عن صفحة حقيقية، وتعي�� عنوانها أو None"""
|
| 93 |
+
wiki = wikipediaapi.Wikipedia(user_agent="BiodiversityExplorer/1.0", language="en")
|
| 94 |
+
# جرب الاسم العلمي كاملاً
|
| 95 |
+
page = wiki.page(scientific_name)
|
| 96 |
+
if page.exists():
|
| 97 |
+
return page.title
|
| 98 |
+
# جرب البحث عن كلمات أولية (مثلاً "Panthera leo" → "Lion")
|
| 99 |
+
words = scientific_name.split()
|
| 100 |
+
if len(words) >= 2:
|
| 101 |
+
simple_name = words[-1] # آخر كلمة غالباً هي الجنس أو النوع
|
| 102 |
+
if len(simple_name) > 3:
|
| 103 |
+
page2 = wiki.page(simple_name.title())
|
| 104 |
+
if page2.exists():
|
| 105 |
+
return page2.title
|
| 106 |
+
# بحث حر - نرجع None ونعرض رابط بحث
|
| 107 |
+
return None
|
| 108 |
+
|
| 109 |
+
def make_links(species_name, species_key):
|
| 110 |
+
links_dict = {}
|
| 111 |
+
# GBIF (دائماً شغال)
|
| 112 |
+
links_dict["GBIF"] = f"https://www.gbif.org/species/{species_key}"
|
| 113 |
+
|
| 114 |
+
# Wikipedia - نحاول نحصل على صفحة حقيقية
|
| 115 |
+
real_title = get_wikipedia_title(species_name)
|
| 116 |
+
if real_title:
|
| 117 |
+
links_dict["Wikipedia"] = f"https://en.wikipedia.org/wiki/{real_title.replace(' ', '_')}"
|
| 118 |
+
else:
|
| 119 |
+
# رابط بحث بدلاً من 404
|
| 120 |
+
search_q = quote(species_name)
|
| 121 |
+
links_dict["Wikipedia"] = f"https://en.wikipedia.org/w/index.php?search={search_q}&title=Special:Search"
|
| 122 |
+
|
| 123 |
+
# iNaturalist و Google Scholar عادي
|
| 124 |
+
links_dict["iNaturalist"] = f"https://www.inaturalist.org/search?q={quote(species_name)}"
|
| 125 |
+
links_dict["Scholar"] = f"https://scholar.google.com/scholar?q={quote(species_name)}"
|
| 126 |
+
|
| 127 |
+
return links_dict
|
| 128 |
+
|
| 129 |
+
# -----------------------------
|
| 130 |
+
# واجهة المستخدم
|
| 131 |
+
# -----------------------------
|
| 132 |
+
user_country = st.text_input("🇺🇳 اسم الدولة (مثال: Sudan, السودان, Egypt, مصر)")
|
| 133 |
+
selected_type = st.selectbox("📂 اختر نوع البحث", list(search_types.keys()))
|
| 134 |
+
|
| 135 |
+
if st.button("🔍 بحث"):
|
| 136 |
+
if not user_country:
|
| 137 |
+
st.warning("يرجى إدخال اسم الدولة أولاً.")
|
| 138 |
st.stop()
|
| 139 |
+
|
| 140 |
+
# تحويل اسم الدولة إلى كود GBIF
|
| 141 |
+
country_code = get_country_code(user_country)
|
| 142 |
+
if not country_code:
|
| 143 |
+
st.error(f"❌ لا أدري رمز البلد لـ '{user_country}'. الرجاء استخدام (Sudan, Egypt, Saudi Arabia, UAE, USA...)")
|
| 144 |
st.stop()
|
| 145 |
+
|
| 146 |
+
with st.spinner(f"جلب البيانات من GBIF لـ {user_country} ({country_code}) ..."):
|
| 147 |
+
species_data = get_species(country_code, selected_type)
|
| 148 |
+
|
| 149 |
+
if not species_data:
|
| 150 |
+
st.warning(f"😕 لم يتم العثور على أنواع لـ {selected_type} في {user_country}. حاول دولة أخرى أو نوعاً مختلفاً.")
|
| 151 |
+
st.stop()
|
| 152 |
+
|
| 153 |
+
st.success(f"✅ تم العثور على {len(species_data)} نوعاً")
|
| 154 |
+
st.subheader(f"📌 {selected_type} في {user_country}")
|
| 155 |
+
|
| 156 |
+
for name, key in species_data:
|
| 157 |
+
st.markdown(f"### 🍃 {name}")
|
| 158 |
+
links = make_links(name, key)
|
| 159 |
+
st.markdown(
|
| 160 |
+
f"🔗 [GBIF]({links['GBIF']}) | "
|
| 161 |
+
f"📘 [Wikipedia]({links['Wikipedia']}) | "
|
| 162 |
+
f"📸 [iNaturalist]({links['iNaturalist']}) | "
|
| 163 |
+
f"📚 [Google Scholar]({links['Scholar']})"
|
| 164 |
+
)
|
| 165 |
st.markdown("---")
|