Elegbede commited on
Commit
3699eb0
·
1 Parent(s): 2a6089a

Upload app (1).py

Browse files
Files changed (1) hide show
  1. app (1).py +101 -0
app (1).py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+ # coding: utf-8
3
+
4
+ # In[3]:
5
+
6
+
7
+ import pandas as pd
8
+ import pmdarima as pm
9
+ import matplotlib.pyplot as plt
10
+ import gradio as gr
11
+ import io
12
+ import warnings
13
+ warnings.simplefilter("ignore")
14
+
15
+
16
+ def predict_timeseries(data_file):
17
+ # Load CSV file into a pandas DataFrame
18
+ data = pd.read_csv(data_file.name, index_col=[0], parse_dates=True)
19
+
20
+ # Convert date column to datetime object
21
+ data.index = pd.to_datetime(data.index)
22
+
23
+ # Fit the auto ARIMA model
24
+ model = pm.auto_arima(data.values, seasonal=True, m=12)
25
+
26
+ # Get ARIMA order
27
+ arima_order = model.order
28
+
29
+ # Plot the actual data
30
+ # Plot the actual data
31
+ fig_actual, ax_actual = plt.subplots()
32
+ ax_actual.plot(data, label = data.columns[-1])
33
+ ax_actual.set_xlabel(data.index.name)
34
+ plt.legend()
35
+ ax_actual.set_ylabel(data.columns[-1])
36
+ ax_actual.set_title("Plot of Actual data for {}".format(data.columns[-1]))
37
+ plt.show()
38
+
39
+ # Get the last date in the actual data
40
+ last_date = data.index[-1]
41
+
42
+ # Make predictions
43
+ predicted_values = model.predict(n_periods=12)
44
+
45
+ # Generate a range of dates starting from the start date
46
+ pred_index = pd.date_range(start=last_date, periods=len(predicted_values)+1, freq="MS")[1:]
47
+
48
+ # Create a new dataframe with the predicted values and the generated dates
49
+ predictions = pd.DataFrame({'predicted_values': predicted_values}, index=pred_index)
50
+ predictions.columns = data.columns
51
+ predictions.index.name = data.index.name
52
+ predictions.index.freq = data.index.freq
53
+
54
+ # Merge the dataframes using the index
55
+ merged_data = pd.concat([data, predictions], axis=0)
56
+
57
+ num_actual = len(data.index)
58
+
59
+ # Plot the actual vs predicted data
60
+ actual_data = merged_data.iloc[:num_actual,:]
61
+ fig, ax = plt.subplots()
62
+ ax.plot(actual_data.index, actual_data[data.columns[-1]], label='Actual')
63
+
64
+ # Plot the predicted data
65
+ predicted_data = merged_data.iloc[num_actual:,:]
66
+ ax.plot(predicted_data.index, predicted_data[data.columns[-1]], label='Predicted')
67
+
68
+ # Add x and y axis labels
69
+ ax.set_xlabel(data.index.name)
70
+ ax.set_ylabel(data.columns[-1])
71
+
72
+ # Add title and legend
73
+ ax.set_title('Actual vs Predicted')
74
+ ax.legend()
75
+ plt.show()
76
+
77
+
78
+ return data.head(), fig_actual, arima_order, predictions, fig
79
+
80
+ input_data = gr.inputs.File(label="Upload CSV file")
81
+
82
+ outputs = [gr.outputs.Dataframe(type = "pandas", label = "FIRST FIVE ROWS OF DATASET"),
83
+ 'plot',
84
+ gr.outputs.Textbox(label = "ARIMA ORDER"),
85
+ gr.outputs.Dataframe(type = "pandas", label = "PREDICTIONS FOR NEXT 12 PERIODS"),
86
+ 'plot'
87
+ ]
88
+ examples = ["Electric_Production.csv"]
89
+ interface = gr.Interface(fn=predict_timeseries, inputs=input_data, outputs=outputs,
90
+ title="Time series Forecast using AUTO ARIMA",
91
+ description="Upload a CSV file of monthly time series data to generate 12 period forecasts using ARIMA.\nPlease Note: \nThe first plot displays the Time Plot of the Actual Series \n\tThe second Plot displays Plot Of Actual and Predicted values",
92
+ theme = "darkhuggingface",
93
+ examples = examples)
94
+ interface.launch()
95
+
96
+
97
+ # In[ ]:
98
+
99
+
100
+
101
+