Spaces:
Runtime error
Runtime error
import gradio as gr | |
import pickle | |
import numpy as np | |
# Load the trained models | |
with open('rf_crop.pkl', 'rb') as file: | |
crop_model = pickle.load(file, errors='ignore') | |
with open('knn_fertilizer.pkl', 'rb') as file: | |
fertilizer_model = pickle.load(file, errors='ignore') | |
with open('Scaler_fertilizer.pkl', 'rb') as file: # Assuming you saved the scaler during model training | |
scaler = pickle.load(file, errors='ignore') | |
# Label Encoders for the models | |
crop_label_encoder = { | |
0: "Sugarcane", 1: "Wheat", 2: "Cotton", 3: "Jowar", 4: "Rice", | |
5: "Maize", 6: "Groundnut", 7: "Grapes", 8: "Tur", 9: "Ginger", | |
10: "Turmeric", 11: "Urad", 12: "Gram", 13: "Moong", 14: "Soybean", 15: "Masoor" | |
} | |
fertilizer_label_encoder = { | |
0: "Urea", 1: "DAP", 2: "MOP", 3: "SSP", 4: "19:19:19 NPK", | |
5: "Chilated Micronutrient", 6: "50:26:26 NPK", 7: "Magnesium Sulphate", | |
8: "10:26:26 NPK", 9: "Ferrous Sulphate", 10: "13:32:26 NPK", | |
11: "10:10:10 NPK", 12: "Ammonium Sulphate", 13: "12:32:16 NPK", | |
14: "White Potash", 15: "Hydrated Lime", 16: "20:20:20 NPK", | |
17: "18:46:00 NPK", 18: "Sulphur" | |
} | |
# Prediction functions | |
def predict_crop(Nitrogen, Phosphorus, Potassium, pH, Rainfall, Temperature): | |
# Prepare the input data and scale it | |
crop_input = np.array([[Nitrogen, Phosphorus, Potassium, pH, Rainfall, Temperature]]) | |
crop_input_scaled = scaler.transform(crop_input) | |
# Predict the crop | |
crop_prediction = crop_model.predict(crop_input_scaled) | |
return crop_prediction[0] | |
def predict_fertilizer(Nitrogen, Phosphorus, Potassium, pH, Rainfall, Temperature, Crop): | |
# Prepare the input data and scale it | |
crop_index = list(crop_label_encoder.keys())[list(crop_label_encoder.values()).index(Crop)] | |
fertilizer_input = np.array([[Nitrogen, Phosphorus, Potassium, pH, Rainfall, Temperature, crop_index]]) | |
fertilizer_input_scaled = scaler.transform(fertilizer_input[:, :-1]) | |
# Add crop index back to the scaled input | |
fertilizer_input_scaled = np.hstack([fertilizer_input_scaled, [[crop_index]]]) | |
# Predict the fertilizer | |
fertilizer_prediction = fertilizer_model.predict(fertilizer_input_scaled) | |
return fertilizer_label_encoder[int(fertilizer_prediction[0])] | |
# Gradio Interface for Crop Prediction | |
crop_interface = gr.Interface( | |
fn=predict_crop, | |
inputs=[ | |
gr.Number(label="Nitrogen"), | |
gr.Number(label="Phosphorus"), | |
gr.Number(label="Potassium"), | |
gr.Number(label="pH"), | |
gr.Number(label="Rainfall"), | |
gr.Number(label="Temperature") | |
], | |
outputs=gr.Label(num_top_classes=1), | |
title="Crop Prediction", | |
allow_flagging='never' | |
) | |
# Gradio Interface for Fertilizer Prediction | |
fertilizer_interface = gr.Interface( | |
fn=predict_fertilizer, | |
inputs=[ | |
gr.Number(label="Nitrogen"), | |
gr.Number(label="Phosphorus"), | |
gr.Number(label="Potassium"), | |
gr.Number(label="pH"), | |
gr.Number(label="Rainfall"), | |
gr.Number(label="Temperature"), | |
gr.Dropdown(label="Crop", choices=list(crop_label_encoder.values())) | |
], | |
outputs=gr.Label(num_top_classes=1), | |
title="Fertilizer Prediction", | |
allow_flagging='never' | |
) | |
# Create a Tabbed Interface in Gradio | |
app = gr.TabbedInterface([crop_interface, fertilizer_interface], ["Crop Prediction", "Fertilizer Prediction"]) | |
# Launch the app | |
app.launch() | |