TuanScientist's picture
Update app.py
602bdb4
raw
history blame
2.14 kB
import gradio as gr
import pandas as pd
from neuralprophet import NeuralProphet
import io
import warnings
import torch
warnings.filterwarnings("ignore", category=UserWarning)
url = "VN Index Historical Data.csv"
df = pd.read_csv(url)
df = df[["Date", "Price"]]
df = df.rename(columns={"Date": "ds", "Price": "y"})
df.fillna(method='ffill', inplace=True)
df.dropna(inplace=True)
class CustomNeuralProphet(NeuralProphet):
def lr_scheduler_step(self, epoch: int = None) -> None:
# Override the lr_scheduler_step method to avoid the MisconfigurationException
if self.lr_scheduler is not None and isinstance(self.lr_scheduler, torch.optim.lr_scheduler.OneCycleLR):
self.lr_scheduler.step()
m = CustomNeuralProphet(
n_forecasts=30,
n_lags=12,
changepoints_range=1,
num_hidden_layers=3,
yearly_seasonality=True,
n_changepoints=150,
trend_reg_threshold=False, # Disable trend regularization threshold
d_hidden=3,
global_normalization=True,
seasonality_reg=1,
unknown_data_normalization=True,
seasonality_mode="multiplicative",
drop_missing=True,
learning_rate=0.1,
)
m.fit(df, freq='D', epochs=10, validate_each_epoch=True, valid_p=0.2) # Specify number of epochs and validation parameters
future = m.make_future_dataframe(df, periods=30, n_historic_predictions=True)
forecast = m.predict(future)
def predict_vn_index(option=None):
fig = m.plot(forecast)
path = "forecast_plot.png"
fig.savefig(path)
disclaimer = "Quý khách chỉ xem đây là tham khảo, công ty không chịu bất cứ trách nhiệm nào về tình trạng đầu tư của quý khách."
return path, disclaimer
if __name__ == "__main__":
dropdown = gr.inputs.Dropdown(["VNIndex"], label="Choose an option", default="VNIndex")
image_output = gr.outputs.Image(type="file", label="Forecast Plot")
disclaimer_output = gr.outputs.Textbox(label="Disclaimer")
interface = gr.Interface(fn=predict_vn_index, inputs=dropdown, outputs=[image_output, disclaimer_output], title="Dự báo VN Index 30 ngày tới")
interface.launch(share=True)