AzizWazir commited on
Commit
ccb5abe
·
verified ·
1 Parent(s): 84bc783

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import pandas as pd
4
+ import matplotlib.pyplot as plt
5
+ from sklearn.linear_model import LinearRegression
6
+ from sklearn.preprocessing import MinMaxScaler
7
+ import joblib
8
+
9
+ # Load the pre-trained model and scaler
10
+ model = joblib.load("water_quality_model.pkl") # Ensure the model file is in the same directory
11
+ scaler = joblib.load("scaler.pkl")
12
+
13
+ # Define water quality categories
14
+ def classify_quality(score):
15
+ if score >= 0.8:
16
+ return "Excellent"
17
+ elif score >= 0.6:
18
+ return "Good"
19
+ elif score >= 0.4:
20
+ return "Fair"
21
+ else:
22
+ return "Poor"
23
+
24
+ # Define the prediction function
25
+ def predict_water_quality(pH, turbidity, dissolved_oxygen, nitrate_levels):
26
+ # Prepare the input data
27
+ data = np.array([[pH, turbidity, dissolved_oxygen, nitrate_levels]])
28
+ scaled_data = scaler.transform(data)
29
+
30
+ # Predict the quality score
31
+ score = model.predict(scaled_data)[0]
32
+ quality = classify_quality(score)
33
+
34
+ # Generate insights (graphical output)
35
+ plt.figure(figsize=(6, 4))
36
+ categories = ["Excellent", "Good", "Fair", "Poor"]
37
+ scores = [0.8, 0.6, 0.4, 0.2]
38
+ colors = ["green", "blue", "orange", "red"]
39
+
40
+ plt.bar(categories, scores, color=colors, alpha=0.7)
41
+ plt.axhline(y=score, color="black", linestyle="--", label=f"Predicted Score: {score:.2f} ({quality})")
42
+ plt.legend()
43
+ plt.title("Water Quality Prediction")
44
+ plt.xlabel("Categories")
45
+ plt.ylabel("Score")
46
+ plt.tight_layout()
47
+
48
+ # Save the graph to a file
49
+ graph_path = "water_quality_graph.png"
50
+ plt.savefig(graph_path)
51
+ plt.close()
52
+
53
+ return quality, graph_path
54
+
55
+ # Define the Gradio interface
56
+ interface = gr.Interface(
57
+ fn=predict_water_quality,
58
+ inputs=[
59
+ gr.inputs.Number(label="pH"),
60
+ gr.inputs.Number(label="Turbidity (NTU)"),
61
+ gr.inputs.Number(label="Dissolved Oxygen (mg/L)"),
62
+ gr.inputs.Number(label="Nitrate Levels (mg/L)"),
63
+ ],
64
+ outputs=[
65
+ gr.outputs.Textbox(label="Water Quality"),
66
+ gr.outputs.Image(label="Water Quality Graph")
67
+ ],
68
+ title="Water Quality Prediction",
69
+ description="Enter water parameters to predict the quality of water. Results include the classification and a visual representation of quality."
70
+ )
71
+
72
+ # Launch the application
73
+ if __name__ == "__main__":
74
+ interface.launch()