Spaces:
Sleeping
Sleeping
import requests | |
import geopy | |
import joblib | |
import gradio as gr | |
# Load the trained model | |
model = joblib.load('hackathonrf.joblib') | |
# Function to get latitude and longitude from location name | |
def get_coordinates(location): | |
geolocator = geopy.geocoders.Nominatim(user_agent="air_quality_app") | |
location = geolocator.geocode(location) | |
return location.latitude, location.longitude, location.address | |
# Function to get AQI value from OpenWeatherMap API | |
def get_aqi(latitude, longitude): | |
api_key = "78b94879cbb50e02397e93687aa24adc" # Hidden API Key | |
url = f"http://api.openweathermap.org/data/2.5/air_pollution?lat={latitude}&lon={longitude}&appid={api_key}" | |
response = requests.get(url) | |
data = response.json() | |
if 'list' in data and data['list']: | |
aqi_value = data['list'][0]['main']['aqi'] | |
return aqi_value | |
else: | |
return None | |
# Function to convert AQI value to corresponding label | |
def convert_to_label(aqi_value): | |
if aqi_value is None: | |
return "unknown" | |
elif aqi_value <= 50: | |
return "good" | |
elif aqi_value <= 100: | |
return "moderate" | |
elif aqi_value <= 150: | |
return "unhealthy for sensitive groups" | |
elif aqi_value <= 200: | |
return "unhealthy" | |
elif aqi_value <= 300: | |
return "very unhealthy" | |
else: | |
return "hazardous" | |
# Function to make prediction | |
def predict_air_quality(location): | |
latitude, longitude, city = get_coordinates(location) | |
aqi_value = get_aqi(latitude, longitude) | |
air_quality_label = convert_to_label(aqi_value) | |
return f"{city} air quality is currently '{air_quality_label}', with AQI {aqi_value}" | |
# Create Gradio interface | |
iface = gr.Interface(fn=predict_air_quality, | |
inputs=["text"], | |
outputs="text", | |
title="Air Quality Prediction", | |
description="Enter location:") | |
iface.launch() | |