Spaces:
Runtime error
Runtime error
app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from statsmodels.tsa.arima.model import ARIMA
|
| 6 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 7 |
+
from tensorflow.keras.models import Sequential
|
| 8 |
+
from tensorflow.keras.layers import LSTM, Dense
|
| 9 |
+
|
| 10 |
+
def forecast_stock(file, model_choice="ARIMA", horizon=30):
|
| 11 |
+
df = pd.read_csv(file, parse_dates=["Date"])
|
| 12 |
+
df = df.set_index("Date").asfreq("B")
|
| 13 |
+
df["Aclose"] = df["Aclose"].interpolate()
|
| 14 |
+
series = df["Aclose"].dropna()
|
| 15 |
+
|
| 16 |
+
if model_choice == "ARIMA":
|
| 17 |
+
model = ARIMA(series, order=(2,1,2))
|
| 18 |
+
model_fit = model.fit()
|
| 19 |
+
forecast = model_fit.forecast(steps=horizon)
|
| 20 |
+
else: # LSTM
|
| 21 |
+
scaler = MinMaxScaler()
|
| 22 |
+
data = scaler.fit_transform(series.values.reshape(-1,1))
|
| 23 |
+
X, y = [], []
|
| 24 |
+
seq_len = 60
|
| 25 |
+
for i in range(len(data)-seq_len):
|
| 26 |
+
X.append(data[i:i+seq_len])
|
| 27 |
+
y.append(data[i+seq_len])
|
| 28 |
+
X, y = np.array(X), np.array(y)
|
| 29 |
+
|
| 30 |
+
model = Sequential([
|
| 31 |
+
LSTM(50, input_shape=(seq_len,1)),
|
| 32 |
+
Dense(1)
|
| 33 |
+
])
|
| 34 |
+
model.compile(optimizer="adam", loss="mse")
|
| 35 |
+
model.fit(X, y, epochs=3, batch_size=32, verbose=0)
|
| 36 |
+
|
| 37 |
+
# Forecast
|
| 38 |
+
last_seq = data[-seq_len:].reshape(1,seq_len,1)
|
| 39 |
+
preds = []
|
| 40 |
+
for _ in range(horizon):
|
| 41 |
+
p = model.predict(last_seq, verbose=0)[0,0]
|
| 42 |
+
preds.append(p)
|
| 43 |
+
last_seq = np.append(last_seq[:,1:,:], [[[p]]], axis=1)
|
| 44 |
+
forecast = scaler.inverse_transform(np.array(preds).reshape(-1,1)).flatten()
|
| 45 |
+
|
| 46 |
+
# Plot results
|
| 47 |
+
plt.figure(figsize=(10,5))
|
| 48 |
+
plt.plot(series[-200:], label="History")
|
| 49 |
+
plt.plot(pd.date_range(series.index[-1], periods=horizon+1, freq="B")[1:], forecast, label="Forecast")
|
| 50 |
+
plt.legend()
|
| 51 |
+
plt.title(f"{model_choice} Stock Forecast")
|
| 52 |
+
plt.xlabel("Date")
|
| 53 |
+
plt.ylabel("Price")
|
| 54 |
+
plt.tight_layout()
|
| 55 |
+
return plt
|
| 56 |
+
|
| 57 |
+
demo = gr.Interface(
|
| 58 |
+
fn=forecast_stock,
|
| 59 |
+
inputs=[
|
| 60 |
+
gr.File(label="Upload Stock CSV"),
|
| 61 |
+
gr.Radio(["ARIMA","LSTM"], value="ARIMA", label="Model"),
|
| 62 |
+
gr.Slider(5,60,step=5,label="Forecast Horizon (days)")
|
| 63 |
+
],
|
| 64 |
+
outputs=gr.Plot(label="Forecast Plot"),
|
| 65 |
+
title="📈 Stock Price Forecasting",
|
| 66 |
+
description="Upload stock data (CSV with Date,Aclose). Choose ARIMA or LSTM."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch()
|