zonemodel / app.py
abhijitrai1619's picture
init
903f7b4 verified
from flask import Flask, request, jsonify
import numpy as np
import tensorflow as tf
from sklearn.preprocessing import StandardScaler
import json
import os
# Load the trained model
model = tf.keras.models.load_model('flood_risk_model.h5')
# Initialize Flask app
app = Flask(__name__)
# Initialize StandardScaler (ensure it matches the scaler used during training)
scaler = StandardScaler()
scaler.mean_ = np.array([0, 0, 0, 0, 0]) # Replace with actual mean of your training data
scaler.scale_ = np.array([1, 1, 1, 1, 1]) # Replace with actual scale of your training data
@app.route('/predict', methods=['POST'])
def predict():
try:
# Parse input JSON data
data = request.get_json()
features = [
data['altitude'],
data['flow_rate'],
data['duration'],
data['slope_factor'],
data['barrier_factor']
]
# Preprocess input data
features = np.array(features).reshape(1, -1)
features_scaled = scaler.transform(features)
# Make prediction
prediction = model.predict(features_scaled)
# Format prediction output
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)