|
import gradio as gr |
|
import pandas as pd |
|
import numpy as np |
|
import yfinance as yf |
|
from prophet import Prophet |
|
from prophet.plot import plot_plotly, plot_components_plotly |
|
import plotly.graph_objects as go |
|
|
|
|
|
|
|
import warnings |
|
warnings.simplefilter(action='ignore', category=FutureWarning) |
|
|
|
|
|
stocks = pd.read_excel('Stocks.xlsx', usecols =[1,2,3]) |
|
|
|
period_options = { |
|
"1wk": "1 Week", |
|
"1mo": "1 Month", |
|
"1y": "1 Year", |
|
"5y": "5 Years" |
|
} |
|
|
|
|
|
|
|
period = gr.Radio(label="Training Period: ", choices=list(period_options.values()), value="1 Week") |
|
|
|
|
|
|
|
def get_forecast(company_name): |
|
symbol_nse = stocks[stocks['Company Name'] == company_name]['Symbol'].values[0] + '.NS' |
|
|
|
|
|
|
|
stock_df = yf.download(symbol_nse, period = '5y') |
|
stock_df.drop(stock_df.columns[[0,1,2,4,5]], axis=1, inplace=True) |
|
stock_df.reset_index(inplace=True) |
|
stock_df.columns = ['ds', 'y'] |
|
|
|
|
|
|
|
model = Prophet() |
|
model.fit(stock_df) |
|
|
|
future = model.make_future_dataframe(periods = 7) |
|
forecast = model.predict(future) |
|
forecast_df = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']] |
|
|
|
|
|
actual_prices = stock_df['y'].values[-1000:] |
|
predicted_prices = forecast_df['yhat'].values[-1000:] |
|
|
|
|
|
|
|
mape = np.mean(np.abs((actual_prices - predicted_prices) / actual_prices)) * 100 |
|
accuracy = 100 - mape |
|
|
|
|
|
fig = plot_plotly(model, forecast_df, xlabel = "Date", ylabel = "Price", figsize=(1400,800)) |
|
|
|
|
|
|
|
|
|
|
|
accuracy_text = f"{accuracy:.2f} % based on past 03 Years Prediction vs Actual Stock Price." |
|
|
|
return fig , accuracy_text |
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
|
gr.Markdown( |
|
""" |
|
# Stock Price Trend Prediction - using PROPHET Model |
|
Select the Stock from Dropdown Menu to get Next Week Prediction |
|
|
|
""" |
|
|
|
) |
|
with gr.Row(): |
|
dropdown = gr.Dropdown(label="Company Name", choices=stocks['Company Name'].tolist(), filterable = True, info = 'Select NSE Stock') |
|
|
|
with gr.Row(): |
|
with gr.Column(): |
|
accuracy_textbox = gr.Textbox(label="Model Accuracy", visible=True, info = "Accuracy above 95% is considerate", interactive = False) |
|
with gr.Column(): |
|
None |
|
with gr.Column(): |
|
None |
|
with gr.Column(): |
|
submit_btn = gr.Button(value = "Predict") |
|
|
|
|
|
|
|
gr.Markdown( |
|
""" |
|
### Select the Plot-Area to check Prediction for Next Week |
|
|
|
""" |
|
|
|
) |
|
|
|
|
|
with gr.Row(): |
|
forecast_plot = gr.Plot() |
|
|
|
|
|
|
|
submit_btn.click(get_forecast, inputs=dropdown, outputs=[forecast_plot, accuracy_textbox]) |
|
|
|
|
|
demo.launch(share=True) |
|
|