|
|
| import gradio as gr |
| import pandas as pd |
| import numpy as np |
| import joblib |
|
|
| |
| stack_model = joblib.load("water_quality_model.pkl") |
|
|
| def predict_water(ph, hardness, solids, chloramines, sulfate, |
| conductivity, organic_carbon, trihalomethanes, turbidity, |
| cost_per_sample=10): |
|
|
| ph_hardness = ph * hardness |
| solids_turbidity = solids / (turbidity + 1) |
| features = np.array([[ph, hardness, solids, chloramines, sulfate, |
| conductivity, organic_carbon, trihalomethanes, |
| turbidity, ph_hardness, solids_turbidity]]) |
|
|
| prob = stack_model.predict_proba(features)[0][1] |
| pred_class = "Safe" if prob >= 0.5 else "Not Safe" |
|
|
| color = "green" if pred_class=="Safe" else "red" |
| result_text = f"<span style='color:{color}; font-size:24px'>Water is {pred_class} (Confidence: {prob*100:.1f}%)</span>" |
|
|
| treatment_cost = cost_per_sample if pred_class=="Not Safe" else 0 |
| econ_text = f"<span style='font-size:20px'>Estimated Treatment Cost: {treatment_cost} EGP</span>" |
|
|
| return result_text, econ_text |
|
|
| interface = gr.Interface( |
| fn=predict_water, |
| inputs=[ |
| gr.Slider(0,14, step=0.1, label="pH"), |
| gr.Slider(0,500, step=1, label="Hardness"), |
| gr.Slider(0,1000, step=1, label="Solids"), |
| gr.Slider(0,20, step=0.1, label="Chloramines"), |
| gr.Slider(0,500, step=1, label="Sulfate"), |
| gr.Slider(0,1500, step=1, label="Conductivity"), |
| gr.Slider(0,20, step=0.1, label="Organic Carbon"), |
| gr.Slider(0,150, step=0.1, label="Trihalomethanes"), |
| gr.Slider(0,10, step=0.1, label="Turbidity"), |
| gr.Number(value=10, label="Cost per Sample (EGP)") |
| ], |
| outputs=[ |
| gr.HTML(label="Prediction Result"), |
| gr.HTML(label="Economic Impact") |
| ], |
| title="Water Quality Prediction with Economic Impact", |
| description="Enter water properties to predict water safety and estimated treatment cost." |
| ) |
|
|
| if __name__ == "__main__": |
| interface.launch() |
|
|