Model_SA_Space / app.py
cd14's picture
update
df2947a
from ast import arg
import FunctionsModelSA_V1
import numpy as np
import streamlit as st
import pandas as pd
import PIL
import time
import main_app
import utils
from colour import Color
import plotly.graph_objects as go
from nltk import tokenize
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
import nltk
nltk.download('punkt')
import codecs
import pickle
import string
from scipy import spatial
import pytorch_lightning as pl
from urlextract import URLExtract
from transformers import BertTokenizerFast as BertTokenizer, BertModel, BertConfig
import streamlit_analytics
from FunctionsModelSA_V1 import *
#from model_V1 import *
def table_data():
# creating table data
field = [
'Data Scientist',
'Dataset',
'Algorithm',
'Framework',
'Ensemble',
'Domain',
'Model Size'
]
data = [
'Jeffrey Ott',
'Internal + Campaign monitor',
'BERT_Uncased_L_2_H_128_A-2, Single Linear Layer Neural Network, Random Forest',
'Pytorch',
'Bootstrapping',
'NLP Text Classification',
'16.8 MB'
]
data = {
'Field':field,
'Data':data
}
df = pd.DataFrame.from_dict(data)
return df
def add_bg_from_url():
st.markdown(
f"""
<style>
.stApp {{
background-image: linear-gradient(#45eff5,#1C8D99);
background-attachment: fixed;
background-size: cover
}}
</style>
""",
unsafe_allow_html=True
)
add_bg_from_url()
#linear-gradient(#0A3144,#126072,#1C8D99);
#add whitespace to fix scroll
def WHITE_SPACE(lines):
for x in range(lines):
st.write('&nbsp;')
with st.sidebar:
with st.expander('Model Description', expanded=False):
img = PIL.Image.open("figures/ModelSA.png")
st.image(img)
st.markdown('The model seeks to solve the problem of how to set the tone for an email campaign appropriately. This 5th generation model from the Loxz family uses state-of-the-art NLP to determine and predict the optimized sentiment of an email using tokenization techniques. The model will analyze any email text “shape” and help the user understand the tone and how that tone correlates with the metric of interest. We applied a pre-trained tiny BERT model to vectorize the email campaign text body, then a softmax dense layer was added to get the multi-label classifications. Email metrics are provided prior to campaign launch, and the model determines the optimal engagement rate based on several factors, including inputs by the campaign engineer.')
with st.expander('Model Information', expanded=False):
hide_table_row_index = """
<style>
thead tr th:first-child {display:none}
tbody th {display:none}
</style>
"""
st.markdown(hide_table_row_index, unsafe_allow_html=True)
st.table(table_data())
utils.url_button('Model Homepage','https://loxz.com/#/models/SA')
# url_button('Full Report','https://resources.loxz.com/reports/realtime-ml-character-count-model')
utils.url_button('Amazon Market Place','https://aws.amazon.com/marketplace')
WHITE_SPACE(15)
st.caption("All models owned by Loxz Digital")
industry_lists = ['Software and Technology', 'Academic and Education',
'Entertainment', 'Finance and Banking', 'Hospitality',
'Real Estate', 'Retail', 'Energy', 'Healthcare']
campaign_types = ['Webinar', 'Engagement', 'Product_Announcement', 'Promotional',
'Newsletter', 'Abandoned_Cart', 'Review_Request', 'Survey',
'Transactional', 'Usage_and_Consumption']
target_variables = ['Conversion_Rate','Click_To_Open_Rate','Revenue_Per_Email']
region_variables = ['Africa', 'America', 'Asia', 'Europe', 'Oceania']
st.markdown("#### Sentiment Analysis: Email Industry v1.2")
stats_col1, stats_col2, stats_col3, stats_col4 = st.columns([1,1,1,1])
with stats_col1:
st.caption("Verified: Production")
#st.metric(label="Verified", value= "Production")
with stats_col2:
st.caption("Accuracy: 85%")
#st.metric(label="Accuracy", value= "85%")
with stats_col3:
st.caption("Speed: 3.86 ms")
#st.metric(label="Speed", value="3.86 ms")
with stats_col4:
st.caption("Industry: Email")
#st.metric(label="Industry", value="Email")
input_text = st.text_area("Please enter your email text here: ", height=50)
with st.expander('Please select your parameters: '):
with streamlit_analytics.track('test123'):
industry = st.selectbox(
'Please select your industry',
industry_lists,
index=6
)
target = st.selectbox(
'Please select your target variable',
target_variables,
index=1
)
campaign = st.selectbox(
'Please select your campaign type',
campaign_types,
index=7
)
region = st.selectbox(
'Select your target region to generate the email with a more appropriate tone:',
region_variables,
index=1
)
with st.expander('Adjust your tone intensities for your preference: '):
#'Joyful', 'Confident', 'Urgent', 'Friendly', 'Optimistic', 'Analytical', 'Casual'
joyful_tone_value = st.slider(
'Joyful: ',
value = 0
)
st.write('Joyful: ', joyful_tone_value)
confident_tone_value = st.slider(
'Confident: ',
value = 0
)
st.write('Confident: ', confident_tone_value)
urgent_tone_value = st.slider(
'Urgent: ',
value = 0
)
st.write('Urgent: ', urgent_tone_value)
friendly_tone_value = st.slider(
'Friendly: ',
value = 0
)
st.write('Friendly: ', friendly_tone_value)
optimistic_tone_value = st.slider(
'Optimistic: ',
value = 0
)
st.write('Optimistic: ', optimistic_tone_value)
analytical_tone_value = st.slider(
'Analytical: ',
value = 0
)
st.write('Analytical: ', analytical_tone_value)
casual_tone_value = st.slider(
'Casual: ',
value = 0
)
st.write('Casual: ', casual_tone_value)
Loxz_recom_box = st.checkbox('Select Loxz Recommended Tones for Optimal Output')
def create_charts():
# Starting predictions
bucket='emailcampaignmodeldata'
# file_key = 'fullEmailBody/fullemailtextbody_labeled_3rates_8tones_20220524.csv'
# email_data = utils.get_files_from_aws(bucket,file_key)
tone_key = 'ModelSADataSets/Tone_and_target.csv'
tone_data = FunctionsModelSA_V1.load_data()
test_predictions,tones = FunctionsModelSA_V1.convert_text_to_tone(input_text)
# st.dataframe(test_predictions)
# st.dataframe(tones)
campaign_val='campaign_type_'+ campaign
industry_val='industry_'+ industry
pred,lower,upper,model = FunctionsModelSA_V1.prediction(tones,campaign_val,industry_val,target)
worst_target,best_target,best_target_tones = FunctionsModelSA_V1.find_max_cat(tone_data,target,industry_val,campaign_val)
#best_target, best_target_tones
#FunctionsModelSA_V1.plot_CI(pred,lower,upper)
st.write('The Predicted Value is ' + str(pred))
st.write('The best_target_tones Value is ' + str(best_target_tones))
fig1 = go.Figure(go.Bar(
name = 'Tone Levels',
x=[joyful_tone_value, confident_tone_value, urgent_tone_value, friendly_tone_value, optimistic_tone_value, analytical_tone_value, casual_tone_value],
y=['Joyful', 'Confident', 'Urgent', 'Friendly', 'Optimistic', 'Analytical', 'Casual'],
orientation='h')
)
st.plotly_chart(fig1, use_container_width=True)
#if((best_target!=0) and (pred<best_target)):
if Loxz_recom_box == True:
recommended_changes=(best_target_tones)
change=best_target-pred
#recommend(tones,recommended_changes,change,target)
fig2 = go.Figure()
fig2.add_trace(go.Bar(
y=tone_labels,
x=recommended_changes,
name='Recommend changes',
orientation='h',
text=np.round(recommended_changes,3),
width=.5,
marker=dict(
color='#e60f00',
line=dict(color='rgba(58, 71, 80, 1.0)', width=1)))
)
fig2.update_traces(textfont_size=18, textposition="outside", cliponaxis=False)
if target == 'Revenue_Per_Email':
out = f"${round(change,2)}"
st.write("The output will be between the range " + str(round(lower,2)) + ' and ' + str(round(upper,2)))
st.write("The Predicted "+str(target) +" is "+ str(round(pred,2)))
else:
out = f"{round(change,2)*100}%"
st.write("The output will be between the range " + str(round(lower,2) * 100) + ' and ' + str(round(upper,2) *100))
st.write("The Predicted "+str(target) +" is "+ str(round(pred,2)*100))
fig2.update_layout(title_text=f'The following Changes will yield a {out} increase in {target}')
st.plotly_chart(fig2, use_container_width=True)
#FunctionsModelSA_V1.corrections(best_target_tones,test_predictions))
if st.button('Generate Predictions'):
start_time = time.time()
if input_text == "":
st.error('Please enter a sentence!')
else:
placeholder = st.empty()
placeholder.text('Loading Data...')
create_charts()
placeholder.empty()