Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -129,19 +129,47 @@ def forecast_models(ticker="AAPL", test_len=180):
|
|
| 129 |
|
| 130 |
# ===== Gradio UI =====
|
| 131 |
def run_app(ticker):
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
-
demo = gr.Interface(
|
| 136 |
-
fn=run_app,
|
| 137 |
-
inputs=gr.Textbox(label="Enter Stock Ticker (e.g., AAPL, MSFT, TSLA)", value="AAPL"),
|
| 138 |
-
outputs=[
|
| 139 |
-
gr.Image(type="filepath", label="Forecast Plot"),
|
| 140 |
-
gr.Dataframe(label="Performance Metrics")
|
| 141 |
-
],
|
| 142 |
-
title="📈 Stock Price Forecasting (ARIMA, Prophet)",
|
| 143 |
-
description="This app compares ARIMA, Prophet models on stock closing prices using RMSE & MAPE."
|
| 144 |
-
)
|
| 145 |
|
| 146 |
if __name__ == "__main__":
|
| 147 |
demo.launch()
|
|
|
|
| 129 |
|
| 130 |
# ===== Gradio UI =====
|
| 131 |
def run_app(ticker):
|
| 132 |
+
stock_data = yf.download(ticker, start="2015-01-01", end="2023-01-01")
|
| 133 |
+
stock_data = stock_data[['Close']].dropna()
|
| 134 |
+
|
| 135 |
+
results = []
|
| 136 |
+
|
| 137 |
+
# ARIMA
|
| 138 |
+
with open("arima_model.pkl", "rb") as f:
|
| 139 |
+
arima_model = pickle.load(f)
|
| 140 |
+
arima_forecast = arima_model.forecast(steps=30)
|
| 141 |
+
arima_rmse = np.sqrt(mean_squared_error(stock_data['Close'][-30:], arima_forecast))
|
| 142 |
+
arima_mape = mean_absolute_percentage_error(stock_data['Close'][-30:], arima_forecast)
|
| 143 |
+
results.append(["ARIMA", arima_rmse, arima_mape])
|
| 144 |
+
|
| 145 |
+
# Prophet
|
| 146 |
+
with open("prophet_model.pkl", "rb") as f:
|
| 147 |
+
prophet_model = pickle.load(f)
|
| 148 |
+
prophet_future = prophet_model.make_future_dataframe(periods=30)
|
| 149 |
+
prophet_forecast = prophet_model.predict(prophet_future)
|
| 150 |
+
prophet_forecast = prophet_forecast.set_index("ds").loc[stock_data.index[-30:]]
|
| 151 |
+
prophet_rmse = np.sqrt(mean_squared_error(stock_data['Close'][-30:], prophet_forecast['yhat']))
|
| 152 |
+
prophet_mape = mean_absolute_percentage_error(stock_data['Close'][-30:], prophet_forecast['yhat'])
|
| 153 |
+
results.append(["Prophet", prophet_rmse, prophet_mape])
|
| 154 |
+
|
| 155 |
+
# Create metrics table
|
| 156 |
+
df_results = pd.DataFrame(results, columns=["Model", "RMSE", "MAPE"])
|
| 157 |
+
df_results = df_results.dropna() # remove any NaN rows
|
| 158 |
+
|
| 159 |
+
# Plot
|
| 160 |
+
plt.figure(figsize=(10, 5))
|
| 161 |
+
plt.plot(stock_data.index, stock_data['Close'], label="Actual")
|
| 162 |
+
plt.plot(stock_data.index[-30:], arima_forecast, label="ARIMA Forecast")
|
| 163 |
+
plt.plot(prophet_forecast.index, prophet_forecast['yhat'], label="Prophet Forecast")
|
| 164 |
+
plt.legend()
|
| 165 |
+
plt.title(f"{ticker} - Stock Price Forecasting")
|
| 166 |
+
plt.xlabel("Date")
|
| 167 |
+
plt.ylabel("Price")
|
| 168 |
+
plt.savefig("forecast_plot.png")
|
| 169 |
+
plt.close()
|
| 170 |
+
|
| 171 |
+
return "forecast_plot.png", df_results
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
if __name__ == "__main__":
|
| 175 |
demo.launch()
|