JohanBeytell commited on
Commit
bdf0fae
·
verified ·
1 Parent(s): 567307d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -11
app.py CHANGED
@@ -4,25 +4,39 @@ from sklearn.preprocessing import LabelEncoder
4
  from xgboost import XGBClassifier
5
  import pickle
6
 
7
- model = pickle.load('crop_recommendation_model.pkl')
8
- le = pickle.load('label_encoder.pkl')
 
9
 
10
  def recommend_crop(nitrogen, phosphorus, potassium, temperature, humidity, ph, rainfall):
11
- X_sample = nitrogen, phosphorus, potassium, temperature, humidity, ph, rainfall
 
12
 
13
  # Predict crop recommendations
14
  y_pred_sample = model.predict(X_sample)
15
 
16
- # Decode the predictions and ground truth back to crop names
17
  crops_pred = le.inverse_transform(y_pred_sample)
18
 
19
- return crops_pred
20
 
21
  # Create the Gradio interface
22
  interface = gr.Interface(
23
- fn=classify_potato_plant,
24
- inputs=[gr.Number(label="Nitrogen - Ratio of Nitrogen in the soil"), gr.Number(label="Phosphorus - Ratio of Phosphorus in the soil"), gr.Number(label="Potassium - Ratio of Potassium in the soil"), gr.Number(label="Temperature - In degrees Celsius"), gr.Number(label="Humidity - Relative humidity in %"), gr.Number(label="pH Value - pH value of the soil"), gr.Number(label="Rainfall - Rainfall in mm")],
25
- outputs=[gr.Textbox(label="Predicted Output"), gr.Textbox(label="Confidence Score")],
26
- title="Acres - PPDC",
27
- description="Acres PPDC, is our Potato Plant Disease Classification vision model, capable of accurately classifying potato plant disease, based on a single image."
28
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  from xgboost import XGBClassifier
5
  import pickle
6
 
7
+ # Load the trained model and label encoder
8
+ model = pickle.load(open('crop_recommendation_model.pkl', 'rb'))
9
+ le = pickle.load(open('label_encoder.pkl', 'rb'))
10
 
11
  def recommend_crop(nitrogen, phosphorus, potassium, temperature, humidity, ph, rainfall):
12
+ # Prepare the input sample as a 2D array
13
+ X_sample = np.array([[nitrogen, phosphorus, potassium, temperature, humidity, ph, rainfall]])
14
 
15
  # Predict crop recommendations
16
  y_pred_sample = model.predict(X_sample)
17
 
18
+ # Decode the prediction back to crop name
19
  crops_pred = le.inverse_transform(y_pred_sample)
20
 
21
+ return crops_pred[0] # Return the predicted crop name
22
 
23
  # Create the Gradio interface
24
  interface = gr.Interface(
25
+ fn=recommend_crop,
26
+ inputs=[
27
+ gr.Number(label="Nitrogen - Ratio of Nitrogen in the soil"),
28
+ gr.Number(label="Phosphorus - Ratio of Phosphorus in the soil"),
29
+ gr.Number(label="Potassium - Ratio of Potassium in the soil"),
30
+ gr.Number(label="Temperature - In degrees Celsius"),
31
+ gr.Number(label="Humidity - Relative humidity in %"),
32
+ gr.Number(label="pH Value - pH value of the soil"),
33
+ gr.Number(label="Rainfall - Rainfall in mm")
34
+ ],
35
+ outputs=gr.Textbox(label="Recommended Crop"),
36
+ title="Acres - CR",
37
+ description="Acres Crop Recommendation recommends the best crop to plant based on soil and climate conditions."
38
+ )
39
+
40
+ # Launch the app
41
+ if __name__ == "__main__":
42
+ interface.launch()