EngrNarmeen commited on
Commit
a9989d5
·
verified ·
1 Parent(s): 4f9dccf

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -0
app.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import matplotlib.pyplot as plt
4
+ import seaborn as sns
5
+ from sklearn.ensemble import RandomForestRegressor
6
+ from sklearn.model_selection import train_test_split
7
+ from sklearn.metrics import mean_squared_error
8
+ import numpy as np
9
+
10
+ # Load sample data (replace with real pollution dataset)
11
+ def load_sample_data():
12
+ data = {
13
+ "Date": pd.date_range(start="2023-01-01", periods=100, freq="D"),
14
+ "AQI": np.random.randint(50, 200, size=100), # Random AQI values
15
+ "Temperature": np.random.uniform(20, 35, size=100),
16
+ "Humidity": np.random.uniform(30, 80, size=100),
17
+ }
18
+ return pd.DataFrame(data)
19
+
20
+ # Train a simple model to predict AQI
21
+ def train_model(data):
22
+ X = data[["Temperature", "Humidity"]]
23
+ y = data["AQI"]
24
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
25
+
26
+ model = RandomForestRegressor(n_estimators=100, random_state=42)
27
+ model.fit(X_train, y_train)
28
+
29
+ y_pred = model.predict(X_test)
30
+ mse = mean_squared_error(y_test, y_pred)
31
+ return model, mse
32
+
33
+ # Predict AQI for a given input
34
+ def predict_aqi(model, temperature, humidity):
35
+ prediction = model.predict([[temperature, humidity]])
36
+ return round(prediction[0], 2)
37
+
38
+ # Visualization of historical trends
39
+ def plot_trends(data):
40
+ plt.figure(figsize=(10, 6))
41
+ sns.lineplot(data=data, x="Date", y="AQI", label="AQI")
42
+ sns.lineplot(data=data, x="Date", y="Temperature", label="Temperature")
43
+ sns.lineplot(data=data, x="Date", y="Humidity", label="Humidity")
44
+ plt.title("Historical Data Trends")
45
+ plt.xlabel("Date")
46
+ plt.ylabel("Values")
47
+ plt.legend()
48
+ plt.grid()
49
+ plt.tight_layout()
50
+
51
+ # Save the plot to a file
52
+ plt.savefig("trends.png")
53
+ return "trends.png"
54
+
55
+ # Load data and train model
56
+ data = load_sample_data()
57
+ model, mse = train_model(data)
58
+
59
+ # Streamlit app
60
+ st.title("\ud83c\udf0d Pollution Data Analysis Tool")
61
+ st.markdown(
62
+ "This app predicts air pollution levels (AQI) based on temperature and humidity. "
63
+ "It also provides a visualization of historical trends."
64
+ )
65
+
66
+ # Sidebar inputs
67
+ st.sidebar.header("Input Parameters")
68
+ temperature = st.sidebar.slider("Temperature (\u00b0C)", 20, 40, 25)
69
+ humidity = st.sidebar.slider("Humidity (%)", 30, 90, 50)
70
+
71
+ # Prediction
72
+ st.subheader("Predicted AQI")
73
+ prediction = predict_aqi(model, temperature, humidity)
74
+ st.write(f"The predicted AQI is: {prediction}")
75
+
76
+ # Historical trends visualization
77
+ st.subheader("Historical Data Trends")
78
+ trends_image = plot_trends(data)
79
+ st.image(trends_image)
80
+
81
+ # Model performance
82
+ st.sidebar.subheader("Model Performance")
83
+ st.sidebar.write(f"Mean Squared Error: {mse:.2f}")