alpertml commited on
Commit
b0e7b5d
·
verified ·
1 Parent(s): 4e119ec

Upload 5 files

Browse files
app.py ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # external libraries
2
+ import streamlit as st
3
+ import pandas as pd
4
+ import numpy as np
5
+ import os
6
+ import datetime
7
+
8
+ from config import Config
9
+
10
+ config = vars(Config)
11
+
12
+ def main():
13
+
14
+ # variables
15
+ if "state" not in st.session_state:
16
+ st.session_state["state"] = True
17
+ st.session_state["predictions_df"] = pd.DataFrame()
18
+
19
+ st.set_page_config(
20
+ layout="centered", # Can be "centered" or "wide". In the future also "dashboard", etc.
21
+ initial_sidebar_state="auto", # Can be "auto", "expanded", "collapsed"
22
+ page_title=config['MAIN_TITLE'], # String or None. Strings get appended with "• Streamlit".
23
+ page_icon=config['ICON_PATH'], # String, anything supported by st.image, or None.
24
+ )
25
+
26
+ col1, col2, col3 = st.columns(3)
27
+ col1.write(' ')
28
+ col2.image(config['ICON_PATH'])
29
+ col3.write(' ')
30
+
31
+ st.markdown(f"<h1 style='text-align: center;'>{config['MAIN_TITLE']}</h1>", unsafe_allow_html=True)
32
+ st.markdown(f"<h3 style='text-align: center;'>{config['SUB_TITLE']}</h3>", unsafe_allow_html=True)
33
+
34
+ if st.session_state["state"]:
35
+
36
+ dates = get_forecasting_horizon(config['FORECAST_START_DATE'],config['FORECAST_END_DATE'])
37
+
38
+ col1, col2= st.columns([100,1])
39
+ input_date = col1.slider(
40
+ "Forecast Interval", dates[0].date(), dates[-1].date(),
41
+ value=(dates[0].date(),dates[0].date() + datetime.timedelta(weeks=12)),
42
+ step=datetime.timedelta(weeks=4))
43
+
44
+ col1, col2, col3 , col4, col5 = st.columns(5)
45
+ with col3 :
46
+ center_button = st.button(config['FORECAST_BUTTON_TEXT'], on_click=predict, args=(input_date,))
47
+
48
+
49
+ if not st.session_state["predictions_df"].empty:
50
+
51
+ pid = st.selectbox(config['LINE_PLOT_SELECTBOX_TEXT'],st.session_state["predictions_df"]['product_id'].unique())
52
+
53
+ st.table(st.session_state["predictions_df"].loc[st.session_state["predictions_df"]['product_id'] == pid,['product_id','date','demand']][:10])
54
+
55
+ st.write(f"Monthly demand for the product {pid}")
56
+ st.line_chart(st.session_state["predictions_df"].loc[st.session_state["predictions_df"]['product_id'] == pid,:],
57
+ x="date", y="demand", use_container_width=True)
58
+
59
+ category = st.selectbox(config['BAR_PLOT_SELECTBOX_TEXT'],['product_application',
60
+ 'product_marketing_name',
61
+ 'product_main_family',
62
+ 'planning_method_latest'])
63
+
64
+ st.write(f'Average demand by category "{category}"')
65
+ st.bar_chart(st.session_state["predictions_df"].loc[st.session_state["predictions_df"]['product_id'] == pid,:].groupby(category)['demand'].mean())
66
+
67
+ input_save = st.checkbox(config['SAVE_CHECKBOX_TEXT'])
68
+ confirm_params = {
69
+ 'input_save':input_save
70
+ }
71
+
72
+ confirm = st.button(config['SAVE_BUTTON_TEXT'], on_click=save, args=(confirm_params,))
73
+ if confirm:
74
+ st.success(config['SAVE_BUTTON_SUCCESS_TEXT'])
75
+
76
+
77
+
78
+
79
+
80
+ def get_forecasting_horizon(start_date, end_date):
81
+ horizon = pd.date_range(start_date,
82
+ end_date,
83
+ freq='MS')
84
+ return horizon
85
+
86
+ # saving results
87
+ def save(params):
88
+
89
+ dir = 'demand_predictions'
90
+ if not os.path.exists(f'{dir}'):
91
+ os.makedirs(f'{dir}')
92
+
93
+ if params['input_save']:
94
+ today = datetime.datetime.today().strftime("%d-%m-%Y")
95
+ st.session_state["predictions_df"].to_excel(f'{dir}/predictions_{today}.xlsx', index=False)
96
+
97
+
98
+ # forecasting
99
+ def predict(input_date):
100
+
101
+ data = {
102
+ 'product_id': [f'P{1}' for i in range(5)],
103
+ 'date':['2022-01-01','2022-02-01','2022-03-01','2022-04-01','2022-05-01'],
104
+ 'demand': np.random.randint(1, 100, size=5),
105
+ 'product_application': ['A','A','A','B','B']
106
+ }
107
+
108
+ data2 = {
109
+ 'product_id': [f'P{2}' for i in range(5)],
110
+ 'date':['2022-01-01','2022-02-01','2022-03-01','2022-04-01','2022-05-01'],
111
+ 'demand': np.random.randint(1, 100, size=5),
112
+ 'product_application': ['A','A','A','B','B']
113
+ }
114
+
115
+ df1 = pd.DataFrame(data)
116
+ df2 = pd.DataFrame(data2)
117
+
118
+ # Concatenate the two DataFrames vertically
119
+ combined_df = pd.concat([df1, df2], ignore_index=True)
120
+
121
+
122
+ forecast_start_date = input_date[0].strftime("%Y-%m-%d")
123
+ forecast_end_date = input_date[1].strftime("%Y-%m-%d")
124
+ print(forecast_start_date, forecast_end_date)
125
+
126
+ st.session_state["predictions_df"] = combined_df
127
+
128
+ if __name__ == "__main__":
129
+ main()
config.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class Config():
2
+
3
+ def __init__(self):
4
+ pass
5
+
6
+ target = 'demand'
7
+
8
+ not_include_features = [
9
+ target,
10
+ 'date'
11
+ ]
12
+
13
+ cat_features = [
14
+ 'product_id',
15
+ 'product_application',
16
+ 'product_marketing_name',
17
+ 'product_main_family',
18
+ 'planning_method_latest'
19
+ ]
20
+
21
+ # production
22
+ MAIN_TITLE = 'Infineon Product Demand Forecasting System'
23
+ SUB_TITLE = 'Data Analytics in Applications'
24
+ ICON_PATH = 'images/infineon-icon-1.png'
25
+
26
+ FORECAST_START_DATE = '01-11-2023'
27
+ FORECAST_END_DATE = '01-07-2024'
28
+
29
+ FORECAST_BUTTON_TEXT = 'Predict'
30
+
31
+ LINE_PLOT_SELECTBOX_TEXT = 'Filter at product level'
32
+ BAR_PLOT_SELECTBOX_TEXT = 'Filter at category level'
33
+
34
+ SAVE_CHECKBOX_TEXT = 'Save predictions'
35
+ SAVE_BUTTON_TEXT = 'Apply'
36
+ SAVE_BUTTON_SUCCESS_TEXT = 'File successfully saved'
images/infineon-icon-1.png ADDED
images/infineon-icon-2.webp ADDED
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ numpy==1.24.3
2
+ pandas==2.0.0
3
+ streamlit==1.23.1