Update app.py
Browse files
app.py
CHANGED
|
@@ -11,8 +11,7 @@ def process_species_data(species_name, habitat, water_disturbance, land_disturba
|
|
| 11 |
if not species_name:
|
| 12 |
return "Please enter a species name.", None, None
|
| 13 |
|
| 14 |
-
# --- A. Fetch Wikipedia Data (
|
| 15 |
-
# We must use a User-Agent header, otherwise Wikipedia blocks the request.
|
| 16 |
wiki_url = "https://en.wikipedia.org/w/api.php"
|
| 17 |
headers = {
|
| 18 |
'User-Agent': 'BioVigilusApp/1.0 (educational-research-project)'
|
|
@@ -23,7 +22,7 @@ def process_species_data(species_name, habitat, water_disturbance, land_disturba
|
|
| 23 |
"prop": "extracts",
|
| 24 |
"titles": species_name.replace(" ", "_"),
|
| 25 |
"explaintext": True,
|
| 26 |
-
"exintro": False,
|
| 27 |
}
|
| 28 |
|
| 29 |
try:
|
|
@@ -42,7 +41,7 @@ def process_species_data(species_name, habitat, water_disturbance, land_disturba
|
|
| 42 |
# --- B. Length Enforcement (150-250 words) ---
|
| 43 |
words = extract.split()
|
| 44 |
|
| 45 |
-
# If text is too short
|
| 46 |
if len(words) < 150:
|
| 47 |
educational_filler = (
|
| 48 |
"\n\n[Additional Ecological Context]\n"
|
|
@@ -53,12 +52,12 @@ def process_species_data(species_name, habitat, water_disturbance, land_disturba
|
|
| 53 |
"Effective conservation strategies include habitat restoration, anti-poaching laws, and community awareness programs."
|
| 54 |
)
|
| 55 |
extract += educational_filler
|
| 56 |
-
words = extract.split()
|
| 57 |
|
| 58 |
-
# Cap at ~250 words
|
| 59 |
summary_text = " ".join(words[:250]) + "..."
|
| 60 |
|
| 61 |
-
# --- C. Impact Analysis
|
| 62 |
analysis_section = f"""
|
| 63 |
|
| 64 |
------------------------------------------------
|
|
@@ -105,36 +104,30 @@ def process_species_data(species_name, habitat, water_disturbance, land_disturba
|
|
| 105 |
return final_output, fig, filename
|
| 106 |
|
| 107 |
# ================================================================
|
| 108 |
-
# 2. VIBRANT CSS (
|
| 109 |
# ================================================================
|
| 110 |
|
| 111 |
vibrant_css = """
|
| 112 |
<style>
|
| 113 |
-
/* Main Background - Stronger Gradient */
|
| 114 |
.gradio-container {
|
| 115 |
background: linear-gradient(135deg, #004d40 0%, #2e7d32 100%) !important;
|
| 116 |
}
|
| 117 |
-
|
| 118 |
-
/* Main Title - White and Bold */
|
| 119 |
#main-title {
|
| 120 |
color: #ffffff !important;
|
| 121 |
-
font-family:
|
| 122 |
font-weight: 800;
|
| 123 |
font-size: 2.5rem;
|
| 124 |
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
| 125 |
text-align: center;
|
| 126 |
margin-bottom: 5px;
|
| 127 |
}
|
| 128 |
-
|
| 129 |
-
/* Subtitle */
|
| 130 |
#sub-title {
|
| 131 |
color: #a5d6a7 !important;
|
| 132 |
font-size: 1.2rem;
|
| 133 |
text-align: center;
|
| 134 |
margin-bottom: 25px;
|
|
|
|
| 135 |
}
|
| 136 |
-
|
| 137 |
-
/* Cards - Pure White with Strong Shadow */
|
| 138 |
.custom-card {
|
| 139 |
background: #ffffff !important;
|
| 140 |
padding: 25px !important;
|
|
@@ -142,35 +135,12 @@ vibrant_css = """
|
|
| 142 |
box-shadow: 0 10px 30px rgba(0,0,0,0.2) !important;
|
| 143 |
border: none !important;
|
| 144 |
}
|
| 145 |
-
|
| 146 |
-
/* Input Labels */
|
| 147 |
-
label span {
|
| 148 |
-
color: #1b5e20 !important;
|
| 149 |
-
font-weight: bold !important;
|
| 150 |
-
font-size: 1rem !important;
|
| 151 |
-
}
|
| 152 |
-
|
| 153 |
/* Button - Gradient Green */
|
| 154 |
-
|
| 155 |
background: linear-gradient(90deg, #1b5e20 0%, #2e7d32 100%) !important;
|
| 156 |
color: white !important;
|
| 157 |
font-size: 1.1rem !important;
|
| 158 |
border-radius: 8px !important;
|
| 159 |
-
border: none !important;
|
| 160 |
-
box-shadow: 0 4px 15px rgba(0,0,0,0.2) !important;
|
| 161 |
-
}
|
| 162 |
-
#analyze-btn:hover {
|
| 163 |
-
transform: translateY(-2px);
|
| 164 |
-
box-shadow: 0 6px 20px rgba(0,0,0,0.3) !important;
|
| 165 |
-
}
|
| 166 |
-
|
| 167 |
-
/* Summary Text Box */
|
| 168 |
-
textarea {
|
| 169 |
-
font-size: 1rem !important;
|
| 170 |
-
line-height: 1.6 !important;
|
| 171 |
-
color: #333 !important;
|
| 172 |
-
background-color: #f9f9f9 !important;
|
| 173 |
-
border: 1px solid #ddd !important;
|
| 174 |
}
|
| 175 |
</style>
|
| 176 |
"""
|
|
@@ -181,10 +151,8 @@ vibrant_css = """
|
|
| 181 |
|
| 182 |
with gr.Blocks() as demo:
|
| 183 |
|
| 184 |
-
# Inject CSS
|
| 185 |
gr.HTML(vibrant_css)
|
| 186 |
|
| 187 |
-
# Header Section
|
| 188 |
gr.HTML("""
|
| 189 |
<div id="main-title">๐ฟ BioVigilus</div>
|
| 190 |
<div id="sub-title">Advanced Biodiversity & Environmental Stress Analyzer</div>
|
|
@@ -211,26 +179,24 @@ with gr.Blocks() as demo:
|
|
| 211 |
|
| 212 |
water = gr.Slider(0, 100, value=25, label="Water Pollution Level")
|
| 213 |
land = gr.Slider(0, 100, value=65, label="Land Degradation")
|
| 214 |
-
noise = gr.Slider(0, 100, value=30, label="Noise Pollution (dB
|
| 215 |
|
| 216 |
-
analyze_btn = gr.Button("๐ Run Analysis",
|
| 217 |
|
| 218 |
# --- RIGHT: OUTPUTS ---
|
| 219 |
with gr.Column(elem_classes="custom-card"):
|
| 220 |
gr.Markdown("### ๐ Analysis Results")
|
| 221 |
|
| 222 |
-
#
|
| 223 |
out_summary = gr.Textbox(
|
| 224 |
label="Ecological Summary & Impact Report",
|
| 225 |
-
lines=12,
|
| 226 |
-
interactive=False
|
| 227 |
-
show_copy_button=True
|
| 228 |
)
|
| 229 |
|
| 230 |
out_map = gr.Plot(label="Global Occurrence Map")
|
| 231 |
out_file = gr.File(label="๐ฅ Download Full Report")
|
| 232 |
|
| 233 |
-
# Click Event
|
| 234 |
analyze_btn.click(
|
| 235 |
process_species_data,
|
| 236 |
inputs=[species_name, habitat, water, land, noise],
|
|
|
|
| 11 |
if not species_name:
|
| 12 |
return "Please enter a species name.", None, None
|
| 13 |
|
| 14 |
+
# --- A. Fetch Wikipedia Data (With User-Agent Fix) ---
|
|
|
|
| 15 |
wiki_url = "https://en.wikipedia.org/w/api.php"
|
| 16 |
headers = {
|
| 17 |
'User-Agent': 'BioVigilusApp/1.0 (educational-research-project)'
|
|
|
|
| 22 |
"prop": "extracts",
|
| 23 |
"titles": species_name.replace(" ", "_"),
|
| 24 |
"explaintext": True,
|
| 25 |
+
"exintro": False,
|
| 26 |
}
|
| 27 |
|
| 28 |
try:
|
|
|
|
| 41 |
# --- B. Length Enforcement (150-250 words) ---
|
| 42 |
words = extract.split()
|
| 43 |
|
| 44 |
+
# If text is too short, append educational filler
|
| 45 |
if len(words) < 150:
|
| 46 |
educational_filler = (
|
| 47 |
"\n\n[Additional Ecological Context]\n"
|
|
|
|
| 52 |
"Effective conservation strategies include habitat restoration, anti-poaching laws, and community awareness programs."
|
| 53 |
)
|
| 54 |
extract += educational_filler
|
| 55 |
+
words = extract.split()
|
| 56 |
|
| 57 |
+
# Cap at ~250 words
|
| 58 |
summary_text = " ".join(words[:250]) + "..."
|
| 59 |
|
| 60 |
+
# --- C. Impact Analysis ---
|
| 61 |
analysis_section = f"""
|
| 62 |
|
| 63 |
------------------------------------------------
|
|
|
|
| 104 |
return final_output, fig, filename
|
| 105 |
|
| 106 |
# ================================================================
|
| 107 |
+
# 2. VIBRANT CSS (Safe Mode)
|
| 108 |
# ================================================================
|
| 109 |
|
| 110 |
vibrant_css = """
|
| 111 |
<style>
|
|
|
|
| 112 |
.gradio-container {
|
| 113 |
background: linear-gradient(135deg, #004d40 0%, #2e7d32 100%) !important;
|
| 114 |
}
|
|
|
|
|
|
|
| 115 |
#main-title {
|
| 116 |
color: #ffffff !important;
|
| 117 |
+
font-family: sans-serif;
|
| 118 |
font-weight: 800;
|
| 119 |
font-size: 2.5rem;
|
| 120 |
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
| 121 |
text-align: center;
|
| 122 |
margin-bottom: 5px;
|
| 123 |
}
|
|
|
|
|
|
|
| 124 |
#sub-title {
|
| 125 |
color: #a5d6a7 !important;
|
| 126 |
font-size: 1.2rem;
|
| 127 |
text-align: center;
|
| 128 |
margin-bottom: 25px;
|
| 129 |
+
font-family: sans-serif;
|
| 130 |
}
|
|
|
|
|
|
|
| 131 |
.custom-card {
|
| 132 |
background: #ffffff !important;
|
| 133 |
padding: 25px !important;
|
|
|
|
| 135 |
box-shadow: 0 10px 30px rgba(0,0,0,0.2) !important;
|
| 136 |
border: none !important;
|
| 137 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
/* Button - Gradient Green */
|
| 139 |
+
button.primary {
|
| 140 |
background: linear-gradient(90deg, #1b5e20 0%, #2e7d32 100%) !important;
|
| 141 |
color: white !important;
|
| 142 |
font-size: 1.1rem !important;
|
| 143 |
border-radius: 8px !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
}
|
| 145 |
</style>
|
| 146 |
"""
|
|
|
|
| 151 |
|
| 152 |
with gr.Blocks() as demo:
|
| 153 |
|
|
|
|
| 154 |
gr.HTML(vibrant_css)
|
| 155 |
|
|
|
|
| 156 |
gr.HTML("""
|
| 157 |
<div id="main-title">๐ฟ BioVigilus</div>
|
| 158 |
<div id="sub-title">Advanced Biodiversity & Environmental Stress Analyzer</div>
|
|
|
|
| 179 |
|
| 180 |
water = gr.Slider(0, 100, value=25, label="Water Pollution Level")
|
| 181 |
land = gr.Slider(0, 100, value=65, label="Land Degradation")
|
| 182 |
+
noise = gr.Slider(0, 100, value=30, label="Noise Pollution (dB)")
|
| 183 |
|
| 184 |
+
analyze_btn = gr.Button("๐ Run Analysis", variant="primary")
|
| 185 |
|
| 186 |
# --- RIGHT: OUTPUTS ---
|
| 187 |
with gr.Column(elem_classes="custom-card"):
|
| 188 |
gr.Markdown("### ๐ Analysis Results")
|
| 189 |
|
| 190 |
+
# REMOVED 'show_copy_button' to fix crash
|
| 191 |
out_summary = gr.Textbox(
|
| 192 |
label="Ecological Summary & Impact Report",
|
| 193 |
+
lines=12,
|
| 194 |
+
interactive=False
|
|
|
|
| 195 |
)
|
| 196 |
|
| 197 |
out_map = gr.Plot(label="Global Occurrence Map")
|
| 198 |
out_file = gr.File(label="๐ฅ Download Full Report")
|
| 199 |
|
|
|
|
| 200 |
analyze_btn.click(
|
| 201 |
process_species_data,
|
| 202 |
inputs=[species_name, habitat, water, land, noise],
|