from flask import Flask, request, jsonify from flask_cors import CORS import tensorflow as tf import numpy as np import pickle app = Flask(__name__) CORS(app) # Allow frontend (CodePen) to access this API # Load trained LSTM model model = tf.keras.models.load_model("nse_lstm_model_fixed.h5") # Update with correct model path # Load MinMaxScaler for inverse transformation with open("close_price_scaler.pkl", "rb") as f: close_scaler = pickle.load(f) @app.route('/predict', methods=['POST']) def predict(): try: data = request.json['features'] # Receive stock data from frontend input_data = np.array(data).reshape(1, 60, 7) # Ensure correct shape # Make prediction prediction = model.predict(input_data) predicted_price = close_scaler.inverse_transform(prediction.reshape(-1, 1)).flatten()[0] return jsonify({'predicted_price': round(predicted_price, 2)}) except Exception as e: return jsonify({'error': str(e)}) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=True)