|
from flask import Flask, request, jsonify |
|
import numpy as np |
|
import tensorflow as tf |
|
from sklearn.preprocessing import StandardScaler |
|
import json |
|
import os |
|
|
|
|
|
model = tf.keras.models.load_model('flood_risk_model.h5') |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
scaler = StandardScaler() |
|
scaler.mean_ = np.array([0, 0, 0, 0, 0]) |
|
scaler.scale_ = np.array([1, 1, 1, 1, 1]) |
|
|
|
@app.route('/predict', methods=['POST']) |
|
def predict(): |
|
try: |
|
|
|
data = request.get_json() |
|
features = [ |
|
data['altitude'], |
|
data['flow_rate'], |
|
data['duration'], |
|
data['slope_factor'], |
|
data['barrier_factor'] |
|
] |
|
|
|
|
|
features = np.array(features).reshape(1, -1) |
|
features_scaled = scaler.transform(features) |
|
|
|
|
|
prediction = model.predict(features_scaled) |
|
|
|
|
|
response = { |
|
'red_radius': float(prediction[0][0]), |
|
'orange_radius': float(prediction[0][1]), |
|
'yellow_radius': float(prediction[0][2]) |
|
} |
|
|
|
return jsonify(response) |
|
|
|
except Exception as e: |
|
return jsonify({'error': str(e)}) |
|
|
|
@app.route('/health', methods=['GET']) |
|
def health_check(): |
|
return jsonify({'status': 'healthy'}) |
|
|
|
if __name__ == '__main__': |
|
port = int(os.environ.get('PORT', 5000)) |
|
app.run(host='0.0.0.0', port=port) |
|
|