RubaAnam1 commited on
Commit
9ac32bc
·
verified ·
1 Parent(s): 0caf940

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -23
app.py CHANGED
@@ -7,7 +7,7 @@ import os
7
  # =========================================
8
 
9
  def call_gemini(prompt, api_key):
10
- url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-flash-lite:generateContent?key={api_key}"
11
 
12
  payload = {
13
  "contents": [
@@ -32,19 +32,28 @@ def call_gemini(prompt, api_key):
32
 
33
  def get_species_links(species_name):
34
  links = []
 
 
 
35
 
36
- # GBIF
37
- gbif_url = f"https://api.gbif.org/v1/species/match?name={species_name}"
38
- r = requests.get(gbif_url).json()
39
-
40
- if "usageKey" in r:
41
- links.append(f"https://www.gbif.org/species/{r['usageKey']}")
 
 
 
42
 
43
- # Wikipedia
44
- links.append(f"https://en.wikipedia.org/wiki/{species_name.replace(' ', '_')}")
 
 
45
 
46
- # Google Scholar
47
- links.append(f"https://scholar.google.com/scholar?q={species_name}")
 
48
 
49
  return links
50
 
@@ -69,8 +78,8 @@ species_type = st.selectbox(
69
 
70
  if st.button("Search Biodiversity"):
71
 
72
- # GBIF
73
- url = f"https://api.gbif.org/v1/species/search?q={species_type}&limit=10"
74
  response = requests.get(url)
75
  data = response.json()
76
 
@@ -78,10 +87,10 @@ if st.button("Search Biodiversity"):
78
 
79
  species_list = [
80
  item.get("scientificName", "Unknown Species")
81
- for item in results
82
  ]
83
 
84
- # ✅ مهم جداً
85
  species_text = "\n".join(species_list)
86
 
87
  st.subheader("🐾 Species Found")
@@ -97,15 +106,19 @@ Species List:
97
  """
98
 
99
  ai_text = call_gemini(prompt, os.getenv("GEMINI_API_KEY"))
100
-
101
  st.write(ai_text)
102
 
103
- # Links - تم ضبط المسافات هنا لتكون داخل الـ Button ومطابقة تماماً لبقية الكود
104
  st.subheader("🔗 Scientific Links")
105
 
106
- all_links_text = ""
107
- for sp in species_list:
108
- links = get_species_links(sp)
109
- all_links_text += "\n".join(links) + "\n"
110
-
111
- st.text(all_links_text)
 
 
 
 
 
 
7
  # =========================================
8
 
9
  def call_gemini(prompt, api_key):
10
+ url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={api_key}"
11
 
12
  payload = {
13
  "contents": [
 
32
 
33
  def get_species_links(species_name):
34
  links = []
35
+
36
+ # تنظيف الاسم: أخذ أول كلمتين فقط لتجنب الأخطاء
37
+ clean_name = " ".join(species_name.split()[:2]).replace(",", "").replace("(", "").replace(")", "")
38
 
39
+ # GBIF - تعديل صيغة الرابط ليكون قابل للضغط
40
+ gbif_url = f"https://api.gbif.org/v1/species/match?name={clean_name}"
41
+ try:
42
+ r = requests.get(gbif_url).json()
43
+ if "usageKey" in r:
44
+ full_url = f"https://www.gbif.org/species/{r['usageKey']}"
45
+ links.append(f"* [GBIF Portal]({full_url})")
46
+ except:
47
+ pass
48
 
49
+ # Wikipedia - تعديل صيغة الرابط ليكون قابل للضغط
50
+ wiki_name = clean_name.replace(' ', '_')
51
+ wiki_url = f"https://en.wikipedia.org/wiki/{wiki_name}"
52
+ links.append(f"* [Wikipedia Article]({wiki_url})")
53
 
54
+ # Google Scholar - تعديل صيغة الرابط ليكون قابل للضغط
55
+ scholar_url = f"https://scholar.google.com/scholar?q={clean_name}"
56
+ links.append(f"* [Google Scholar Search]({scholar_url})")
57
 
58
  return links
59
 
 
78
 
79
  if st.button("Search Biodiversity"):
80
 
81
+ # GBIF API Search
82
+ url = f"https://api.gbif.org/v1/species/search?q={species_type}&limit=5"
83
  response = requests.get(url)
84
  data = response.json()
85
 
 
87
 
88
  species_list = [
89
  item.get("scientificName", "Unknown Species")
90
+ for item in results if "scientificName" in item
91
  ]
92
 
93
+ # عرض الأسماء الموجودة
94
  species_text = "\n".join(species_list)
95
 
96
  st.subheader("🐾 Species Found")
 
106
  """
107
 
108
  ai_text = call_gemini(prompt, os.getenv("GEMINI_API_KEY"))
 
109
  st.write(ai_text)
110
 
111
+ # --- عرض الروابط النشطة والقابلة للضغط ---
112
  st.subheader("🔗 Scientific Links")
113
 
114
+ with st.spinner("Fetching scientific links..."):
115
+ all_links_text = ""
116
+ for sp in species_list:
117
+ links = get_species_links(sp)
118
+ if links:
119
+ all_links_text += f"\n### 🔬 {sp}\n" + "\n".join(links) + "\n"
120
+
121
+ if all_links_text.strip():
122
+ st.markdown(all_links_text) # عرض الروابط بصيغة الـ Markdown النشطة
123
+ else:
124
+ st.warning("No specific links could be generated for these species names.")