File size: 1,647 Bytes
706626a
3fedf2b
 
706626a
 
 
 
 
3fedf2b
 
 
 
4e3a55f
3fedf2b
706626a
ff11245
 
60d53f2
752a6a3
 
 
64c2ffd
706626a
64c2ffd
ff11245
4e3a55f
ff11245
64c2ffd
4e3a55f
 
 
 
 
 
 
 
 
 
706626a
 
 
ff11245
64c2ffd
 
ff11245
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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()
    aqi_value = data['list'][0]['main']['aqi']
    return aqi_value

# Function to make prediction
def predict_air_quality(location):
    latitude, longitude, city = get_coordinates(location)
    aqi_value = get_aqi(latitude, longitude)
    prediction = model.predict([[aqi_value, aqi_value, aqi_value, aqi_value]])
    label_mapping = {
        0: 'good',
        1: 'moderate',
        2: 'unhealthy_sensitive',
        3: 'unhealthy',
        4: 'very_unhealthy',
        5: 'hazardous'
    }
    air_quality_label = label_mapping[prediction[0]]
    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()