Spaces:
Runtime error
Runtime error
Andy Lau
commited on
Commit
·
8170b82
1
Parent(s):
8c65dfe
clean up the model a little bit
Browse files
app.py
CHANGED
@@ -6,8 +6,7 @@ import re
|
|
6 |
from io import StringIO
|
7 |
import boto3
|
8 |
from urlextract import URLExtract
|
9 |
-
|
10 |
-
|
11 |
# from joblib import dump, load
|
12 |
|
13 |
import joblib
|
@@ -230,7 +229,7 @@ char_reco_preference = st.selectbox(
|
|
230 |
["Increase", "Decrease"],
|
231 |
index=1)
|
232 |
|
233 |
-
|
234 |
def get_files_from_aws(bucket,prefix):
|
235 |
"""
|
236 |
get files from aws s3 bucket
|
@@ -255,6 +254,7 @@ def get_files_from_aws(bucket,prefix):
|
|
255 |
|
256 |
|
257 |
if st.button('Generate Predictions'):
|
|
|
258 |
if uploaded_file is None:
|
259 |
st.error('Please upload a email (HTML format)')
|
260 |
else:
|
@@ -273,9 +273,10 @@ if st.button('Generate Predictions'):
|
|
273 |
|
274 |
# Dataset:
|
275 |
training_dataset = get_files_from_aws('emailcampaigntrainingdata','modelCC/training.csv')
|
276 |
-
X_test = get_files_from_aws('emailcampaigntrainingdata','modelCC/Xtest.csv')
|
277 |
-
Y_test = get_files_from_aws('emailcampaigntrainingdata','modelCC/ytest.csv')
|
278 |
|
|
|
279 |
|
280 |
industry_code_dict = get_industry_code_dict(email_data)
|
281 |
bytes_data = uploaded_file.getvalue()
|
@@ -293,16 +294,17 @@ if st.button('Generate Predictions'):
|
|
293 |
predicted_rate = model.predict(df_uploaded_test)[0]
|
294 |
output_rate = round(predicted_rate,4)
|
295 |
|
|
|
296 |
if output_rate < 0:
|
297 |
print("Sorry, Current model couldn't provide predictions on the target variable you selected.")
|
298 |
else:
|
299 |
-
st.markdown('
|
300 |
# st.info('The model predicts that it achieves a {} of {}%'.format(target, str(round(output_rate*100,2))))
|
301 |
if target == 'conversion_rate':
|
302 |
target_vis = 'Conversion Rate'
|
303 |
else:
|
304 |
target_vis = 'Click-to-Open Rate'
|
305 |
-
st.markdown('
|
306 |
selected_industry_code = industry_code_dict.get(industry)
|
307 |
|
308 |
if target == "click_to_open_rate":
|
@@ -325,33 +327,23 @@ if st.button('Generate Predictions'):
|
|
325 |
if preference == "Decrease":
|
326 |
df_reco_opt = df_reco[(df_reco[selected_variable] > output_rate) & (df_reco["character_cnt"] < character_cnt)]
|
327 |
df_reco_opt_rank = df_reco_opt.nlargest(3,[selected_variable])
|
328 |
-
|
329 |
-
|
330 |
-
# something wrong here
|
331 |
-
# if char_reco_preference == "Increase":
|
332 |
-
# df_reco_opt = df_reco[(df_reco[selected_variable] > output_rate) & (df_reco["character_cnt"] > character_cnt) & (df_reco["character_cnt"] <= (1.5*character_cnt))]
|
333 |
-
# df_reco_opt_rank = df_reco_opt.nlargest(3,[selected_variable])
|
334 |
-
# if char_reco_preference == "Decrease":
|
335 |
-
# df_reco_opt = df_reco[(df_reco[selected_variable] > output_rate) & (df_reco["character_cnt"] < character_cnt)]
|
336 |
-
# df_reco_opt_rank = df_reco_opt.nlargest(3,[selected_variable])
|
337 |
-
|
338 |
-
# print(df_reco_opt)
|
339 |
-
# print(df_reco_opt_rank)
|
340 |
|
341 |
if selected_variable == "Open_Rate":
|
342 |
selected_variable = "Click-to-Open_Rate"
|
343 |
if selected_variable == "Click_Through_Rate":
|
344 |
selected_variable = "Conversion_Rate"
|
345 |
|
346 |
-
st.markdown('
|
347 |
if len(df_reco_opt_rank) == 0:
|
348 |
-
st.
|
349 |
else:
|
350 |
for _, row in df_reco_opt_rank.iterrows():
|
351 |
Character_Count = row[1]
|
352 |
selected_variable = row[3]
|
353 |
# print(f"·Number of Characters: {int(Character_Count)}, Target Rate: {round(selected_variable, 3)*100}", "%")
|
354 |
-
st.markdown('
|
355 |
|
356 |
|
357 |
-
placeholder.empty()
|
|
|
|
6 |
from io import StringIO
|
7 |
import boto3
|
8 |
from urlextract import URLExtract
|
9 |
+
import time
|
|
|
10 |
# from joblib import dump, load
|
11 |
|
12 |
import joblib
|
|
|
229 |
["Increase", "Decrease"],
|
230 |
index=1)
|
231 |
|
232 |
+
|
233 |
def get_files_from_aws(bucket,prefix):
|
234 |
"""
|
235 |
get files from aws s3 bucket
|
|
|
254 |
|
255 |
|
256 |
if st.button('Generate Predictions'):
|
257 |
+
start_time = time.time()
|
258 |
if uploaded_file is None:
|
259 |
st.error('Please upload a email (HTML format)')
|
260 |
else:
|
|
|
273 |
|
274 |
# Dataset:
|
275 |
training_dataset = get_files_from_aws('emailcampaigntrainingdata','modelCC/training.csv')
|
276 |
+
# X_test = get_files_from_aws('emailcampaigntrainingdata','modelCC/Xtest.csv')
|
277 |
+
# Y_test = get_files_from_aws('emailcampaigntrainingdata','modelCC/ytest.csv')
|
278 |
|
279 |
+
# print("Getting Data Time: %s seconds" % (time.time() - start_time))
|
280 |
|
281 |
industry_code_dict = get_industry_code_dict(email_data)
|
282 |
bytes_data = uploaded_file.getvalue()
|
|
|
294 |
predicted_rate = model.predict(df_uploaded_test)[0]
|
295 |
output_rate = round(predicted_rate,4)
|
296 |
|
297 |
+
|
298 |
if output_rate < 0:
|
299 |
print("Sorry, Current model couldn't provide predictions on the target variable you selected.")
|
300 |
else:
|
301 |
+
st.markdown('#### Current Character Count in Your Email is: <span style="color:blue">{}</span>'.format(character_cnt), unsafe_allow_html=True)
|
302 |
# st.info('The model predicts that it achieves a {} of {}%'.format(target, str(round(output_rate*100,2))))
|
303 |
if target == 'conversion_rate':
|
304 |
target_vis = 'Conversion Rate'
|
305 |
else:
|
306 |
target_vis = 'Click-to-Open Rate'
|
307 |
+
st.markdown('#### The model predicts that it achieves a <span style="color:blue">{}</span> of <span style="color:blue">{}</span>%'.format(target_vis, str(round(output_rate*100,2))),unsafe_allow_html=True)
|
308 |
selected_industry_code = industry_code_dict.get(industry)
|
309 |
|
310 |
if target == "click_to_open_rate":
|
|
|
327 |
if preference == "Decrease":
|
328 |
df_reco_opt = df_reco[(df_reco[selected_variable] > output_rate) & (df_reco["character_cnt"] < character_cnt)]
|
329 |
df_reco_opt_rank = df_reco_opt.nlargest(3,[selected_variable])
|
330 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
331 |
|
332 |
if selected_variable == "Open_Rate":
|
333 |
selected_variable = "Click-to-Open_Rate"
|
334 |
if selected_variable == "Click_Through_Rate":
|
335 |
selected_variable = "Conversion_Rate"
|
336 |
|
337 |
+
st.markdown('#### To get higher, <span style="color:blue">{}</span>, the model recommends the following options:'.format(selected_variable),unsafe_allow_html=True)
|
338 |
if len(df_reco_opt_rank) == 0:
|
339 |
+
st.markdown('#### You ve already achieved the highest, <span style="color:blue">{}</span>, with the current character count!'.format(selected_variable),unsafe_allow_html=True)
|
340 |
else:
|
341 |
for _, row in df_reco_opt_rank.iterrows():
|
342 |
Character_Count = row[1]
|
343 |
selected_variable = row[3]
|
344 |
# print(f"·Number of Characters: {int(Character_Count)}, Target Rate: {round(selected_variable, 3)*100}", "%")
|
345 |
+
st.markdown('Number of Characters: {}, Target Rate: {}'.format(int(Character_Count), round(selected_variable*100, 3)))
|
346 |
|
347 |
|
348 |
+
placeholder.empty()
|
349 |
+
# print(time.time() - start_time)
|