File size: 5,648 Bytes
ee608de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b309db
 
 
 
 
 
 
 
ee608de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import pandas as pd
import streamlit as st
from fpdf import FPDF
from groq import Groq

# Initialize Groq client
client = Groq(
    api_key="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",
)

def analyze_water_quality(data, standards):
    """Analyze water quality data against standards."""
    results = []
    for parameter, value in data.items():
        standard = standards.get(parameter, None)
        if standard:
            exceeded = ((value - standard) / standard) * 100
            if value < standard:
                remark = "Below standard. Treatment needed."
                treatment = "Primary Treatment: Screening, Sedimentation, Grit Removal."
            else:
                remark = "Above standard. Treatment needed."
                if parameter in ["Bacteria"]:
                    treatment = "Tertiary Treatment: Disinfection using Chlorination or UV."
                elif parameter in ["Lead", "Mercury", "Arsenic"]:
                    treatment = "Advanced Treatment: Chemical Precipitation, Adsorption."
                elif parameter in ["Temperature"]:
                    treatment = "Cooling, Aeration."
                elif parameter in ["Clarity"]:
                    treatment = "Coagulation, Flocculation."
                elif parameter in ["Conductivity"]:
                    treatment = "Ion Exchange, Reverse Osmosis."
                elif parameter in ["pH"]:
                    treatment = "pH Adjustment: Lime or Acid Addition."
                elif parameter in ["Alkalinity"]:
                    treatment = "Alkalinity Control: Lime or Soda Ash."
                elif parameter in ["Chlorine"]:
                    treatment = "Dechlorination: Activated Carbon or Sulfite."
                elif parameter in ["Hardness"]:
                    treatment = "Softening: Ion Exchange or Reverse Osmosis."
                elif parameter in ["Dissolved Oxygen"]:
                    treatment = "Aeration."
                else:
                    treatment = "Preliminary Treatment: Removal of large debris."
            results.append({
                "Parameter": parameter,
                "Value": value,
                "Standard": standard,
                "Exceedance (%)": exceeded,
                "Remark": remark,
                "Treatment": treatment
            })
    return results

def generate_pdf_report(results, filename="Water_Quality_Report.pdf"):
    """Generate a PDF report for water quality analysis."""
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=12)
    pdf.cell(200, 10, txt="Water Quality Assessment Report", ln=True, align='C')
    pdf.ln(10)

    for result in results:
        pdf.cell(0, 10, txt=f"Parameter: {result['Parameter']}", ln=True)
        pdf.cell(0, 10, txt=f"Value: {result['Value']} | Standard: {result['Standard']} | Exceedance: {result['Exceedance (%)']:.2f}%", ln=True)
        pdf.cell(0, 10, txt=f"Remark: {result['Remark']}", ln=True)
        pdf.cell(0, 10, txt=f"Treatment: {result['Treatment']}", ln=True)
        pdf.ln(5)

    pdf.output(filename)

def main():
    st.title("Water Quality Assessment App")
    st.write("Upload water sample data and analyze it against Punjab, Pakistan water quality standards.")

    uploaded_file = st.file_uploader("Upload your water sample data (Excel format):", type=["xls", "xlsx"])

    if uploaded_file:
        # Load water sample data
        sample_data = pd.read_excel(uploaded_file)
        st.write("Sample Data:")
        st.dataframe(sample_data)

        # Debug: Show column names
        st.write("Columns in uploaded data:", sample_data.columns)

        # Ensure the column names are correct
        if 'Parameter' not in sample_data.columns or 'Value' not in sample_data.columns:
            st.error("The uploaded file must contain 'Parameter' and 'Value' columns.")
            return

        # Load water quality standards (to be replaced with your data)
        standards = {
            "Lead": 0.01,
            "Mercury": 0.001,
            "Bacteria": 0.0,
            "Temperature": 25,  # Example standard for temperature in Celsius
            "Clarity": 5,  # Example standard for clarity (NTU)
            "Conductivity": 1000,  # Example standard for conductivity (µS/cm)
            "pH": 7,  # Example standard for pH
            "Alkalinity": 200,  # Example standard for alkalinity (mg/L)
            "Chlorine": 0.5,  # Example standard for chlorine (mg/L)
            "Hardness": 200,  # Example standard for hardness (mg/L)
            "Dissolved Oxygen": 5  # Example standard for dissolved oxygen (mg/L)
        }

        # Analyze the data
        results = []
        for _, row in sample_data.iterrows():
            parameter = row['Parameter']
            value = row['Value']
            result = analyze_water_quality({parameter: value}, standards)
            results.extend(result)

        # Display results
        st.write("Analysis Results:")
        results_df = pd.DataFrame(results)
        st.dataframe(results_df)

        # Generate and download report
        if st.button("Generate PDF Report"):
            report_filename = "Water_Quality_Report.pdf"
            generate_pdf_report(results, filename=report_filename)
            st.success("Report generated successfully!")
            with open(report_filename, "rb") as file:
                st.download_button(
                    label="Download Report",
                    data=file,
                    file_name=report_filename,
                    mime="application/pdf"
                )

if __name__ == "__main__":
    main()