Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import numpy as np | |
| import pandas as pd | |
| import plotly.express as px | |
| import plotly.graph_objs as go | |
| from statsforecast import StatsForecast | |
| from statsforecast.models import auto_arima | |
| from utils import calc_seasonality | |
| def convert_df(df): | |
| return df.to_csv().encode('utf-8') | |
| st.title('Forecasting time series') | |
| uploaded_file = st.file_uploader("Choose a CSV file", accept_multiple_files=False) | |
| if uploaded_file is not None: | |
| dataframe = pd.read_csv(uploaded_file) | |
| st.write(dataframe) | |
| series = st.text_input("Write the name of the variable you want to forecast") | |
| date = st.text_input('Write the first date') | |
| # freq = st.text_input('Write the frequency') | |
| freq = st.selectbox('Select the frequency', | |
| ('Y', 'Q', 'M', 'W', 'D')) | |
| if series is not None and freq is not None: | |
| horizons = int(st.text_input("Write the number of horizons", value="10")) | |
| seasonality = calc_seasonality(freq) | |
| st.write(f'Seasonality: {seasonality}') | |
| series_train = pd.DataFrame( | |
| { | |
| 'ds': pd.date_range(start=date, periods=dataframe.shape[0], freq=freq), | |
| 'y': dataframe[series].values | |
| }, | |
| index=pd.Index([0] * dataframe.shape[0], name='unique_id') | |
| ) | |
| fig = px.line(series_train, x='ds', y='y', title=f'{series}') | |
| st.plotly_chart(fig) | |
| with st.spinner('Wait for it...'): | |
| fcst = StatsForecast( | |
| series_train, | |
| models=[(auto_arima, seasonality), ], | |
| freq=freq, | |
| n_jobs=1 | |
| ) | |
| forecasts = fcst.forecast(horizons, level=(80, 95)) | |
| st.success('Done!') | |
| st.write(forecasts) | |
| csv = convert_df(forecasts) | |
| st.download_button( | |
| label="Download data as CSV", | |
| data=csv, | |
| file_name='forecast.csv', | |
| mime='text/csv', | |
| ) | |
| fig_forecast = go.Figure([ | |
| go.Scatter( | |
| name=series, | |
| x=series_train['ds'], | |
| y=series_train['y'], | |
| mode='lines', | |
| marker=dict(color='blue'), | |
| line=dict(width=1), | |
| showlegend=True | |
| ), | |
| go.Scatter( | |
| name=f'auto_arima_season_length-{seasonality}_mean', | |
| x=forecasts['ds'], | |
| y=forecasts[f'auto_arima_season_length-{seasonality}_mean'], | |
| mode='lines', | |
| marker=dict(color='green'), | |
| line=dict(width=1), | |
| showlegend=True | |
| ), | |
| ]) | |
| fig_forecast.update_layout( | |
| yaxis_title=series, | |
| title=f'Forecasting {series}' | |
| ) | |
| st.plotly_chart(fig_forecast) |