import gradio as gr import pandas as pd import numpy as np import math import matplotlib.pyplot as plt from sklearn.preprocessing import MinMaxScaler from sklearn.metrics import mean_squared_error import tensorflow as tf from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Dense from tensorflow.keras.layers import LSTM import yfinance as yf from statsmodels.tsa.seasonal import seasonal_decompose def get_ans(inp): plt.close() tickers = yf.Tickers(inp) x = tickers.tickers[inp].history(period="15y") df = x df.reset_index(inplace=True) df1 = df.reset_index()['Close'] df['Date'] = pd.to_datetime(df['Date']) scaler = MinMaxScaler(feature_range=(0, 1)) df1 = scaler.fit_transform(np.array(df1).reshape(-1, 1)) training_size = int(len(df1) * 0.65) test_size = len(df1) - training_size train_data, test_data = df1[0:training_size, :], df1[training_size:len(df1), :1] def create_dataset(dataset, time_step=1): dataX, dataY = [], [] for i in range(len(dataset) - time_step - 1): a = dataset[i:(i + time_step), 0] dataX.append(a) dataY.append(dataset[i + time_step, 0]) return np.array(dataX), np.array(dataY) time_step = 100 X_train, y_train = create_dataset(train_data, time_step) X_test, ytest = create_dataset(test_data, time_step) X_train = X_train.reshape(X_train.shape[0], X_train.shape[1], 1) X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], 1) model = Sequential() model.add(tf.keras.Input(shape=(100, 1))) # 👈 Explicit input layer model.add(LSTM(50, return_sequences=True)) model.add(LSTM(50, return_sequences=True)) model.add(LSTM(50)) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') model.fit(X_train,y_train,validation_data=(X_test,ytest),epochs=2,batch_size=64,verbose=1) train_predict=model.predict(X_train) test_predict=model.predict(X_test) train_predict=scaler.inverse_transform(train_predict) test_predict=scaler.inverse_transform(test_predict) look_back=100 trainPredictPlot = np.empty_like(df1) trainPredictPlot[:, :] = np.nan trainPredictPlot[look_back:len(train_predict)+look_back, :] = train_predict # shift test predictions for plotting testPredictPlot = np.empty_like(df1) testPredictPlot[:, :] = np.nan testPredictPlot[len(train_predict)+(look_back*2)+1:len(df1)-1, :] = test_predict # plot baseline and predictions plt.plot(scaler.inverse_transform(df1)) plt.plot(trainPredictPlot) plt.plot(testPredictPlot) x_input=test_data[341:].reshape(1,-1) resize_var = x_input.size temp_input=list(x_input) temp_input=temp_input[0].tolist() lst_output=[] n_steps=100 i=0 while(i<30): if(len(temp_input)>100): #print(temp_input) x_input=np.array(temp_input[1:]) # print("{} day input {}".format(i,x_input)) x_input=x_input.reshape(1,-1) x_input = x_input.reshape((1, x_input.size, 1)) #print(x_input) yhat = model.predict(x_input, verbose=0) # print("{} day output {}".format(i,yhat)) temp_input.extend(yhat[0].tolist()) temp_input=temp_input[1:] #print(temp_input) lst_output.extend(yhat.tolist()) i=i+1 else: x_input = x_input.reshape((1, n_steps,1)) yhat = model.predict(x_input, verbose=0) # print(yhat[0]) temp_input.extend(yhat[0].tolist()) # print(len(temp_input)) lst_output.extend(yhat.tolist()) i=i+1 day_new=np.arange(1,101) day_pred=np.arange(101,131) df3=df1. tolist() df3.extend (lst_output) len_lis = len(lst_output) df3=pd.DataFrame(df3, columns=['Values']) df3['index']=range(1, len(df3) + 1) lst_output = pd.DataFrame(lst_output, columns=["Values"]) lst_output['index']=range(1, len(lst_output) + 1) the_max = max(np.asarray(df['Open'])) df3['Values'] = [i * the_max for i in df3['Values']] 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:])), (max(np.asarray(df['Open']))) * (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) def get_seo(inp): plt.close() time_step = 100 tickers = yf.Tickers(inp) x = tickers.tickers[inp].history(period="15y") df = x df.reset_index(inplace=True) df1 = df.reset_index()['Close'] df['Date'] = pd.to_datetime(df['Date']) scaler = MinMaxScaler(feature_range=(0, 1)) df1 = scaler.fit_transform(np.array(df1).reshape(-1, 1)) def create_dataset(dataset, time_step=1): dataX, dataY = [], [] for i in range(len(dataset) - time_step - 1): a = dataset[i:(i + time_step), 0] dataX.append(a) dataY.append(dataset[i + time_step, 0]) return np.array(dataX), np.array(dataY) X_train, y_train = create_dataset(df1, time_step) decompose_result_mult = seasonal_decompose(X_train, model="additive", period=time_step) trend = decompose_result_mult.trend seasonal = decompose_result_mult.seasonal residual = decompose_result_mult.resid z = [i[0] for i in trend] z = pd.DataFrame(z, columns=['Values']) z['index'] = range(1, len(z) + 1) y = [i[0] for i in seasonal] y = pd.DataFrame(y, columns=['Values']) y['index'] = range(1, len(z) + 1) a = [i[0] for i in residual] a = pd.DataFrame(a, columns=['Values']) a['index'] = range(1, len(a) + 1) return gr.update(visible=True, value=z, x='index', y='Values', height=500, width=800), gr.update(visible=True, value=y[:100], x='index', y='Values', height=500, width=800), gr.update(visible=True, value=a, x='index', y='Values', height=500, width=800) def get_info(inp): tickers = yf.Ticker(inp) info = tickers.info balance = tickers.balance_sheet long_info= info['longBusinessSummary'] curr_rat = info['currentRatio'] quick_rat = info['quickRatio'] short_rat = info['shortRatio'] debt_eq = info['debtToEquity'] volume = info['volume'] market_cap = info['marketCap'] curr_price = info['currentPrice'] rev_per = info['revenuePerShare'] return long_info, curr_rat, quick_rat, short_rat, debt_eq, volume, market_cap, curr_price, rev_per with gr.Blocks() as demo: with gr.Row().style(equal_height=True): with gr.Column(): gr.Markdown("