Spaces:
Sleeping
Sleeping
ataurAGI commited on
Commit ·
43868c4
1
Parent(s): 0789bb6
added app.py, models and requirements
Browse files- README.md +10 -0
- app.py +60 -0
- requirements.txt +8 -0
README.md
CHANGED
|
@@ -10,3 +10,13 @@ pinned: false
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
-------------------------------------------------------------------------------------------------------------
|
| 16 |
+
|
| 17 |
+
This Repository contain following models with app root file app.py
|
| 18 |
+
* ARIMA Daily prediction
|
| 19 |
+
* Prophet Daily Prediction
|
| 20 |
+
* Prophet Seasonal
|
| 21 |
+
|
| 22 |
+
User interface lets user select a specific model and days ahead for prediction. Results shows in the form of an image, Model Prediction.
|
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import yfinance as yf
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
import pickle
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
# Load pickled models
|
| 9 |
+
with open("arima_model.pkl", "rb") as f:
|
| 10 |
+
arima_model = pickle.load(f)
|
| 11 |
+
|
| 12 |
+
with open("prophet_daily.pkl", "rb") as f:
|
| 13 |
+
prophet_daily_model = pickle.load(f)
|
| 14 |
+
|
| 15 |
+
with open("prophet_seasonal.pkl", "rb") as f:
|
| 16 |
+
prophet_seasonal_model = pickle.load(f)
|
| 17 |
+
|
| 18 |
+
def forecast_stock(ticker, model_type, days=1):
|
| 19 |
+
# Fetch recent data for plotting
|
| 20 |
+
df = yf.download(ticker, start="2010-01-01", interval="1d")
|
| 21 |
+
df.columns = df.columns.get_level_values(0)
|
| 22 |
+
df = df[['Close']].sort_index()
|
| 23 |
+
df = df.asfreq('B').ffill()
|
| 24 |
+
|
| 25 |
+
# Forecast
|
| 26 |
+
if model_type == "ARIMA":
|
| 27 |
+
forecast = arima_model.forecast(steps=days)
|
| 28 |
+
elif model_type == "Prophet Daily":
|
| 29 |
+
future = prophet_daily_model.make_future_dataframe(periods=days, freq='B')
|
| 30 |
+
forecast_df = prophet_daily_model.predict(future)
|
| 31 |
+
forecast = forecast_df['yhat'].iloc[-days:].values
|
| 32 |
+
else: # Prophet Seasonal
|
| 33 |
+
future = prophet_seasonal_model.make_future_dataframe(periods=days, freq='B')
|
| 34 |
+
forecast_df = prophet_seasonal_model.predict(future)
|
| 35 |
+
forecast = forecast_df['yhat'].iloc[-days:].values
|
| 36 |
+
|
| 37 |
+
# Plot
|
| 38 |
+
plt.figure(figsize=(10,5))
|
| 39 |
+
plt.plot(df.index[-50:], df['Close'].values[-50:], label='Recent Actual')
|
| 40 |
+
plt.plot(pd.date_range(df.index[-1]+pd.Timedelta(days=1), periods=days, freq='B'),
|
| 41 |
+
forecast, label='Forecast', marker='o')
|
| 42 |
+
plt.legend()
|
| 43 |
+
plt.title(f"{ticker} Stock Forecast ({model_type})")
|
| 44 |
+
plot_path = "temp_plot.png" # temporary file
|
| 45 |
+
plt.savefig(plot_path)
|
| 46 |
+
plt.close()
|
| 47 |
+
return plot_path
|
| 48 |
+
# Gradio interface
|
| 49 |
+
ticker_input = gr.Textbox(label="Ticker Symbol", value="AAPL")
|
| 50 |
+
model_input = gr.Radio(["ARIMA", "Prophet Daily", "Prophet Seasonal"], label="Model")
|
| 51 |
+
days_input = gr.Slider(1, 30, step=1, label="Days Ahead")
|
| 52 |
+
|
| 53 |
+
gr.Interface(
|
| 54 |
+
forecast_stock,
|
| 55 |
+
inputs=[ticker_input, model_input, days_input],
|
| 56 |
+
outputs=gr.Image(type="pil"),
|
| 57 |
+
live=True,
|
| 58 |
+
title="Stock Price Forecasting",
|
| 59 |
+
description="Forecast next n days stock prices using ARIMA or Prophet"
|
| 60 |
+
).launch(server_name="0.0.0.0", server_port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
numpy
|
| 3 |
+
matplotlib
|
| 4 |
+
prophet
|
| 5 |
+
statsmodels
|
| 6 |
+
gradio
|
| 7 |
+
yfinance
|
| 8 |
+
pmdarima
|