charan
Deployment Ready
8e3e9e5
from flask import Flask, request, jsonify
import numpy as np
from keras.models import load_model
app = Flask(__name__)
# Load the trained model
model = load_model('CNN+LSTM.h5')
@app.route('/predict', methods=['POST'])
def predict():
try:
# Get the data from the POST request
data = request.get_json(force=True)
# Convert data into numpy array and reshape for prediction
data_array = np.array(data['data'])
data_array = np.reshape(data_array, (data_array.shape[0], data_array.shape[1], 1))
# Make prediction using the loaded model
prediction = model.predict(data_array)
labels=["Normal","Abnormal"]
# Convert prediction to binary (0 or 1)
prediction_binary = [1 if p >= 0.5 else 0 for p in prediction]
# lables=["Normal","Abnormal"]
# Return the prediction
return jsonify({'prediction':labels[prediction_binary[0]]})
except Exception as e:
return jsonify({'error': str(e)})
# # This is the entry point for AWS Lambda
# def lambda_handler(event, context):
# return awsgi.response(app, event, context)
if __name__ == '__main__':
# Save the model to a file
model.save('CNN+LSTM.h5')
# Run the Flask app
app.run(debug=True)