-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_streamlit.py
138 lines (107 loc) · 4.75 KB
/
app_streamlit.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import streamlit as st
import torch
import os
import numpy as np
from market_insight.data.data_handler import PolygonDataHandler
from market_insight.models.lstm_model import LSTM
from market_insight.training.trainer import Trainer
from market_insight.prediction.predictor import Predictor
from torch.utils.data import Dataset
import datetime
import pandas as pd
class StockDataset(Dataset):
def __init__(self, series, sequence_length):
self.data = series
self.sequence_length = sequence_length
def __len__(self):
return len(self.data) - self.sequence_length
def __getitem__(self, index):
sequence = self.data[index:index+self.sequence_length]
label = self.data[index+self.sequence_length]
return (sequence.unsqueeze(1), label)
def train_and_save_model_for_symbol(symbol):
poligon_api_key = os.environ.get('POLIGON_API_KEY')
data_handler = PolygonDataHandler(api_key=poligon_api_key)
data = data_handler.get_historical_data(symbol)
scaled_data, scaler = data_handler.preprocess_data(data)
# Extract the last sequence
# Assuming your model uses the last 60 data points
last_sequence = scaled_data[-60:]
model = LSTM()
train_dataset = StockDataset(scaled_data, 60)
trainer = Trainer(model, train_dataset)
trainer.train_model()
# Save Model, Scaler, and Last Sequence with symbol as part of the filename
model_filename = f'model/{symbol}_lstm_model.pth'
scaler_filename = f'model/{symbol}_scaler.pth'
last_sequence_filename = f'model/{symbol}_last_sequence.pth'
torch.save(model.state_dict(), model_filename)
torch.save(scaler, scaler_filename)
torch.save(last_sequence, last_sequence_filename)
print(
f"Model, scaler, and last sequence for {symbol} saved as {model_filename}, {scaler_filename}, and {last_sequence_filename}")
def load_model(symbol):
model_filename = f'model/{symbol}_lstm_model.pth'
scaler_filename = f'model/{symbol}_scaler.pth'
last_sequence_filename = f'model/{symbol}_last_sequence.pth'
if os.path.exists(model_filename) and os.path.exists(scaler_filename) and os.path.exists(last_sequence_filename):
model = LSTM()
model.load_state_dict(torch.load(model_filename))
scaler = torch.load(scaler_filename)
last_sequence = torch.load(last_sequence_filename)
return model, scaler, last_sequence
else:
return None, None, None
def predict_stock_price(model, scaler, last_sequence, days):
# Use the last sequence for the symbol
start_sequence = last_sequence[-60:].tolist()
predictor = Predictor(model)
predicted_highs, predicted_lows = predictor.predict_next_days(start_sequence, days)
# Rescale the predicted values to their original scale
predicted_highs_scaled = scaler.inverse_transform(np.array(predicted_highs).reshape(-1, 1)).reshape(-1)
predicted_lows_scaled = scaler.inverse_transform(np.array(predicted_lows).reshape(-1, 1)).reshape(-1)
# Generate response
response = []
today = datetime.date.today()
for i in range(days):
date = today + datetime.timedelta(days=i)
response.append({
"date": date.isoformat(),
"predicted_high": predicted_highs_scaled[i],
"predicted_low": predicted_lows_scaled[i]
})
return response
# Streamlit app
def main():
st.title('Market Insight AI')
symbol = st.text_input(
"Enter the stock symbol (e.g., AAPL):", value='AAPL')
day = st.text_input(
"No of future days", value='10')
model, scaler, last_sequence = None, None, None
if st.button('Train/Load Model'):
with st.spinner('Processing...'):
model, scaler, last_sequence = load_model(symbol)
if model is None:
st.info(
f"No pre-trained model found for {symbol}. Training a new model.")
train_and_save_model_for_symbol(symbol)
model, scaler, last_sequence = load_model(symbol)
if model:
model, scaler, last_sequence = load_model(symbol)
st.success(f'Model ready for {symbol}!')
if st.button('Predict'):
with st.spinner('Making prediction...'):
try:
model, scaler, last_sequence = load_model(symbol)
prediction = predict_stock_price(
model, scaler, last_sequence, int(day))
# Convert predictions to DataFrame and display
predictions_df = pd.DataFrame(prediction)
st.success(
f'Predicted stock price for {symbol}!')
st.dataframe(predictions_df)
except Exception as e:
st.error(f'An error occurred: {e}')
if __name__ == "__main__":
main()