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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -15
app.py CHANGED
@@ -3,6 +3,7 @@ import geopy
3
  import joblib
4
  import gradio as gr
5
 
 
6
  # Load the trained model
7
  model = joblib.load('hackathonrf.joblib')
8
 
@@ -13,23 +14,21 @@ 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):
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()
 
3
  import joblib
4
  import gradio as gr
5
 
6
+
7
  # Load the trained model
8
  model = joblib.load('hackathonrf.joblib')
9
 
 
14
  return location.latitude, location.longitude
15
 
16
  # Function to get AQI value from OpenWeatherMap API
17
+ def get_aqi(latitude, longitude, api_key):
18
+ url = f"http://api.openweathermap.org/data/2.5/air_pollution/history?lat={latitude}&lon={longitude}&start=1614556800&end=1614643200&appid=78b94879cbb50e02397e93687aa24adc"
19
+ return aqi_value
 
 
20
 
21
+ # Function to make prediction
22
+ def predict_air_quality(location, api_key):
23
  latitude, longitude = get_coordinates(location)
24
+ aqi_value = get_aqi(latitude, longitude, api_key)
25
+ prediction = model.predict([[aqi_value, aqi_value, aqi_value, aqi_value]])
26
+ return prediction[0]
27
 
28
  # Create Gradio interface
29
  iface = gr.Interface(fn=predict_air_quality,
30
+ inputs=["text", "text"],
31
+ outputs="text",
32
+ title="Air Quality Prediction",
33
+ description="Enter location:")
34
+ iface.launch()