Spaces:
Sleeping
Sleeping
Add app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from prophet import Prophet
|
| 5 |
+
import yfinance as yf
|
| 6 |
+
from sklearn.metrics import mean_absolute_error, mean_squared_error
|
| 7 |
+
import matplotlib.pyplot as plt
|
| 8 |
+
from prophet.plot import plot_plotly, plot_components_plotly
|
| 9 |
+
|
| 10 |
+
# Function to fetch stock data from Yahoo Finance
|
| 11 |
+
def fetch_stock_data(ticker_symbol, start_date, end_date):
|
| 12 |
+
stock_data = yf.download(ticker_symbol, start=start_date, end=end_date)
|
| 13 |
+
df = stock_data[['Adj Close']].reset_index()
|
| 14 |
+
df = df.rename(columns={'Date': 'ds', 'Adj Close': 'y'})
|
| 15 |
+
return df
|
| 16 |
+
|
| 17 |
+
# Function to train the Prophet model
|
| 18 |
+
def train_prophet_model(df):
|
| 19 |
+
model = Prophet()
|
| 20 |
+
model.fit(df)
|
| 21 |
+
return model
|
| 22 |
+
|
| 23 |
+
# Function to make the forecast
|
| 24 |
+
def make_forecast(model, periods):
|
| 25 |
+
future = model.make_future_dataframe(periods=periods)
|
| 26 |
+
forecast = model.predict(future)
|
| 27 |
+
return forecast
|
| 28 |
+
|
| 29 |
+
# Function to calculate performance metrics
|
| 30 |
+
def calculate_performance_metrics(actual, predicted):
|
| 31 |
+
mae = mean_absolute_error(actual, predicted)
|
| 32 |
+
mse = mean_squared_error(actual, predicted)
|
| 33 |
+
rmse = np.sqrt(mse)
|
| 34 |
+
return {'MAE': mae, 'MSE': mse, 'RMSE': rmse}
|
| 35 |
+
|
| 36 |
+
# Function to handle the complete process and return results
|
| 37 |
+
def forecast_stock(ticker_symbol, start_date, end_date, forecast_horizon):
|
| 38 |
+
# Fetch stock data
|
| 39 |
+
df = fetch_stock_data(ticker_symbol, start_date, end_date)
|
| 40 |
+
|
| 41 |
+
# Train the model
|
| 42 |
+
model = train_prophet_model(df)
|
| 43 |
+
|
| 44 |
+
# Convert forecast horizon to days
|
| 45 |
+
horizon_mapping = {'1 Month': 30, '6 months': (365/2), '1 year': 365, '2 years': 730, '3 years': 1095, '5 years': 1825}
|
| 46 |
+
forecast_days = horizon_mapping[forecast_horizon]
|
| 47 |
+
|
| 48 |
+
# Make forecast
|
| 49 |
+
forecast = make_forecast(model, forecast_days)
|
| 50 |
+
|
| 51 |
+
# Plot the forecast results using matplotlib
|
| 52 |
+
plt.figure(figsize=(10, 6))
|
| 53 |
+
plt.plot(df['ds'], df['y'], label='Actual Data')
|
| 54 |
+
plt.plot(forecast['ds'], forecast['yhat'], label='Forecast', color='orange')
|
| 55 |
+
plt.fill_between(forecast['ds'], forecast['yhat_lower'], forecast['yhat_upper'], color='orange', alpha=0.2)
|
| 56 |
+
plt.xlabel('Date')
|
| 57 |
+
plt.ylabel('Price')
|
| 58 |
+
plt.legend()
|
| 59 |
+
plt.title('Stock Price Forecast')
|
| 60 |
+
plt.savefig('forecast_plot.png')
|
| 61 |
+
plt.close()
|
| 62 |
+
|
| 63 |
+
# Plot the forecast components
|
| 64 |
+
model.plot_components(forecast)
|
| 65 |
+
plt.savefig('forecast_components.png')
|
| 66 |
+
plt.close()
|
| 67 |
+
|
| 68 |
+
return 'forecast_plot.png', 'forecast_components.png'
|
| 69 |
+
|
| 70 |
+
# Gradio Interface
|
| 71 |
+
def main():
|
| 72 |
+
with gr.Blocks() as demo:
|
| 73 |
+
gr.Markdown("# Stock Forecasting")
|
| 74 |
+
|
| 75 |
+
ticker_symbol = gr.Textbox(label="Enter Ticker Symbol", value="RACE")
|
| 76 |
+
start_date = gr.Textbox(label="Start Date (YYYY-MM-DD) of Data", value="2015-01-01")
|
| 77 |
+
end_date = gr.Textbox(label="End Date (YYYY-MM-DD) of Data", value=str(pd.to_datetime('today').date()))
|
| 78 |
+
forecast_horizon = gr.Dropdown(
|
| 79 |
+
label="Forecast Horizon",
|
| 80 |
+
choices=['1 Month','6 months','1 year', '2 years', '3 years', '5 years'],
|
| 81 |
+
value='1 year'
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
forecast_button = gr.Button("Forecast Stock Prices")
|
| 85 |
+
plot_output1 = gr.Image(label="Forecast Plot")
|
| 86 |
+
plot_output2 = gr.Image(label="Forecast Components")
|
| 87 |
+
|
| 88 |
+
forecast_button.click(forecast_stock,
|
| 89 |
+
inputs=[ticker_symbol, start_date, end_date, forecast_horizon],
|
| 90 |
+
outputs=[plot_output1, plot_output2])
|
| 91 |
+
|
| 92 |
+
demo.launch()
|
| 93 |
+
|
| 94 |
+
# Run the Gradio app
|
| 95 |
+
if __name__ == "__main__":
|
| 96 |
+
main()
|