Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,23 +18,34 @@ 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 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
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
|
|
|
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 |
+
if 'list' in data and data['list']:
|
22 |
+
aqi_value = data['list'][0]['main']['aqi']
|
23 |
+
return aqi_value
|
24 |
+
else:
|
25 |
+
return None
|
26 |
+
|
27 |
+
# Function to convert AQI value to corresponding label
|
28 |
+
def convert_to_label(aqi_value):
|
29 |
+
if aqi_value is None:
|
30 |
+
return "unknown"
|
31 |
+
elif aqi_value <= 50:
|
32 |
+
return "good"
|
33 |
+
elif aqi_value <= 100:
|
34 |
+
return "moderate"
|
35 |
+
elif aqi_value <= 150:
|
36 |
+
return "unhealthy for sensitive groups"
|
37 |
+
elif aqi_value <= 200:
|
38 |
+
return "unhealthy"
|
39 |
+
elif aqi_value <= 300:
|
40 |
+
return "very unhealthy"
|
41 |
+
else:
|
42 |
+
return "hazardous"
|
43 |
|
44 |
# Function to make prediction
|
45 |
def predict_air_quality(location):
|
46 |
latitude, longitude, city = get_coordinates(location)
|
47 |
aqi_value = get_aqi(latitude, longitude)
|
48 |
+
air_quality_label = convert_to_label(aqi_value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
return f"{city} air quality is currently '{air_quality_label}', with AQI {aqi_value}"
|
50 |
|
51 |
# Create Gradio interface
|