MiguelCh's picture
Update app.py
2125012 verified
raw
history blame
3.68 kB
import gradio as gr
import numpy as np
import pickle
# Cargar el modelo
with open('model_rf.pkl', 'rb') as file:
rf = pickle.load(file)
# Cargar el scaler
with open('my-standard-scaler.pkl', 'rb') as file:
s_c = pickle.load(file)
# Definir la funci贸n de predicci贸n
def predict_1(SOC: float):
SOC = SOC/1.724 # SOC <- MO/1.724
prediction = 1.58 + np.exp(-0.07*SOC)
return prediction.round(2)
def predict_2(SOC: float, Cy: float):
SOC = SOC/1.724 # SOC <- MO/1.724
prediction = np.array(2.03 - 0.008*Cy - 0.008*SOC)
return prediction.round(2)
def predict_3(SOC: float, Cy: float, eCa: float, eMg: float, eK: float, eNa: float, eAlH: float):
SOC = SOC/1.724 # SOC <- MO/1.724
ECEC = eCa + eMg + eK + eNa + eAlH
xK = eK/ECEC
prediction = np.array(1.53 - 0.076*SOC + 0.004*Cy - 2.04*xK)
return prediction.round(2)
def predict_4(pH: float, EC: float, CCE: float, SOC: float, Sa: float, Si: float,
Cy: float, CEC: float, eCa: float, eMg: float, eK: float, eNa: float, eAlH: float):
SOC = SOC/1.724 # SOC <- MO/1.724
ECEC = eCa + eMg + eK + eNa + eAlH
xCa = eCa/ECEC
xMg = eMg/ECEC
xK = eK/ECEC
xNa = eNa/ECEC
xAlH = eAlH/ECEC
BS1 = (eCa + eMg + eK + eNa)/CEC
BS2 = (eCa + eMg + eK + eNa)/ECEC
input_features = np.array([[pH, EC, CCE, SOC, Sa, Si, Cy, CEC, ECEC, xCa, xMg, xK, xNa, xAlH, BS1, BS2]])
input_features_scale = s_c.transform(input_features)
prediction = rf.predict(input_features_scale)[0].round(2)
return prediction
# Crear la interfaz Gradio
with gr.Blocks() as demo:
gr.Markdown("# Estimaci贸n de la densidad aparente del suelo")
with gr.Row():
with gr.Column():
SOC = gr.Number(label="MO (%)", value=2.9408, interactive=True)
eK = gr.Number(label="K (meq/100g)", value=0.47, interactive=True)
pH = gr.Number(label="pH (1:1)", value=7.09, interactive=True)
EC = gr.Number(label="C.E. (dS/m)", value=0.31, interactive=True)
with gr.Column():
Cy = gr.Number(label="Arcilla (%)", value=31.0, interactive=True)
eNa = gr.Number(label="Na (meq/100g)", value=0.15, interactive=True)
CCE = gr.Number(label="CaCO3 (%)", value=0.20, interactive=True)
with gr.Column():
eCa = gr.Number(label="Ca (meq/100g)", value=19.44, interactive=True)
eAlH = gr.Number(label="Al + H (meq/100g)", value=0.0, interactive=True)
Si = gr.Number(label="Limo (%)", value=24.0, interactive=True)
with gr.Column():
eMg = gr.Number(label="Mg (meq/100g)", value=3.47, interactive=True)
Sa = gr.Number(label="Arena (%)", value=45.0, interactive=True)
CEC = gr.Number(label="CIC (meq/100g)", value=23.52, interactive=True)
with gr.Row():
with gr.Column():
submit_1 = gr.Button(value='Abdelbaki')
with gr.Column():
submit_2 = gr.Button(value='Benites')
with gr.Column():
submit_3 = gr.Button(value='MLRegression')
with gr.Column():
submit_4 = gr.Button(value='Random Forest')
output = gr.Textbox(label=" Densidad aparente del suelo (%).", interactive=False)
submit_1.click(predict_1, inputs=[SOC], outputs=[output])
submit_2.click(predict_2, inputs=[SOC, Cy], outputs=[output])
submit_3.click(predict_3, inputs=[SOC, Cy, eCa, eMg, eK, eNa, eAlH], outputs=[output])
submit_4.click(predict_4, inputs=[pH , EC, CCE, SOC, Sa, Si, Cy, CEC, eCa, eMg, eK, eNa, eAlH], outputs=[output])
demo.launch(share=False, debug=False)