TLeonidas commited on
Commit
0a26a8f
·
verified ·
1 Parent(s): 2ac8bf6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -18
app.py CHANGED
@@ -6,21 +6,11 @@ import gradio as gr
6
  # Load the trained model
7
  model = joblib.load('hackathonrf.joblib')
8
 
9
- # Define AQI labels
10
- aqi_labels = {
11
- 0: 'good',
12
- 1: 'moderate',
13
- 2: 'unhealthy_sensitive',
14
- 3: 'unhealthy',
15
- 4: 'very_unhealthy',
16
- 5: 'hazardous'
17
- }
18
-
19
  # Function to get latitude and longitude from location name
20
  def get_coordinates(location):
21
  geolocator = geopy.geocoders.Nominatim(user_agent="air_quality_app")
22
  location = geolocator.geocode(location)
23
- return location.latitude, location.longitude
24
 
25
  # Function to get AQI value from OpenWeatherMap API
26
  def get_aqi(latitude, longitude):
@@ -28,17 +18,16 @@ def get_aqi(latitude, longitude):
28
  url = f"http://api.openweathermap.org/data/2.5/air_pollution?lat={latitude}&lon={longitude}&appid={api_key}"
29
  response = requests.get(url)
30
  data = response.json()
31
- pm25_aqi_level = data['list'][0]['components']['pm2_5']
32
- aqi_value = data['list'][0]['main']['aqi']
33
- return pm25_aqi_level, aqi_value
34
 
35
  # Function to make prediction
36
  def predict_air_quality(location):
37
- latitude, longitude = get_coordinates(location)
38
- pm25_aqi_level, aqi_value = get_aqi(latitude, longitude)
39
  prediction = model.predict([[aqi_value, aqi_value, aqi_value, aqi_value]])
40
- label_string = aqi_labels[prediction[0]]
41
- return f"{location} air quality is currently {label_string}, with AQI {pm25_aqi_level}"
42
 
43
  # Create Gradio interface
44
  iface = gr.Interface(fn=predict_air_quality,
 
6
  # Load the trained model
7
  model = joblib.load('hackathonrf.joblib')
8
 
 
 
 
 
 
 
 
 
 
 
9
  # Function to get latitude and longitude from location name
10
  def get_coordinates(location):
11
  geolocator = geopy.geocoders.Nominatim(user_agent="air_quality_app")
12
  location = geolocator.geocode(location)
13
+ return location.latitude, location.longitude, location.address.split(", ")[-2]
14
 
15
  # Function to get AQI value from OpenWeatherMap API
16
  def get_aqi(latitude, longitude):
 
18
  url = f"http://api.openweathermap.org/data/2.5/air_pollution?lat={latitude}&lon={longitude}&appid={api_key}"
19
  response = requests.get(url)
20
  data = response.json()
21
+ aqi_value = sum([pollutant['value'] for pollutant in data['list'][0]['components'].values()])
22
+ return aqi_value
 
23
 
24
  # Function to make prediction
25
  def predict_air_quality(location):
26
+ latitude, longitude, city_state = get_coordinates(location)
27
+ aqi_value = get_aqi(latitude, longitude)
28
  prediction = model.predict([[aqi_value, aqi_value, aqi_value, aqi_value]])
29
+ label_string = "good" if prediction[0] == 0 else "moderate" if prediction[0] == 1 else "unhealthy" if prediction[0] == 2 else "very unhealthy" if prediction[0] == 3 else "hazardous"
30
+ return f"{city_state} air quality is currently {label_string}, with AQI {aqi_value:.2f}"
31
 
32
  # Create Gradio interface
33
  iface = gr.Interface(fn=predict_air_quality,