TLeonidas commited on
Commit
4e3a55f
·
verified ·
1 Parent(s): 5403a98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -14
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 category 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):
@@ -33,11 +23,19 @@ def get_aqi(latitude, longitude):
33
 
34
  # Function to make prediction
35
  def predict_air_quality(location):
36
- latitude, longitude = get_coordinates(location)
37
  aqi_value = get_aqi(latitude, longitude)
38
  prediction = model.predict([[aqi_value, aqi_value, aqi_value, aqi_value]])
39
- label_string = aqi_labels[prediction[0]]
40
- return f"{location} air quality is currently '{label_string}'"
 
 
 
 
 
 
 
 
41
 
42
  # Create Gradio interface
43
  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
14
 
15
  # Function to get AQI value from OpenWeatherMap API
16
  def get_aqi(latitude, longitude):
 
23
 
24
  # Function to make prediction
25
  def predict_air_quality(location):
26
+ latitude, longitude, city = get_coordinates(location)
27
  aqi_value = get_aqi(latitude, longitude)
28
  prediction = model.predict([[aqi_value, aqi_value, aqi_value, aqi_value]])
29
+ label_mapping = {
30
+ 0: 'good',
31
+ 1: 'moderate',
32
+ 2: 'unhealthy_sensitive',
33
+ 3: 'unhealthy',
34
+ 4: 'very_unhealthy',
35
+ 5: 'hazardous'
36
+ }
37
+ air_quality_label = label_mapping[prediction[0]]
38
+ return f"{city} air quality is currently '{air_quality_label}', with AQI {aqi_value}"
39
 
40
  # Create Gradio interface
41
  iface = gr.Interface(fn=predict_air_quality,