TLeonidas commited on
Commit
340f728
·
verified ·
1 Parent(s): ed979fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -13,24 +13,23 @@ def get_coordinates(location):
13
  return location.latitude, location.longitude
14
 
15
  # Function to get AQI value from OpenWeatherMap API
16
- def get_aqi(latitude, longitude, api_key):
17
- url = f"http://api.openweathermap.org/data/2.5/air_pollution?lat={latitude}&lon={longitude}&appid={api_key}"
18
  response = requests.get(url)
19
  data = response.json()
20
- aqi_value = data['list'][0]['main']['aqi']
21
- return aqi_value
22
 
23
- # Function to make prediction
24
- def predict_air_quality(location, api_key):
25
  latitude, longitude = get_coordinates(location)
26
- aqi_value = get_aqi(latitude, longitude, api_key)
27
- prediction = model.predict([[aqi_value, aqi_value, aqi_value, aqi_value]])
28
- return prediction[0]
29
 
30
  # Create Gradio interface
31
  iface = gr.Interface(fn=predict_air_quality,
32
- inputs=["text", "text"],
33
- outputs="text",
34
- title="Air Quality Prediction",
35
- description="Enter location and OpenWeatherMap API key:")
36
  iface.launch()
 
13
  return location.latitude, location.longitude
14
 
15
  # Function to get AQI value from OpenWeatherMap API
16
+ def get_aqi(latitude, longitude):
17
+ url = f"http://api.openweathermap.org/data/2.5/air_pollution/history?lat={latitude}&lon={longitude}&start=1614556800&end=1614643200&appid=78b94879cbb50e02397e93687aa24adc" # Hardcoded API key
18
  response = requests.get(url)
19
  data = response.json()
20
+ return data['list']
 
21
 
22
+ # Function to make prediction and fetch historical air pollution data
23
+ def predict_air_quality(location):
24
  latitude, longitude = get_coordinates(location)
25
+ historical_data = get_aqi(latitude, longitude)
26
+ prediction = model.predict([[historical_data[0]['main']['aqi'], historical_data[0]['main']['aqi'], historical_data[0]['main']['aqi'], historical_data[0]['main']['aqi']]])
27
+ return prediction[0], historical_data
28
 
29
  # Create Gradio interface
30
  iface = gr.Interface(fn=predict_air_quality,
31
+ inputs=["text"],
32
+ outputs=["text", "text"],
33
+ title="Air Quality Prediction with Historical Data",
34
+ description="Enter city name:")
35
  iface.launch()