Untitled

 avatar
unknown
plain_text
10 days ago
1.6 kB
3
Indexable
# app.py
from flask import Flask, render_template, request, jsonify
import numpy as np
import pandas as pd
from tensorflow.keras.models import load_model
from datetime import datetime, timedelta

app = Flask(__name__)

# Load your pre-trained LSTM model
model = load_model('swftc_lstm_model.h5')

# Function to fetch data (replace with actual API call)
def fetch_swftc_data():
    # Example: Fetch data from a CSV or API
    data = pd.read_csv('swftc_historical_data.csv')
    return data

# Function to preprocess data
def preprocess_data(data):
    # Normalize and prepare data for the model
    # This is just a placeholder; replace with actual preprocessing
    data = data['close'].values
    data = data.reshape(-1, 1)
    return data

# Function to predict the next 7 days
def predict_next_7_days(data):
    predictions = []
    for _ in range(7):
        prediction = model.predict(data[-30:].reshape(1, 30, 1))
        predictions.append(prediction[0][0])
        data = np.append(data, prediction)
    return predictions

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/predict', methods=['POST'])
def predict():
    data = fetch_swftc_data()
    processed_data = preprocess_data(data)
    predictions = predict_next_7_days(processed_data)
    
    # Format predictions
    dates = [datetime.now() + timedelta(days=i) for i in range(7)]
    result = {str(date): float(pred) for date, pred in zip(dates, predictions)}
    
    return jsonify(result)

if __name__ == '__main__':
    app.run(debug=True)
Leave a Comment