File size: 4,211 Bytes
133e28a
22bdf52
133e28a
 
3863325
22bdf52
c897235
 
 
223f148
 
133e28a
3863325
133e28a
b134c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
133e28a
223f148
c897235
133e28a
22bdf52
133e28a
3863325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6522d9b
3863325
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6522d9b
 
 
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
import gradio as gr
from huggingface_hub import InferenceClient
import os

# --- 1. SETUP API CONNECTION ---
API_KEY = os.getenv("HF_API_KEY")
if not API_KEY:
    raise ValueError("⚠️ HF_API_KEY not found. Set it in your environment variables.")

# Use a small, free model that actually works
client = InferenceClient("google/flan-t5-small", token=API_KEY)

# --- 2. DEFINE THE LOGIC ---
def analyze_biodiversity(species, habitat, region, activity, noise, flow, pollutants, breeding, disturbance):
    prompt = f"""
You are BioVigilus, an expert AI Ecologist. Create a short, professional Impact Report with emojis.

Analyze Scenario:
- Species: {species}
- Habitat: {habitat} ({region})
- Activity: {activity}
- Conditions: Noise {noise}%, Water Flow {flow}%, Land Disturbance {disturbance}%
- Pollutants: {pollutants}
- Breeding Season: {breeding}

OUTPUT REQUIRED:
1. πŸ›‘ Risk Level (Critical/High/Medium/Low)
2. 🌑️ Sensitivity Score (0-100)
3. πŸ’€ Consequences (Short summary)
4. πŸ›‘οΈ Mitigation (3 bullet points)
5. βš–οΈ Verdict
"""
    try:
        response = client.text_generation(prompt, max_new_tokens=300)
        return response[0].generated_text.strip()
    except Exception as e:
        return f"⚠️ Analysis Failed: {str(e)}"

# --- 3. CUSTOM STYLE ---
custom_style = """
<style>
    body, .gradio-container { background-color: #0f172a !important; color: #e2e8f0 !important; }
    h1 { color: #34d399 !important; text-align: center; font-size: 2.5em !important; margin-bottom: 0; }
    h3 { color: #94a3b8 !important; text-align: center; margin-top: 0; }
    
    button.primary { 
        background: linear-gradient(90deg, #10b981, #06b6d4) !important; 
        border: none !important; 
        color: black !important; 
        font-weight: bold !important; 
        font-size: 1.2em !important;
    }
    button.primary:hover { transform: scale(1.02); }

    .gr-box, .gr-input, input, textarea { 
        background-color: #1e293b !important; 
        border: 1px solid #334155 !important; 
        color: white !important;
    }
    label span { color: #64748b !important; font-weight: bold; }
    
    .output-text { font-family: monospace; background-color: #1e293b; padding: 15px; border-radius: 8px; border: 1px solid #475569; }
</style>
"""

# --- 4. BUILD THE FULL UI ---
with gr.Blocks() as app:
    
    gr.HTML(custom_style)

    with gr.Row():
        gr.Markdown("# 🌿 BioVigilus")
    gr.Markdown("### *AI-Powered Biodiversity Impact Analyzer*")
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("### πŸ¦… Species Info")
            species = gr.Textbox(label="Species Name", placeholder="e.g. Snow Leopard")
            habitat = gr.Textbox(label="Habitat", placeholder="e.g. Alpine Cliffs")
            region = gr.Textbox(label="Region", placeholder="e.g. Gilgit-Baltistan")
            activity = gr.Textbox(label="Activity", placeholder="e.g. Road Construction")
            breeding = gr.Radio(["Yes", "No"], label="Breeding Season?", value="No")

        with gr.Column():
            gr.Markdown("### πŸ“‰ Stress Factors")
            noise = gr.Slider(0, 100, label="Noise Level")
            flow = gr.Slider(0, 100, label="Water Flow Change")
            disturbance = gr.Slider(0, 100, label="Land Disturbance")
            pollutants = gr.Textbox(label="Pollutants", placeholder="Dust, Oil, Chemicals")

    btn = gr.Button("πŸ” Analyze Ecological Impact", variant="primary")
    
    gr.Markdown("### πŸ“‹ Scientific Report")
    output = gr.Markdown("Results will appear here...", elem_classes=["output-text"])

    gr.Examples(
        examples=[
            ["Snow Leopard", "Rocky Mountains", "Gilgit", "Highway Construction", 85, 10, "Explosive Dust", "Yes", 70],
            ["Indus Dolphin", "River Indus", "Sukkur", "Bridge Maintenance", 60, 80, "Oil Leak", "No", 20]
        ],
        inputs=[species, habitat, region, activity, noise, flow, pollutants, breeding, disturbance]
    )

    btn.click(
        analyze_biodiversity, 
        inputs=[species, habitat, region, activity, noise, flow, pollutants, breeding, disturbance], 
        outputs=output
    )

if __name__ == "__main__":
    app.launch()