Update app.py
Browse files
app.py
CHANGED
|
@@ -70,109 +70,64 @@ species_type = st.selectbox(
|
|
| 70 |
|
| 71 |
if st.button("Search Biodiversity"):
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# =========================
|
| 76 |
-
# GBIF DATA
|
| 77 |
-
# =========================
|
| 78 |
-
|
| 79 |
url = f"https://api.gbif.org/v1/species/search?q={species_type}&limit=10"
|
| 80 |
response = requests.get(url)
|
| 81 |
data = response.json()
|
| 82 |
|
| 83 |
results = data.get("results", [])
|
| 84 |
|
| 85 |
-
if not results:
|
| 86 |
-
st.error("No biodiversity data found.")
|
| 87 |
-
st.stop()
|
| 88 |
-
|
| 89 |
species_list = [
|
| 90 |
item.get("scientificName", "Unknown Species")
|
| 91 |
for item in results
|
| 92 |
]
|
| 93 |
|
| 94 |
-
# ✅
|
| 95 |
species_text = "\n".join(species_list)
|
| 96 |
|
| 97 |
st.subheader("🐾 Species Found")
|
| 98 |
st.text(species_text)
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
# GEMINI ANALYSIS
|
| 102 |
-
# =========================
|
| 103 |
-
|
| 104 |
prompt = f"""
|
| 105 |
Country: {country}
|
| 106 |
Species Type: {species_type}
|
| 107 |
|
| 108 |
Species List:
|
| 109 |
{species_text}
|
| 110 |
-
|
| 111 |
-
1. Give a short biodiversity summary.
|
| 112 |
-
2. Explain GIS mapping use cases.
|
| 113 |
-
3. Give a beginner QGIS roadmap.
|
| 114 |
"""
|
| 115 |
|
| 116 |
-
|
| 117 |
-
ai_text = call_gemini(prompt, GEMINI_API_KEY)
|
| 118 |
-
else:
|
| 119 |
-
ai_text = "ERROR: GEMINI_API_KEY not set"
|
| 120 |
|
| 121 |
-
st.subheader("🤖 Gemini AI Analysis")
|
| 122 |
st.write(ai_text)
|
| 123 |
|
| 124 |
-
#
|
| 125 |
-
# LINKS
|
| 126 |
-
# =========================
|
| 127 |
-
|
| 128 |
-
st.subheader("📚 Research Links")
|
| 129 |
-
|
| 130 |
all_links_text = ""
|
| 131 |
|
| 132 |
for sp in species_list:
|
| 133 |
-
st.markdown(f"### 🧬 {sp}")
|
| 134 |
-
|
| 135 |
links = get_species_links(sp)
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
st.markdown(f"- {link}")
|
| 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 |
-
|
| 148 |
-
|
| 149 |
-
Country: {country}
|
| 150 |
-
Species Type: {species_type}
|
| 151 |
-
|
| 152 |
-
SPECIES FOUND:
|
| 153 |
{species_text}
|
| 154 |
|
| 155 |
-
|
| 156 |
{ai_text}
|
| 157 |
|
| 158 |
-
|
| 159 |
{all_links_text}
|
| 160 |
"""
|
| 161 |
|
| 162 |
pdf = FPDF()
|
| 163 |
pdf.add_page()
|
| 164 |
-
pdf.set_font("Arial",
|
| 165 |
|
| 166 |
for line in final_text.split("\n"):
|
| 167 |
pdf.multi_cell(0, 10, line)
|
| 168 |
|
| 169 |
-
|
| 170 |
-
pdf.output(pdf_file)
|
| 171 |
|
| 172 |
-
with open(
|
| 173 |
-
st.download_button(
|
| 174 |
-
label="📄 Download PDF Report",
|
| 175 |
-
data=f,
|
| 176 |
-
file_name="biodiversity_report.pdf",
|
| 177 |
-
mime="application/pdf"
|
| 178 |
-
)
|
|
|
|
| 70 |
|
| 71 |
if st.button("Search Biodiversity"):
|
| 72 |
|
| 73 |
+
# GBIF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
url = f"https://api.gbif.org/v1/species/search?q={species_type}&limit=10"
|
| 75 |
response = requests.get(url)
|
| 76 |
data = response.json()
|
| 77 |
|
| 78 |
results = data.get("results", [])
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
species_list = [
|
| 81 |
item.get("scientificName", "Unknown Species")
|
| 82 |
for item in results
|
| 83 |
]
|
| 84 |
|
| 85 |
+
# ✅ مهم جداً
|
| 86 |
species_text = "\n".join(species_list)
|
| 87 |
|
| 88 |
st.subheader("🐾 Species Found")
|
| 89 |
st.text(species_text)
|
| 90 |
|
| 91 |
+
# Gemini
|
|
|
|
|
|
|
|
|
|
| 92 |
prompt = f"""
|
| 93 |
Country: {country}
|
| 94 |
Species Type: {species_type}
|
| 95 |
|
| 96 |
Species List:
|
| 97 |
{species_text}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
"""
|
| 99 |
|
| 100 |
+
ai_text = call_gemini(prompt, os.getenv("GEMINI_API_KEY"))
|
|
|
|
|
|
|
|
|
|
| 101 |
|
|
|
|
| 102 |
st.write(ai_text)
|
| 103 |
|
| 104 |
+
# Links
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
all_links_text = ""
|
| 106 |
|
| 107 |
for sp in species_list:
|
|
|
|
|
|
|
| 108 |
links = get_species_links(sp)
|
| 109 |
+
all_links_text += "\n".join(links) + "\n"
|
| 110 |
|
| 111 |
+
# PDF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
final_text = f"""
|
| 113 |
+
SPECIES:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
{species_text}
|
| 115 |
|
| 116 |
+
AI:
|
| 117 |
{ai_text}
|
| 118 |
|
| 119 |
+
LINKS:
|
| 120 |
{all_links_text}
|
| 121 |
"""
|
| 122 |
|
| 123 |
pdf = FPDF()
|
| 124 |
pdf.add_page()
|
| 125 |
+
pdf.set_font("Arial", 12)
|
| 126 |
|
| 127 |
for line in final_text.split("\n"):
|
| 128 |
pdf.multi_cell(0, 10, line)
|
| 129 |
|
| 130 |
+
pdf.output("report.pdf")
|
|
|
|
| 131 |
|
| 132 |
+
with open("report.pdf", "rb") as f:
|
| 133 |
+
st.download_button("Download PDF", f, "report.pdf")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|