File size: 2,362 Bytes
e552386
 
9a00a5d
e552386
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
import joblib  # Bỏ comment dòng này nếu bạn dùng thư viện joblib để load model

# ==========================================
# 1. LOAD MÔ HÌNH (Uncomment khi có file model thật)
# ==========================================
model = joblib.load("water_quality_model.pkl") 


# ==========================================
# 2. HÀM XỬ LÝ DỰ ĐOÁN
# ==========================================
def predict_potability(ph, hardness, solids, chloramines, sulfate, conductivity, organic_carbon, trihalomethanes, turbidity):
    # Gom dữ liệu người dùng nhập vào thành mảng 2D đúng với format mô hình cần
    input_data = np.array([[ph, hardness, solids, chloramines, sulfate, conductivity, organic_carbon, trihalomethanes, turbidity]])
    
    # Chạy dự đoán (Uncomment khi có model)
    prediction = model.predict(input_data)[0]
    return "Nước có thể uống được (1)" if prediction == 1 else "Nước KHÔNG an toàn (0)"
    # ----------------------------------------------------------------

# ==========================================
# 3. XÂY DỰNG GIAO DIỆN GRADIO
# ==========================================
# Tạo danh sách 9 ô nhập liệu tương ứng với 9 features
input_features = [
    gr.Number(label="pH", info="Độ pH của nước"),
    gr.Number(label="Hardness", info="Độ cứng (mg/L)"),
    gr.Number(label="Solids", info="Tổng chất rắn hòa tan - TDS (ppm)"),
    gr.Number(label="Chloramines", info="Lượng Chloramines (ppm)"),
    gr.Number(label="Sulfate", info="Lượng Sulfate (mg/L)"),
    gr.Number(label="Conductivity", info="Độ dẫn điện (μS/cm)"),
    gr.Number(label="Organic_carbon", info="Lượng Carbon hữu cơ (ppm)"),
    gr.Number(label="Trihalomethanes", info="Lượng Trihalomethanes (μg/L)"),
    gr.Number(label="Turbidity", info="Độ đục (NTU)")
]

demo = gr.Interface(
    fn=predict_potability,
    inputs=input_features,
    outputs=gr.Text(label="Kết quả dự đoán (Potability)"),
    title="Hệ thống Dự đoán Chất lượng Nước",
    description="Nhập các chỉ số môi trường nước bên dưới để hệ thống phân loại khả năng uống được (Potability)."
)

# Chạy ứng dụng
if __name__ == "__main__":
    demo.launch()