Duy-Anh Dang commited on
Commit
86ea239
·
1 Parent(s): 33f7765

app version 1.2

Browse files
Files changed (1) hide show
  1. app.py +168 -49
app.py CHANGED
@@ -1,13 +1,45 @@
1
  from ast import arg
2
  import FunctionsModelSA_V1
 
3
  import streamlit as st
4
  import pandas as pd
5
  import PIL
6
  import time
7
- import streamlit_analytics
8
 
9
  import main_app
10
  import utils
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  def table_data():
13
  # creating table data
@@ -58,7 +90,7 @@ def add_bg_from_url():
58
 
59
  # add_bg_from_url()
60
 
61
- st.markdown("### Sentiment Analysis: Email Industry")
62
 
63
  stats_col1, stats_col2, stats_col3, stats_col4 = st.columns([1,1,1,1])
64
 
@@ -108,59 +140,146 @@ campaign_types = ['Webinar', 'Engagement', 'Product_Announcement', 'Promotional'
108
 
109
  target_variables = ['Conversion_Rate','Click_To_Open_Rate','Revenue_Per_Email']
110
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
 
 
 
 
 
 
112
 
113
- input_text = st.text_area("Please enter your email text here", height=50)
114
-
115
- with streamlit_analytics.track(unsafe_password="test123"):
116
- industry = st.selectbox(
117
- 'Please select your industry',
118
- industry_lists,
119
- index=6
120
  )
 
121
 
122
- campaign = st.selectbox(
123
- 'Please select your campaign type',
124
- campaign_types,
125
- index=5
126
  )
 
127
 
128
- target = st.selectbox(
129
- 'Please select your target variable',
130
- target_variables,
131
- index=1
132
  )
 
133
 
 
 
 
 
 
134
 
135
- if st.button('Generate Predictions'):
136
- start_time = time.time()
137
- if input_text is "":
138
- st.error('Please enter a sentence!')
139
- else:
140
- placeholder = st.empty()
141
- placeholder.text('Loading Data')
142
-
143
- # Starting predictions
144
- bucket='emailcampaignmodeldata'
145
- # file_key = 'fullEmailBody/fullemailtextbody_labeled_3rates_8tones_20220524.csv'
146
-
147
- # email_data = utils.get_files_from_aws(bucket,file_key)
148
- tone_key = 'ModelSADataSets/Tone_and_target.csv'
149
- tone_data = FunctionsModelSA_V1.load_data(bucket,tone_key)
150
- test_predictions,tones = FunctionsModelSA_V1.convert_text_to_tone(input_text)
151
-
152
- # st.dataframe(test_predictions)
153
- # st.dataframe(tones)
154
- campaign_val='campaign_type_'+campaign
155
- industry_val='industry_'+ industry
156
- pred,lower,upper,model = FunctionsModelSA_V1.prediction(tones,campaign_val,industry_val,target)
157
- best_target,best_target_tones = FunctionsModelSA_V1.find_max_cat(tone_data,target,industry_val,campaign_val)
158
-
159
- FunctionsModelSA_V1.plot_CI(pred,lower,upper,streamlit=True)
160
- if((best_target!=0) and (pred<best_target)):
161
- recommended_changes=(best_target_tones-tones.loc[0])
162
- change=best_target-pred
163
- FunctionsModelSA_V1.recommend(tones,recommended_changes,change,target,streamlit=True)
164
- FunctionsModelSA_V1.corrections(best_target_tones,test_predictions,streamlit=True)
165
-
166
- placeholder.empty()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from ast import arg
2
  import FunctionsModelSA_V1
3
+ import numpy as np
4
  import streamlit as st
5
  import pandas as pd
6
  import PIL
7
  import time
 
8
 
9
  import main_app
10
  import utils
11
+ import s3fs
12
+ from colour import Color
13
+ import plotly.graph_objects as go
14
+ from nltk import tokenize
15
+ from IPython.display import Markdown
16
+ from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
17
+ import nltk
18
+ nltk.download('punkt')
19
+ import email
20
+ import codecs
21
+ import pickle
22
+ import string
23
+ from scipy import spatial
24
+ import re
25
+ import pytorch_lightning as pl
26
+ from bs4 import BeautifulSoup
27
+ import ipywidgets as widgets
28
+ from ipywidgets import FileUpload
29
+ from urlextract import URLExtract
30
+ from transformers import BertTokenizerFast as BertTokenizer, BertModel, BertConfig
31
+ import torch.nn as nn
32
+ import torch
33
+ from ipywidgets import interact, Dropdown
34
+ import boto3
35
+ from sagemaker import get_execution_role
36
+ from scipy import spatial
37
+ from ipyfilechooser import FileChooser
38
+ import random
39
+ import streamlit_analytics
40
+
41
+ from FunctionsModelSA_V1 import *
42
+ #from model_V1 import *
43
 
44
  def table_data():
45
  # creating table data
 
90
 
91
  # add_bg_from_url()
92
 
93
+ st.markdown("#### Sentiment Analysis: Email Industry v1.2")
94
 
95
  stats_col1, stats_col2, stats_col3, stats_col4 = st.columns([1,1,1,1])
96
 
 
140
 
141
  target_variables = ['Conversion_Rate','Click_To_Open_Rate','Revenue_Per_Email']
142
 
143
+ region_variables = ['Africa', 'America', 'Asia', 'Europe', 'Oceania']
144
+
145
+ input_text = st.text_area("Please enter your email text here: ", height=50)
146
+
147
+ with st.expander('Please select your parameters: '):
148
+ with streamlit_analytics.track('test123'):
149
+ industry = st.selectbox(
150
+ 'Please select your industry',
151
+ industry_lists,
152
+ index=6
153
+ )
154
+
155
+ target = st.selectbox(
156
+ 'Please select your target variable',
157
+ target_variables,
158
+ index=1
159
+ )
160
+
161
+ campaign = st.selectbox(
162
+ 'Please select your campaign type',
163
+ campaign_types,
164
+ index=7
165
+ )
166
+
167
+ region = st.selectbox(
168
+ 'Select your target region to generate the email with a more appropriate tone:',
169
+ region_variables,
170
+ index=1
171
+ )
172
+
173
+
174
+ with st.expander('Adjust your tone intensities for your preference: '):
175
+ #'Joyful', 'Confident', 'Urgent', 'Friendly', 'Optimistic', 'Analytical', 'Casual'
176
+ joyful_tone_value = st.slider(
177
+ 'Joyful: ',
178
+ value = 0
179
+ )
180
+ st.write('Joyful: ', joyful_tone_value)
181
 
182
+ confident_tone_value = st.slider(
183
+ 'Confident: ',
184
+ value = 0
185
+ )
186
+ st.write('Confident: ', confident_tone_value)
187
 
188
+ urgent_tone_value = st.slider(
189
+ 'Urgent: ',
190
+ value = 0
 
 
 
 
191
  )
192
+ st.write('Urgent: ', urgent_tone_value)
193
 
194
+ friendly_tone_value = st.slider(
195
+ 'Friendly: ',
196
+ value = 0
 
197
  )
198
+ st.write('Friendly: ', friendly_tone_value)
199
 
200
+ optimistic_tone_value = st.slider(
201
+ 'Optimistic: ',
202
+ value = 0
 
203
  )
204
+ st.write('Optimistic: ', optimistic_tone_value)
205
 
206
+ analytical_tone_value = st.slider(
207
+ 'Analytical: ',
208
+ value = 0
209
+ )
210
+ st.write('Analytical: ', analytical_tone_value)
211
 
212
+ casual_tone_value = st.slider(
213
+ 'Casual: ',
214
+ value = 0
215
+ )
216
+ st.write('Casual: ', casual_tone_value)
217
+
218
+ Loxz_recom_box = st.checkbox('Select Loxz Recommended Tones for Optimal Output')
219
+
220
+ if st.button('Generate Predictions'):
221
+ start_time = time.time()
222
+ if input_text == "":
223
+ st.error('Please enter a sentence!')
224
+ else:
225
+ placeholder = st.empty()
226
+ placeholder.text('Loading Data...')
227
+
228
+ # Starting predictions
229
+ #bucket='emailcampaignmodeldata'
230
+ # file_key = 'fullEmailBody/fullemailtextbody_labeled_3rates_8tones_20220524.csv'
231
+
232
+ # email_data = utils.get_files_from_aws(bucket,file_key)
233
+ #tone_key = 'ModelSADataSets/Tone_and_target.csv'
234
+ #tone_data = FunctionsModelSA_V1.load_data(bucket,tone_key)
235
+ tone_data = FunctionsModelSA_V1.load_data()
236
+ test_predictions,tones = FunctionsModelSA_V1.convert_text_to_tone(input_text)
237
+
238
+ # st.dataframe(test_predictions)
239
+ # st.dataframe(tones)
240
+ campaign_val='campaign_type_'+ campaign
241
+ industry_val='industry_'+ industry
242
+ pred,lower,upper,model = FunctionsModelSA_V1.prediction(tones,campaign_val,industry_val,target)
243
+ worst_target,best_target,best_target_tones = FunctionsModelSA_V1.find_max_cat(tone_data,target,industry_val,campaign_val)
244
+ #best_target, best_target_tones
245
+ #FunctionsModelSA_V1.plot_CI(pred,lower,upper)
246
+ st.write('The Predicted Value is ' + str(pred))
247
+ fig1 = go.Figure(go.Bar(
248
+ name = 'Tone Levels',
249
+ x=[joyful_tone_value, confident_tone_value, urgent_tone_value, friendly_tone_value, optimistic_tone_value, analytical_tone_value, casual_tone_value],
250
+ y=['Joyful', 'Confident', 'Urgent', 'Friendly', 'Optimistic', 'Analytical', 'Casual'],
251
+ orientation='h')
252
+ )
253
+ st.plotly_chart(fig1, use_container_width=True)
254
+
255
+ #if((best_target!=0) and (pred<best_target)):
256
+ if Loxz_recom_box == True:
257
+ recommended_changes=(best_target_tones)
258
+ change=best_target-pred
259
+ #recommend(tones,recommended_changes,change,target)
260
+ fig2 = go.Figure()
261
+ fig2.add_trace(go.Bar(
262
+ y=tone_labels,
263
+ x=recommended_changes,
264
+ name='Recommend changes',
265
+ orientation='h',
266
+ text=np.round(recommended_changes,3),
267
+ width=.5,
268
+ marker=dict(
269
+ color='#e60f00',
270
+ line=dict(color='rgba(58, 71, 80, 1.0)', width=1)))
271
+ )
272
+ fig2.update_traces(textfont_size=18, textposition="outside", cliponaxis=False)
273
+ if target == 'Revenue_Per_Email':
274
+ out = f"${round(change,2)}"
275
+ st.write("The output will be between the range " + str(round(lower,2)) + ' and ' + str(round(upper,2)))
276
+ st.write("The Predicted "+str(target) +" is "+ str(round(pred,2)))
277
+ else:
278
+ out = f"{round(change,2)*100}%"
279
+ st.write("The output will be between the range " + str(round(lower,2) * 100) + ' and ' + str(round(upper,2) *100))
280
+ st.write("The Predicted "+str(target) +" is "+ str(round(pred,2)*100))
281
+ fig2.update_layout(title_text=f'The following Changes will yield a {out} increase in {target}')
282
+ st.plotly_chart(fig2, use_container_width=True)
283
+ #FunctionsModelSA_V1.corrections(best_target_tones,test_predictions))
284
+
285
+ placeholder.empty()