alexander-lazarin commited on
Commit
8dc3a0f
·
1 Parent(s): 81ea222

Add forecast export button, add interactive Plotly plot

Browse files
Files changed (2) hide show
  1. app.py +38 -23
  2. requirements.txt +2 -1
app.py CHANGED
@@ -2,7 +2,7 @@ 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
@@ -29,26 +29,40 @@ def forecast_time_series(file):
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(
@@ -56,12 +70,13 @@ interface = gr.Interface(
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",
63
- description="Upload a CSV file with a time series to forecast the next 24 periods and see the YoY % change."
64
  )
65
 
66
  # Launch the interface
67
- interface.launch()
 
2
  import pandas as pd
3
  import pmdarima as pm
4
  from pmdarima import auto_arima
5
+ import plotly.graph_objs as go
6
 
7
  def forecast_time_series(file):
8
  # Load data
 
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
+ # Create an interactive plot with Plotly
33
+ fig = go.Figure()
34
+ fig.add_trace(go.Scatter(x=data.index, y=data.iloc[:, 0], mode='lines', name='Original Series'))
35
+ fig.add_trace(go.Scatter(x=forecast_df.index, y=forecast_df['Forecast'], mode='lines', name='Forecast', line=dict(color='red')))
36
+ fig.add_trace(go.Scatter(
37
+ x=forecast_df.index,
38
+ y=forecast_df['Lower CI'],
39
+ fill=None,
40
+ mode='lines',
41
+ line=dict(color='pink'),
42
+ showlegend=False
43
+ ))
44
+ fig.add_trace(go.Scatter(
45
+ x=forecast_df.index,
46
+ y=forecast_df['Upper CI'],
47
+ fill='tonexty',
48
+ mode='lines',
49
+ line=dict(color='pink'),
50
+ name='Confidence Interval'
51
+ ))
52
+ fig.update_layout(
53
+ title='Original Time Series and Forecast with Confidence Intervals',
54
+ xaxis_title='Date',
55
+ yaxis_title='Values',
56
+ hovermode='x unified'
57
+ )
58
 
59
+ # Combine original data and forecast data into one DataFrame for export
60
+ combined_df = pd.concat([data, forecast_df], axis=0)
61
+ combined_file = 'combined_data.csv'
62
+ combined_df.to_csv(combined_file)
63
+
64
  # Return plot file path and YoY change
65
+ return fig, f'Year-over-Year Change in Sum of Values: {yoy_change:.2%}', combined_file
66
 
67
  # Create Gradio interface
68
  interface = gr.Interface(
 
70
  fn=forecast_time_series,
71
  inputs=gr.File(label="Upload Time Series CSV"),
72
  outputs=[
73
+ gr.Plot(label="Time Series + Forecast Chart"),
74
+ gr.Text(label="YoY % Change"),
75
+ gr.File(label="Download Combined Data CSV")
76
  ],
77
  title="Time Series Forecasting",
78
+ description="Upload a CSV file with a time series to forecast the next 24 periods and see the YoY % change. Download the combined original and forecast data."
79
  )
80
 
81
  # Launch the interface
82
+ interface.launch()
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  gradio
2
  pandas
3
  pmdarima
4
- matplotlib
 
 
1
  gradio
2
  pandas
3
  pmdarima
4
+ matplotlib
5
+ plotly