File size: 2,310 Bytes
8cfefb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
633fb1d
 
 
 
8cfefb8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import pandas as pd
import numpy as np
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout
from sklearn.preprocessing import MinMaxScaler

app = FastAPI()

class StockRequest(BaseModel):
    stock_name: str

# Define file paths (assuming the structure is already known)
STOCK_FILE_PATHS = {
    "TSLA": "TSLA_data.csv",
    "AAPL": "AAPL_data.csv",
    "AMZN": "AMZN_data.csv",
    "MSFT": "MSFT_data.csv",
}

def load_and_preprocess_data(filepath):
    df = pd.read_csv(filepath)
    df['Close'] = df['Close'].astype(float)  # Ensure data is float
    scaler = MinMaxScaler(feature_range=(0, 1))
    scaled_data = scaler.fit_transform(df['Close'].values.reshape(-1, 1))
    return scaled_data, scaler

def prepare_datasets(data, seq_len):
    x, y = [], []
    for i in range(seq_len, len(data)):
        x.append(data[i-seq_len:i, 0])
        y.append(data[i, 0])
    x, y = np.array(x), np.array(y)
    x = np.reshape(x, (x.shape[0], x.shape[1], 1))
    return x, y

@app.post("/LSTM_Predict")
async def predict(stock_request: StockRequest):
    stock_name = stock_request.stock_name
    if stock_name not in STOCK_FILE_PATHS:
        raise HTTPException(status_code=422, detail="Invalid Stock Name")

    filepath = STOCK_FILE_PATHS[stock_name]
    data, scaler = load_and_preprocess_data(filepath)
    seq_len = 60  # Sequence length for LSTM
    x_train, y_train = prepare_datasets(data[:int(0.8 * len(data))], seq_len)
    x_test = prepare_datasets(data[int(0.8 * len(data)):], seq_len)[0]  # Only x_test needed

    # Define and compile the model
    model = Sequential([
        LSTM(128, return_sequences=True, input_shape=(seq_len, 1)),
        Dropout(0.2),
        LSTM(64, return_sequences=False),
        Dropout(0.2),
        Dense(1)
    ])
    model.compile(optimizer='adam', loss='mean_squared_error')

    # Fit model
    model.fit(x_train, y_train, epochs=5, batch_size=32)

    # Predicting
    predictions = model.predict(x_test)
    predictions = scaler.inverse_transform(predictions)  # Inverse transform to get actual values

    predict_prices = predictions.flatten().tolist()  # Convert to list for JSON response
    return {"prediction": predict_prices}