aqiModel / app.py
rishi002's picture
Upload 3 files
788a979 verified
from flask import Flask, request, jsonify
import numpy as np
import tensorflow as tf
import joblib
app = Flask(__name__)
# Load the saved model and scaler
model = tf.keras.models.load_model('aqi_model.h5')
scaler = joblib.load('scaler.pkl')
@app.route('/predict', methods=['POST'])
def predict():
try:
# Get the input features from the JSON request
data = request.get_json()
features = [
data['PM10'],
data['PM2.5'],
data['NO2'],
data['O3'],
data['CO'],
data['SO2'],
data['NH3']
]
# Convert to numpy array and reshape for a single prediction
features_array = np.array(features).reshape(1, -1)
# Scale the input features using the loaded scaler
features_scaled = scaler.transform(features_array)
# Make prediction using the loaded model
prediction = model.predict(features_scaled)
predicted_aqi = prediction[0][0]
# Convert the result to a standard Python float
predicted_aqi = float(predicted_aqi)
# Return the predicted AQI
return jsonify({'predicted_aqi': predicted_aqi})
except Exception as e:
return jsonify({'error': str(e)}), 400
if __name__ == "__main__":
app.run(host='0.0.0.0', port=8080)