Wall06's picture
Update app.py
3863325 verified
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()