Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import requests | |
| import pandas as pd | |
| import plotly.express as px | |
| # Title and Description | |
| st.title("🌫️ Air Quality Index (AQI) Analyzer") | |
| st.write(""" | |
| This app fetches and analyzes real-time air quality data from the OpenWeatherMap API. | |
| You can visualize AQI levels, explore pollutant concentrations, and get health recommendations. | |
| """) | |
| # Input Section | |
| api_key = st.text_input("Enter your OpenWeatherMap API Key:", type="password") | |
| location = st.text_input("Enter a city or location (e.g., 'New York'):") | |
| if api_key and location: | |
| # Fetch AQI data | |
| api_url = f"http://api.openweathermap.org/data/2.5/air_pollution?appid={api_key}" | |
| geo_url = f"http://api.openweathermap.org/geo/1.0/direct?q={location}&limit=1&appid={api_key}" | |
| try: | |
| # Get location coordinates | |
| geo_response = requests.get(geo_url).json() | |
| if geo_response: | |
| lat = geo_response[0]["lat"] | |
| lon = geo_response[0]["lon"] | |
| # Fetch AQI data for coordinates | |
| response = requests.get(f"{api_url}&lat={lat}&lon={lon}") | |
| data = response.json() | |
| if "list" in data: | |
| aqi_data = data["list"][0] | |
| aqi_index = aqi_data["main"]["aqi"] | |
| components = aqi_data["components"] | |
| # AQI Mapping | |
| aqi_levels = { | |
| 1: "Good (0–50)", | |
| 2: "Fair (51–100)", | |
| 3: "Moderate (101–150)", | |
| 4: "Poor (151–200)", | |
| 5: "Very Poor (201+)", | |
| } | |
| # Display AQI and Details | |
| st.subheader(f"🌍 Location: {location}") | |
| st.metric("Air Quality Index (AQI)", aqi_index, delta=f"Level: {aqi_levels[aqi_index]}") | |
| st.subheader("Pollutant Concentrations (µg/m³):") | |
| pollutants_df = pd.DataFrame.from_dict([components]) | |
| st.dataframe(pollutants_df.T, height=250) | |
| # Visualization | |
| fig = px.bar( | |
| pollutants_df.T.reset_index(), | |
| x="index", | |
| y=0, | |
| labels={"index": "Pollutant", 0: "Concentration (µg/m³)"}, | |
| title="Pollutant Levels", | |
| ) | |
| st.plotly_chart(fig) | |
| # Health Recommendations | |
| st.subheader("Health Recommendations:") | |
| health_advice = { | |
| 1: "Air quality is considered satisfactory, and air pollution poses little or no risk.", | |
| 2: "Air quality is acceptable; however, some pollutants may be a concern for sensitive groups.", | |
| 3: "Sensitive individuals should reduce prolonged outdoor exertion.", | |
| 4: "Avoid outdoor activities. Sensitive groups may experience serious health effects.", | |
| 5: "Health warning of emergency conditions: everyone may experience serious health effects.", | |
| } | |
| st.write(health_advice[aqi_index]) | |
| else: | |
| st.error("Could not fetch AQI data. Please check your API key or location.") | |
| else: | |
| st.error("Invalid location. Please try again.") | |
| except Exception as e: | |
| st.error(f"An error occurred: {e}") | |
| else: | |
| st.info("Enter your API key and location to analyze AQI data.") | |
| st.write("---") | |
| st.markdown( | |
| "API data powered by [OpenWeatherMap Air Pollution API](https://openweathermap.org/api/air-pollution)." | |
| ) | |