Update app.py
Browse files
app.py
CHANGED
@@ -3,64 +3,62 @@ import numpy as np
|
|
3 |
import joblib
|
4 |
|
5 |
# Load the pre-trained crop yield prediction model
|
6 |
-
crop_yield_model = joblib.load('
|
7 |
|
8 |
-
#
|
9 |
-
states = [
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
]
|
16 |
|
17 |
-
crops = [
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
'Sesamum', 'Soyabean', 'Sunflower', 'Sweetpotato', 'Tapioca', 'Tomato', 'Turmeric', 'Wheat'
|
22 |
-
]
|
23 |
|
24 |
seasons = ['Kharif', 'Rabi', 'Summer', 'Whole Year']
|
25 |
|
|
|
26 |
def predict_yield(state, crop, season, pH, rainfall, temperature, area, production):
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
31 |
|
32 |
-
|
33 |
-
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
38 |
except Exception as e:
|
39 |
-
return f"Error: {e}"
|
40 |
|
41 |
-
# Gradio
|
42 |
-
|
43 |
-
|
44 |
-
inputs
|
45 |
-
gr.Dropdown(
|
46 |
-
gr.Dropdown(
|
47 |
-
gr.Dropdown(
|
48 |
-
gr.Number(label="Soil pH Value", value=
|
49 |
-
gr.Number(label="Rainfall (mm)", value=100.0, precision=
|
50 |
-
gr.Number(label="Temperature (°C)", value=25.0, precision=
|
51 |
gr.Number(label="Area (hectares)", value=1.0, precision=2),
|
52 |
-
gr.Number(label="Production (tons)", value=
|
53 |
-
]
|
54 |
-
|
55 |
-
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
inputs=inputs,
|
60 |
-
outputs=outputs,
|
61 |
-
title="Crop Yield Prediction",
|
62 |
-
description="Enter the details below to predict crop yield using the trained model."
|
63 |
-
).launch()
|
64 |
|
65 |
-
if __name__ == "__main__":
|
66 |
-
interface()
|
|
|
3 |
import joblib
|
4 |
|
5 |
# Load the pre-trained crop yield prediction model
|
6 |
+
crop_yield_model = joblib.load('voting_yield.sav')
|
7 |
|
8 |
+
# Predefined options
|
9 |
+
states = ['Andaman and Nicobar Islands', 'Andhra Pradesh', 'Arunachal Pradesh', 'Assam', 'Bihar', 'Chandigarh',
|
10 |
+
'Chhattisgarh', 'Dadra and Nagar Haveli', 'Goa', 'Gujarat', 'Haryana', 'Himachal Pradesh',
|
11 |
+
'Jammu and Kashmir', 'Jharkhand', 'Karnataka', 'Kerala', 'Madhya Pradesh', 'Maharashtra',
|
12 |
+
'Manipur', 'Meghalaya', 'Mizoram', 'Nagaland', 'Odisha', 'Puducherry', 'Punjab',
|
13 |
+
'Rajasthan', 'Sikkim', 'Tamil Nadu', 'Telangana', 'Tripura', 'Uttar Pradesh',
|
14 |
+
'Uttarakhand', 'West Bengal']
|
|
|
15 |
|
16 |
+
crops = ['Arecanut', 'Barley', 'Banana', 'Blackpepper', 'Brinjal', 'Cabbage', 'Cardamom', 'Cashewnuts', 'Cauliflower',
|
17 |
+
'Coriander', 'Cotton', 'Garlic', 'Grapes', 'Horsegram', 'Jowar', 'Jute', 'Ladyfinger', 'Maize',
|
18 |
+
'Mango', 'Moong', 'Onion', 'Orange', 'Papaya', 'Pineapple', 'Potato', 'Rapeseed', 'Ragi', 'Rice',
|
19 |
+
'Sesamum', 'Soyabean', 'Sunflower', 'Sweetpotato', 'Tapioca', 'Tomato', 'Turmeric', 'Wheat']
|
|
|
|
|
20 |
|
21 |
seasons = ['Kharif', 'Rabi', 'Summer', 'Whole Year']
|
22 |
|
23 |
+
|
24 |
def predict_yield(state, crop, season, pH, rainfall, temperature, area, production):
|
25 |
+
try:
|
26 |
+
# Encode categorical inputs
|
27 |
+
state_encoded = [1 if state == s else 0 for s in states]
|
28 |
+
crop_encoded = [1 if crop == c else 0 for c in crops]
|
29 |
+
season_encoded = [1 if season == s else 0 for s in seasons]
|
30 |
|
31 |
+
# Combine inputs
|
32 |
+
input_features = np.array(state_encoded + crop_encoded + season_encoded + [pH, rainfall, temperature, area, production]).reshape(1, -1)
|
33 |
|
34 |
+
# Check feature length
|
35 |
+
expected_features = len(states) + len(crops) + len(seasons) + 5
|
36 |
+
if input_features.shape[1] != expected_features:
|
37 |
+
return "Error: Feature shape mismatch!"
|
38 |
+
else:
|
39 |
+
predicted_yield = crop_yield_model.predict(input_features)
|
40 |
+
return f"The predicted yield is: {predicted_yield[0]:.2f} tons/hectare"
|
41 |
except Exception as e:
|
42 |
+
return f"Error: {str(e)}"
|
43 |
|
44 |
+
# Gradio Interface
|
45 |
+
interface = gr.Interface(
|
46 |
+
fn=predict_yield,
|
47 |
+
inputs=[
|
48 |
+
gr.Dropdown(states, label="Select State"),
|
49 |
+
gr.Dropdown(crops, label="Select Crop"),
|
50 |
+
gr.Dropdown(seasons, label="Select Season"),
|
51 |
+
gr.Number(label="Soil pH Value", value=6.5, precision=2),
|
52 |
+
gr.Number(label="Rainfall (mm)", value=100.0, precision=2),
|
53 |
+
gr.Number(label="Temperature (°C)", value=25.0, precision=2),
|
54 |
gr.Number(label="Area (hectares)", value=1.0, precision=2),
|
55 |
+
gr.Number(label="Production (tons)", value=1.0, precision=2),
|
56 |
+
],
|
57 |
+
outputs="text",
|
58 |
+
title="Crop Yield Prediction",
|
59 |
+
description="Enter the required parameters to predict the crop yield in tons/hectare.",
|
60 |
+
)
|
61 |
|
62 |
+
# Launch the app
|
63 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
64 |
|
|
|
|