File size: 3,526 Bytes
6a3cbeb
 
 
 
 
 
4190a45
7cf79a4
6a3cbeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caa7623
6a3cbeb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
caa7623
7aaed4d
 
caa7623
 
 
 
 
 
 
1cf0ace
caa7623
 
6a3cbeb
caa7623
 
7aaed4d
caa7623
 
6a3cbeb
 
 
 
 
 
caa7623
 
 
6a3cbeb
 
 
 
 
 
 
 
caa7623
6a3cbeb
 
 
 
 
 
caa7623
 
 
6422d88
 
caa7623
6422d88
 
caa7623
6422d88
6a3cbeb
caa7623
6a3cbeb
caa7623
4adba77
 
caa7623
 
6a3cbeb
 
0014409
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
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)