alpertml commited on
Commit
699b778
·
verified ·
1 Parent(s): fa10c3d

Upload 19 files

Browse files
app.py CHANGED
@@ -3,6 +3,7 @@ import streamlit as st
3
  import pandas as pd
4
  import os
5
  import datetime
 
6
 
7
  import pipeline
8
  from config import Config
@@ -15,6 +16,10 @@ def main():
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.
@@ -64,9 +69,20 @@ def main():
64
  st.write(f'Average demand by category "{category}"')
65
  st.bar_chart(st.session_state["predictions_df"].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,))
@@ -94,10 +110,27 @@ def save(params):
94
  today = datetime.datetime.today().strftime("%d-%m-%Y")
95
  st.session_state["predictions_df"][['product_id','date','demand']].to_excel(f'{dir}/predictions_{today}.xlsx', index=False)
96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
97
 
98
  # forecasting
99
  def predict(input_date):
100
 
 
 
101
  forecast_start_date = input_date[0].strftime("%Y-%m-%d")
102
  forecast_end_date = input_date[1].strftime("%Y-%m-%d")
103
 
 
3
  import pandas as pd
4
  import os
5
  import datetime
6
+ from src.utils.helper_functions import send_mail
7
 
8
  import pipeline
9
  from config import Config
 
16
  if "state" not in st.session_state:
17
  st.session_state["state"] = True
18
  st.session_state["predictions_df"] = pd.DataFrame()
19
+ st.session_state["send_mail"] = False
20
+ st.session_state["text_input"] = ''
21
+ st.session_state["input_date"] = None
22
+
23
 
24
  st.set_page_config(
25
  layout="centered", # Can be "centered" or "wide". In the future also "dashboard", etc.
 
69
  st.write(f'Average demand by category "{category}"')
70
  st.bar_chart(st.session_state["predictions_df"].groupby(category)['demand'].mean())
71
 
72
+
73
+ do_mail_sending = st.checkbox('Send mail')
74
+ if do_mail_sending:
75
+ # Display a text area if the checkbox is clicked
76
+ st.session_state["send_mail"] = do_mail_sending
77
+ st.session_state["text_input"] = st.text_input('Mail address:')
78
+
79
  input_save = st.checkbox(config['SAVE_CHECKBOX_TEXT'])
80
  confirm_params = {
81
+ 'input_save':input_save,
82
+ 'send_mail':st.session_state["send_mail"],
83
+ 'email_adress': st.session_state["text_input"],
84
+ 'forecast_start_date':st.session_state["input_date"][0].strftime("%Y/%m/%d"),
85
+ 'forecast_end_date':st.session_state["input_date"][1].strftime("%Y/%m/%d")
86
  }
87
 
88
  confirm = st.button(config['SAVE_BUTTON_TEXT'], on_click=save, args=(confirm_params,))
 
110
  today = datetime.datetime.today().strftime("%d-%m-%Y")
111
  st.session_state["predictions_df"][['product_id','date','demand']].to_excel(f'{dir}/predictions_{today}.xlsx', index=False)
112
 
113
+ if params['send_mail']:
114
+ status = send_mail(
115
+ name='Alper Temel',
116
+ email=params['email_adress'],
117
+ subject='Forecast Results',
118
+ message='Hi Captain',
119
+ dataframe=st.session_state["predictions_df"].loc[:, ['product_id','date','demand']],
120
+ forecast_start_date=params['forecast_start_date'],
121
+ forecast_end_date=params['forecast_end_date'],
122
+ toMail=params['email_adress']
123
+ )
124
+
125
+ if not status: # not successfull
126
+ return False
127
+
128
 
129
  # forecasting
130
  def predict(input_date):
131
 
132
+ st.session_state["input_date"] = input_date
133
+
134
  forecast_start_date = input_date[0].strftime("%Y-%m-%d")
135
  forecast_end_date = input_date[1].strftime("%Y-%m-%d")
136
 
config.py CHANGED
@@ -5,6 +5,9 @@ class Config():
5
  def __init__(self):
6
  pass
7
 
 
 
 
8
  target = 'demand'
9
  split_local_test = False
10
 
@@ -26,6 +29,7 @@ class Config():
26
  fold = 5
27
  fold_models_directory = 'models/date_models_test'
28
  fold_input_directory = 'maps/date_models_test'
 
29
 
30
  catboost_params = {
31
  'learning_rate': 0.03,
@@ -56,4 +60,4 @@ class Config():
56
 
57
  SAVE_CHECKBOX_TEXT = 'Save predictions'
58
  SAVE_BUTTON_TEXT = 'Apply'
59
- SAVE_BUTTON_SUCCESS_TEXT = 'File successfully saved'
 
5
  def __init__(self):
6
  pass
7
 
8
+ MJ_APIKEY_PUBLIC = '2846230e59f26adb7ec9ff2790be29dc'
9
+ MJ_APIKEY_PRIVATE = 'fcbcb59ae402ef96b7b7d9bcb211fd44'
10
+
11
  target = 'demand'
12
  split_local_test = False
13
 
 
29
  fold = 5
30
  fold_models_directory = 'models/date_models_test'
31
  fold_input_directory = 'maps/date_models_test'
32
+ result_path = 'demand_predictions/'
33
 
34
  catboost_params = {
35
  'learning_rate': 0.03,
 
60
 
61
  SAVE_CHECKBOX_TEXT = 'Save predictions'
62
  SAVE_BUTTON_TEXT = 'Apply'
63
+ SAVE_BUTTON_SUCCESS_TEXT = 'Successfully Applied!'
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  catboost==1.2.5
 
2
  numpy==1.24.3
3
  pandas==2.0.0
4
  scikit_learn==1.2.2
 
1
  catboost==1.2.5
2
+ mailjet_rest==1.3.4
3
  numpy==1.24.3
4
  pandas==2.0.0
5
  scikit_learn==1.2.2
src/utils/__pycache__/helper_functions.cpython-311.pyc CHANGED
Binary files a/src/utils/__pycache__/helper_functions.cpython-311.pyc and b/src/utils/__pycache__/helper_functions.cpython-311.pyc differ
 
src/utils/__pycache__/mail.cpython-311.pyc ADDED
Binary file (3.35 kB). View file
 
src/utils/helper_functions.py CHANGED
@@ -3,6 +3,10 @@ import os
3
  import numpy as np
4
  from datetime import datetime
5
  import pandas as pd
 
 
 
 
6
 
7
  def save_models(models, model_type, directory):
8
 
@@ -61,8 +65,21 @@ def save_parquet(dataframe, path):
61
  def load_parquet(path):
62
  return pd.read_parquet(path)
63
 
 
64
 
 
65
 
 
 
 
 
 
 
 
 
 
66
 
 
 
67
 
68
-
 
3
  import numpy as np
4
  from datetime import datetime
5
  import pandas as pd
6
+ from src.utils.mail import Mail
7
+ from config import Config
8
+
9
+ config = vars(Config)
10
 
11
  def save_models(models, model_type, directory):
12
 
 
65
  def load_parquet(path):
66
  return pd.read_parquet(path)
67
 
68
+ def send_mail(**kwargs):
69
 
70
+ mail = Mail(config['MJ_APIKEY_PUBLIC'], config['MJ_APIKEY_PRIVATE'])
71
 
72
+ status = mail.send_mail(
73
+ fromName=kwargs.get('name'),
74
+ fromMail=kwargs.get('email'),
75
+ fromSubject=kwargs.get('subject'),
76
+ fromMsg=kwargs.get('message'),
77
+ dataframe=kwargs.get('dataframe'),
78
+ forecast_dates=(kwargs.get('forecast_start_date'),kwargs.get('forecast_end_date')),
79
+ toMail=kwargs.get('toMail')
80
+ )
81
 
82
+ if status == 200:
83
+ return True
84
 
85
+ return False
src/utils/mail.py ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from mailjet_rest import Client
2
+ import base64
3
+ import pandas as pd
4
+ from io import BytesIO
5
+ import datetime
6
+
7
+ class Mail():
8
+
9
+ def __init__(self, api_key, api_secret):
10
+
11
+ self.api_key = api_key
12
+ self.api_secret = api_secret
13
+ self.mailjet = Client(auth=(self.api_key, self.api_secret), version='v3.1')
14
+
15
+ def send_mail(self, fromName, fromMail, fromSubject, fromMsg, dataframe, forecast_dates, toMail):
16
+
17
+ Subject = 'Predictions are ready! - Infineon Product Demand Forecasting System'
18
+ TextPart = f'Greetings from {Subject}'
19
+
20
+ with BytesIO() as output:
21
+ with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
22
+ dataframe.to_excel(writer, sheet_name='Sheet1', index=False)
23
+ output.seek(0)
24
+ excel_content = output.read()
25
+
26
+ # Encode Excel content to base64
27
+ base64_content = base64.b64encode(excel_content).decode()
28
+
29
+ today = datetime.datetime.today().strftime("%d-%m-%Y")
30
+
31
+ data = {
32
+ 'Messages': [
33
+ {
34
+ "From": {
35
+ "Email": "[email protected]",
36
+ "Name": "Me"
37
+ },
38
+ "To": [
39
+ {
40
+ "Email": toMail,
41
+ "Name": "You"
42
+ }
43
+ ],
44
+ "Subject": Subject,
45
+ "TextPart": TextPart,
46
+ "HTMLPart": f"Hello,<br/><br/>You can access the demand forecasting output for Infenion for the following dates <b>{forecast_dates[0]}</b> - <b>{forecast_dates[1]}</b> in the attachments<br/>If you have any problems, you can let us know via this e-mail address.<br/><br/>Have a good day,<br/><h3>Infineon AI Department</h3>",
47
+ 'Attachments': [
48
+ {
49
+ 'ContentType': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
50
+ 'Filename': f'demand_predictions_{today}.xlsx',
51
+ 'Base64Content': base64_content
52
+ }
53
+ ]
54
+ }
55
+
56
+
57
+ ]
58
+ }
59
+
60
+ result = self.mailjet.send.create(data=data)
61
+
62
+ return result.status_code