-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
594 lines (468 loc) ยท 17.8 KB
/
app.py
File metadata and controls
594 lines (468 loc) ยท 17.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
from google.cloud import bigquery
# BigQuery will automatically use GOOGLE_APPLICATION_CREDENTIALS
bq_client = bigquery.Client()
import os
import io
import json
from datetime import datetime
from typing import List, Dict
import streamlit as st
from PIL import Image
import pandas as pd
import matplotlib.pyplot as plt
# ================= CUSTOM CSS FOR UI ENHANCEMENT ====================
# ===================== DARK MODE (GLOBAL) ======================
st.markdown("""
<style>
/* Main layout background */
body, .main, .block-container {
background-color: #1b1b1b !important;
color: #e0e0e0 !important;
}
/* Card styling */
.card {
background-color: #262626 !important;
padding: 22px;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0,0,0,0.45);
margin-bottom: 20px;
}
/* Section headers */
.section-header {
font-size: 28px;
font-weight: 700;
padding-bottom: 6px;
border-bottom: 2px solid #444;
margin-bottom: 15px;
color: #f3f3f3;
}
/* Buttons */
.stButton>button {
background-color: #3a6df0 !important;
color: #ffffff !important;
border-radius: 8px;
padding: 10px 20px;
font-weight: 600;
border: none;
}
.stButton>button:hover {
background-color: #2c56cc !important;
}
/* File uploader */
.css-1n76uvr, .stFileUploader {
background-color: #2a2a2a !important;
border-radius: 10px;
padding: 10px;
}
/* Text inputs */
.stTextInput>div>div>input {
background-color: #2a2a2a !important;
color: #f0f0f0 !important;
}
/* DataFrames */
.dataframe, .stDataFrame {
color: #ffffff !important;
}
/* JSON box */
.stJson {
background-color: #262626 !important;
padding: 15px;
border-radius: 10px;
}
/* Tabs */
.stTabs [role="tab"] {
background-color: #2a2a2a !important;
color: #cccccc !important;
padding: 8px 14px;
border-radius: 6px;
}
.stTabs [role="tab"][aria-selected="true"] {
background-color: #3a3a3a !important;
color: #ffffff !important;
}
/* Chart background */
.stPlotlyChart, .stAltairChart, .stPyplotChart {
background-color: #1b1b1b !important;
}
/* Titles */
h1, h2, h3, h4, h5, h6 {
color: #f5f5f5 !important;
}
</style>
""", unsafe_allow_html=True)
# CLIP fallback
from transformers import pipeline
# Gemini SDK import
try:
import google.generativeai as genai
HAS_GEMINI = True
except:
HAS_GEMINI = False
# =========================================================
# STREAMLIT CONFIG
# =========================================================
st.set_page_config(page_title="GaiaNet โ Gemini Hybrid Mode", layout="wide")
st.title("๐ GaiaNet โ Biodiversity AI Platform (Gemini Hybrid Mode)")
st.write("Upload an image โ get species detection โ forecast population โ ecosystem modeling โ recommendations.")
# =========================================================
# SIDEBAR โ API KEY
# =========================================================
st.sidebar.header("Configuration")
api_key = st.sidebar.text_input("Gemini API Key", type="password")
if api_key:
os.environ["GEMINI_API_KEY"] = api_key
if HAS_GEMINI and "GEMINI_API_KEY" in os.environ:
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
GEMINI_MODEL = "gemini-2.5-pro"
st.sidebar.success("Gemini Ready โ")
else:
st.sidebar.warning("Gemini key missing โ CLIP fallback only.")
# =========================================================
# UTILITY HELPERS
# =========================================================
def image_to_bytes(img):
buf = io.BytesIO()
img.save(buf, format="JPEG")
return buf.getvalue()
def safe_json_parse(text):
try:
return json.loads(text)
except:
try:
s = text.find("{")
e = text.rfind("}")
if s != -1 and e != -1:
return json.loads(text[s:e+1])
except:
return None
return None
# =========================================================
# CLIP FALLBACK MODEL
# =========================================================
@st.cache_resource
def load_clip():
try:
return pipeline("zero-shot-image-classification", model="openai/clip-vit-base-patch32")
except:
return None
clip_pipe = load_clip()
# =========================================================
# GEMINI FUNCTIONS โ SAFE (no f-string braces)
# =========================================================
# ---------- Species Detection ----------
def gemini_species_analysis(image_bytes):
if not HAS_GEMINI or "GEMINI_API_KEY" not in os.environ:
return {"error": "Gemini unavailable"}
prompt = """
You are a wildlife biologist. Analyze the image and return ONLY JSON in this structure:
{
"species_detected": [
{"common_name": "...", "scientific_name": "...", "confidence": "high|medium|low"}
],
"count": 0,
"habitat_type": "...",
"observations": "...",
"recommendation_summary": "..."
}
"""
model = genai.GenerativeModel(
GEMINI_MODEL,
generation_config={
"temperature": 0.1,
"top_p": 0.9
}
)
result = model.generate_content(
[
prompt,
{"mime_type": "image/jpeg", "data": image_bytes}
]
)
return {"raw": result.text, "parsed": safe_json_parse(result.text)}
# ---------- Population Forecast ----------
def gemini_population_forecast(species_name, history_list):
if not HAS_GEMINI or "GEMINI_API_KEY" not in os.environ:
return {"error": "Gemini unavailable"}
csv_data = "date,count\n" + "\n".join(
f"{r['date']},{r['count']}" for r in history_list
)
prompt = """
You are an ecologist. Given species NAME_HERE and historical monthly counts:
CSV_DATA_HERE
Return ONLY JSON:
{
"forecast": [
{"month": "...", "population": 0, "confidence": "high|medium|low"}
],
"risk_level": "Low|Medium|High|Critical",
"reasoning": "..."
}
"""
prompt = prompt.replace("NAME_HERE", species_name)
prompt = prompt.replace("CSV_DATA_HERE", csv_data)
model = genai.GenerativeModel(
GEMINI_MODEL,
generation_config={
"temperature": 0.1,
"top_p": 0.9
}
)
result = model.generate_content(prompt)
return {"raw": result.text, "parsed": safe_json_parse(result.text)}
# ---------- Ecosystem Modeling ----------
def gemini_ecosystem_model(species_list, context=""):
if not HAS_GEMINI or "GEMINI_API_KEY" not in os.environ:
return {"error": "Gemini unavailable"}
species_csv = ", ".join(species_list)
first_species = species_list[0] if species_list else "unknown species"
prompt = """
You are an ecosystem modeler.
Species: SPECIES_LIST
Context: CONTEXT_INFO
Return ONLY JSON:
{
"keystone_species": ["..."],
"interaction_graph": {
"species": ["interaction1", "interaction2"]
},
"health_score": 0,
"collapse_risk": "Low|Medium|High|Critical",
"simulate": {
"decline_30pct": {
"focal_species": "FSP",
"affected_species": {
"speciesA": 0,
"speciesB": 0
}
}
}
}
"""
prompt = prompt.replace("SPECIES_LIST", species_csv)
prompt = prompt.replace("CONTEXT_INFO", context)
prompt = prompt.replace("FSP", first_species)
model = genai.GenerativeModel(
GEMINI_MODEL,
generation_config={
"temperature": 0.1,
"top_p": 0.9
}
)
result = model.generate_content(prompt)
return {"raw": result.text, "parsed": safe_json_parse(result.text)}
# ---------- Recommendations ----------
def gemini_recommendations(species, status, eco_summary):
if not HAS_GEMINI or "GEMINI_API_KEY" not in os.environ:
return {"error": "Gemini unavailable"}
eco_json = json.dumps(eco_summary)
prompt = """
You are a conservation planner.
Given:
Species: SPECIES_NAME
Status: SPECIES_STATUS
Ecosystem Summary: ECO_SUMMARY
Return ONLY JSON:
{
"recommended_actions": [
{"action": "...", "impact": 0.0, "urgency": "...", "cost_estimate": "..."}
],
"rationale": "..."
}
"""
prompt = prompt.replace("SPECIES_NAME", species)
prompt = prompt.replace("SPECIES_STATUS", status)
prompt = prompt.replace("ECO_SUMMARY", eco_json)
model = genai.GenerativeModel(
GEMINI_MODEL,
generation_config={
"temperature": 0.1,
"top_p": 0.9
}
)
result = model.generate_content(prompt)
return {"raw": result.text, "parsed": safe_json_parse(result.text)}
# =========================================================
# TABS
# =========================================================
tabs = st.tabs(["Home", "Species Detection", "Forecast", "Ecosystem", "Recommendations", "Raw JSON"])
# =========================================================
# 0) HOME PAGE TAB
# =========================================================
with tabs[0]:
st.markdown("<div class='section-header'>๐ Welcome to GaiaNet</div>", unsafe_allow_html=True)
st.markdown("""
<div class='card'>
<h3>What is GaiaNet?</h3>
<p style='font-size:17px;'>
GaiaNet is an AI-powered biodiversity monitoring platform built using Google Gemini 2.5 Pro.
It helps conservationists, researchers, and policymakers understand ecosystem conditions
through intelligent analysis of wildlife images and ecological data.
</p>
<h3>โจ Key Features</h3>
<ul style='font-size:17px;'>
<li><b>Species Detection:</b> Upload a wildlife image to detect species and habitat type.</li>
<li><b>Population Forecasting:</b> Predict future population health using Gemini reasoning.</li>
<li><b>Ecosystem Modeling:</b> Build interaction networks and assess collapse risk.</li>
<li><b>Conservation Recommendations:</b> Get ranked interventions for maximum impact.</li>
<li><b>AI Reasoning:</b> All insights are explained with ecological logic and structured JSON.</li>
</ul>
<h3>๐ How to Use</h3>
<ol style='font-size:17px;'>
<li>Go to <b>Species Detection</b> and upload a wildlife image.</li>
<li>Review detected species and auto-generated ecosystem info.</li>
<li>Check <b>Forecast</b> for trends and <b>Ecosystem</b> for stability assessment.</li>
<li>See <b>Recommendations</b> for actionable ideas.</li>
</ol>
<p style='font-size:17px;'>GaiaNet is built for the <b>SEED Hackathon</b> to showcase the future of AI-driven conservation.</p>
</div>
""", unsafe_allow_html=True)
st.subheader("๐ WDPA Test Query (BigQuery Connection Check)")
query = """
SELECT *
FROM `gen-lang-client-0789978240.gaia_net_dwc_data.wdpa_temp_1`
LIMIT 5
"""
df = bq_client.query(query).to_dataframe()
st.dataframe(df)
# =========================================================
# 1) SPECIES DETECTION TAB
# =========================================================
with tabs[1]:
st.markdown("<div class='section-header'>๐ฆ Species Detection</div>", unsafe_allow_html=True)
colA, colB = st.columns([1, 2])
with colA:
uploaded = st.file_uploader("๐ค Upload wildlife image", type=["jpg", "png"])
history_csv = st.file_uploader("๐ Optional: Population History CSV", type=["csv"])
auto_run = st.checkbox("โก Auto-run advanced analysis", value=True)
with colB:
if uploaded:
img = Image.open(uploaded).convert("RGB")
st.image(img, width=450, caption="Uploaded Image")
st.markdown("<div class='card'>", unsafe_allow_html=True)
st.subheader("๐ Running Species Detectionโฆ")
img_bytes = image_to_bytes(img)
if HAS_GEMINI and "GEMINI_API_KEY" in os.environ:
with st.spinner("Analyzing image with Gemini..."):
analysis = gemini_species_analysis(img_bytes)
else:
st.info("Using CLIP fallback (Gemini not available).")
labels = ["bear", "lion", "tiger", "wolf", "elephant", "deer"]
preds = clip_pipe(img, labels)
top = preds[0]["label"]
analysis = {
"parsed": {
"species_detected": [{"common_name": top, "scientific_name": "", "confidence": "medium"}],
"count": 1,
"habitat_type": "unknown",
"observations": "CLIP fallback",
"recommendation_summary": "Observe habitat quality."
},
"raw": preds
}
st.session_state["analysis"] = analysis
parsed = analysis["parsed"]
if parsed:
st.success(f"**Species Detected:** {parsed['species_detected'][0]['common_name']}")
st.write(f"**Scientific Name:** {parsed['species_detected'][0]['scientific_name']}")
st.write(f"**Confidence:** {parsed['species_detected'][0]['confidence']}")
st.write(f"**Habitat:** {parsed['habitat_type']}")
st.write(f"**Notes:** {parsed['observations']}")
else:
st.warning("Could not parse model output.")
st.markdown("</div>", unsafe_allow_html=True)
# History Handling
if history_csv:
df = pd.read_csv(history_csv, parse_dates=["date"])
else:
today = datetime.utcnow().date()
hist = []
base = 120
for i in range(6):
d = today.replace(day=1) - pd.DateOffset(months=i)
hist.append({"date": d.strftime("%Y-%m-%d"), "count": base - i * 12})
df = pd.DataFrame(hist[::-1])
st.session_state["history"] = df
st.markdown("<div class='card'>", unsafe_allow_html=True)
if auto_run:
species = parsed["species_detected"][0]["common_name"]
history_list = [
{"date": r["date"].strftime("%Y-%m-%d"), "count": int(r["count"])}
for _, r in df.iterrows()
]
with st.spinner("๐ Forecasting population..."):
st.session_state["forecast"] = gemini_population_forecast(species, history_list)
with st.spinner("๐ธ Modeling ecosystem..."):
species_list = [s["common_name"] for s in parsed["species_detected"]]
st.session_state["ecosystem"] = gemini_ecosystem_model(species_list)
with st.spinner("๐ Generating conservation recommendations..."):
status = parsed["recommendation_summary"]
st.session_state["recs"] = gemini_recommendations(
species, status, st.session_state["ecosystem"]["parsed"]
)
st.success("All advanced analyses completed.")
st.markdown("</div>", unsafe_allow_html=True)
else:
st.info("Upload an image to begin analysis.")
# =========================================================
# 2) FORECAST TAB
# =========================================================
with tabs[2]:
st.markdown("<div class='section-header'>๐ Population Forecast</div>", unsafe_allow_html=True)
if "forecast" in st.session_state:
parsed = st.session_state["forecast"]["parsed"]
st.markdown("<div class='card'>", unsafe_allow_html=True)
st.subheader("๐ฎ Forecast Summary")
st.json(parsed)
st.markdown("</div>", unsafe_allow_html=True)
if parsed and "forecast" in parsed:
df = pd.DataFrame(parsed["forecast"])
st.markdown("<div class='card'>", unsafe_allow_html=True)
st.subheader("๐ Population Trend Visualization")
fig, ax = plt.subplots(figsize=(6, 3)) # Smaller width and height
ax.plot(pd.to_datetime(df["month"]), df["population"],marker="o", linewidth=2,markersize=6)
ax.set_title("Population Forecast",fontsize=14)
ax.set_ylabel("Population",fontsize=12)
ax.grid(alpha=0.3)
plt.tight_layout()
st.pyplot(fig)
st.markdown("</div>", unsafe_allow_html=True)
st.markdown("</div>", unsafe_allow_html=True)
else:
st.info("Run detection to generate forecast data.")
# =========================================================
# 3) ECOSYSTEM TAB
# =========================================================
with tabs[3]:
st.markdown("<div class='section-header'>๐ธ Ecosystem Modeling</div>", unsafe_allow_html=True)
if "ecosystem" in st.session_state:
st.markdown("<div class='card'>", unsafe_allow_html=True)
st.subheader("๐ฟ Ecosystem Stability Model")
st.json(st.session_state["ecosystem"]["parsed"])
st.markdown("</div>", unsafe_allow_html=True)
else:
st.info("No ecosystem model available yet.")
# =========================================================
# 4) RECOMMENDATIONS TAB
# =========================================================
with tabs[4]:
st.markdown("<div class='section-header'>๐ Conservation Recommendations</div>", unsafe_allow_html=True)
if "recs" in st.session_state:
st.markdown("<div class='card'>", unsafe_allow_html=True)
st.subheader("๐ฑ Recommended Actions")
st.json(st.session_state["recs"]["parsed"])
st.markdown("</div>", unsafe_allow_html=True)
else:
st.info("No recommendations generated yet.")
# =========================================================
# 5) RAW JSON TAB
# =========================================================
with tabs[5]:
st.header("Debug: Raw Model Output")
st.subheader("Species Detection Raw Output:")
st.json(st.session_state.get("analysis"))
st.subheader("Forecast Raw Output:")
st.json(st.session_state.get("forecast"))
st.subheader("Ecosystem Raw Output:")
st.json(st.session_state.get("ecosystem"))
st.subheader("Recommendations Raw Output:")
st.json(st.session_state.get("recs"))