Spaces:
Sleeping
Sleeping
Accuracy Implemented
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import yfinance as yf
|
|
5 |
from prophet import Prophet
|
6 |
from prophet.plot import plot_plotly, plot_components_plotly
|
7 |
import plotly.graph_objects as go
|
|
|
8 |
|
9 |
|
10 |
import warnings
|
@@ -25,7 +26,7 @@ period_options = {
|
|
25 |
period = gr.Radio(label="Training Period: ", choices=list(period_options.values()), value="1 Week")
|
26 |
|
27 |
|
28 |
-
|
29 |
def get_forecast(company_name):
|
30 |
symbol_nse = stocks[stocks['Company Name'] == company_name]['Symbol'].values[0] + '.NS'
|
31 |
#period_key = [key for key, value in period_options.items() if value == period][0]
|
@@ -45,19 +46,34 @@ def get_forecast(company_name):
|
|
45 |
forecast = model.predict(future)
|
46 |
forecast_df = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']]
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
fig = plot_plotly(model, forecast_df, xlabel = "Date", ylabel = "Price", figsize=(1400,800))
|
49 |
-
#fig.update_layout(
|
|
|
50 |
|
51 |
-
|
|
|
|
|
|
|
|
|
52 |
|
53 |
|
54 |
with gr.Blocks() as demo:
|
55 |
|
56 |
gr.Markdown(
|
57 |
"""
|
58 |
-
#
|
59 |
-
Select the Stock from Dropdown Menu to get
|
60 |
-
|
61 |
"""
|
62 |
|
63 |
)
|
@@ -66,35 +82,31 @@ with gr.Blocks() as demo:
|
|
66 |
|
67 |
with gr.Row():
|
68 |
with gr.Column():
|
69 |
-
|
70 |
with gr.Column():
|
71 |
None
|
72 |
with gr.Column():
|
73 |
None
|
74 |
with gr.Column():
|
75 |
submit_btn = gr.Button(value = "Predict")
|
76 |
-
|
|
|
|
|
77 |
gr.Markdown(
|
78 |
"""
|
79 |
-
### Select the Plot-Area to check Prediction
|
80 |
|
81 |
"""
|
|
|
82 |
)
|
83 |
-
|
84 |
|
|
|
85 |
with gr.Row():
|
86 |
-
forecast_plot = gr.Plot(
|
87 |
-
|
88 |
|
89 |
-
gr.Markdown(
|
90 |
-
"""
|
91 |
-
Disclaimer : This model is for Informative Purpose Only, and doesn't support any particular Stock.
|
92 |
-
"""
|
93 |
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
submit_btn.click(get_forecast, inputs=dropdown, outputs=forecast_plot)
|
98 |
|
99 |
|
100 |
demo.launch(share=True)
|
|
|
5 |
from prophet import Prophet
|
6 |
from prophet.plot import plot_plotly, plot_components_plotly
|
7 |
import plotly.graph_objects as go
|
8 |
+
from sklearn.metrics import mean_absolute_error, mean_squared_error
|
9 |
|
10 |
|
11 |
import warnings
|
|
|
26 |
period = gr.Radio(label="Training Period: ", choices=list(period_options.values()), value="1 Week")
|
27 |
|
28 |
|
29 |
+
|
30 |
def get_forecast(company_name):
|
31 |
symbol_nse = stocks[stocks['Company Name'] == company_name]['Symbol'].values[0] + '.NS'
|
32 |
#period_key = [key for key, value in period_options.items() if value == period][0]
|
|
|
46 |
forecast = model.predict(future)
|
47 |
forecast_df = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']]
|
48 |
|
49 |
+
# Calculate accuracy metrics
|
50 |
+
actual_prices = stock_df['y'].values[-1000:] # Last 7 actual prices
|
51 |
+
predicted_prices = forecast_df['yhat'].values[-1000:] # Forecasted prices for the last 7 days
|
52 |
+
#mae = mean_absolute_error(actual_prices, predicted_prices)
|
53 |
+
#mse = mean_squared_error(actual_prices, predicted_prices)
|
54 |
+
#rmse = np.sqrt(mse)
|
55 |
+
mape = np.mean(np.abs((actual_prices - predicted_prices) / actual_prices)) * 100
|
56 |
+
accuracy = 100 - mape
|
57 |
+
|
58 |
+
|
59 |
fig = plot_plotly(model, forecast_df, xlabel = "Date", ylabel = "Price", figsize=(1400,800))
|
60 |
+
#fig.update_layout(autosize=True)
|
61 |
+
#fig.update_xaxes(automargin =True)
|
62 |
|
63 |
+
# Display accuracy metrics
|
64 |
+
# accuracy_text = f"MAE: {mae:.2f}, MSE: {mse:.2f}, RMSE: {rmse:.2f}, MAPE: {mape:.2f}%"
|
65 |
+
accuracy_text = f"{accuracy:.2f} %"
|
66 |
+
|
67 |
+
return fig , accuracy_text
|
68 |
|
69 |
|
70 |
with gr.Blocks() as demo:
|
71 |
|
72 |
gr.Markdown(
|
73 |
"""
|
74 |
+
# Stock Price Trend Prediction - using PROPHET Model
|
75 |
+
Select the Stock from Dropdown Menu to get Next Week Prediction
|
76 |
+
|
77 |
"""
|
78 |
|
79 |
)
|
|
|
82 |
|
83 |
with gr.Row():
|
84 |
with gr.Column():
|
85 |
+
accuracy_textbox = gr.Textbox(label="Model Accuracy", visible=True, info = "Accuracy above 95% is considerate", interactive = False)
|
86 |
with gr.Column():
|
87 |
None
|
88 |
with gr.Column():
|
89 |
None
|
90 |
with gr.Column():
|
91 |
submit_btn = gr.Button(value = "Predict")
|
92 |
+
|
93 |
+
|
94 |
+
|
95 |
gr.Markdown(
|
96 |
"""
|
97 |
+
### Select the Plot-Area to check Prediction for Next Week
|
98 |
|
99 |
"""
|
100 |
+
|
101 |
)
|
|
|
102 |
|
103 |
+
|
104 |
with gr.Row():
|
105 |
+
forecast_plot = gr.Plot()
|
|
|
106 |
|
|
|
|
|
|
|
|
|
107 |
|
108 |
+
|
109 |
+
submit_btn.click(get_forecast, inputs=dropdown, outputs=[forecast_plot, accuracy_textbox])
|
|
|
|
|
110 |
|
111 |
|
112 |
demo.launch(share=True)
|