alexander-lazarin commited on
Commit
a04211d
·
1 Parent(s): 4cd25ce

Initial version

Browse files
Files changed (2) hide show
  1. app.py +67 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import pmdarima as pm
4
+ from pmdarima import auto_arima
5
+ import matplotlib.pyplot as plt
6
+
7
+ def forecast_time_series(file):
8
+ # Load data
9
+ data = pd.read_csv(file.name, skiprows=2)
10
+ period_type = data.columns[0]
11
+ data[period_type] = pd.to_datetime(data[period_type])
12
+ data.set_index(period_type, inplace=True)
13
+
14
+ # Fit the SARIMAX model automatically
15
+ model = auto_arima(data)
16
+
17
+ # Forecasting
18
+ n_periods = 24 # Number of periods to forecast
19
+ forecast, conf_int = model.predict(n_periods=n_periods, return_conf_int=True)
20
+
21
+ # Create a DataFrame with the forecast and confidence intervals
22
+ forecast_index = pd.date_range(start=data.index[-1], periods=n_periods + 1, freq=data.index.inferred_freq)[1:]
23
+ forecast_df = pd.DataFrame(forecast, index=forecast_index, columns=['Forecast'])
24
+ conf_int_df = pd.DataFrame(conf_int, index=forecast_index, columns=['Lower CI', 'Upper CI'])
25
+ forecast_df = pd.concat([forecast_df, conf_int_df], axis=1)
26
+
27
+ # Calculate the YoY change
28
+ sum_last_12_original = data.iloc[-12:, 0].sum()
29
+ sum_first_12_forecast = forecast_df['Forecast'].iloc[:12].sum()
30
+ yoy_change = (sum_first_12_forecast - sum_last_12_original) / sum_last_12_original
31
+
32
+ # Plot the original time series and forecast
33
+ plt.figure(figsize=(12, 6))
34
+ plt.plot(data.index, data.iloc[:, 0], label='Original Series')
35
+ plt.plot(forecast_df.index, forecast_df['Forecast'], color='red', label='Forecast')
36
+ plt.fill_between(forecast_df.index,
37
+ forecast_df['Lower CI'],
38
+ forecast_df['Upper CI'],
39
+ color='pink', alpha=0.3, label='Confidence Interval')
40
+ plt.xlabel('Date')
41
+ plt.ylabel('Values')
42
+ plt.title('Original Time Series and Forecast with Confidence Intervals')
43
+ plt.legend()
44
+
45
+ # Save plot to a file
46
+ plot_file = 'forecast_plot.png'
47
+ plt.savefig(plot_file)
48
+ plt.close()
49
+
50
+ # Return plot file path and YoY change
51
+ return plot_file, f'Year-over-Year Change in Sum of Values: {yoy_change:.2%}'
52
+
53
+ # Create Gradio interface
54
+ interface = gr.Interface(
55
+ theme=gr.themes.Monochrome(),
56
+ fn=forecast_time_series,
57
+ inputs=gr.File(label="Upload Time Series CSV"),
58
+ outputs=[
59
+ gr.Image(label="Time Series + Forecast Chart"),
60
+ gr.Text(label="YoY % Change")
61
+ ],
62
+ title="Time Series Forecasting with SARIMAX",
63
+ description="Upload a CSV file with a time series to forecast the next 12 periods and see the YoY % change."
64
+ )
65
+
66
+ # Launch the interface
67
+ interface.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ pandas
3
+ pmdarima
4
+ matplotlib