GodfreyOwino commited on
Commit
0ded05a
1 Parent(s): 2241cf3

Update: Add custom NPKPredictionModel implementation

Browse files
Files changed (5) hide show
  1. README.md +33 -0
  2. config.json +8 -0
  3. label_encoder.pkl +3 -0
  4. modeling_npk.py +62 -0
  5. npk_prediction_model.pkl +3 -0
README.md ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - npk-prediction
4
+ - xgboost
5
+ ---
6
+
7
+ # NPK Prediction Model
8
+
9
+ This model predicts the Nitrogen, Phosphorus, and Potassium needs for crops based on various input features.
10
+
11
+ ## Usage
12
+
13
+ ```python
14
+ from transformers import AutoConfig, AutoModel
15
+
16
+ # Load the model
17
+ config = AutoConfig.from_pretrained("GodfreyOwino/NPK_prediction_model1")
18
+ model = AutoModel.from_config(config)
19
+
20
+ input_data = {
21
+ 'crop_name': ['maize (corn)'],
22
+ 'target_yield': [150],
23
+ 'field_size': [10],
24
+ 'ph': [6.5],
25
+ 'organic_carbon': [1.2],
26
+ 'nitrogen': [0.15],
27
+ 'phosphorus': [20],
28
+ 'potassium': [150],
29
+ 'soil_moisture': [30]
30
+ }
31
+
32
+ prediction = model(input_data)
33
+ print(prediction)
config.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_type": "npk",
3
+ "architectures": ["NPKPredictionModel"],
4
+ "auto_map": {
5
+ "AutoConfig": "modeling_npk.NPKConfig",
6
+ "AutoModel": "modeling_npk.NPKPredictionModel"
7
+ }
8
+ }
label_encoder.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ac25981a59fd92e29772ecf03261fa9992cc2df471d6fd05d77977930203066c
3
+ size 1233
modeling_npk.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import pickle
3
+ import pandas as pd
4
+ from transformers import PreTrainedModel, PretrainedConfig
5
+ from huggingface_hub import hf_hub_download
6
+
7
+ class NPKConfig(PretrainedConfig):
8
+ model_type = "npk"
9
+
10
+ def __init__(self, **kwargs):
11
+ super().__init__(**kwargs)
12
+
13
+ class NPKPredictionModel(PreTrainedModel):
14
+ config_class = NPKConfig
15
+
16
+ def __init__(self, config):
17
+ super().__init__(config)
18
+ self.xgb_model = None
19
+ self.label_encoder = None
20
+
21
+ def forward(self, inputs):
22
+ # Preprocess inputs
23
+ processed_inputs = {}
24
+ for key, value in inputs.items():
25
+ if isinstance(value, list):
26
+ processed_inputs[key] = value[0] if value else None
27
+ else:
28
+ processed_inputs[key] = value
29
+
30
+ crop_name = processed_inputs['crop_name']
31
+ processed_inputs['crop_name'] = self.label_encoder.transform([crop_name])[0]
32
+
33
+ input_df = pd.DataFrame([processed_inputs])
34
+
35
+ # Make prediction
36
+ prediction = self.xgb_model.predict(input_df)
37
+
38
+ return {
39
+ 'Nitrogen Need': float(prediction[0][0]),
40
+ 'Phosphorus Need': float(prediction[0][1]),
41
+ 'Potassium Need': float(prediction[0][2])
42
+ }
43
+
44
+ @classmethod
45
+ def from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs):
46
+ config = kwargs.pop("config", None)
47
+ if config is None:
48
+ config = NPKConfig.from_pretrained(pretrained_model_name_or_path, **kwargs)
49
+
50
+ model = cls(config)
51
+
52
+ # Load the XGBoost model and label encoder
53
+ xgb_path = hf_hub_download(repo_id=pretrained_model_name_or_path, filename="npk_prediction_model.pkl")
54
+ le_path = hf_hub_download(repo_id=pretrained_model_name_or_path, filename="label_encoder.pkl")
55
+
56
+ with open(xgb_path, 'rb') as f:
57
+ model.xgb_model = pickle.load(f)
58
+
59
+ with open(le_path, 'rb') as f:
60
+ model.label_encoder = pickle.load(f)
61
+
62
+ return model
npk_prediction_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:03507cafdc8061c0a14f23d1791e4d7c908752e68c5b1f330f0c7854a398a640
3
+ size 129759981