RubaAnam1 commited on
Commit
9e1b2a5
Β·
verified Β·
1 Parent(s): 2ef5d36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -15
app.py CHANGED
@@ -4,11 +4,10 @@ import os
4
  from fpdf import FPDF
5
 
6
  # =========================================
7
- # GEMINI API (HTTP - no library)
8
  # =========================================
9
 
10
  def call_gemini(prompt, api_key):
11
-
12
  url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-flash-lite:generateContent?key={api_key}"
13
 
14
  payload = {
@@ -29,20 +28,44 @@ def call_gemini(prompt, api_key):
29
  except:
30
  return str(result)
31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  # =========================================
33
  # STREAMLIT UI
34
  # =========================================
35
 
36
  st.set_page_config(page_title="Biodiversity GIS Explorer", layout="wide")
37
-
38
  st.title("🌍 Biodiversity GIS Explorer")
39
- st.write("GBIF + Gemini AI + GIS Roadmap + PDF Export")
40
 
41
  # =========================
42
  # INPUTS
43
  # =========================
44
 
45
  country = st.text_input("Enter Country")
 
46
  species_type = st.selectbox(
47
  "Select Species Type",
48
  ["Birds", "Plants", "Mammals", "Reptiles", "Fish"]
@@ -61,7 +84,6 @@ if st.button("Search Biodiversity"):
61
  # =========================
62
 
63
  url = f"https://api.gbif.org/v1/species/search?q={species_type}&limit=10"
64
-
65
  response = requests.get(url)
66
  data = response.json()
67
 
@@ -82,29 +104,47 @@ if st.button("Search Biodiversity"):
82
  st.text(species_text)
83
 
84
  # =========================
85
- # GEMINI PROMPT
86
  # =========================
87
 
88
  prompt = f"""
89
  Country: {country}
90
  Species Type: {species_type}
91
 
92
- Species Found:
93
  {species_text}
94
 
95
- 1. Give a short biodiversity summary.
96
- 2. Explain GIS mapping use cases.
97
- 3. Give a beginner QGIS roadmap.
98
  """
99
 
100
  if GEMINI_API_KEY:
101
  ai_text = call_gemini(prompt, GEMINI_API_KEY)
102
  else:
103
- ai_text = "ERROR: GEMINI_API_KEY not set in Secrets"
104
 
105
- st.subheader("πŸ€– AI Analysis + GIS Roadmap")
106
  st.write(ai_text)
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  # =========================
109
  # PDF EXPORT
110
  # =========================
@@ -118,8 +158,11 @@ Species Type: {species_type}
118
  SPECIES FOUND:
119
  {species_text}
120
 
121
- AI ANALYSIS:
122
  {ai_text}
 
 
 
123
  """
124
 
125
  pdf = FPDF()
@@ -132,10 +175,10 @@ AI ANALYSIS:
132
  pdf_file = "biodiversity_report.pdf"
133
  pdf.output(pdf_file)
134
 
135
- with open(pdf_file, "rb") as f:
136
  st.download_button(
137
  label="πŸ“„ Download PDF Report",
138
  data=f,
139
  file_name="biodiversity_report.pdf",
140
  mime="application/pdf"
141
- )
 
4
  from fpdf import FPDF
5
 
6
  # =========================================
7
+ # GEMINI API
8
  # =========================================
9
 
10
  def call_gemini(prompt, api_key):
 
11
  url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-flash-lite:generateContent?key={api_key}"
12
 
13
  payload = {
 
28
  except:
29
  return str(result)
30
 
31
+
32
+ # =========================================
33
+ # SCIENTIFIC LINKS
34
+ # =========================================
35
+
36
+ def get_species_links(species_name):
37
+ links = []
38
+
39
+ # GBIF
40
+ gbif_url = f"https://api.gbif.org/v1/species/match?name={species_name}"
41
+ r = requests.get(gbif_url).json()
42
+
43
+ if "usageKey" in r:
44
+ links.append(f"https://www.gbif.org/species/{r['usageKey']}")
45
+
46
+ # Wikipedia
47
+ links.append(f"https://en.wikipedia.org/wiki/{species_name.replace(' ', '_')}")
48
+
49
+ # OpenAlex research
50
+ links.append(f"https://api.openalex.org/works?search={species_name}&per_page=3")
51
+
52
+ return links
53
+
54
+
55
  # =========================================
56
  # STREAMLIT UI
57
  # =========================================
58
 
59
  st.set_page_config(page_title="Biodiversity GIS Explorer", layout="wide")
 
60
  st.title("🌍 Biodiversity GIS Explorer")
61
+ st.write("GBIF + Gemini AI + Scientific Links + GIS Insights")
62
 
63
  # =========================
64
  # INPUTS
65
  # =========================
66
 
67
  country = st.text_input("Enter Country")
68
+
69
  species_type = st.selectbox(
70
  "Select Species Type",
71
  ["Birds", "Plants", "Mammals", "Reptiles", "Fish"]
 
84
  # =========================
85
 
86
  url = f"https://api.gbif.org/v1/species/search?q={species_type}&limit=10"
 
87
  response = requests.get(url)
88
  data = response.json()
89
 
 
104
  st.text(species_text)
105
 
106
  # =========================
107
+ # GEMINI ANALYSIS
108
  # =========================
109
 
110
  prompt = f"""
111
  Country: {country}
112
  Species Type: {species_type}
113
 
114
+ Species List:
115
  {species_text}
116
 
117
+ 1. Give a short biodiversity summary of this country.
118
+ 2. Explain GIS mapping use cases for these species.
119
+ 3. Give a beginner QGIS roadmap for biodiversity analysis.
120
  """
121
 
122
  if GEMINI_API_KEY:
123
  ai_text = call_gemini(prompt, GEMINI_API_KEY)
124
  else:
125
+ ai_text = "ERROR: GEMINI_API_KEY not set"
126
 
127
+ st.subheader("πŸ€– Gemini AI Analysis")
128
  st.write(ai_text)
129
 
130
+ # =========================
131
+ # RESEARCH LINKS
132
+ # =========================
133
+
134
+ st.subheader("πŸ“š Scientific Research Links")
135
+
136
+ all_links_text = ""
137
+
138
+ for sp in species_list:
139
+ st.markdown(f"### 🧬 {sp}")
140
+
141
+ links = get_species_links(sp)
142
+
143
+ for link in links:
144
+ st.markdown(f"- {link}")
145
+
146
+ all_links_text += f"\n{sp}:\n" + "\n".join(links) + "\n"
147
+
148
  # =========================
149
  # PDF EXPORT
150
  # =========================
 
158
  SPECIES FOUND:
159
  {species_text}
160
 
161
+ GEMINI ANALYSIS:
162
  {ai_text}
163
+
164
+ RESEARCH LINKS:
165
+ {all_links_text}
166
  """
167
 
168
  pdf = FPDF()
 
175
  pdf_file = "biodiversity_report.pdf"
176
  pdf.output(pdf_file)
177
 
178
+ with open(pdf_file, "rb") as f:
179
  st.download_button(
180
  label="πŸ“„ Download PDF Report",
181
  data=f,
182
  file_name="biodiversity_report.pdf",
183
  mime="application/pdf"
184
+ )