Update app.py
Browse files
app.py
CHANGED
|
@@ -32,14 +32,14 @@ def analyze_location(location_name):
|
|
| 32 |
temp_score = np.clip((historical['temperature'].mean() - 15) / (30 - 15), 0, 1)
|
| 33 |
humidity_score = np.clip((historical['humidity'].mean() - 50) / (80 - 50), 0, 1)
|
| 34 |
rainfall_score = np.clip(historical['rainfall'].mean() / 5, 0, 1)
|
| 35 |
-
ndvi_score = np.clip((historical['estimated_ndvi'].mean() + 1) / 2, 0, 1)
|
| 36 |
|
| 37 |
# Get trends analysis
|
| 38 |
trends = analyzer.analyze_trends(df)
|
| 39 |
if trends is None:
|
| 40 |
return None, None, "Error calculating trends.", None
|
| 41 |
|
| 42 |
-
# Calculate overall score
|
| 43 |
weights = {
|
| 44 |
'temperature': 0.3,
|
| 45 |
'humidity': 0.2,
|
|
@@ -72,11 +72,11 @@ def analyze_location(location_name):
|
|
| 72 |
Humidity: {historical['humidity'].mean():.1f}% (±{historical['humidity'].std():.1f}%)
|
| 73 |
Rainfall: {historical['rainfall'].mean():.1f}mm/day (±{historical['rainfall'].std():.1f}mm)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
Vegetation: {
|
| 80 |
|
| 81 |
🔮 Forecast Analysis (Next 90 Days):
|
| 82 |
Temperature: {forecast['temperature'].mean():.1f}°C (±{forecast['temperature'].std():.1f}°C)
|
|
@@ -103,13 +103,32 @@ def analyze_location(location_name):
|
|
| 103 |
• Long-term Outlook: {get_long_term_outlook(trends)}
|
| 104 |
"""
|
| 105 |
|
| 106 |
-
return
|
| 107 |
|
| 108 |
except Exception as e:
|
| 109 |
error_message = f"An error occurred: {str(e)}"
|
| 110 |
print(f"Error details: {e}")
|
| 111 |
return None, None, error_message, None
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
def get_recommendations(score, ndvi):
|
| 114 |
"""Get detailed recommendations based on scores"""
|
| 115 |
if score >= 0.8 and ndvi >= 0.6:
|
|
@@ -195,28 +214,62 @@ def get_long_term_outlook(trends):
|
|
| 195 |
return "Deteriorating conditions"
|
| 196 |
return "Mixed conditions - monitor closely"
|
| 197 |
|
| 198 |
-
# Create Gradio interface
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
# Launch the app
|
| 222 |
if __name__ == "__main__":
|
|
|
|
| 32 |
temp_score = np.clip((historical['temperature'].mean() - 15) / (30 - 15), 0, 1)
|
| 33 |
humidity_score = np.clip((historical['humidity'].mean() - 50) / (80 - 50), 0, 1)
|
| 34 |
rainfall_score = np.clip(historical['rainfall'].mean() / 5, 0, 1)
|
| 35 |
+
ndvi_score = np.clip((historical['estimated_ndvi'].mean() + 1) / 2, 0, 1)
|
| 36 |
|
| 37 |
# Get trends analysis
|
| 38 |
trends = analyzer.analyze_trends(df)
|
| 39 |
if trends is None:
|
| 40 |
return None, None, "Error calculating trends.", None
|
| 41 |
|
| 42 |
+
# Calculate overall score with weights
|
| 43 |
weights = {
|
| 44 |
'temperature': 0.3,
|
| 45 |
'humidity': 0.2,
|
|
|
|
| 72 |
Humidity: {historical['humidity'].mean():.1f}% (±{historical['humidity'].std():.1f}%)
|
| 73 |
Rainfall: {historical['rainfall'].mean():.1f}mm/day (±{historical['rainfall'].std():.1f}mm)
|
| 74 |
|
| 75 |
+
🌿 Vegetation Analysis:
|
| 76 |
+
Current NDVI: {historical['estimated_ndvi'].mean():.2f}
|
| 77 |
+
Minimum NDVI: {historical['estimated_ndvi'].min():.2f}
|
| 78 |
+
Maximum NDVI: {historical['estimated_ndvi'].max():.2f}
|
| 79 |
+
Vegetation Status: {get_vegetation_status(historical['estimated_ndvi'].mean())}
|
| 80 |
|
| 81 |
🔮 Forecast Analysis (Next 90 Days):
|
| 82 |
Temperature: {forecast['temperature'].mean():.1f}°C (±{forecast['temperature'].std():.1f}°C)
|
|
|
|
| 103 |
• Long-term Outlook: {get_long_term_outlook(trends)}
|
| 104 |
"""
|
| 105 |
|
| 106 |
+
return location_map, analysis_text, time_series_plot, gauge_plot
|
| 107 |
|
| 108 |
except Exception as e:
|
| 109 |
error_message = f"An error occurred: {str(e)}"
|
| 110 |
print(f"Error details: {e}")
|
| 111 |
return None, None, error_message, None
|
| 112 |
|
| 113 |
+
def get_vegetation_status(ndvi):
|
| 114 |
+
"""Get detailed vegetation status based on NDVI value"""
|
| 115 |
+
if ndvi < 0:
|
| 116 |
+
return "Very Low - Bare soil or water bodies"
|
| 117 |
+
elif ndvi < 0.1:
|
| 118 |
+
return "Low - Very sparse vegetation"
|
| 119 |
+
elif ndvi < 0.2:
|
| 120 |
+
return "Sparse - Stressed vegetation"
|
| 121 |
+
elif ndvi < 0.3:
|
| 122 |
+
return "Moderate - Typical agricultural land"
|
| 123 |
+
elif ndvi < 0.4:
|
| 124 |
+
return "Good - Healthy vegetation"
|
| 125 |
+
elif ndvi < 0.5:
|
| 126 |
+
return "High - Very healthy vegetation"
|
| 127 |
+
elif ndvi < 0.6:
|
| 128 |
+
return "Very High - Dense vegetation"
|
| 129 |
+
else:
|
| 130 |
+
return "Dense - Very dense, healthy vegetation"
|
| 131 |
+
|
| 132 |
def get_recommendations(score, ndvi):
|
| 133 |
"""Get detailed recommendations based on scores"""
|
| 134 |
if score >= 0.8 and ndvi >= 0.6:
|
|
|
|
| 214 |
return "Deteriorating conditions"
|
| 215 |
return "Mixed conditions - monitor closely"
|
| 216 |
|
| 217 |
+
# Create Gradio interface with enhanced layout
|
| 218 |
+
with gr.Blocks(theme=gr.themes.Base()) as demo:
|
| 219 |
+
gr.Markdown(
|
| 220 |
+
"""
|
| 221 |
+
# 🌱 Agricultural Credit Risk Assessment System
|
| 222 |
+
Comprehensive Weather, Vegetation, and Credit Scoring Analysis for Tobacco Farming
|
| 223 |
+
"""
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
# Input Section
|
| 227 |
+
with gr.Row():
|
| 228 |
+
location_input = gr.Textbox(
|
| 229 |
+
label="Enter Location",
|
| 230 |
+
placeholder="e.g., Tabora, Tanzania",
|
| 231 |
+
scale=4
|
| 232 |
+
)
|
| 233 |
+
analyze_button = gr.Button("Analyze", variant="primary", scale=1)
|
| 234 |
+
|
| 235 |
+
# Map and Analysis Section
|
| 236 |
+
with gr.Row():
|
| 237 |
+
with gr.Column(scale=2): # Larger map
|
| 238 |
+
location_map = gr.HTML(label="NDVI Analysis Map")
|
| 239 |
+
with gr.Column(scale=1):
|
| 240 |
+
analysis_text = gr.Textbox(
|
| 241 |
+
label="Analysis Results",
|
| 242 |
+
lines=25,
|
| 243 |
+
show_label=False
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
# Plots Section
|
| 247 |
+
with gr.Row():
|
| 248 |
+
weather_plot = gr.Plot(label="Weather Analysis")
|
| 249 |
+
|
| 250 |
+
with gr.Row():
|
| 251 |
+
score_gauge = gr.Plot(label="Growing Conditions Score")
|
| 252 |
+
|
| 253 |
+
# Examples
|
| 254 |
+
gr.Examples(
|
| 255 |
+
examples=[
|
| 256 |
+
["Tabora, Tanzania"],
|
| 257 |
+
["Urambo, Tabora, Tanzania"],
|
| 258 |
+
["Sikonge, Tabora, Tanzania"],
|
| 259 |
+
["Nzega, Tabora, Tanzania"]
|
| 260 |
+
],
|
| 261 |
+
inputs=location_input,
|
| 262 |
+
outputs=[location_map, analysis_text, weather_plot, score_gauge],
|
| 263 |
+
fn=analyze_location,
|
| 264 |
+
cache_examples=True
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
# Set up the click event
|
| 268 |
+
analyze_button.click(
|
| 269 |
+
fn=analyze_location,
|
| 270 |
+
inputs=[location_input],
|
| 271 |
+
outputs=[location_map, analysis_text, weather_plot, score_gauge]
|
| 272 |
+
)
|
| 273 |
|
| 274 |
# Launch the app
|
| 275 |
if __name__ == "__main__":
|