sudoraj commited on
Commit
95b61df
·
verified ·
1 Parent(s): 37c8167
Files changed (2) hide show
  1. main.py +79 -0
  2. requirements.txt +4 -0
main.py ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from flask import Flask, request, jsonify
2
+ # import tensorflow as tf
3
+ # import numpy as np
4
+
5
+ # app = Flask(__name__)
6
+
7
+ # # Load the model
8
+ # model = tf.keras.models.load_model('walking_classifier_tf.h5')
9
+
10
+ # @app.route('/predict', methods=['POST'])
11
+ # def predict():
12
+ # data = request.get_json(force=True)
13
+ # predictions = model.predict(np.array(data['features']))
14
+ # return jsonify({'predictions': predictions.tolist()})
15
+
16
+ # if __name__ == '__main__':
17
+ # app.run(host='0.0.0.0', port=5000)
18
+
19
+
20
+ import os
21
+ from flask import Flask, request, jsonify
22
+ import numpy as np
23
+ import tensorflow as tf
24
+ from sklearn.preprocessing import StandardScaler
25
+ import joblib
26
+
27
+ app = Flask(__name__)
28
+
29
+ # Load the saved scaler and model
30
+ scaler = StandardScaler()
31
+ scaler.mean_ = np.loadtxt('scaler_mean.csv', delimiter=',')
32
+ scaler.scale_ = np.loadtxt('scaler_std.csv', delimiter=',')
33
+
34
+ # Load the TFLite model and allocate tensors
35
+ interpreter = tf.lite.Interpreter(model_path="walking_classifier.tflite")
36
+ interpreter.allocate_tensors()
37
+
38
+ input_details = interpreter.get_input_details()
39
+ output_details = interpreter.get_output_details()
40
+
41
+ @app.route('/predict', methods=['POST'])
42
+ def predict():
43
+ try:
44
+ # Get the data from the request
45
+ input_data = request.json['data']
46
+
47
+ # Convert to numpy array and reshape
48
+ input_data = np.array(input_data, dtype=np.float32)
49
+
50
+ # Normalize the data
51
+ input_data = scaler.transform(input_data)
52
+
53
+ # Check the input shape
54
+ if input_data.shape[1] != input_details[0]['shape'][1]:
55
+ return jsonify({"error": "Input shape does not match model expected shape."})
56
+
57
+ # Prepare the prediction list
58
+ predictions = []
59
+
60
+ # Run the model for each input data
61
+ for i in range(input_data.shape[0]):
62
+ single_input_data = input_data[i].reshape(1, -1)
63
+ interpreter.set_tensor(input_details[0]['index'], single_input_data)
64
+ interpreter.invoke()
65
+ output_data = interpreter.get_tensor(output_details[0]['index'])[0]
66
+ predictions.append(float(output_data))
67
+
68
+ # Convert to binary labels
69
+ threshold = 0.5
70
+ predicted_labels = (np.array(predictions) > threshold).astype(int).tolist()
71
+
72
+ # Return the predictions as JSON
73
+ return jsonify({"predictions": predicted_labels})
74
+ except Exception as e:
75
+ return jsonify({"error": str(e)})
76
+
77
+ if __name__ == '__main__':
78
+ port = int(os.environ.get('PORT', 8080))
79
+ app.run(host='0.0.0.0', port=port)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ Flask
2
+ tensorflow
3
+ scikit-learn
4
+ numpy