Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,9 @@ import gradio as gr
|
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
import numpy as np
|
| 5 |
-
|
| 6 |
-
# Load the trained model
|
| 7 |
import os
|
| 8 |
-
import joblib
|
| 9 |
|
|
|
|
| 10 |
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 11 |
model_path = os.path.join(script_dir, "district_yield_pipeline.pkl")
|
| 12 |
|
|
@@ -50,7 +48,9 @@ STATES = [
|
|
| 50 |
'Uttar Pradesh', 'Uttarakhand', 'West Bengal'
|
| 51 |
]
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
"""
|
| 55 |
Predict crop yield based on input features
|
| 56 |
"""
|
|
@@ -66,7 +66,8 @@ def predict_yield(crop, season, state, area, annual_rainfall, fertilizer, pestic
|
|
| 66 |
'Area': [float(area)],
|
| 67 |
'Annual_Rainfall': [float(annual_rainfall)],
|
| 68 |
'Fertilizer': [float(fertilizer)],
|
| 69 |
-
'Pesticide': [float(pesticide)]
|
|
|
|
| 70 |
})
|
| 71 |
|
| 72 |
# Make prediction
|
|
@@ -88,6 +89,7 @@ def predict_yield(crop, season, state, area, annual_rainfall, fertilizer, pestic
|
|
| 88 |
- ๐ง๏ธ **Annual Rainfall:** {annual_rainfall} mm
|
| 89 |
- ๐ **Fertilizer:** {fertilizer} kg
|
| 90 |
- ๐งช **Pesticide:** {pesticide} kg
|
|
|
|
| 91 |
|
| 92 |
---
|
| 93 |
|
|
@@ -127,14 +129,14 @@ def predict_yield(crop, season, state, area, annual_rainfall, fertilizer, pestic
|
|
| 127 |
def load_example(example_name):
|
| 128 |
"""Load predefined examples"""
|
| 129 |
examples = {
|
| 130 |
-
"Rice - Kharif Season": ("Rice", "Kharif", "West Bengal", 5000, 2000, 500000, 1000),
|
| 131 |
-
"Wheat - Rabi Season": ("Wheat", "Rabi", "Punjab", 3000, 1200, 400000, 800),
|
| 132 |
-
"Cotton - Kharif Season": ("Cotton(Lint)", "Kharif", "Gujarat", 4000, 800, 350000, 900),
|
| 133 |
-
"Sugarcane - Whole Year": ("Sugarcane", "Whole Year", "Maharashtra", 2500, 1500, 600000, 1200),
|
| 134 |
-
"Potato - Rabi Season": ("Potato", "Rabi", "Uttar Pradesh", 1500, 900, 250000, 600)
|
| 135 |
}
|
| 136 |
|
| 137 |
-
return examples.get(example_name, ("Rice", "Kharif", "Karnataka", 1000, 1500, 100000, 500))
|
| 138 |
|
| 139 |
# Custom CSS for better styling
|
| 140 |
custom_css = """
|
|
@@ -199,6 +201,13 @@ with gr.Blocks(css=custom_css, title="Crop Yield Prediction System") as demo:
|
|
| 199 |
info="Choose the state where the crop is grown"
|
| 200 |
)
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
# Numeric inputs
|
| 203 |
area_input = gr.Number(
|
| 204 |
label="๐ Area (in hectares)",
|
|
@@ -228,8 +237,8 @@ with gr.Blocks(css=custom_css, title="Crop Yield Prediction System") as demo:
|
|
| 228 |
with gr.Row():
|
| 229 |
predict_btn = gr.Button("๐ฎ Predict Yield", variant="primary", size="lg")
|
| 230 |
clear_btn = gr.ClearButton(
|
| 231 |
-
components=[crop_input, season_input, state_input,
|
| 232 |
-
rainfall_input, fertilizer_input, pesticide_input],
|
| 233 |
value="๐ Clear"
|
| 234 |
)
|
| 235 |
|
|
@@ -270,16 +279,17 @@ with gr.Blocks(css=custom_css, title="Crop Yield Prediction System") as demo:
|
|
| 270 |
- **Testing Accuracy:** 97.82% Rยฒ Score
|
| 271 |
- **RMSE:** 122.72
|
| 272 |
- **Dataset:** Indian Agricultural Crop Yield Data (1997-2019)
|
| 273 |
-
- **Features:**
|
| 274 |
|
| 275 |
### How to Use
|
| 276 |
|
| 277 |
1. Select the crop type from the dropdown
|
| 278 |
2. Choose the appropriate growing season
|
| 279 |
3. Select the state where cultivation occurs
|
| 280 |
-
4.
|
| 281 |
-
5.
|
| 282 |
-
6.
|
|
|
|
| 283 |
|
| 284 |
### Data Ranges (for reference)
|
| 285 |
|
|
@@ -287,6 +297,7 @@ with gr.Blocks(css=custom_css, title="Crop Yield Prediction System") as demo:
|
|
| 287 |
- **Rainfall:** 300 - 6,500 mm/year
|
| 288 |
- **Fertilizer:** 100 - 100,000,000 kg
|
| 289 |
- **Pesticide:** 1 - 300,000 kg
|
|
|
|
| 290 |
|
| 291 |
### Disclaimer
|
| 292 |
|
|
@@ -299,7 +310,7 @@ with gr.Blocks(css=custom_css, title="Crop Yield Prediction System") as demo:
|
|
| 299 |
predict_btn.click(
|
| 300 |
fn=predict_yield,
|
| 301 |
inputs=[crop_input, season_input, state_input, area_input,
|
| 302 |
-
rainfall_input, fertilizer_input, pesticide_input],
|
| 303 |
outputs=[prediction_output, insights_output]
|
| 304 |
)
|
| 305 |
|
|
@@ -307,7 +318,7 @@ with gr.Blocks(css=custom_css, title="Crop Yield Prediction System") as demo:
|
|
| 307 |
fn=load_example,
|
| 308 |
inputs=[example_dropdown],
|
| 309 |
outputs=[crop_input, season_input, state_input, area_input,
|
| 310 |
-
rainfall_input, fertilizer_input, pesticide_input]
|
| 311 |
)
|
| 312 |
|
| 313 |
# Footer
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
import numpy as np
|
|
|
|
|
|
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
+
# Load the trained model
|
| 8 |
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 9 |
model_path = os.path.join(script_dir, "district_yield_pipeline.pkl")
|
| 10 |
|
|
|
|
| 48 |
'Uttar Pradesh', 'Uttarakhand', 'West Bengal'
|
| 49 |
]
|
| 50 |
|
| 51 |
+
YEARS = list(range(1997, 2020))
|
| 52 |
+
|
| 53 |
+
def predict_yield(crop, season, state, area, annual_rainfall, fertilizer, pesticide, year):
|
| 54 |
"""
|
| 55 |
Predict crop yield based on input features
|
| 56 |
"""
|
|
|
|
| 66 |
'Area': [float(area)],
|
| 67 |
'Annual_Rainfall': [float(annual_rainfall)],
|
| 68 |
'Fertilizer': [float(fertilizer)],
|
| 69 |
+
'Pesticide': [float(pesticide)],
|
| 70 |
+
'Year': [int(year)]
|
| 71 |
})
|
| 72 |
|
| 73 |
# Make prediction
|
|
|
|
| 89 |
- ๐ง๏ธ **Annual Rainfall:** {annual_rainfall} mm
|
| 90 |
- ๐ **Fertilizer:** {fertilizer} kg
|
| 91 |
- ๐งช **Pesticide:** {pesticide} kg
|
| 92 |
+
- ๐
**Year:** {year}
|
| 93 |
|
| 94 |
---
|
| 95 |
|
|
|
|
| 129 |
def load_example(example_name):
|
| 130 |
"""Load predefined examples"""
|
| 131 |
examples = {
|
| 132 |
+
"Rice - Kharif Season": ("Rice", "Kharif", "West Bengal", 5000, 2000, 500000, 1000, 2015),
|
| 133 |
+
"Wheat - Rabi Season": ("Wheat", "Rabi", "Punjab", 3000, 1200, 400000, 800, 2015),
|
| 134 |
+
"Cotton - Kharif Season": ("Cotton(Lint)", "Kharif", "Gujarat", 4000, 800, 350000, 900, 2015),
|
| 135 |
+
"Sugarcane - Whole Year": ("Sugarcane", "Whole Year", "Maharashtra", 2500, 1500, 600000, 1200, 2015),
|
| 136 |
+
"Potato - Rabi Season": ("Potato", "Rabi", "Uttar Pradesh", 1500, 900, 250000, 600, 2015)
|
| 137 |
}
|
| 138 |
|
| 139 |
+
return examples.get(example_name, ("Rice", "Kharif", "Karnataka", 1000, 1500, 100000, 500, 2015))
|
| 140 |
|
| 141 |
# Custom CSS for better styling
|
| 142 |
custom_css = """
|
|
|
|
| 201 |
info="Choose the state where the crop is grown"
|
| 202 |
)
|
| 203 |
|
| 204 |
+
year_input = gr.Dropdown(
|
| 205 |
+
choices=YEARS,
|
| 206 |
+
label="๐
Select Year",
|
| 207 |
+
value=2015,
|
| 208 |
+
info="Select the year for prediction (1997-2019)"
|
| 209 |
+
)
|
| 210 |
+
|
| 211 |
# Numeric inputs
|
| 212 |
area_input = gr.Number(
|
| 213 |
label="๐ Area (in hectares)",
|
|
|
|
| 237 |
with gr.Row():
|
| 238 |
predict_btn = gr.Button("๐ฎ Predict Yield", variant="primary", size="lg")
|
| 239 |
clear_btn = gr.ClearButton(
|
| 240 |
+
components=[crop_input, season_input, state_input, year_input,
|
| 241 |
+
area_input, rainfall_input, fertilizer_input, pesticide_input],
|
| 242 |
value="๐ Clear"
|
| 243 |
)
|
| 244 |
|
|
|
|
| 279 |
- **Testing Accuracy:** 97.82% Rยฒ Score
|
| 280 |
- **RMSE:** 122.72
|
| 281 |
- **Dataset:** Indian Agricultural Crop Yield Data (1997-2019)
|
| 282 |
+
- **Features:** 8 input features including crop type, season, location, year, and farming inputs
|
| 283 |
|
| 284 |
### How to Use
|
| 285 |
|
| 286 |
1. Select the crop type from the dropdown
|
| 287 |
2. Choose the appropriate growing season
|
| 288 |
3. Select the state where cultivation occurs
|
| 289 |
+
4. Select the year for prediction
|
| 290 |
+
5. Enter numerical values for area, rainfall, fertilizer, and pesticide
|
| 291 |
+
6. Click "Predict Yield" to get results
|
| 292 |
+
7. Review the prediction and insights provided
|
| 293 |
|
| 294 |
### Data Ranges (for reference)
|
| 295 |
|
|
|
|
| 297 |
- **Rainfall:** 300 - 6,500 mm/year
|
| 298 |
- **Fertilizer:** 100 - 100,000,000 kg
|
| 299 |
- **Pesticide:** 1 - 300,000 kg
|
| 300 |
+
- **Year:** 1997 - 2019
|
| 301 |
|
| 302 |
### Disclaimer
|
| 303 |
|
|
|
|
| 310 |
predict_btn.click(
|
| 311 |
fn=predict_yield,
|
| 312 |
inputs=[crop_input, season_input, state_input, area_input,
|
| 313 |
+
rainfall_input, fertilizer_input, pesticide_input, year_input],
|
| 314 |
outputs=[prediction_output, insights_output]
|
| 315 |
)
|
| 316 |
|
|
|
|
| 318 |
fn=load_example,
|
| 319 |
inputs=[example_dropdown],
|
| 320 |
outputs=[crop_input, season_input, state_input, area_input,
|
| 321 |
+
rainfall_input, fertilizer_input, pesticide_input, year_input]
|
| 322 |
)
|
| 323 |
|
| 324 |
# Footer
|