File size: 3,230 Bytes
78b2d2f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fe07170
78b2d2f
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
import gradio as gr
import pandas as pd
from PIL import Image
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier 

df=pd.read_csv('Dataset/water_potability.csv')
for i in ['ph','Trihalomethanes','Sulfate']:
    missing1 = df.query('Potability == 0')[i][df[i].isna()].index
    df.loc[missing1,i] = df.query('Potability == 0')[i][df[i].notna()].mean()
    missing2 = df.query('Potability == 1')[i][df[i].isna()].index
    df.loc[missing2,i] = df.query('Potability == 1')[i][df[i].notna()].mean()
    
dataInp=df.drop('Potability',axis=1)
dataOp=df['Potability']
X_train,X_test,y_train,y_test=train_test_split(dataInp,dataOp,test_size=0.25,random_state=42)
model= RandomForestClassifier()
model.fit(X_train,y_train)


inputs = [
    gr.Number(label='ph', default=6.5),
    gr.Number(label='Hardness', default=1),
    gr.Number(label='Solids', default=500),
    gr.Number(label='Chloramines', default=4),
    gr.Number(label='Sulfate', default=250),
    gr.Number(label='Conductivity', default=800),
    gr.Number(label='Organic_Carbon', default=2),
    gr.Number(label='Trihalomethanes', default=80),
    gr.Number(label='Turbidity', default=5)
]

outputs = [
    gr.Label(label="Potability"),
    gr.Image(type='pil',label="Image")
]


def predict_water_quality(ph, Hardness, Solids, Chloramines, Sulfate, Conductivity, Organic_Carbon, Trihalomethanes, Turbidity):
    
    try:    
        data=pd.DataFrame({'ph':[ph],'Hardness':[Hardness],'Solids':[Solids],
                    'Chloramines':[Chloramines],'Sulfate':[Sulfate],'Conductivity':[Conductivity],
                    'Organic_carbon':[Organic_Carbon],'Trihalomethanes':[Trihalomethanes],'Turbidity':[Turbidity]})
        X_new=data[['ph','Hardness','Solids','Chloramines','Sulfate','Conductivity','Organic_carbon','Trihalomethanes','Turbidity']]
        prediction = model.predict(X_new)
        potable_water = Image.open("images/Portable.png")
        non_potable_water = Image.open("images/NonPortable.jpg")
    
        if prediction== 1:
            image = potable_water
        else:
            image = non_potable_water
        if prediction==0:
            print('Non Potable water')
            return 'Non Potable water',image
        else:
            print('Potable water')
            return 'Potable water',image   
         
    except Exception as e:
        return (e)    

app = gr.Interface(fn=predict_water_quality,inputs=inputs, outputs=outputs,description='Water quality is a crucial aspect of public health, and the potability of water samples is a significant concern for people worldwide. Predicting whether a water sample is potable or not is a critical task that can be accomplished through the use of machine learning techniques. \n One approach to predicting water potability is to use a Random Forest Classifier model. The model can take in various features of the water sample, such as pH, hardness, solid content, turbidity, sulfate content, trihalomethanes, conductivity, and organic carbon content, as input. Each of these features is a potential contributor to water potability.', css="footer {visibility: hidden}", title='Water Potability Prediction')
app.launch(share=True)