|
from flask import Flask, request, jsonify
|
|
import numpy as np
|
|
import tensorflow as tf
|
|
import joblib
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
model = tf.keras.models.load_model('aqi_model.h5')
|
|
scaler = joblib.load('scaler.pkl')
|
|
|
|
@app.route('/predict', methods=['POST'])
|
|
def predict():
|
|
try:
|
|
|
|
data = request.get_json()
|
|
features = [
|
|
data['PM10'],
|
|
data['PM2.5'],
|
|
data['NO2'],
|
|
data['O3'],
|
|
data['CO'],
|
|
data['SO2'],
|
|
data['NH3']
|
|
]
|
|
|
|
|
|
features_array = np.array(features).reshape(1, -1)
|
|
|
|
|
|
features_scaled = scaler.transform(features_array)
|
|
|
|
|
|
prediction = model.predict(features_scaled)
|
|
predicted_aqi = prediction[0][0]
|
|
|
|
|
|
predicted_aqi = float(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)
|
|
|
|
|