Commit
·
8c8fd10
1
Parent(s):
b3993d8
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,147 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import math
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
from sklearn.preprocessing import MinMaxScaler
|
6 |
+
from sklearn.metrics import mean_squared_error
|
7 |
+
import tensorflow as tf
|
8 |
+
from tensorflow.keras.models import Sequential
|
9 |
+
from tensorflow.keras.layers import Dense
|
10 |
+
from tensorflow.keras.layers import LSTM
|
11 |
+
|
12 |
+
import gradio as gr
|
13 |
+
|
14 |
+
import yfinance as yf
|
15 |
+
|
16 |
+
def get_ans(inp):
|
17 |
+
tickers = yf.Tickers(inp)
|
18 |
+
x = tickers.tickers[inp].history(period="15y")
|
19 |
+
df = x
|
20 |
+
df.reset_index(inplace=True)
|
21 |
+
df1 = df.reset_index()['Close']
|
22 |
+
df['Date'] = pd.to_datetime(df['Date'])
|
23 |
+
scaler = MinMaxScaler(feature_range=(0, 1))
|
24 |
+
df1 = scaler.fit_transform(np.array(df1).reshape(-1, 1))
|
25 |
+
training_size = int(len(df1) * 0.65)
|
26 |
+
test_size = len(df1) - training_size
|
27 |
+
train_data, test_data = df1[0:training_size, :], df1[training_size:len(df1), :1]
|
28 |
+
def create_dataset(dataset, time_step=1):
|
29 |
+
dataX, dataY = [], []
|
30 |
+
for i in range(len(dataset) - time_step - 1):
|
31 |
+
a = dataset[i:(i + time_step), 0]
|
32 |
+
dataX.append(a)
|
33 |
+
dataY.append(dataset[i + time_step, 0])
|
34 |
+
return np.array(dataX), np.array(dataY)
|
35 |
+
time_step = 100
|
36 |
+
X_train, y_train = create_dataset(train_data, time_step)
|
37 |
+
X_test, ytest = create_dataset(test_data, time_step)
|
38 |
+
|
39 |
+
X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)
|
40 |
+
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)
|
41 |
+
model = Sequential()
|
42 |
+
model.add(LSTM(50, return_sequences=True, input_shape=(100, 1)))
|
43 |
+
model.add(LSTM(50, return_sequences=True))
|
44 |
+
model.add(LSTM(50))
|
45 |
+
model.add(Dense(1))
|
46 |
+
model.compile(loss='mean_squared_error', optimizer='adam')
|
47 |
+
model.fit(X_train,y_train,validation_data=(X_test,ytest),epochs=2,batch_size=64,verbose=1)
|
48 |
+
train_predict=model.predict(X_train)
|
49 |
+
test_predict=model.predict(X_test)
|
50 |
+
train_predict=scaler.inverse_transform(train_predict)
|
51 |
+
test_predict=scaler.inverse_transform(test_predict)
|
52 |
+
look_back=100
|
53 |
+
trainPredictPlot = np.empty_like(df1)
|
54 |
+
trainPredictPlot[:, :] = np.nan
|
55 |
+
trainPredictPlot[look_back:len(train_predict)+look_back, :] = train_predict
|
56 |
+
# shift test predictions for plotting
|
57 |
+
testPredictPlot = np.empty_like(df1)
|
58 |
+
testPredictPlot[:, :] = np.nan
|
59 |
+
testPredictPlot[len(train_predict)+(look_back*2)+1:len(df1)-1, :] = test_predict
|
60 |
+
# plot baseline and predictions
|
61 |
+
plt.plot(scaler.inverse_transform(df1))
|
62 |
+
plt.plot(trainPredictPlot)
|
63 |
+
plt.plot(testPredictPlot)
|
64 |
+
|
65 |
+
x_input=test_data[341:].reshape(1,-1)
|
66 |
+
resize_var = x_input.size
|
67 |
+
temp_input=list(x_input)
|
68 |
+
temp_input=temp_input[0].tolist()
|
69 |
+
lst_output=[]
|
70 |
+
n_steps=100
|
71 |
+
i=0
|
72 |
+
while(i<30):
|
73 |
+
|
74 |
+
if(len(temp_input)>100):
|
75 |
+
#print(temp_input)
|
76 |
+
x_input=np.array(temp_input[1:])
|
77 |
+
# print("{} day input {}".format(i,x_input))
|
78 |
+
x_input=x_input.reshape(1,-1)
|
79 |
+
x_input = x_input.reshape((1, x_input.size, 1))
|
80 |
+
#print(x_input)
|
81 |
+
yhat = model.predict(x_input, verbose=0)
|
82 |
+
# print("{} day output {}".format(i,yhat))
|
83 |
+
temp_input.extend(yhat[0].tolist())
|
84 |
+
temp_input=temp_input[1:]
|
85 |
+
#print(temp_input)
|
86 |
+
lst_output.extend(yhat.tolist())
|
87 |
+
i=i+1
|
88 |
+
else:
|
89 |
+
x_input = x_input.reshape((1, n_steps,1))
|
90 |
+
yhat = model.predict(x_input, verbose=0)
|
91 |
+
# print(yhat[0])
|
92 |
+
temp_input.extend(yhat[0].tolist())
|
93 |
+
# print(len(temp_input))
|
94 |
+
lst_output.extend(yhat.tolist())
|
95 |
+
i=i+1
|
96 |
+
|
97 |
+
day_new=np.arange(1,101)
|
98 |
+
day_pred=np.arange(101,131)
|
99 |
+
|
100 |
+
df3=df1. tolist()
|
101 |
+
df3.extend (lst_output)
|
102 |
+
len_lis = len(lst_output)
|
103 |
+
df3=pd.DataFrame(df3, columns=['Values'])
|
104 |
+
df3['index']=range(1, len(df3) + 1)
|
105 |
+
lst_output = pd.DataFrame(lst_output, columns=["Values"])
|
106 |
+
lst_output['index']=range(1, len(lst_output) + 1)
|
107 |
+
return plt, gr.update(visible=True,value=df, x="Date",y="Open", height=500, width=800),gr.update(visible=True,value=df[-300:], x="Date",y="Open", height=500, width=800),gr.update(visible=True,value=df[-30:], x="Date",y="Open", height=500, width=800), max(np.asarray(df['Open'])), min(np.asarray(df['Open'])), max(np.asarray(df['Open'])[-300:]), min(np.asarray(df['Open'][-300:])), max(np.asarray(df['Open'])[-30:]), min(np.asarray(df['Open'][-30:])), lst_output["Values"][0], gr.update(visible=True,value=lst_output, x="index",y="Values", height=500, width=800), gr.update(visible=True,value=df3, x="index",y="Values", height=500, width=800), gr.update(visible=True,value=df3[-300:], x="index",y="Values", height=500, width=800)
|
108 |
+
|
109 |
+
|
110 |
+
with gr.Blocks() as demo:
|
111 |
+
with gr.Row().style(equal_height=True):
|
112 |
+
with gr.Column():
|
113 |
+
gr.Markdown("<center><h1>BI Project<h1></center>")
|
114 |
+
gr.Markdown("<center><h3>Give the Ticker of the company you want to analyse. We will provide complete insights on the given company.</h3></center>")
|
115 |
+
with gr.Row():
|
116 |
+
with gr.Column():
|
117 |
+
Name_of_the_company = gr.Textbox(placeholder="eg, GOOG / MSFT / AAPL", label="TICKER of the company")
|
118 |
+
btn = gr.Button("ANALYSE")
|
119 |
+
gr.Markdown("<center><h2>Analysis<h2></center>")
|
120 |
+
gr.Markdown("<h3>Regression Trends of Price<h3>")
|
121 |
+
mp = gr.Plot()
|
122 |
+
gr.Markdown("<h3>Price over time<h3>")
|
123 |
+
with gr.Tab("All Time"):
|
124 |
+
mp1 = gr.LinePlot(visible=False, label="All time", height=1000, width=1000)
|
125 |
+
with gr.Row():
|
126 |
+
Max_all = gr.Textbox(placeholder="The Maximum price the stock has ever reached", label='Maximum of all time')
|
127 |
+
Min_all = gr.Textbox(placeholder="The Minimum price the stock has ever reached", label="Minimum of all time")
|
128 |
+
with gr.Tab("Past year"):
|
129 |
+
mp2 = gr.LinePlot(visible=False, label="Last year")
|
130 |
+
with gr.Row():
|
131 |
+
Max_year = gr.Textbox(placeholder="The Maximum price for the last year", label='Maximum')
|
132 |
+
Min_year = gr.Textbox(placeholder="The Minimum price for the last year", label="Minimum")
|
133 |
+
with gr.Tab("Past few Days"):
|
134 |
+
mp3 = gr.LinePlot(visible=False, label="Past few Days")
|
135 |
+
with gr.Row():
|
136 |
+
Max_rec = gr.Textbox(placeholder="The Maximum price for the last few days", label='Recent Maximum')
|
137 |
+
Min_rec = gr.Textbox(placeholder="The Minimum price for the last few days", label="Recent Minimum")
|
138 |
+
gr.Markdown("<center><h2>Predictive Analysis</h2></center>")
|
139 |
+
Next_day = gr.Textbox(placeholder="Predicted price for tomorrow", label="Predicted price for Tomorrow")
|
140 |
+
Next_plot = gr.LinePlot(visible=False)
|
141 |
+
Next_plot_all = gr.LinePlot(visible=False)
|
142 |
+
Next_plot_year = gr.LinePlot(visible=False)
|
143 |
+
|
144 |
+
|
145 |
+
btn.click(get_ans, inputs=Name_of_the_company, outputs= [mp,mp1,mp2,mp3, Max_all, Min_all,Max_year, Min_year, Max_rec, Min_rec, Next_day, Next_plot, Next_plot_all, Next_plot_year])
|
146 |
+
|
147 |
+
demo.launch(inline = False)
|