Spaces:
Sleeping
Sleeping
| 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 | |
| #from sklearn.metrics import mean_absolute_error, mean_squared_error | |
| 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" | |
| } | |
| # Create a Gradio radio button group for the period | |
| 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' | |
| #period_key = [key for key, value in period_options.items() if value == period][0] | |
| #stock_df = yf.download(symbol_nse, period = period_key) | |
| 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'] | |
| #stock_df = stock_df[['ds', 'y', 'cap']] | |
| #model = Prophet(growth='logistic') | |
| 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']] | |
| # Calculate accuracy metrics | |
| actual_prices = stock_df['y'].values[-1000:] # Last 1000 actual prices | |
| predicted_prices = forecast_df['yhat'].values[-1000:] # Forecasted prices for the last 1000 days | |
| #mae = mean_absolute_error(actual_prices, predicted_prices) | |
| #mse = mean_squared_error(actual_prices, predicted_prices) | |
| #rmse = np.sqrt(mse) | |
| 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)) | |
| #fig.update_layout(autosize=True) | |
| #fig.update_xaxes(automargin =True) | |
| # Display accuracy metrics | |
| # accuracy_text = f"MAE: {mae:.2f}, MSE: {mse:.2f}, RMSE: {rmse:.2f}, MAPE: {mape:.2f}%" | |
| 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) | |