File size: 1,271 Bytes
8e3e9e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)