Update app.py
Browse files
app.py
CHANGED
|
@@ -1,2 +1,141 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
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 = {
|
| 15 |
+
"contents": [
|
| 16 |
+
{
|
| 17 |
+
"parts": [
|
| 18 |
+
{"text": prompt}
|
| 19 |
+
]
|
| 20 |
+
}
|
| 21 |
+
]
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
response = requests.post(url, json=payload)
|
| 25 |
+
result = response.json()
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
return result["candidates"][0]["content"]["parts"][0]["text"]
|
| 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"]
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
# =========================
|
| 52 |
+
# BUTTON
|
| 53 |
+
# =========================
|
| 54 |
+
|
| 55 |
+
if st.button("Search Biodiversity"):
|
| 56 |
+
|
| 57 |
+
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 58 |
+
|
| 59 |
+
# =========================
|
| 60 |
+
# GBIF DATA
|
| 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 |
+
|
| 68 |
+
results = data.get("results", [])
|
| 69 |
+
|
| 70 |
+
if not results:
|
| 71 |
+
st.error("No biodiversity data found.")
|
| 72 |
+
st.stop()
|
| 73 |
+
|
| 74 |
+
species_list = [
|
| 75 |
+
item.get("scientificName", "Unknown Species")
|
| 76 |
+
for item in results
|
| 77 |
+
]
|
| 78 |
+
|
| 79 |
+
species_text = "\n".join(species_list)
|
| 80 |
+
|
| 81 |
+
st.subheader("🐾 Species Found")
|
| 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 |
+
# =========================
|
| 111 |
+
|
| 112 |
+
final_text = f"""
|
| 113 |
+
BIODIVERSITY REPORT
|
| 114 |
+
|
| 115 |
+
Country: {country}
|
| 116 |
+
Species Type: {species_type}
|
| 117 |
+
|
| 118 |
+
SPECIES FOUND:
|
| 119 |
+
{species_text}
|
| 120 |
+
|
| 121 |
+
AI ANALYSIS:
|
| 122 |
+
{ai_text}
|
| 123 |
+
"""
|
| 124 |
+
|
| 125 |
+
pdf = FPDF()
|
| 126 |
+
pdf.add_page()
|
| 127 |
+
pdf.set_font("Arial", size=12)
|
| 128 |
+
|
| 129 |
+
for line in final_text.split("\n"):
|
| 130 |
+
pdf.multi_cell(0, 10, line)
|
| 131 |
+
|
| 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 |
+
)
|