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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import os
|
| 4 |
+
import pickle
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
from sklearn import set_config
|
| 7 |
+
set_config(transform_output="pandas")
|
| 8 |
+
|
| 9 |
+
# PAGE CONFIG
|
| 10 |
+
# page_icon = "π"
|
| 11 |
+
|
| 12 |
+
# Setup variables and constants
|
| 13 |
+
# datetime.now().strftime('%d-%m-%Y _ %Hh %Mm %Ss')
|
| 14 |
+
DIRPATH = os.path.dirname(os.path.realpath(__file__))
|
| 15 |
+
tmp_dir = os.path.join(DIRPATH, "src", "assets", "tmp",)
|
| 16 |
+
os.system(f'rm -vf {tmp_dir}/*')
|
| 17 |
+
tmp_df_fp = os.path.join(
|
| 18 |
+
tmp_dir, f"history_{datetime.now().strftime('%d-%m-%Y _ %Hh %Mm %Ss')}.csv")
|
| 19 |
+
ml_core_fp = os.path.join(DIRPATH, "src", "assets",
|
| 20 |
+
"ml", "crop_recommandation2.pkl")
|
| 21 |
+
init_df = pd.DataFrame(
|
| 22 |
+
{
|
| 23 |
+
"N": [],
|
| 24 |
+
"P": [],
|
| 25 |
+
"K": [],
|
| 26 |
+
"temperature": [],
|
| 27 |
+
"humidity": [],
|
| 28 |
+
"ph": [],
|
| 29 |
+
"rainfall": [],
|
| 30 |
+
}
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# FUNCTIONS
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def load_ml_components(fp):
|
| 37 |
+
"Load the ml component to re-use in app"
|
| 38 |
+
with open(fp, "rb") as f:
|
| 39 |
+
object = pickle.load(f)
|
| 40 |
+
return object
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def setup(fp):
|
| 44 |
+
"Setup the required elements like files, models, global variables, etc"
|
| 45 |
+
|
| 46 |
+
# history frame
|
| 47 |
+
if not os.path.exists(fp):
|
| 48 |
+
df_history = init_df.copy()
|
| 49 |
+
else:
|
| 50 |
+
df_history = pd.read_csv(fp)
|
| 51 |
+
|
| 52 |
+
df_history.to_csv(fp, index=False)
|
| 53 |
+
|
| 54 |
+
return df_history
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def select_categorical_widget(col_index, col_name, encoder):
|
| 58 |
+
"""This function will return the right widget to use for each categorical feature
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
categories = encoder.categories_[col_index].tolist()
|
| 62 |
+
n_unique = len(categories)
|
| 63 |
+
|
| 64 |
+
# print(
|
| 65 |
+
# f"[Info] unique categories for feature {col_name} ({type(categories)}) are : {categories}")
|
| 66 |
+
|
| 67 |
+
if n_unique == 2:
|
| 68 |
+
print(
|
| 69 |
+
f"[Info] unique categories for feature '{col_index}' {col_name} ({type(categories)}) are : {categories}")
|
| 70 |
+
|
| 71 |
+
widget = gr.Checkbox(label=f"Enter {col_name}", value=categories)
|
| 72 |
+
elif n_unique <= 5:
|
| 73 |
+
widget = gr.Radio(label=f"Enter {col_name}", choices=categories)
|
| 74 |
+
else:
|
| 75 |
+
widget = gr.Dropdown(label=f"Enter {col_name}", choices=categories)
|
| 76 |
+
|
| 77 |
+
return widget
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def make_prediction(*args):
|
| 81 |
+
"""Function that takes values from fields to make 1-by-1 prediction
|
| 82 |
+
"""
|
| 83 |
+
print(
|
| 84 |
+
f"[Info] input args of the function {args} ")
|
| 85 |
+
raw = {k: [val if not isinstance(val, list) else val[0]]
|
| 86 |
+
for val, k in zip(args, num_cols+cat_cols)}
|
| 87 |
+
print(
|
| 88 |
+
f"[Info] input modified a bit {raw}\n")
|
| 89 |
+
|
| 90 |
+
df_input = pd.DataFrame(raw)
|
| 91 |
+
global df_history
|
| 92 |
+
|
| 93 |
+
# print(f"\n[Info] Input information as dataframe: \n{df_input.to_string()}")
|
| 94 |
+
df_input.drop_duplicates(inplace=True, ignore_index=True)
|
| 95 |
+
print(f"\n[Info] Input with duplicated rows: \n{df_input.to_string()}")
|
| 96 |
+
|
| 97 |
+
df_input_num, df_input_cat = None, None
|
| 98 |
+
|
| 99 |
+
if len(cat_cols) > 0:
|
| 100 |
+
df_input_cat = df_input[cat_cols].copy()
|
| 101 |
+
if cat_imputer:
|
| 102 |
+
df_input_cat = cat_imputer.transform(df_input_cat)
|
| 103 |
+
if encoder:
|
| 104 |
+
df_input_cat = encoder.transform(df_input_cat)
|
| 105 |
+
|
| 106 |
+
if len(num_cols) > 0:
|
| 107 |
+
df_input_num = df_input[num_cols].copy()
|
| 108 |
+
if num_imputer:
|
| 109 |
+
df_input_num = num_imputer.transform(df_input_num)
|
| 110 |
+
if scaler:
|
| 111 |
+
df_input_num = scaler.transform(df_input_num)
|
| 112 |
+
|
| 113 |
+
df_input_ok = pd.concat([df_input_num, df_input_cat], axis=1)
|
| 114 |
+
|
| 115 |
+
prediction_output = model.predict_proba(df_input_ok)
|
| 116 |
+
|
| 117 |
+
output = model.predict_proba(df_input_ok)
|
| 118 |
+
|
| 119 |
+
# store confidence score/ probability for the predicted class
|
| 120 |
+
confidence_score = output.max(axis=-1)
|
| 121 |
+
df_input["confidence score"] = confidence_score
|
| 122 |
+
|
| 123 |
+
# get index of the predicted class
|
| 124 |
+
predicted_idx = output.argmax(axis=-1)
|
| 125 |
+
|
| 126 |
+
# store index then replace by the matching label
|
| 127 |
+
df_input["predicted crop"] = predicted_idx
|
| 128 |
+
predicted_label = df_input["predicted crop"].replace(idx_to_labels)
|
| 129 |
+
df_input["predicted crop"] = predicted_label
|
| 130 |
+
|
| 131 |
+
print(
|
| 132 |
+
f"[Info] Prediction output (of type '{type(prediction_output)}') from passed input: {prediction_output} of shape {prediction_output.shape}")
|
| 133 |
+
|
| 134 |
+
# print(f"[Info] Prediction: {prediction_output}")
|
| 135 |
+
# df_input['prediction'] = prediction_output
|
| 136 |
+
|
| 137 |
+
print(
|
| 138 |
+
f"\n[Info] output information as dataframe: \n{df_input.to_string()}")
|
| 139 |
+
df_history = pd.concat([df_history, df_input], ignore_index=True).drop_duplicates(
|
| 140 |
+
ignore_index=True, keep='last')
|
| 141 |
+
df_history.to_csv(tmp_df_fp, index=False, )
|
| 142 |
+
|
| 143 |
+
return df_input
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def download():
|
| 147 |
+
return gr.File.update(label="History File",
|
| 148 |
+
visible=True,
|
| 149 |
+
value=tmp_df_fp)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def hide_download():
|
| 153 |
+
return gr.File.update(label="History File",
|
| 154 |
+
visible=False)
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
# Setup execution
|
| 158 |
+
ml_components_dict = load_ml_components(fp=ml_core_fp)
|
| 159 |
+
|
| 160 |
+
num_cols = [
|
| 161 |
+
"N",
|
| 162 |
+
"P",
|
| 163 |
+
"K",
|
| 164 |
+
"temperature",
|
| 165 |
+
"humidity",
|
| 166 |
+
"ph",
|
| 167 |
+
"rainfall",
|
| 168 |
+
]
|
| 169 |
+
cat_cols = ml_components_dict['cat_cols'] if 'cat_cols' in ml_components_dict else [
|
| 170 |
+
]
|
| 171 |
+
num_imputer = ml_components_dict['num_imputer'].set_output(transform="pandas") if (
|
| 172 |
+
'num_cols' in ml_components_dict and 'num_imputer' in ml_components_dict) else None
|
| 173 |
+
cat_imputer = ml_components_dict['cat_imputer'].set_output(transform="pandas") if (
|
| 174 |
+
'cat_cols' in ml_components_dict and 'cat_imputer' in ml_components_dict) else None
|
| 175 |
+
scaler = ml_components_dict['scaler'].set_output(
|
| 176 |
+
transform="pandas") if 'scaler' in ml_components_dict else None
|
| 177 |
+
encoder = ml_components_dict['encoder'] if 'encoder' in ml_components_dict else None
|
| 178 |
+
model = ml_components_dict['model']
|
| 179 |
+
labels = ml_components_dict['labels'] if 'labels' in ml_components_dict else []
|
| 180 |
+
idx_to_labels = {i: l for (i, l) in enumerate(labels)}
|
| 181 |
+
end2end_pipeline = ml_components_dict['pipeline']
|
| 182 |
+
print(f"\n[Info] ML components loaded: {list(ml_components_dict.keys())}")
|
| 183 |
+
|
| 184 |
+
df_history = setup(tmp_df_fp)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
# APP Interface
|
| 188 |
+
|
| 189 |
+
# Main page
|
| 190 |
+
demo_inputs = []
|
| 191 |
+
with gr.Blocks() as demo:
|
| 192 |
+
gr.Markdown('''<img class="center" src="https://www.verdict.co.uk/wp-content/uploads/2018/12/Agri-tech.jpg" width="60%" height="60%">
|
| 193 |
+
<style>
|
| 194 |
+
.center {
|
| 195 |
+
display: block;
|
| 196 |
+
margin-left: auto;
|
| 197 |
+
margin-right: auto;
|
| 198 |
+
width: 50%;
|
| 199 |
+
}
|
| 200 |
+
</style>''')
|
| 201 |
+
gr.Markdown('''<center><h1> π Agri-Tech App π </h1><center>''')
|
| 202 |
+
gr.Markdown('''
|
| 203 |
+
This is a ML API for classification of crop to plant on a land regarding some features
|
| 204 |
+
''')
|
| 205 |
+
|
| 206 |
+
multiple_of = 7
|
| 207 |
+
print(f"\n[INFO] {init_df.shape[1]}\n")
|
| 208 |
+
|
| 209 |
+
with gr.Row():
|
| 210 |
+
for i in range(0, init_df.shape[1],):
|
| 211 |
+
demo_inputs.append(gr.Number(label=f"Enter {num_cols[i]}"))
|
| 212 |
+
|
| 213 |
+
# with gr.Row():
|
| 214 |
+
# for i in range(0,init_df.shape[1], multiple_of):
|
| 215 |
+
# gr.Number(label=f"Enter {num_cols[i]}")
|
| 216 |
+
# gr.Number(label=f"Enter {num_cols[i+1]}")
|
| 217 |
+
# # gr.Number(label=f"Enter {num_cols[i+2]}")
|
| 218 |
+
output = gr.Dataframe(df_history)
|
| 219 |
+
|
| 220 |
+
btn_predict = gr.Button("Predict")
|
| 221 |
+
btn_predict.click(fn=make_prediction, inputs=demo_inputs, outputs=output)
|
| 222 |
+
|
| 223 |
+
file_obj = gr.File(label="History File",
|
| 224 |
+
visible=False
|
| 225 |
+
)
|
| 226 |
+
|
| 227 |
+
btn_download = gr.Button("Download")
|
| 228 |
+
btn_download.click(fn=download, inputs=[], outputs=file_obj)
|
| 229 |
+
output.change(fn=hide_download, inputs=[], outputs=file_obj)
|
| 230 |
+
|
| 231 |
+
# second demo
|
| 232 |
+
# num_inputs = [gr.Number(label=f"Enter {col}") for col in num_cols]
|
| 233 |
+
# cat_inputs = [select_categorical_widget(col_index=i, col_name=col, encoder=encoder) for i, col in enumerate(cat_cols)]
|
| 234 |
+
|
| 235 |
+
# inputs = num_inputs + cat_inputs
|
| 236 |
+
|
| 237 |
+
# demo = gr.Interface(
|
| 238 |
+
# make_prediction,
|
| 239 |
+
# inputs,
|
| 240 |
+
# "dataframe", # "number"
|
| 241 |
+
# examples=[
|
| 242 |
+
# # [2, "cat", ["Japan", "Pakistan"], "park", ["ate", "swam"], True],
|
| 243 |
+
# ]
|
| 244 |
+
# )
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
if __name__ == "__main__":
|
| 248 |
+
demo.launch()
|