File size: 1,381 Bytes
788a979 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
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)
|