Spaces:
Sleeping
Sleeping
Upload 3 files
Browse filesIDS model version 2
- XGBoost_Model_v1.pkl +3 -0
- app2.py +167 -0
- requirements.txt +48 -0
XGBoost_Model_v1.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7e24dd8ae7e0b225b74239e64da6d8230f382fd7f4abeb611c9a68031bfdf9b7
|
| 3 |
+
size 321731
|
app2.py
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import numpy as np
|
| 4 |
+
import pandas as pd
|
| 5 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 6 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 7 |
+
|
| 8 |
+
# Define the ScalableModel class
|
| 9 |
+
class ScalableModel:
|
| 10 |
+
def __init__(self, model, feature_scaler=None, target_scaler=None):
|
| 11 |
+
self.model = model
|
| 12 |
+
self.feature_scaler = feature_scaler if feature_scaler else MinMaxScaler()
|
| 13 |
+
self.target_scaler = target_scaler if target_scaler else MinMaxScaler()
|
| 14 |
+
|
| 15 |
+
def predict(self, X_test):
|
| 16 |
+
X_test_scaled = self.feature_scaler.transform(X_test)
|
| 17 |
+
predictions_scaled = self.model.predict(X_test_scaled).reshape(-1, 1)
|
| 18 |
+
return self.target_scaler.inverse_transform(predictions_scaled).flatten()
|
| 19 |
+
|
| 20 |
+
# Load the trained model and scalers
|
| 21 |
+
xgboost_model = joblib.load("XGBoost_Model_v1.pkl") # Ensure the model is saved
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
# Feature names (MUST match your training data exactly)
|
| 25 |
+
feature_names = [
|
| 26 |
+
'garlic_kgs_organic_pesticides', #
|
| 27 |
+
'ginger_kgs_organic_pesticides', #
|
| 28 |
+
'plastic_tanks_120_ltrs_liquid_manure', #
|
| 29 |
+
'sacks_liquid_manure', #
|
| 30 |
+
'hoes_tools', #
|
| 31 |
+
'spades_tools', #
|
| 32 |
+
'pick_axes_tools', #
|
| 33 |
+
'forked_hoes_tools', #
|
| 34 |
+
'pangas_tools', #
|
| 35 |
+
'wheelbarrows_tools', #
|
| 36 |
+
'trowels_tools', #
|
| 37 |
+
'watering_cans_tools', #
|
| 38 |
+
'spray_pumps_tools', #
|
| 39 |
+
'Beans_seeds', #
|
| 40 |
+
'Maize_seeds', #
|
| 41 |
+
'Soyabean_seeds', #
|
| 42 |
+
'Gnuts_seeds', #
|
| 43 |
+
'Irish Potatoes_seeds', #
|
| 44 |
+
# 'Cassava (bags)',
|
| 45 |
+
# 'Mugavu tree seedlings',
|
| 46 |
+
# 'Onions (Kg)_seeds',
|
| 47 |
+
# 'Millet.1_seeds',
|
| 48 |
+
'GPS-Altitude',
|
| 49 |
+
'Land_size_agriculture',
|
| 50 |
+
'Time_to_collect_Water_for_Household_use_Minutes',
|
| 51 |
+
'Beans_total_yield', #
|
| 52 |
+
'Cassava_total_yield',
|
| 53 |
+
'Maize_total_yield',
|
| 54 |
+
'Sweet_potatoes_total_yield',
|
| 55 |
+
'Food_banana_total_yield',
|
| 56 |
+
'Coffee_total_yield'
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
# Define prediction function
|
| 61 |
+
def predict_agriculture_value(*features):
|
| 62 |
+
input_data = np.array(features).reshape(1, -1)
|
| 63 |
+
# Check if all input values are zero
|
| 64 |
+
if np.all(input_data == 0):
|
| 65 |
+
return 0.0 # Return zero directly
|
| 66 |
+
|
| 67 |
+
df_input = pd.DataFrame(input_data, columns=feature_names)
|
| 68 |
+
|
| 69 |
+
# Use the model's built-in predict function
|
| 70 |
+
prediction = xgboost_model.predict(df_input)
|
| 71 |
+
|
| 72 |
+
return float(prediction[0])
|
| 73 |
+
|
| 74 |
+
# Create Gradio interface
|
| 75 |
+
input_components = [gr.Number(label=feature) for feature in feature_names]
|
| 76 |
+
|
| 77 |
+
iface = gr.Interface(
|
| 78 |
+
fn=predict_agriculture_value,
|
| 79 |
+
inputs=input_components,
|
| 80 |
+
outputs=gr.Number(label="Predicted Agriculture Value (USD)"),
|
| 81 |
+
title="Agriculture Value Prediction",
|
| 82 |
+
description="Enter input values to predict Agriculture Value (USD) using the trained XGBoost model."
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Run the Gradio app
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
iface.launch()
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
# # XGBoost Model
|
| 91 |
+
|
| 92 |
+
# import gradio as gr
|
| 93 |
+
# import joblib
|
| 94 |
+
# import numpy as np
|
| 95 |
+
# import pandas as pd
|
| 96 |
+
# from xgboost import XGBRegressor
|
| 97 |
+
# from sklearn.preprocessing import MinMaxScaler
|
| 98 |
+
|
| 99 |
+
# # Define ScalableModel class
|
| 100 |
+
# class ScalableModel:
|
| 101 |
+
# def __init__(self, model, feature_scaler=None, target_scaler=None):
|
| 102 |
+
# self.model = model
|
| 103 |
+
# self.feature_scaler = feature_scaler if feature_scaler else MinMaxScaler()
|
| 104 |
+
# self.target_scaler = target_scaler if target_scaler else MinMaxScaler()
|
| 105 |
+
|
| 106 |
+
# def fit(self, X_train, y_train):
|
| 107 |
+
# self.feature_scaler.fit(X_train)
|
| 108 |
+
# self.target_scaler.fit(y_train.values.reshape(-1, 1))
|
| 109 |
+
# self.model.fit(self.feature_scaler.transform(X_train), self.target_scaler.transform(y_train.values.reshape(-1, 1)).flatten())
|
| 110 |
+
|
| 111 |
+
# def predict(self, X_test):
|
| 112 |
+
# X_test_scaled = self.feature_scaler.transform(X_test)
|
| 113 |
+
# predictions_scaled = self.model.predict(X_test_scaled)
|
| 114 |
+
# return self.target_scaler.inverse_transform(predictions_scaled.reshape(-1, 1)).flatten()
|
| 115 |
+
|
| 116 |
+
# # Load the trained XGBoost model
|
| 117 |
+
# xgboost_model = joblib.load("XGBoost_Model_v1.pkl")
|
| 118 |
+
|
| 119 |
+
# # Load the scalers
|
| 120 |
+
# scalers = joblib.load("Scalers_XGBoost.pkl")
|
| 121 |
+
# feature_scaler = scalers["feature_scaler"]
|
| 122 |
+
# target_scaler = scalers["target_scaler"]
|
| 123 |
+
|
| 124 |
+
# # Define feature names (must match training data)
|
| 125 |
+
# feature_names = [
|
| 126 |
+
# 'garlic_kgs_organic_pesticides', 'ginger_kgs_organic_pesticides',
|
| 127 |
+
# 'plastic_tanks_120_ltrs_liquid_manure', 'sacks_liquid_manure',
|
| 128 |
+
# 'hoes_tools', 'spades_tools', 'pick_axes_tools', 'forked_hoes_tools',
|
| 129 |
+
# 'pangas_tools', 'wheelbarrows_tools', 'trowels_tools', 'watering_cans_tools',
|
| 130 |
+
# 'spray_pumps_tools', 'Beans_seeds', 'Maize_seeds', 'Soyabean_seeds',
|
| 131 |
+
# 'Gnuts_seeds', 'Irish Potatoes_seeds', 'GPS-Altitude', 'Land_size_agriculture',
|
| 132 |
+
# 'Time_to_collect_Water_for_Household_use_Minutes', 'Beans_total_yield',
|
| 133 |
+
# 'Cassava_total_yield', 'Maize_total_yield', 'Sweet_potatoes_total_yield',
|
| 134 |
+
# 'Food_banana_total_yield', 'Coffee_total_yield'
|
| 135 |
+
# ]
|
| 136 |
+
|
| 137 |
+
# # Define prediction function
|
| 138 |
+
# def predict_agriculture_value(*features):
|
| 139 |
+
# input_data = np.array(features).reshape(1, -1)
|
| 140 |
+
|
| 141 |
+
# # Scale the input features
|
| 142 |
+
# input_data_scaled = feature_scaler.transform(input_data)
|
| 143 |
+
|
| 144 |
+
# # Get scaled predictions
|
| 145 |
+
# prediction_scaled = xgboost_model.predict(input_data_scaled).reshape(-1, 1)
|
| 146 |
+
|
| 147 |
+
# # Inverse transform to get predictions in original scale
|
| 148 |
+
# prediction = target_scaler.inverse_transform(prediction_scaled).flatten()
|
| 149 |
+
|
| 150 |
+
# return float(prediction[0])
|
| 151 |
+
|
| 152 |
+
# # Create Gradio interface
|
| 153 |
+
# input_components = [gr.Number(label=feature) for feature in feature_names]
|
| 154 |
+
|
| 155 |
+
# iface = gr.Interface(
|
| 156 |
+
# fn=predict_agriculture_value,
|
| 157 |
+
# inputs=input_components,
|
| 158 |
+
# outputs=gr.Number(label="Predicted Agriculture Value (USD)"),
|
| 159 |
+
# title="Agriculture Value Prediction",
|
| 160 |
+
# description="Enter input values to predict Agriculture Value (USD) using the trained XGBoost model."
|
| 161 |
+
# )
|
| 162 |
+
|
| 163 |
+
# # Run the Gradio app
|
| 164 |
+
# if __name__ == "__main__":
|
| 165 |
+
# iface.launch()
|
| 166 |
+
|
| 167 |
+
# print("Expected feature order:", feature_names)
|
requirements.txt
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
asttokens==3.0.0
|
| 2 |
+
comm==0.2.2
|
| 3 |
+
contourpy==1.3.1
|
| 4 |
+
cycler==0.12.1
|
| 5 |
+
debugpy==1.8.13
|
| 6 |
+
decorator==5.2.1
|
| 7 |
+
et_xmlfile==2.0.0
|
| 8 |
+
executing==2.2.0
|
| 9 |
+
fonttools==4.56.0
|
| 10 |
+
ipykernel==6.29.5
|
| 11 |
+
ipython==9.0.2
|
| 12 |
+
ipython_pygments_lexers==1.1.1
|
| 13 |
+
jedi==0.19.2
|
| 14 |
+
joblib==1.4.2
|
| 15 |
+
jupyter_client==8.6.3
|
| 16 |
+
jupyter_core==5.7.2
|
| 17 |
+
kiwisolver==1.4.8
|
| 18 |
+
matplotlib==3.10.1
|
| 19 |
+
matplotlib-inline==0.1.7
|
| 20 |
+
nest-asyncio==1.6.0
|
| 21 |
+
numpy==2.2.3
|
| 22 |
+
openpyxl==3.1.5
|
| 23 |
+
packaging==24.2
|
| 24 |
+
pandas==2.2.3
|
| 25 |
+
parso==0.8.4
|
| 26 |
+
pexpect==4.9.0
|
| 27 |
+
pillow==11.1.0
|
| 28 |
+
platformdirs==4.3.6
|
| 29 |
+
prompt_toolkit==3.0.50
|
| 30 |
+
psutil==7.0.0
|
| 31 |
+
ptyprocess==0.7.0
|
| 32 |
+
pure_eval==0.2.3
|
| 33 |
+
Pygments==2.19.1
|
| 34 |
+
pyparsing==3.2.1
|
| 35 |
+
python-dateutil==2.9.0.post0
|
| 36 |
+
pytz==2025.1
|
| 37 |
+
pyzmq==26.3.0
|
| 38 |
+
scikit-learn==1.6.1
|
| 39 |
+
scipy==1.15.2
|
| 40 |
+
seaborn==0.13.2
|
| 41 |
+
six==1.17.0
|
| 42 |
+
stack-data==0.6.3
|
| 43 |
+
threadpoolctl==3.5.0
|
| 44 |
+
tornado==6.4.2
|
| 45 |
+
traitlets==5.14.3
|
| 46 |
+
tzdata==2025.1
|
| 47 |
+
wcwidth==0.2.13
|
| 48 |
+
gradio>=3.50.0
|