SparshSG commited on
Commit
92ffed5
Β·
verified Β·
1 Parent(s): 9c8abb5

Upload 4 files

Browse files
Files changed (4) hide show
  1. app_hf.py +117 -0
  2. df_cleaned.csv +0 -0
  3. model.pkl +3 -0
  4. requirements.txt +0 -0
app_hf.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import mlflow.sklearn
4
+ import numpy as np
5
+ import os
6
+
7
+ from src.inference.predict import load_model_from_run, predict
8
+ from src.data.data_loader import load_processed_data
9
+ import joblib
10
+
11
+ # --- 1. CONFIG & MODEL LOADING ---
12
+ st.set_page_config(page_title="AQI Master Predictor", page_icon="🌬️", layout="wide")
13
+
14
+
15
+ @st.cache_resource
16
+ def load_final_model():
17
+ return joblib.load("model.pkl")
18
+
19
+ try:
20
+ model = load_final_model()
21
+ except Exception as e:
22
+ st.error(f"Error loading model from path. Please check if the path is correct. Details: {e}")
23
+ st.stop()
24
+
25
+ # --- 2. AQI CATEGORY LOGIC ---
26
+ def get_aqi_info(aqi):
27
+ if aqi <= 50:
28
+ return "Good", "🟒", "Air quality is considered satisfactory, and air pollution poses little or no risk."
29
+ elif aqi <= 100:
30
+ return "Satisfactory", "🟑", "Air quality is acceptable; however, there may be a moderate health concern for a very small number of people."
31
+ elif aqi <= 200:
32
+ return "Moderate", "🟠", "Members of sensitive groups may experience health effects. The general public is not likely to be affected."
33
+ elif aqi <= 300:
34
+ return "Poor", "πŸ”΄", "Everyone may begin to experience health effects; members of sensitive groups may experience more serious health effects."
35
+ elif aqi <= 400:
36
+ return "Very Poor", "🟣", "Health alert: everyone may experience more serious health effects."
37
+ else:
38
+ return "Severe", "🟀", "Health warnings of emergency conditions. The entire population is more likely to be affected."
39
+
40
+ # --- 3. UI SIDEBAR (Inputs) ---
41
+ st.sidebar.header("πŸ“ Location & Time")
42
+
43
+ @st.cache_data
44
+ def get_city_list():
45
+ df = load_processed_data("data/cleaned/df_cleaned.csv")
46
+ return sorted(df["City"].dropna().unique())
47
+
48
+ @st.cache_data
49
+ def get_year_list():
50
+ df = load_processed_data("data/cleaned/df_cleaned.csv")
51
+ return sorted(df["Year"].dropna().unique())
52
+
53
+
54
+ cities = get_city_list()
55
+ years = get_year_list()
56
+
57
+ city = st.sidebar.selectbox(
58
+ "City",
59
+ cities,
60
+ index=cities.index("Delhi") if "Delhi" in cities else 0
61
+ )
62
+
63
+ season = st.sidebar.selectbox("Season", ["Winter", "Summer", "Monsoon", "Post-Monsoon"])
64
+
65
+ month = st.sidebar.slider("Month", 1, 12, 3)
66
+
67
+ year = st.sidebar.selectbox(
68
+ "Year",
69
+ years,
70
+ index=len(years) - 1 # default to latest available year
71
+ )
72
+
73
+ st.sidebar.header("πŸ§ͺ Pollutant Levels (Β΅g/mΒ³)")
74
+ pm25 = st.sidebar.number_input("PM2.5", value=45.0)
75
+ pm10 = st.sidebar.number_input("PM10", value=90.0)
76
+ no2 = st.sidebar.number_input("NO2", value=20.0)
77
+ no = st.sidebar.number_input("NO", value=10.0)
78
+ nox = st.sidebar.number_input("NOx", value=30.0)
79
+ nh3 = st.sidebar.number_input("NH3", value=5.0)
80
+ co = st.sidebar.number_input("CO", value=0.5)
81
+ so2 = st.sidebar.number_input("SO2", value=10.0)
82
+ o3 = st.sidebar.number_input("O3", value=35.0)
83
+ benzene = st.sidebar.number_input("Benzene", value=2.0)
84
+ toluene = st.sidebar.number_input("Toluene", value=4.0)
85
+
86
+ # --- 4. MAIN PAGE ---
87
+ st.title("🌍 Real-time AQI Prediction System")
88
+ st.markdown(f"Currently predicting for **{city}** in **{season} {year}**.")
89
+
90
+ # Prediction Logic
91
+ input_df = pd.DataFrame({
92
+ 'City': [city], 'PM2.5': [pm25], 'PM10': [pm10], 'NO': [no],
93
+ 'NO2': [no2], 'NOx': [nox], 'NH3': [nh3], 'CO': [co],
94
+ 'SO2': [so2], 'O3': [o3], 'Benzene': [benzene],
95
+ 'Toluene': [toluene], 'Month': [month], 'Year': [year], 'Season': [season]
96
+ })
97
+
98
+ if st.button("Generate Prediction", use_container_width=True):
99
+ prediction = predict(model, input_df)[0]
100
+ label, icon, desc = get_aqi_info(prediction)
101
+
102
+ st.divider()
103
+
104
+ # Results Display
105
+ c1, c2 = st.columns([1, 2])
106
+ with c1:
107
+ st.metric(label="Calculated AQI Score", value=round(prediction, 2))
108
+ with c2:
109
+ st.subheader(f"{icon} Status: {label}")
110
+ st.write(desc)
111
+
112
+ # Health Recommendations (Fun Addition)
113
+ st.info("**Health Advice:** " +
114
+ ("Wear a mask if going outside." if prediction > 150 else "Enjoy your day! Air quality is safe.")
115
+ )
116
+ else:
117
+ st.write("Adjust the values in the sidebar and click the button to see results.")
df_cleaned.csv ADDED
The diff for this file is too large to render. See raw diff
 
model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:79e1f09e2611132915e5e96f691339a6fa32a0c70355050ea1a54b30de95c018
3
+ size 158722034
requirements.txt ADDED
Binary file (8.58 kB). View file