import joblib import numpy as np import os current_dir = os.path.dirname(os.path.abspath(__file__)) models_dir = os.path.join(current_dir, '..', 'models') crop_prediction_model = joblib.load(os.path.join(models_dir, 'crop_prediction_model.pkl')) crop_prediction_scaler = joblib.load(os.path.join(models_dir, 'crop_prediction_scaler.pkl')) fertilizer_prediction_model = joblib.load(os.path.join(models_dir, 'fertilizer_prediction_model.pkl')) fertilizer_prediction_scaler = joblib.load(os.path.join(models_dir, 'fertilizer_prediction_scaler.pkl')) crop_encoder = joblib.load(os.path.join(models_dir, 'crop_encoder.pkl')) soil_encoder = joblib.load(os.path.join(models_dir, 'soil_encoder.pkl')) fertilizer_encoder = joblib.load(os.path.join(models_dir, 'fertilizer_encoder.pkl')) def get_crop_prediction(data): features = np.array([[ data.Nitrogen, data.Phosphorus, data.Potassium, data.temperature, data.humidity, data.ph, data.rainfall ]]) scaled_features = crop_prediction_scaler.transform(features) prediction = crop_prediction_model.predict(scaled_features) return {"predicted_crop": prediction[0]} def get_fertilizer_prediction(data): crop_encoded = crop_encoder.transform(np.array([[data.crop_type]])) soil_encoded = soil_encoder.transform(np.array([[data.soil_type]])) features = np.array([[ data.temperature, data.humidity, data.moisture, data.Nitrogen, data.Potassium, data.Phosphorus ]]) scaled_features = fertilizer_prediction_scaler.transform(features) final_features = np.concatenate([scaled_features, [soil_encoded], [crop_encoded]], axis=1) prediction_encoded = fertilizer_prediction_model.predict(final_features) prediction = fertilizer_encoder.inverse_transform(prediction_encoded) return {"predicted_fertilizer": prediction[0]} # import joblib # import numpy as np # import os # current_dir = os.path.dirname(os.path.abspath(__file__)) # models_dir = os.path.join(current_dir, '..', 'models') # crop_prediction_model = joblib.load(os.path.join(models_dir, 'crop_prediction_model.pkl')) # crop_prediction_scaler = joblib.load(os.path.join(models_dir, 'crop_prediction_scaler.pkl')) # fertilizer_prediction_model = joblib.load(os.path.join(models_dir, 'fertilizer_prediction_model.pkl')) # fertilizer_prediction_scaler = joblib.load(os.path.join(models_dir, 'fertilizer_prediction_scaler.pkl')) # crop_encoder = joblib.load(os.path.join(models_dir, 'crop_encoder.pkl')) # soil_encoder = joblib.load(os.path.join(models_dir, 'soil_encoder.pkl')) # fertilizer_encoder = joblib.load(os.path.join(models_dir, 'fertilizer_encoder.pkl')) # def get_crop_prediction(data): # features = np.array([[ # data.Nitrogen, # data.Phosphorous, # data.Potassium, # data.temperature, # data.humidity, # data.ph, # data.rainfall # ]]) # scaled_features = crop_prediction_scaler.transform(features) # prediction = crop_prediction_model.predict(scaled_features) # return {"predicted_crop": prediction[0]} # def get_fertilizer_prediction(data): # crop_encoded = crop_encoder.transform(np.array([[data.crop_type]])) # soil_encoded = soil_encoder.transform(np.array([[data.soil_type]])) # features = np.array([[ # data.Temperature, # data.Humidity, # data.Moisture, # data.Nitrogen, # data.Potassium, # data.Phosphorus # ]]) # scaled_features = fertilizer_prediction_scaler.transform(features) # final_features = np.concatenate([scaled_features, [soil_encoded], [crop_encoded]], axis=1) # prediction_encoded = fertilizer_prediction_model.predict(final_features) # prediction = fertilizer_encoder.inverse_transform(prediction_encoded) # return {"predicted_fertilizer": prediction[0]} # # import joblib # # import numpy as np # # import os # # # Get the absolute path to the current file's directory # # current_dir = os.path.dirname(os.path.abspath(__file__)) # # # Construct the absolute path to the models directory # # models_dir = os.path.join(current_dir, '..', 'models') # # # Load crop prediction model and scaler # # crop_prediction_model = joblib.load(os.path.join(models_dir, 'crop_prediction_model.pkl')) # # crop_prediction_scaler = joblib.load(os.path.join(models_dir, 'crop_prediction_scaler.pkl')) # # # Load fertilizer prediction model and encoders/scalers # # fertilizer_prediction_model = joblib.load(os.path.join(models_dir, 'fertilizer_prediction_model.pkl')) # # fertilizer_prediction_scaler = joblib.load(os.path.join(models_dir, 'fertilizer_prediction_scaler.pkl')) # # crop_encoder = joblib.load(os.path.join(models_dir, 'crop_encoder.pkl')) # # soil_encoder = joblib.load(os.path.join(models_dir, 'soil_encoder.pkl')) # # fertilizer_encoder = joblib.load(os.path.join(models_dir, 'fertilizer_encoder.pkl')) # # def get_crop_prediction(data): # # """ # # Predicts the recommended crop based on soil and weather conditions. # # """ # # features = np.array([[ # # data.Nitrogen, # # data.Phosphorous, # # data.Potassium, # # data.temperature, # # data.humidity, # # data.ph, # # data.rainfall # # ]]) # # scaled_features = crop_prediction_scaler.transform(features) # # prediction = crop_prediction_model.predict(scaled_features) # # return {"predicted_crop": prediction[0]} # # def get_fertilizer_prediction(data): # # """ # # Predicts the recommended fertilizer based on soil, weather, and crop type. # # """ # # soil_encoded = soil_encoder.transform(np.array([[data.soil_type]])) # # crop_encoded = crop_encoder.transform(np.array([[data.crop_type]])) # # features = np.array([[ # # data.Nitrogen, # # data.Phosphorus, # # data.Potassium, # # data.Temperature, # # data.Humidity, # # data.Moisture # # ]]) # # scaled_features = fertilizer_prediction_scaler.transform(features) # # final_features = np.concatenate([scaled_features, [soil_encoded], [crop_encoded]], axis=1) # # prediction_encoded = fertilizer_prediction_model.predict(final_features) # # prediction = fertilizer_encoder.inverse_transform(prediction_encoded) # # return {"predicted_fertilizer": prediction[0]} # import joblib # import numpy as np # import os # # Get the absolute path to the current file's directory # current_dir = os.path.dirname(os.path.abspath(__file__)) # # Construct the absolute path to the models directory # models_dir = os.path.join(current_dir, '..', 'models') # # Load crop prediction model and scaler # crop_prediction_model = joblib.load(os.path.join(models_dir, 'crop_prediction_model.pkl')) # crop_prediction_scaler = joblib.load(os.path.join(models_dir, 'crop_prediction_scaler.pkl')) # # Load fertilizer prediction model and encoders/scalers # fertilizer_prediction_model = joblib.load(os.path.join(models_dir, 'fertilizer_prediction_model.pkl')) # fertilizer_prediction_scaler = joblib.load(os.path.join(models_dir, 'fertilizer_prediction_scaler.pkl')) # crop_encoder = joblib.load(os.path.join(models_dir, 'crop_encoder.pkl')) # soil_encoder = joblib.load(os.path.join(models_dir, 'soil_encoder.pkl')) # fertilizer_encoder = joblib.load(os.path.join(models_dir, 'fertilizer_encoder.pkl')) # def get_crop_prediction(data): # """ # Predicts the recommended crop based on soil and weather conditions. # """ # features = np.array([[ # data.Nitrogen, # data.Phosphorous, # data.Potassium, # data.temperature, # data.humidity, # data.ph, # data.rainfall # ]]) # scaled_features = crop_prediction_scaler.transform(features) # prediction = crop_prediction_model.predict(scaled_features) # return {"predicted_crop": prediction[0]} # def get_fertilizer_prediction(data): # """ # Predicts the recommended fertilizer based on soil, weather, and crop type. # """ # soil_encoded = soil_encoder.transform(np.array([[data.soil_type]])) # crop_encoded = crop_encoder.transform(np.array([[data.crop_type]])) # features = np.array([[ # data.Nitrogen, # data.Phosphorus, # data.Potassium, # data.Temperature, # data.Humidity, # data.Moisture # ]]) # scaled_features = fertilizer_prediction_scaler.transform(features) # final_features = np.concatenate([scaled_features, [soil_encoded], [crop_encoded]], axis=1) # prediction_encoded = fertilizer_prediction_model.predict(final_features) # prediction = fertilizer_encoder.inverse_transform(prediction_encoded) # return {"predicted_fertilizer": prediction[0]}