aksha1141 commited on
Commit
dd4eb07
·
verified ·
1 Parent(s): 8bb4420

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from flask_cors import CORS
3
+ import tensorflow as tf
4
+ import numpy as np
5
+ import pickle
6
+
7
+ app = Flask(__name__)
8
+ CORS(app) # Allow frontend (CodePen) to access this API
9
+
10
+ # Load trained LSTM model
11
+ model = tf.keras.models.load_model("nse_lstm_model_fixed.h5") # Update with correct model path
12
+
13
+ # Load MinMaxScaler for inverse transformation
14
+ with open("close_price_scaler.pkl", "rb") as f:
15
+ close_scaler = pickle.load(f)
16
+
17
+ @app.route('/predict', methods=['POST'])
18
+ def predict():
19
+ try:
20
+ data = request.json['features'] # Receive stock data from frontend
21
+ input_data = np.array(data).reshape(1, 60, 7) # Ensure correct shape
22
+
23
+ # Make prediction
24
+ prediction = model.predict(input_data)
25
+ predicted_price = close_scaler.inverse_transform(prediction.reshape(-1, 1)).flatten()[0]
26
+
27
+ return jsonify({'predicted_price': round(predicted_price, 2)})
28
+
29
+ except Exception as e:
30
+ return jsonify({'error': str(e)})
31
+ if __name__ == '__main__':
32
+ app.run(host='0.0.0.0', port=5000, debug=True)