hellorahulk commited on
Commit
04affa2
·
1 Parent(s): ae34fe7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from prophet import Prophet
3
+ import plotly.express as px
4
+ import gradio as gr
5
+ from prophet.plot import plot_plotly, plot_components_plotly
6
+
7
+
8
+ def forecast_timeseries(data_file, forecast_periods):
9
+ # Load the input data
10
+
11
+ df = pd.read_csv(data_file.name,encoding='utf-8')
12
+ df['WEIGHT_LBS'] = df['WEIGHT_LBS'].astype('int64')
13
+ df = df.rename(columns={'RECV_DATE': 'ds', 'WEIGHT_LBS': 'y'})
14
+
15
+ weekly_df = df[['ds','y']].sort_values('ds')
16
+ weekly_df = weekly_df.groupby('ds',as_index=False)['y'].sum()
17
+ weekly_df['ds'] = pd.to_datetime(weekly_df['ds'])
18
+ weekly_df.set_index('ds', inplace=True)
19
+ weekly_df = weekly_df.resample('w').sum()
20
+ weekly_df.reset_index(inplace=True)
21
+
22
+ train = weekly_df[:int(0.8*(weekly_df.shape[0]))]
23
+ test = weekly_df[int(0.8*(weekly_df.shape[0]))+1:]
24
+
25
+ m = Prophet()
26
+ m.fit(train[['ds','y']])
27
+
28
+
29
+ # Create a future dataframe for forecasting
30
+ future = m.make_future_dataframe(periods=int(forecast_periods),freq='W')
31
+
32
+ # Make predictions
33
+ forecast = m.predict(future)
34
+
35
+ # Extract relevant columns from the forecast
36
+ forecast_data = forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']]
37
+
38
+
39
+ # Plot the forecasted data with upper and lower bounds
40
+ # fig = px.line(forecast, x='ds', y='yhat', title='Forecasted Time Series')
41
+ # fig.add_scatter(x=df['ds'], y=df['y'], mode='markers', name='Actual Values')
42
+ # fig.add_scatter(x=forecast['ds'], y=forecast['yhat_upper'], mode='lines', name='Upper Bound')
43
+ # fig.add_scatter(x=forecast['ds'], y=forecast['yhat_lower'], mode='lines', name='Lower Bound')
44
+
45
+ fig = plot_plotly(m, forecast)
46
+ fig1 = plot_components_plotly(m, forecast)
47
+
48
+
49
+
50
+
51
+ return df.head(),fig,fig1 ,forecast_data
52
+
53
+
54
+ # Define input and output interfaces for the Gradio app
55
+ inputs = [
56
+ gr.inputs.File(label="Upload CSV File"),
57
+ gr.inputs.Number(label="Forecast Periods", default=7),
58
+ ]
59
+ outputs = [
60
+ gr.Dataframe(label="Input Data"),
61
+ gr.Plot(label="Forecast Plot"),
62
+ gr.Plot(label="Trends"),
63
+ gr.Dataframe(label="Forecast Results"),
64
+ ]
65
+
66
+ # Create the Gradio app
67
+ iface = gr.Interface(fn=forecast_timeseries, inputs=inputs, outputs=outputs, title="Time Series Forecasting with Prophet")
68
+ iface.launch(inline = False,share=True,debug=False)
69
+