shubham5027 commited on
Commit
150569d
·
verified ·
1 Parent(s): 44d4010

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -48
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('ensemble.pkl')
7
 
8
- # Define states, crops, and seasons for dropdown menus
9
- states = [
10
- 'Andaman and Nicobar Islands', 'Andhra Pradesh', 'Arunachal Pradesh', 'Assam', 'Bihar', 'Chandigarh',
11
- 'Chhattisgarh', 'Dadra and Nagar Haveli', 'Goa', 'Gujarat', 'Haryana', 'Himachal Pradesh',
12
- 'Jammu and Kashmir', 'Jharkhand', 'Karnataka', 'Kerala', 'Madhya Pradesh', 'Maharashtra', 'Manipur',
13
- 'Meghalaya', 'Mizoram', 'Nagaland', 'Odisha', 'Puducherry', 'Punjab', 'Rajasthan', 'Sikkim', 'Tamil Nadu',
14
- 'Telangana', 'Tripura', 'Uttar Pradesh', 'Uttarakhand', 'West Bengal'
15
- ]
16
 
17
- crops = [
18
- 'Arecanut', 'Barley', 'Banana', 'Blackpepper', 'Brinjal', 'Cabbage', 'Cardamom', 'Cashewnuts', 'Cauliflower',
19
- 'Coriander', 'Cotton', 'Garlic', 'Grapes', 'Horsegram', 'Jowar', 'Jute', 'Ladyfinger', 'Maize',
20
- 'Mango', 'Moong', 'Onion', 'Orange', 'Papaya', 'Pineapple', 'Potato', 'Rapeseed', 'Ragi', 'Rice',
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
- """Predict crop yield based on user inputs."""
28
- state_encoded = [1 if s == state else 0 for s in states]
29
- crop_encoded = [1 if c == crop else 0 for c in crops]
30
- season_encoded = [1 if s == season else 0 for s in seasons]
 
31
 
32
- input_features = np.array(state_encoded + crop_encoded + season_encoded + [pH, rainfall, temperature, area, production])
33
- input_features = input_features.reshape(1, -1)
34
 
35
- try:
36
- predicted_yield = crop_yield_model.predict(input_features)[0]
37
- return f"The predicted yield is {predicted_yield:.2f} tons per hectare."
 
 
 
 
38
  except Exception as e:
39
- return f"Error: {e}"
40
 
41
- # Gradio interface
42
- def interface():
43
- """Define the Gradio interface."""
44
- inputs = [
45
- gr.Dropdown(choices=states, label="Select State"),
46
- gr.Dropdown(choices=crops, label="Select Crop"),
47
- gr.Dropdown(choices=seasons, label="Select Season"),
48
- gr.Number(label="Soil pH Value", value=7.0, precision=2),
49
- gr.Number(label="Rainfall (mm)", value=100.0, precision=1),
50
- gr.Number(label="Temperature (°C)", value=25.0, precision=1),
51
  gr.Number(label="Area (hectares)", value=1.0, precision=2),
52
- gr.Number(label="Production (tons)", value=2.0, precision=2)
53
- ]
54
-
55
- outputs = gr.Textbox(label="Predicted Yield")
 
 
56
 
57
- gr.Interface(
58
- fn=predict_yield,
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