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 = """ """ # --- 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()