Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import streamlit as st
|
4 |
+
from fpdf import FPDF
|
5 |
+
from groq import Groq
|
6 |
+
|
7 |
+
# Initialize Groq client
|
8 |
+
client = Groq(
|
9 |
+
api_key="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",
|
10 |
+
)
|
11 |
+
|
12 |
+
def analyze_water_quality(data, standards):
|
13 |
+
"""Analyze water quality data against standards."""
|
14 |
+
results = []
|
15 |
+
for parameter, value in data.items():
|
16 |
+
standard = standards.get(parameter, None)
|
17 |
+
if standard:
|
18 |
+
exceeded = ((value - standard) / standard) * 100
|
19 |
+
if value < standard:
|
20 |
+
remark = "Below standard. Treatment needed."
|
21 |
+
treatment = "Primary Treatment: Screening, Sedimentation, Grit Removal."
|
22 |
+
else:
|
23 |
+
remark = "Above standard. Treatment needed."
|
24 |
+
if parameter in ["Bacteria"]:
|
25 |
+
treatment = "Tertiary Treatment: Disinfection using Chlorination or UV."
|
26 |
+
elif parameter in ["Lead", "Mercury", "Arsenic"]:
|
27 |
+
treatment = "Advanced Treatment: Chemical Precipitation, Adsorption."
|
28 |
+
elif parameter in ["Temperature"]:
|
29 |
+
treatment = "Cooling, Aeration."
|
30 |
+
elif parameter in ["Clarity"]:
|
31 |
+
treatment = "Coagulation, Flocculation."
|
32 |
+
elif parameter in ["Conductivity"]:
|
33 |
+
treatment = "Ion Exchange, Reverse Osmosis."
|
34 |
+
elif parameter in ["pH"]:
|
35 |
+
treatment = "pH Adjustment: Lime or Acid Addition."
|
36 |
+
elif parameter in ["Alkalinity"]:
|
37 |
+
treatment = "Alkalinity Control: Lime or Soda Ash."
|
38 |
+
elif parameter in ["Chlorine"]:
|
39 |
+
treatment = "Dechlorination: Activated Carbon or Sulfite."
|
40 |
+
elif parameter in ["Hardness"]:
|
41 |
+
treatment = "Softening: Ion Exchange or Reverse Osmosis."
|
42 |
+
elif parameter in ["Dissolved Oxygen"]:
|
43 |
+
treatment = "Aeration."
|
44 |
+
else:
|
45 |
+
treatment = "Preliminary Treatment: Removal of large debris."
|
46 |
+
results.append({
|
47 |
+
"Parameter": parameter,
|
48 |
+
"Value": value,
|
49 |
+
"Standard": standard,
|
50 |
+
"Exceedance (%)": exceeded,
|
51 |
+
"Remark": remark,
|
52 |
+
"Treatment": treatment
|
53 |
+
})
|
54 |
+
return results
|
55 |
+
|
56 |
+
def generate_pdf_report(results, filename="Water_Quality_Report.pdf"):
|
57 |
+
"""Generate a PDF report for water quality analysis."""
|
58 |
+
pdf = FPDF()
|
59 |
+
pdf.add_page()
|
60 |
+
pdf.set_font("Arial", size=12)
|
61 |
+
pdf.cell(200, 10, txt="Water Quality Assessment Report", ln=True, align='C')
|
62 |
+
pdf.ln(10)
|
63 |
+
|
64 |
+
for result in results:
|
65 |
+
pdf.cell(0, 10, txt=f"Parameter: {result['Parameter']}", ln=True)
|
66 |
+
pdf.cell(0, 10, txt=f"Value: {result['Value']} | Standard: {result['Standard']} | Exceedance: {result['Exceedance (%)']:.2f}%", ln=True)
|
67 |
+
pdf.cell(0, 10, txt=f"Remark: {result['Remark']}", ln=True)
|
68 |
+
pdf.cell(0, 10, txt=f"Treatment: {result['Treatment']}", ln=True)
|
69 |
+
pdf.ln(5)
|
70 |
+
|
71 |
+
pdf.output(filename)
|
72 |
+
|
73 |
+
def main():
|
74 |
+
st.title("Water Quality Assessment App")
|
75 |
+
st.write("Upload water sample data and analyze it against Punjab, Pakistan water quality standards.")
|
76 |
+
|
77 |
+
uploaded_file = st.file_uploader("Upload your water sample data (Excel format):", type=["xls", "xlsx"])
|
78 |
+
|
79 |
+
if uploaded_file:
|
80 |
+
# Load water sample data
|
81 |
+
sample_data = pd.read_excel(uploaded_file)
|
82 |
+
st.write("Sample Data:")
|
83 |
+
st.dataframe(sample_data)
|
84 |
+
|
85 |
+
# Load water quality standards (to be replaced with your data)
|
86 |
+
standards = {
|
87 |
+
"Lead": 0.01,
|
88 |
+
"Mercury": 0.001,
|
89 |
+
"Bacteria": 0.0,
|
90 |
+
"Temperature": 25, # Example standard for temperature in Celsius
|
91 |
+
"Clarity": 5, # Example standard for clarity (NTU)
|
92 |
+
"Conductivity": 1000, # Example standard for conductivity (µS/cm)
|
93 |
+
"pH": 7, # Example standard for pH
|
94 |
+
"Alkalinity": 200, # Example standard for alkalinity (mg/L)
|
95 |
+
"Chlorine": 0.5, # Example standard for chlorine (mg/L)
|
96 |
+
"Hardness": 200, # Example standard for hardness (mg/L)
|
97 |
+
"Dissolved Oxygen": 5 # Example standard for dissolved oxygen (mg/L)
|
98 |
+
}
|
99 |
+
|
100 |
+
# Analyze the data
|
101 |
+
results = []
|
102 |
+
for _, row in sample_data.iterrows():
|
103 |
+
parameter = row['Parameter']
|
104 |
+
value = row['Value']
|
105 |
+
result = analyze_water_quality({parameter: value}, standards)
|
106 |
+
results.extend(result)
|
107 |
+
|
108 |
+
# Display results
|
109 |
+
st.write("Analysis Results:")
|
110 |
+
results_df = pd.DataFrame(results)
|
111 |
+
st.dataframe(results_df)
|
112 |
+
|
113 |
+
# Generate and download report
|
114 |
+
if st.button("Generate PDF Report"):
|
115 |
+
report_filename = "Water_Quality_Report.pdf"
|
116 |
+
generate_pdf_report(results, filename=report_filename)
|
117 |
+
st.success("Report generated successfully!")
|
118 |
+
with open(report_filename, "rb") as file:
|
119 |
+
st.download_button(
|
120 |
+
label="Download Report",
|
121 |
+
data=file,
|
122 |
+
file_name=report_filename,
|
123 |
+
mime="application/pdf"
|
124 |
+
)
|
125 |
+
|
126 |
+
if __name__ == "__main__":
|
127 |
+
main()
|