File size: 9,574 Bytes
fbe47e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
df2947a
fbe47e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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()