File size: 7,926 Bytes
334c27d
 
 
 
b2e65e5
 
334c27d
b2e65e5
334c27d
 
b2e65e5
334c27d
b2e65e5
334c27d
b2e65e5
82fef47
334c27d
b1b23a5
55eae35
945b4da
 
3ce65dd
 
 
8df8d71
 
 
 
 
 
 
 
 
 
55eae35
b2e65e5
 
 
334c27d
82fef47
334c27d
c7d5c20
 
 
945b4da
 
 
 
 
c7d5c20
945b4da
 
 
 
 
 
334c27d
b2e65e5
 
945c40c
334c27d
 
945c40c
334c27d
 
 
 
 
 
 
 
 
 
 
 
 
945c40c
82fef47
945c40c
 
 
 
 
82fef47
 
 
945c40c
 
 
237c4a0
945c40c
237c4a0
945c40c
 
82fef47
1547fb9
237cf0f
 
 
 
 
 
 
841434a
b2e65e5
945c40c
b2e65e5
c61f4a1
 
945c40c
 
c61f4a1
 
945c40c
 
 
 
 
 
 
 
1547fb9
334c27d
7bba835
b2e65e5
 
1547fb9
c61f4a1
 
 
7bba835
c61f4a1
8df8d71
334c27d
1547fb9
04cbcb7
 
 
 
e269e0c
334c27d
8df8d71
 
 
 
 
 
04cbcb7
7ce9c98
8df8d71
 
 
 
 
 
 
334c27d
237cf0f
 
 
334c27d
b2e65e5
1547fb9
945c40c
0a3e2aa
945c40c
 
 
 
334c27d
abd923c
 
 
e6daf44
abd923c
 
 
 
 
 
 
 
 
 
 
8df8d71
 
 
 
 
 
 
 
334c27d
 
945c40c
 
 
 
 
 
b2e65e5
237cf0f
 
 
 
 
 
 
 
 
 
b2e65e5
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import gradio as gr
import pandas as pd
import numpy as np
import joblib

# -----------------------------
# 1️⃣ Load model and data
# -----------------------------
MODEL_PATH = "linear_model.pkl"
EXCEL_PATH = "excel sheet of plant 2.xlsx"

linear_model = joblib.load(MODEL_PATH)
df = pd.read_excel(EXCEL_PATH)
df.columns = df.columns.str.strip()

# Strains and rows
strain_names = df['pea plant strain'].unique().tolist()
row_options = ["None, Enter Manually"] + [str(i) for i in range(len(df))]

#-------------- To be commented --------------------

# -----------------------------
# 2️⃣ Autofill function
# -----------------------------
# def autofill_fields(row_index):
#     if row_index == "None, Enter Manually":
#         return [None]*11
#     row = df.iloc[int(row_index)]
#     return (
#         row['Dose (g/pot)'], row['Soil N (ppm)'], row['Soil P (ppm)'],
#         row['Soil K (ppm)'], row['pH'], row['Chlorophyll (SPAD)'],
#         row['Shoot Length (cm)'], row['Root Length (cm)'], row['Shoot Wt (g)'],
#         row['Root Wt (g)'], row['Yield (g/pot)']
#     )

# -----------------------------
# 3️⃣ Prediction function
# -----------------------------
def predict_linear(strain, dose, soil_n, soil_p, soil_k, ph,
                   chlorophyll, shoot_len, root_len, shoot_wt, root_wt, yield_gp):
    logs = []
    # -----------------------------
    # 🧩 Convert textbox inputs to floats (or None if blank)
    # -----------------------------
    # def to_float(x):
    #     try:
    #         return float(x)
    #     except (TypeError, ValueError):
    #         return None

    # dose = to_float(dose)
    # soil_n = to_float(soil_n)
    # soil_p = to_float(soil_p)
    # soil_k = to_float(soil_k)
    # ph = to_float(ph)
    # # -----------------------------
    required = [dose, soil_n, soil_p, soil_k, ph]
    if any(v is None for v in required):
        logs.append("[DEBUG] Missing numeric inputs!")
        return pd.DataFrame(), "\n⚠️ Fill all required inputs", "\n".join(logs)
    logs.append("[DEBUG] Inputs received.")

    # Prepare DataFrame for model
    X_input = pd.DataFrame([{
        'pea plant strain': strain,
        'Dose (g/pot)': dose,
        'Soil N (ppm)': soil_n,
        'Soil P (ppm)': soil_p,
        'Soil K (ppm)': soil_k,
        'pH': ph
    }])
    logs.append(f"[DEBUG] Input DataFrame:\n{X_input}")

    y_pred = linear_model.predict(X_input)[0]
    logs.append(f"[DEBUG] Predicted values:\n{y_pred}")

    # Actuals and errors
    actuals = [chlorophyll, shoot_len, root_len, shoot_wt, root_wt, yield_gp]
    abs_errors = [
        round(abs(p - a), 2) if a is not None else "N/A"
        for p, a in zip(y_pred[:6], actuals)
    ]

    target_cols = ['Chlorophyll (SPAD)', 'Shoot Length (cm)', 'Root Length (cm)',
                   'Shoot Wt (g)', 'Root Wt (g)', 'Yield (g/pot)', 'Relative Yield (%)']

    # Build table DataFrame
    data = {
        "Output Metric": target_cols,
        # "Actual Value": actuals + ["N/A"],
        "Predicted Value": [round(v, 2) for v in y_pred],
        # "Absolute Error": abs_errors + ["N/A"]
    }
    result_df = pd.DataFrame(data)

    return result_df, "Prediction complete!", "\n".join(logs)
    
# -----------------------------
#  Clear Inputs Function
# -----------------------------
def clear_inputs():
    # Reset all input fields to None
    return [None] * 11  # same number of numeric input fields

# -----------------------------
# 4️⃣ Gradio Interface (Green Theme)
# -----------------------------
invalid_strains = ["Strains", "strain1", "strain2", ""]
valid_strain = next(
    (s for s in strain_names if pd.notna(s) and s not in invalid_strains),
    strain_names[0]
)

with gr.Blocks(
    title="Linear Regression Plant Predictor",
    theme=gr.themes.Soft(
        primary_hue="green",
        secondary_hue="green",
        neutral_hue="green"
    )
) as demo:
    gr.Markdown("<h1 style='text-align:center; color:#2E8B57;'>Linear Regression, Plant Yield Predictor</h1>")

    strain_names = ["Tetradesmus nigardi", "Clostroprosis acicularis"]
    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("### Plant & Strain")
            strain_input = gr.Dropdown(
                strain_names,
                label="Select Strain",
                value=strain_names[0]  # default: Tetradesmus nigardi
            )
            # row_selector = gr.Dropdown(row_options, label="Select Row", value="None, Enter Manually")

            gr.Markdown("### Input Parameters")
            dose = gr.Number(label="Dose (g/pot)",placeholder="Enter value") # ,placeholder="Enter value"
            soil_n = gr.Number(label="Soil N (ppm)",placeholder="Enter value")
            soil_p = gr.Number(label="Soil P (ppm)",placeholder="Enter value")
            soil_k = gr.Number(label="Soil K (ppm)",placeholder="Enter value")
            ph = gr.Number(label="pH")

            # gr.Markdown("### Autofilled Actual Metrics (from Excel)")
            # chlorophyll = gr.Number(label="Chlorophyll (SPAD)")
            # shoot_len = gr.Number(label="Shoot Length (cm)")
            # root_len = gr.Number(label="Root Length (cm)")
            # shoot_wt = gr.Number(label="Shoot Wt (g)")
            # root_wt = gr.Number(label="Root Wt (g)")
            # yield_gp = gr.Number(label="Yield (g/pot)")

            # Hidden placeholders for metrics
            chlorophyll = gr.State(None)
            shoot_len = gr.State(None)
            root_len = gr.State(None)
            shoot_wt = gr.State(None)
            root_wt = gr.State(None)
            yield_gp = gr.State(None)

            with gr.Row():
                predict_btn = gr.Button(" Predict", variant="primary")
                clear_btn = gr.Button("🧹 Clear Inputs", variant="secondary")

        with gr.Column(scale=1):
            gr.Markdown("### Prediction Results Table")
            result_table = gr.DataFrame(
                headers=["Output Metric", "Predicted Value"],      #"Actual Value", "Absolute Error"
                label="Results Comparison",
                interactive=False
            )
            status_box = gr.Markdown("")
            log_box = gr.Textbox(label="Debug Logs", lines=15)
            gr.Markdown(
                """
                ### Input Tips:
                - Select the **plant strain** you want to analyze.
                - Provide all essential input parameters: **Dose (g/pot), Soil N, Soil P, Soil K, and pH**.
                - Click **“Predict”** to generate the model’s predictions.

                ### Output Tips:
                <div>
                <p style="margin:6px 0;"><strong>Prediction Table:</strong> Displays model predictions for all plant growth metrics.</p>
                <p style="margin:6px 0;"><strong>Debug Logs:</strong> Provides detailed trace of model inputs and internal calculations.</p>
                </div>
                """
            )

    # #Autofill callback
    # row_selector.change(
    #     fn=autofill_fields,
    #     inputs=[row_selector],
    #     outputs=[dose, soil_n, soil_p, soil_k, ph,
    #              chlorophyll, shoot_len, root_len,
    #              shoot_wt, root_wt, yield_gp]
    # )

    # Prediction callback
    predict_btn.click(
        fn=predict_linear,
        inputs=[strain_input, dose, soil_n, soil_p, soil_k, ph,
                chlorophyll, shoot_len, root_len, shoot_wt, root_wt, yield_gp],
        outputs=[result_table, status_box, log_box]
    )

    # Clear button callback
    clear_btn.click(
        fn=clear_inputs,
        inputs=[],
        outputs=[dose, soil_n, soil_p, soil_k, ph,
             chlorophyll, shoot_len, root_len,
             shoot_wt, root_wt, yield_gp]
    )


# -----------------------------
# 5️⃣ Launch
# -----------------------------
if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)