Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
|
|
|
| 5 |
API_KEY = os.getenv("HF_API_KEY")
|
| 6 |
if not API_KEY:
|
| 7 |
raise ValueError("⚠️ HF_API_KEY not found. Set it in your environment variables.")
|
|
@@ -9,6 +10,7 @@ if not API_KEY:
|
|
| 9 |
# Use a small, free model that actually works
|
| 10 |
client = InferenceClient("google/flan-t5-small", token=API_KEY)
|
| 11 |
|
|
|
|
| 12 |
def analyze_biodiversity(species, habitat, region, activity, noise, flow, pollutants, breeding, disturbance):
|
| 13 |
prompt = f"""
|
| 14 |
You are BioVigilus, an expert AI Ecologist. Create a short, professional Impact Report with emojis.
|
|
@@ -28,27 +30,82 @@ OUTPUT REQUIRED:
|
|
| 28 |
4. 🛡️ Mitigation (3 bullet points)
|
| 29 |
5. ⚖️ Verdict
|
| 30 |
"""
|
| 31 |
-
|
| 32 |
try:
|
| 33 |
response = client.text_generation(prompt, max_new_tokens=300)
|
| 34 |
return response[0].generated_text.strip()
|
| 35 |
except Exception as e:
|
| 36 |
return f"⚠️ Analysis Failed: {str(e)}"
|
| 37 |
|
| 38 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
with gr.Blocks() as app:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
app.launch()
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# --- 1. SETUP API CONNECTION ---
|
| 6 |
API_KEY = os.getenv("HF_API_KEY")
|
| 7 |
if not API_KEY:
|
| 8 |
raise ValueError("⚠️ HF_API_KEY not found. Set it in your environment variables.")
|
|
|
|
| 10 |
# Use a small, free model that actually works
|
| 11 |
client = InferenceClient("google/flan-t5-small", token=API_KEY)
|
| 12 |
|
| 13 |
+
# --- 2. DEFINE THE LOGIC ---
|
| 14 |
def analyze_biodiversity(species, habitat, region, activity, noise, flow, pollutants, breeding, disturbance):
|
| 15 |
prompt = f"""
|
| 16 |
You are BioVigilus, an expert AI Ecologist. Create a short, professional Impact Report with emojis.
|
|
|
|
| 30 |
4. 🛡️ Mitigation (3 bullet points)
|
| 31 |
5. ⚖️ Verdict
|
| 32 |
"""
|
|
|
|
| 33 |
try:
|
| 34 |
response = client.text_generation(prompt, max_new_tokens=300)
|
| 35 |
return response[0].generated_text.strip()
|
| 36 |
except Exception as e:
|
| 37 |
return f"⚠️ Analysis Failed: {str(e)}"
|
| 38 |
|
| 39 |
+
# --- 3. CUSTOM STYLE ---
|
| 40 |
+
custom_style = """
|
| 41 |
+
<style>
|
| 42 |
+
body, .gradio-container { background-color: #0f172a !important; color: #e2e8f0 !important; }
|
| 43 |
+
h1 { color: #34d399 !important; text-align: center; font-size: 2.5em !important; margin-bottom: 0; }
|
| 44 |
+
h3 { color: #94a3b8 !important; text-align: center; margin-top: 0; }
|
| 45 |
+
|
| 46 |
+
button.primary {
|
| 47 |
+
background: linear-gradient(90deg, #10b981, #06b6d4) !important;
|
| 48 |
+
border: none !important;
|
| 49 |
+
color: black !important;
|
| 50 |
+
font-weight: bold !important;
|
| 51 |
+
font-size: 1.2em !important;
|
| 52 |
+
}
|
| 53 |
+
button.primary:hover { transform: scale(1.02); }
|
| 54 |
+
|
| 55 |
+
.gr-box, .gr-input, input, textarea {
|
| 56 |
+
background-color: #1e293b !important;
|
| 57 |
+
border: 1px solid #334155 !important;
|
| 58 |
+
color: white !important;
|
| 59 |
+
}
|
| 60 |
+
label span { color: #64748b !important; font-weight: bold; }
|
| 61 |
+
|
| 62 |
+
.output-text { font-family: monospace; background-color: #1e293b; padding: 15px; border-radius: 8px; border: 1px solid #475569; }
|
| 63 |
+
</style>
|
| 64 |
+
"""
|
| 65 |
+
|
| 66 |
+
# --- 4. BUILD THE FULL UI ---
|
| 67 |
with gr.Blocks() as app:
|
| 68 |
+
|
| 69 |
+
gr.HTML(custom_style)
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
gr.Markdown("# 🌿 BioVigilus")
|
| 73 |
+
gr.Markdown("### *AI-Powered Biodiversity Impact Analyzer*")
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
with gr.Column():
|
| 77 |
+
gr.Markdown("### 🦅 Species Info")
|
| 78 |
+
species = gr.Textbox(label="Species Name", placeholder="e.g. Snow Leopard")
|
| 79 |
+
habitat = gr.Textbox(label="Habitat", placeholder="e.g. Alpine Cliffs")
|
| 80 |
+
region = gr.Textbox(label="Region", placeholder="e.g. Gilgit-Baltistan")
|
| 81 |
+
activity = gr.Textbox(label="Activity", placeholder="e.g. Road Construction")
|
| 82 |
+
breeding = gr.Radio(["Yes", "No"], label="Breeding Season?", value="No")
|
| 83 |
+
|
| 84 |
+
with gr.Column():
|
| 85 |
+
gr.Markdown("### 📉 Stress Factors")
|
| 86 |
+
noise = gr.Slider(0, 100, label="Noise Level")
|
| 87 |
+
flow = gr.Slider(0, 100, label="Water Flow Change")
|
| 88 |
+
disturbance = gr.Slider(0, 100, label="Land Disturbance")
|
| 89 |
+
pollutants = gr.Textbox(label="Pollutants", placeholder="Dust, Oil, Chemicals")
|
| 90 |
+
|
| 91 |
+
btn = gr.Button("🔍 Analyze Ecological Impact", variant="primary")
|
| 92 |
+
|
| 93 |
+
gr.Markdown("### 📋 Scientific Report")
|
| 94 |
+
output = gr.Markdown("Results will appear here...", elem_classes=["output-text"])
|
| 95 |
+
|
| 96 |
+
gr.Examples(
|
| 97 |
+
examples=[
|
| 98 |
+
["Snow Leopard", "Rocky Mountains", "Gilgit", "Highway Construction", 85, 10, "Explosive Dust", "Yes", 70],
|
| 99 |
+
["Indus Dolphin", "River Indus", "Sukkur", "Bridge Maintenance", 60, 80, "Oil Leak", "No", 20]
|
| 100 |
+
],
|
| 101 |
+
inputs=[species, habitat, region, activity, noise, flow, pollutants, breeding, disturbance]
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
btn.click(
|
| 105 |
+
analyze_biodiversity,
|
| 106 |
+
inputs=[species, habitat, region, activity, noise, flow, pollutants, breeding, disturbance],
|
| 107 |
+
outputs=output
|
| 108 |
+
)
|
| 109 |
|
| 110 |
if __name__ == "__main__":
|
| 111 |
app.launch()
|