Update app.py
Browse files
app.py
CHANGED
|
@@ -8,14 +8,12 @@ from fpdf import FPDF
|
|
| 8 |
# =========================================
|
| 9 |
|
| 10 |
def call_gemini(prompt, api_key):
|
| 11 |
-
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-
|
| 12 |
|
| 13 |
payload = {
|
| 14 |
"contents": [
|
| 15 |
{
|
| 16 |
-
"parts": [
|
| 17 |
-
{"text": prompt}
|
| 18 |
-
]
|
| 19 |
}
|
| 20 |
]
|
| 21 |
}
|
|
@@ -36,7 +34,7 @@ def call_gemini(prompt, api_key):
|
|
| 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 |
|
|
@@ -46,7 +44,7 @@ def get_species_links(species_name):
|
|
| 46 |
# Wikipedia
|
| 47 |
links.append(f"https://en.wikipedia.org/wiki/{species_name.replace(' ', '_')}")
|
| 48 |
|
| 49 |
-
# Google Scholar
|
| 50 |
links.append(f"https://scholar.google.com/scholar?q={species_name}")
|
| 51 |
|
| 52 |
return links
|
|
@@ -58,11 +56,6 @@ def get_species_links(species_name):
|
|
| 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 + PDF Export")
|
| 62 |
-
|
| 63 |
-
# =========================
|
| 64 |
-
# INPUTS
|
| 65 |
-
# =========================
|
| 66 |
|
| 67 |
country = st.text_input("Enter Country")
|
| 68 |
|
|
@@ -71,9 +64,9 @@ species_type = st.selectbox(
|
|
| 71 |
["Birds", "Plants", "Mammals", "Reptiles", "Fish"]
|
| 72 |
)
|
| 73 |
|
| 74 |
-
# =========================
|
| 75 |
-
# BUTTON
|
| 76 |
-
# =========================
|
| 77 |
|
| 78 |
if st.button("Search Biodiversity"):
|
| 79 |
|
|
@@ -98,6 +91,7 @@ if st.button("Search Biodiversity"):
|
|
| 98 |
for item in results
|
| 99 |
]
|
| 100 |
|
|
|
|
| 101 |
species_text = "\n".join(species_list)
|
| 102 |
|
| 103 |
st.subheader("πΎ Species Found")
|
|
@@ -128,7 +122,7 @@ Species List:
|
|
| 128 |
st.write(ai_text)
|
| 129 |
|
| 130 |
# =========================
|
| 131 |
-
#
|
| 132 |
# =========================
|
| 133 |
|
| 134 |
st.subheader("π Research Links")
|
|
@@ -145,11 +139,11 @@ Species List:
|
|
| 145 |
|
| 146 |
all_links_text += f"\n{sp}:\n" + "\n".join(links) + "\n"
|
| 147 |
|
| 148 |
-
# =========================
|
| 149 |
-
# PDF EXPORT
|
| 150 |
-
# =========================
|
| 151 |
|
| 152 |
-
final_text = f"""
|
| 153 |
BIODIVERSITY REPORT
|
| 154 |
|
| 155 |
Country: {country}
|
|
@@ -165,21 +159,20 @@ RESEARCH LINKS:
|
|
| 165 |
{all_links_text}
|
| 166 |
"""
|
| 167 |
|
| 168 |
-
pdf = FPDF()
|
| 169 |
-
pdf.add_page()
|
| 170 |
-
pdf.set_font("Arial", size=12)
|
| 171 |
-
|
| 172 |
-
for line in final_text.split("\n"):
|
| 173 |
-
pdf.multi_cell(0, 10, line)
|
| 174 |
|
| 175 |
-
|
|
|
|
| 176 |
|
| 177 |
-
|
|
|
|
| 178 |
|
| 179 |
-
with open(pdf_file, "rb") as f:
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
| 8 |
# =========================================
|
| 9 |
|
| 10 |
def call_gemini(prompt, api_key):
|
| 11 |
+
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={api_key}"
|
| 12 |
|
| 13 |
payload = {
|
| 14 |
"contents": [
|
| 15 |
{
|
| 16 |
+
"parts": [{"text": prompt}]
|
|
|
|
|
|
|
| 17 |
}
|
| 18 |
]
|
| 19 |
}
|
|
|
|
| 34 |
def get_species_links(species_name):
|
| 35 |
links = []
|
| 36 |
|
| 37 |
+
# GBIF
|
| 38 |
gbif_url = f"https://api.gbif.org/v1/species/match?name={species_name}"
|
| 39 |
r = requests.get(gbif_url).json()
|
| 40 |
|
|
|
|
| 44 |
# Wikipedia
|
| 45 |
links.append(f"https://en.wikipedia.org/wiki/{species_name.replace(' ', '_')}")
|
| 46 |
|
| 47 |
+
# Google Scholar
|
| 48 |
links.append(f"https://scholar.google.com/scholar?q={species_name}")
|
| 49 |
|
| 50 |
return links
|
|
|
|
| 56 |
|
| 57 |
st.set_page_config(page_title="Biodiversity GIS Explorer", layout="wide")
|
| 58 |
st.title("π Biodiversity GIS Explorer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
country = st.text_input("Enter Country")
|
| 61 |
|
|
|
|
| 64 |
["Birds", "Plants", "Mammals", "Reptiles", "Fish"]
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# =========================================
|
| 68 |
+
# MAIN BUTTON
|
| 69 |
+
# =========================================
|
| 70 |
|
| 71 |
if st.button("Search Biodiversity"):
|
| 72 |
|
|
|
|
| 91 |
for item in results
|
| 92 |
]
|
| 93 |
|
| 94 |
+
# β
IMPORTANT: define this before using it
|
| 95 |
species_text = "\n".join(species_list)
|
| 96 |
|
| 97 |
st.subheader("πΎ Species Found")
|
|
|
|
| 122 |
st.write(ai_text)
|
| 123 |
|
| 124 |
# =========================
|
| 125 |
+
# LINKS
|
| 126 |
# =========================
|
| 127 |
|
| 128 |
st.subheader("π Research Links")
|
|
|
|
| 139 |
|
| 140 |
all_links_text += f"\n{sp}:\n" + "\n".join(links) + "\n"
|
| 141 |
|
| 142 |
+
# =========================
|
| 143 |
+
# PDF EXPORT
|
| 144 |
+
# =========================
|
| 145 |
|
| 146 |
+
final_text = f"""
|
| 147 |
BIODIVERSITY REPORT
|
| 148 |
|
| 149 |
Country: {country}
|
|
|
|
| 159 |
{all_links_text}
|
| 160 |
"""
|
| 161 |
|
| 162 |
+
pdf = FPDF()
|
| 163 |
+
pdf.add_page()
|
| 164 |
+
pdf.set_font("Arial", size=12)
|
|
|
|
|
|
|
|
|
|
| 165 |
|
| 166 |
+
for line in final_text.split("\n"):
|
| 167 |
+
pdf.multi_cell(0, 10, line)
|
| 168 |
|
| 169 |
+
pdf_file = "biodiversity_report.pdf"
|
| 170 |
+
pdf.output(pdf_file)
|
| 171 |
|
| 172 |
+
with open(pdf_file, "rb") as f:
|
| 173 |
+
st.download_button(
|
| 174 |
+
label="π Download PDF Report",
|
| 175 |
+
data=f,
|
| 176 |
+
file_name="biodiversity_report.pdf",
|
| 177 |
+
mime="application/pdf"
|
| 178 |
+
)
|