amoldwalunj commited on
Commit
b9fb5ed
·
1 Parent(s): 682cf28

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +846 -0
app.py ADDED
@@ -0,0 +1,846 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import re
3
+ import gspread
4
+ from gspread_dataframe import get_as_dataframe, set_with_dataframe
5
+
6
+ import pandas as pd
7
+ import os
8
+ import openai
9
+ import time
10
+
11
+
12
+ os.environ["OPENAI_API_KEY"] = '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'
13
+ openai.api_key = os.getenv("OPENAI_API_KEY")
14
+
15
+
16
+
17
+ # Initialize session state
18
+ if 'df_final_output' not in st.session_state:
19
+ st.session_state['df_final_output'] = None
20
+
21
+ # def get_chatgpt_response(messages):
22
+ # response = openai.ChatCompletion.create(
23
+ # model="gpt-3.5-turbo",
24
+ # messages=messages
25
+ # )
26
+ # return response['choices'][0]['message']['content']
27
+
28
+ def get_chatgpt_response(messages, selected_model):
29
+ response = openai.ChatCompletion.create(
30
+ model=selected_model,
31
+ messages=messages
32
+ )
33
+ return response['choices'][0]['message']['content']
34
+
35
+ # Define pages in sidebar
36
+ page = st.sidebar.radio('Select a page:', ('Review Analysis', 'Feature Benefits', 'Identify Avatars', 'Tone of Voice Manual'))
37
+
38
+
39
+ if page == 'Review Analysis':
40
+ st.title('Review Analysis Page')
41
+
42
+ # Streamlit settings
43
+ #st.title('Customer Reviews Analysis')
44
+
45
+ # Authenticate Google Sheets API
46
+ gc = gspread.service_account(filename='arctic-rite-381810-e8bee8664772.json')
47
+
48
+ # Ask user for Google Sheet URL
49
+ sheet_url = st.text_input('Enter the URL of your Google Sheet')
50
+
51
+ # Add this in the part of your Streamlit app where the user can select the model
52
+ models = ["gpt-3.5-turbo", "gpt-4"]
53
+ selected_model = st.selectbox("Choose a model:", models)
54
+
55
+ # User input for prompts
56
+ phase1_prompt = st.text_area("Enter your prompt for Phase 1- wherever you see '{}', we are passing reviews at that place", value="Analyze a set of customer reviews and identify the unique pros and cons and their themes mentioned by customers. for each pros and cons also mention unique theme associated for that pro/cons. Your response should have format like this.\n\nPros:\n\n1. Theme1 (please replace theme identified here): explanaition\n2. Theme2 (please replace theme identified here): explaination\n\ncons:\n1. Theme1: explanaition\n2. Theme2: explaination\n\nIn your analysis, consider the language and tone used in the reviews to accurately represent the customer's experience. here are the reviews\n {}")
57
+ phase2_prompt = st.text_area("Enter your prompt for Phase 2- wherever you see '{}', we are passing reviews at that place", value="Analyze a set of customer reviews pros/cons and identify the only unique pros and cons and their themes mentioned by customers. for each pros and cons also mention unique theme associated for that pro/cons. Your response should have format like this.\n\nPros:\n\n1. Theme1 (please replace theme identified here): explanaition\n2. Theme2 (please replace theme identified here): explaination\n\ncons:\n1. Theme1: explanaition\n2. Theme2: explaination\n\nIn your analysis, consider the language and tone used in the reviews to accurately represent the customer's experience. here are the reviews\n {}")
58
+ final_prompt = st.text_area("Enter your final prompt", value="please analyze below reviews and give me unique best 20(twenty) pros and cons mentioned. Your response should be in same format in 2 sections. one for pros and other for cons. e.g. Pros:\n1. Quality: explaination, 2. 2nd theme: explaination. \n\n {}\n\nPlease note, response should have 2 sections, one for pros and one for cons. All the unique pros should be in pros section and same should be done for cons. Response format: Pros:\n1. Quality: explaination, 2. 2nd theme: explaination. Make sure your response is long and cover all points (at least top 20 pros and cons)")
59
+
60
+ # If the user has input a URL, fetch the data from Google Sheets
61
+ if st.button('generate'):
62
+ #if sheet_url:
63
+ # Extract sheet ID from URL
64
+ sheet_id = sheet_url.split('/')[5]
65
+ # Open the Google spreadsheet
66
+ sheet = gc.open_by_key(sheet_id)
67
+
68
+ #st.session_state['sheet'] = sheet
69
+ # Select sheet named 'Reviews'
70
+ worksheet = sheet.worksheet('Reviews')
71
+ # Get all records of the data
72
+ df = get_as_dataframe(worksheet)
73
+ # Convert dataframe to list and drop NaNs
74
+ df['Body']= df['Body'].astype(str)
75
+ # drop rows where 'Body' column has NaN
76
+ df = df.dropna(subset=['Body'])
77
+
78
+ ####first chunking
79
+
80
+ separator = '\n'
81
+ char_limit = 9000
82
+
83
+ top_reviews = []
84
+
85
+ start_index = 0
86
+ chunk_count = 1
87
+ while start_index < len(df):
88
+ char_count = 0
89
+ end_index = start_index
90
+
91
+ # Get the reviews within the character limit
92
+ while end_index < len(df) and char_count + len(df['Body'][end_index]) <= char_limit:
93
+ char_count += len(df['Body'][end_index])
94
+ end_index += 1
95
+
96
+ chunk_reviews = df['Body'][start_index:end_index]
97
+ reviews_string = separator.join(chunk_reviews)
98
+
99
+ num_reviews = len(chunk_reviews)
100
+ print(f'Chunk {chunk_count}:')
101
+ print(f'Number of reviews: {num_reviews}')
102
+ print(f'Character length: {len(reviews_string)}')
103
+
104
+ # For the first phase, replace the hardcoded prompt with the user's input
105
+ messages = [
106
+ {"role": "system", "content": "You are helpful assistant"},
107
+ {"role": "user", "content": phase1_prompt.format(reviews_string)}
108
+ ]
109
+
110
+ model_response = get_chatgpt_response(messages, selected_model)
111
+
112
+ top_reviews.append(model_response)
113
+
114
+ start_index = end_index
115
+ chunk_count += 1
116
+
117
+
118
+ # After the first chunking phase, you create reviews_string_1
119
+ separator = "\n" + "-" * 80 + "\n"
120
+ reviews_string_1 = separator.join(top_reviews)
121
+
122
+ print(len(reviews_string_1))
123
+
124
+ # Now, we start a loop to repeatedly perform the second chunking phase
125
+ while len(reviews_string_1) > 11000:
126
+ dfn= pd.DataFrame(top_reviews, columns=['pros/cons']) #convert top_reviews in df
127
+
128
+ separator = '\n'
129
+ char_limit = 9000
130
+
131
+ top_reviews = []
132
+
133
+ start_index = 0
134
+ chunk_count = 1
135
+ while start_index < len(dfn):
136
+ char_count = 0
137
+ end_index = start_index
138
+
139
+ # Get the reviews within the character limit
140
+ while end_index < len(dfn) and char_count + len(dfn['pros/cons'][end_index]) <= char_limit:
141
+ char_count += len(dfn['pros/cons'][end_index])
142
+ end_index += 1
143
+
144
+ chunk_reviews = dfn['pros/cons'][start_index:end_index]
145
+ reviews_string = separator.join(chunk_reviews)
146
+
147
+ num_reviews = len(chunk_reviews)
148
+ print(f'Chunk {chunk_count}:')
149
+ print(f'Number of reviews: {num_reviews}')
150
+ print(f'Character length: {len(reviews_string)}')
151
+ # For the second phase, replace the hardcoded prompt with the user's input
152
+ messages = [
153
+ {"role": "system", "content": "You are helpful assistant"},
154
+ {"role": "user", "content": phase2_prompt.format(reviews_string)}
155
+ ]
156
+
157
+ try:
158
+ model_response = get_chatgpt_response(messages, selected_model)
159
+ top_reviews.append(model_response)
160
+ except Exception as e:
161
+ if e.__class__ =="RateLimitError":
162
+ print("here")
163
+ time.sleep(30)
164
+ model_response = get_chatgpt_response(messages, selected_model)
165
+
166
+ start_index = end_index
167
+ chunk_count += 1
168
+
169
+ # After the second chunking phase, you again create reviews_string_1
170
+ separator = "\n" + "-" * 80 + "\n"
171
+ reviews_string_1 = separator.join(top_reviews)
172
+
173
+ # [...]
174
+ # The code continues as is until the final message
175
+
176
+ # For the final message, replace the hardcoded prompt with the user's input
177
+ messages = [
178
+ {"role": "system", "content": "You are helpful assistant who analyzes reviews and gives top 20 pros and cons from them"},
179
+ {"role": "user", "content": final_prompt.format(reviews_string_1)}
180
+ ]
181
+
182
+ final_output = get_chatgpt_response(messages, selected_model)
183
+
184
+ # Display the output
185
+ st.text('Here are the top pros and cons:')
186
+ st.write(final_output)
187
+
188
+ st.session_state.df_final_output = final_output
189
+
190
+ # Create a button that will trigger writing to Google Sheets
191
+ if st.button('Write output to sheet'):
192
+ #if st.button('Write output to sheet'):
193
+ #st.session_state.df_final_output = final_output
194
+
195
+ sheet_id = sheet_url.split('/')[5]
196
+ # Open the Google spreadsheet
197
+ sheet = gc.open_by_key(sheet_id)
198
+
199
+ final_output= st.session_state.df_final_output
200
+
201
+ st.write("Button clicked, processing data...")
202
+
203
+ lines = final_output.split('\n')
204
+ # Split each line into theme and description
205
+ split_lines = []
206
+ for line in lines:
207
+ if "Pros:" in line:
208
+ type_ = "Pros"
209
+ elif "Cons:" in line:
210
+ type_ = "Cons"
211
+ elif ": " in line:
212
+ theme, description = re.split(r': ', line, 1)
213
+ split_lines.append([type_, theme, description])
214
+
215
+ # Convert the list of lists into a DataFrame
216
+ df_final_output = pd.DataFrame(split_lines, columns=['Type', 'Theme', 'Description'])
217
+
218
+ st.write(df_final_output)
219
+
220
+ # Create a new worksheet and write the final output to it
221
+ # worksheet_output = sheet.add_worksheet(title="top_pros_cons", rows="100", cols="20")
222
+ # set_with_dataframe(worksheet_output, df_final_output)
223
+
224
+ # # Then, outside of your if statement for the button click, check if df_final_output exists in the session state
225
+ # if st.session_state.df_final_output is not None:
226
+
227
+ try:
228
+ worksheet_output = sheet.add_worksheet(title="top_pros_cons", rows="100", cols="20")
229
+ set_with_dataframe(worksheet_output, df_final_output)
230
+ st.write("Data written successfully to Google Sheets.")
231
+ except Exception as e:
232
+ st.error(f"An error occurred while writing to Google Sheets: {e}")
233
+
234
+ # # Wrap your button in a form
235
+ # with st.form(key='output_form'):
236
+ # # Change your button click event to set a session state variable
237
+ # if st.form_submit_button('Write output to sheet'):
238
+ # # Rest of the code to populate df_final_output
239
+ # st.session_state.df_final_output = df_final_output
240
+
241
+ # # Then, outside of your form, check if df_final_output exists in the session state
242
+ # if st.session_state.df_final_output is not None:
243
+ # try:
244
+ # worksheet_output = sheet.add_worksheet(title="top_pros_cons", rows="100", cols="20")
245
+ # set_with_dataframe(worksheet_output, st.session_state.df_final_output)
246
+ # st.write("Data written successfully to Google Sheets.")
247
+ # except Exception as e:
248
+ # st.error(f"An error occurred while writing to Google Sheets: {e}")
249
+
250
+
251
+ if page == 'Feature Benefits':
252
+ st.title('Feature Benefits Page')
253
+
254
+ # Authenticate Google Sheets API
255
+ gc = gspread.service_account(filename='arctic-rite-381810-e8bee8664772.json')
256
+
257
+ # Ask user for Google Sheet URL
258
+ sheet_url = st.text_input('Enter the URL of your Google Sheet')
259
+
260
+ # # Display default prompt to user
261
+ # default_prompt = """Using below information of the listing, please extract the top 30 Features and Benefits:
262
+ # Title: {}\n
263
+ # bullet_str: {}\n
264
+ # Legacy Product Description: {}\n
265
+ # A+ Description: {}\n
266
+ # Image_text: {}\n
267
+ # backend_search_terms: {}\n
268
+
269
+ # Please note, you need to create comprehensive top 30 Features and top 30 Benefits for listing using this information. Your response should have 2 sections. one for features and other for benefits
270
+ # """.format(title, bullet_str, legacy_desc, a_plus_desc, image_text_string, backend_search_terms)
271
+
272
+ # prompt = st.text_area("Edit the prompt:", value=default_prompt, height=200)
273
+
274
+ # Add this in the part of your Streamlit app where the user can select the model
275
+ models = ["gpt-3.5-turbo", "gpt-4"]
276
+ selected_model = st.selectbox("Choose a model:", models)
277
+
278
+ # Extract sheet ID from URL
279
+ sheet_id = sheet_url.split('/')[5]
280
+ # Open the Google spreadsheet
281
+ sheet = gc.open_by_key(sheet_id)
282
+
283
+ # Select sheet named 'Listing'
284
+ worksheet = sheet.worksheet('Listing')
285
+ # Get all records of the data
286
+ df = get_as_dataframe(worksheet)
287
+
288
+ # Convert DataFrame to strings
289
+ df_str = df.astype(str)
290
+
291
+ title = df[df.eq('Title').any(axis=1)].iloc[0, 2]
292
+ bullets = [df[df.eq(f"Bullet #{i}").any(axis=1)].iloc[0, 2] for i in range(1, 6)]
293
+
294
+ backend_search_terms = df[df.astype(str).apply(lambda x: 'Backend Search Terms' in ' '.join(x), axis=1)].iloc[0, 2] if len(df[df.astype(str).apply(lambda x: 'Backend Search Terms' in ' '.join(x), axis=1)]) > 0 else None
295
+
296
+ image_text_row = df[df.eq('Image Text').any(axis=1)]
297
+ image_text = list(image_text_row.dropna(axis=1).iloc[0, :])
298
+
299
+ a_plus_desc_mask = df.astype(str).apply(lambda x: 'A+ Description' in ' '.join(x), axis=1)
300
+ if a_plus_desc_mask.any():
301
+ a_plus_desc_row = df[a_plus_desc_mask].index[0]
302
+ a_plus_desc = df.iloc[a_plus_desc_row:, :].fillna('').values.flatten()
303
+ a_plus_desc = ' '.join(a_plus_desc).strip()
304
+ else:
305
+ a_plus_desc = None
306
+
307
+ legacy_desc = df[df.astype(str).apply(lambda x: 'Legacy Product Description' in ' '.join(x), axis=1)].iloc[0, 2] if len(df[df.astype(str).apply(lambda x: 'Legacy Product Description' in ' '.join(x), axis=1)]) > 0 else None
308
+
309
+ image_text_string = ' '.join(image_text)
310
+
311
+ bullet_str = ""
312
+ for i, bullet in enumerate(bullets, 1):
313
+ bullet_str += f"Bullet #{i}: {bullet}\n"
314
+
315
+ # Display default prompt to user
316
+ default_prompt = """Using below information of the listing, please extract the top 30 Features and Benefits:
317
+ ------------------------------------
318
+ Title: {}\n
319
+ bullet_str: {}\n
320
+ Legacy Product Description: {}\n
321
+ A+ Description: {}\n
322
+ Image_text: {}\n
323
+ backend_search_terms: {}\n
324
+ -------------------------------------
325
+
326
+ Please note, you need to create comprehensive top 30 Features and top 30 Benefits for listing using this information. Your response should have 2 sections, one for features and other for benefits
327
+ """.format(title, bullet_str, legacy_desc, a_plus_desc, image_text_string, backend_search_terms)
328
+
329
+ prompt = st.text_area("Edit the prompt:", value=default_prompt, height=200)
330
+
331
+ #st.write(prompt)
332
+
333
+ if st.button('generate'):
334
+ #st.write(prompt)
335
+
336
+ messages = [
337
+ {"role": "system", "content": "You are helpful assistant who help create features and benefits for amazon listings"},
338
+ {"role": "user", "content": prompt}
339
+ ]
340
+
341
+
342
+ # messages = [
343
+ # {"role": "system", "content": "You are helpful assistant who help create features and benefits for amazon listings"},
344
+ # {"role": "user", "content": """Using below information of the listing, please extract the top 30 Features and Benefits:
345
+ # Title: {}\n
346
+ # bullet_str: {}\n
347
+ # Legacy Product Description: {}\n
348
+ # A+ Description: {}\n
349
+ # Image_text: {}\n
350
+ # backend_search_terms: {}\n
351
+
352
+ # Please note, you need to create comprehensive top 30 Features and top 30 Benefits for listing using this information. Your response should have 2 sections. one for features and other for benefits
353
+ # """.format(title, bullet_str, legacy_desc, a_plus_desc,image_text_string, backend_search_terms)}
354
+ # ]
355
+
356
+ model_response = get_chatgpt_response(messages, selected_model)
357
+
358
+ #st.write(model_response)
359
+
360
+ # messages = [
361
+ # {"role": "system", "content": "You are helpful assistant who help create features and benefits for amazon listings"},
362
+ # {"role": "user", "content": prompt}
363
+ # ]
364
+
365
+ messages1 = [
366
+ {"role": "system", "content": "You are helpful assistant who help create features and benefits for amazon listings"},
367
+ {"role": "user", "content": """{}
368
+
369
+ Please re-write above features and benefits in same way in 2 sections.You need to add short heading/theme of 1-3 words for each feature benefit.
370
+ Your response should have 2 sections. one for features and other for benefits
371
+ e.g features: 1. 100% cotton (this is theme that you will add if not available): 100% cotton cold press paper ensures exceptional quality and strength (this is exisitng description) 2. .....
372
+ Benefits:
373
+ 1.Superior Watercolor Surface (this is theme that you will add if not available): Provides a high-quality surface for watercolor painting, with a unique texture that allows for even spreading and vivid colors.(this is exisitng description) 2. ...
374
+ please note, you should not miss any feature benefits. all 30 should be covered with short heading.""".format(model_response)}
375
+ ]
376
+
377
+ final_model_response = get_chatgpt_response(messages1, selected_model)
378
+
379
+ feature_benefits=final_model_response
380
+
381
+ # Display the output
382
+ # Display the output
383
+ st.text('Here are the top features and benefits:')
384
+ st.write(feature_benefits)
385
+
386
+ # Store the result in the session state
387
+ st.session_state.feature_benefits = feature_benefits
388
+
389
+ # Create a button that will trigger writing to Google Sheets
390
+ if st.button('Write features and benefits to sheet'):
391
+ sheet_id = sheet_url.split('/')[5] # Open the Google spreadsheet
392
+ sheet = gc.open_by_key(sheet_id)
393
+
394
+ # Retrieve the result from the session state
395
+ feature_benefits = st.session_state.feature_benefits
396
+
397
+ st.write("Button clicked, processing data...")
398
+
399
+ lines = feature_benefits.split('\n')
400
+
401
+ # # Split each line into feature/benefit and description
402
+ # split_lines = []
403
+ # for line in lines:
404
+ # if "Features:" in line:
405
+ # type_ = "Features"
406
+ # elif "Benefits:" in line:
407
+ # type_ = "Benefits"
408
+ # elif ": " in line:
409
+ # theme, description = re.split(r': ', line, 1)
410
+ # split_lines.append([type_, theme, description])
411
+
412
+ # # Convert the list of lists into a DataFrame
413
+ # df_feature_benefits = pd.DataFrame(split_lines, columns=['Type', 'Theme', 'Description'])
414
+
415
+ # Split each line into feature/benefit and description
416
+ # Split each line into feature/benefit and description
417
+ split_lines = []
418
+ for line in lines:
419
+ if "Features:" in line:
420
+ type_ = "Features"
421
+ elif "Benefits:" in line:
422
+ type_ = "Benefits"
423
+ elif line.strip() == '':
424
+ continue # skip empty lines
425
+ else:
426
+ if ": " in line: # If theme is present
427
+ theme, description = re.split(r': ', line, 1)
428
+ else: # If theme is not present
429
+ theme = ''
430
+ description = line.strip()
431
+
432
+ split_lines.append([type_, theme, description])
433
+
434
+ # Convert the list of lists into a DataFrame
435
+ df_feature_benefits = pd.DataFrame(split_lines, columns=['Type', 'Theme', 'Description'])
436
+
437
+
438
+
439
+ st.write(df_feature_benefits)
440
+
441
+ # Create a new worksheet and write the final output to it
442
+ try:
443
+ worksheet_output = sheet.add_worksheet(title="top_features_benefits", rows="100", cols="20")
444
+ set_with_dataframe(worksheet_output, df_feature_benefits)
445
+ st.write("Data written successfully to Google Sheets.")
446
+ except Exception as e:
447
+ st.error(f"An error occurred while writing to Google Sheets: {e}")
448
+
449
+
450
+ if page == 'Identify Avatars':
451
+ st.title('Identify Avatars Page')
452
+
453
+ # Authenticate Google Sheets API
454
+ gc = gspread.service_account(filename='arctic-rite-381810-e8bee8664772.json')
455
+
456
+
457
+
458
+ # Ask user for Google Sheet URL
459
+ sheet_url = st.text_input('Enter the URL of your Google Sheet')
460
+
461
+ # Extract sheet ID from URL
462
+ sheet_id = sheet_url.split('/')[5]
463
+ # Open the Google spreadsheet
464
+ sheet = gc.open_by_key(sheet_id)
465
+
466
+ # Add this in the part of your Streamlit app where the user can select the model
467
+ models = ["gpt-3.5-turbo", "gpt-4"]
468
+ selected_model = st.selectbox("Choose a model:", models)
469
+
470
+
471
+ # Let user choose between pros/cons and reviews
472
+ user_choice = st.selectbox("Select input for avatars:", ('Top Pros/Cons', 'Reviews'))
473
+
474
+
475
+ if user_choice == 'Top Pros/Cons':
476
+ worksheet = sheet.worksheet('top_pros_cons')
477
+
478
+ #st.write("Worksheet:", worksheet)
479
+ # df_pros_cons = get_as_dataframe(worksheet).dropna()
480
+
481
+ data = worksheet.get_all_records()
482
+ df_pros_cons = pd.DataFrame(data)
483
+ df_pros_cons.dropna(how='all', inplace=True)
484
+
485
+ #st.write(df_pros_cons)
486
+
487
+ # Filter pros and cons separately
488
+ pros_df = df_pros_cons[df_pros_cons['Type'] == 'Pros']
489
+ cons_df = df_pros_cons[df_pros_cons['Type'] == 'Cons']
490
+
491
+ # Initialize pros and cons strings
492
+ pros_str = "Pros:\n"
493
+ cons_str = "Cons:\n"
494
+
495
+ # Iterate through pros DataFrame and append Theme and Description
496
+ for _, row in pros_df.iterrows():
497
+ theme = row['Theme']
498
+ description = row['Description']
499
+ pros_str += f"{theme}: {description}\n"
500
+
501
+ # Iterate through cons DataFrame and append Theme and Description
502
+ for _, row in cons_df.iterrows():
503
+ theme = row['Theme']
504
+ description = row['Description']
505
+ cons_str += f"{theme}: {description}\n"
506
+
507
+ # Combine pros and cons into a single string
508
+ pros_and_cons = pros_str + "\n" + cons_str
509
+
510
+ #st.write(pros_and_cons)
511
+
512
+ elif user_choice == 'Reviews':
513
+ worksheet = sheet.worksheet('Reviews')
514
+ # Get all records of the data
515
+ df = get_as_dataframe(worksheet)
516
+
517
+ #st.write(df)
518
+
519
+ # df['Body']= df['Body'].astype(str)
520
+ # # drop rows where 'Body' column has NaN
521
+ # df = df.dropna(subset=['Body'])
522
+
523
+ # ####first chunking
524
+
525
+ # separator = '\n\n'
526
+
527
+ # pros_and_cons= separator.join(df['Body'])
528
+
529
+ # # df['Body']= df['Body'].astype(str)
530
+ # # # drop rows where 'Body' column has NaN
531
+ # # df = df.dropna(subset=['Body'])
532
+
533
+ # # Clean up data
534
+ # pros_and_cons = df['Body'].str.cat(sep='\n\n')
535
+
536
+ # Select only the 'Body' column and drop rows with NaN values
537
+ body_series = df['Body'].dropna()
538
+
539
+ #### first chunking
540
+ separator = '\n\n'
541
+ pros_and_cons = separator.join(body_series)
542
+
543
+ # Clean up data
544
+ pros_and_cons = body_series.str.cat(sep='\n\n')
545
+
546
+
547
+
548
+
549
+
550
+ # Get all records of the data
551
+ #df = get_as_dataframe(worksheet)
552
+
553
+
554
+
555
+ # Display pros and cons or reviews to the user
556
+ #st.text('Selected pros and cons or reviews:')
557
+ #st.write(pros_and_cons)
558
+
559
+ # Prepare the prompt
560
+ prompt = """Below are the pros and cons we have identified from customer reviews for our product:
561
+
562
+ -----------------------------------------------
563
+ {}
564
+
565
+ -----------------------------------------------
566
+
567
+
568
+ You need to use above information and Write a list of the top 5 Avatars (persona) that you identify from the product reviews of the listing.
569
+ For each avatar pls specify the following:
570
+
571
+ - Name
572
+ - Gender
573
+ - Age range
574
+ - Income range
575
+ - Demographics
576
+ - General description of the avatar
577
+ - Personality Traits
578
+ - Interests and Hobbies
579
+ - Pains (Psychological, Spiritual, Physical)
580
+ - Top 10 insecurities
581
+ - Top 10 books that they read with title and author
582
+ - Top 5 movies
583
+ - Top 5 TV shows
584
+ - List their top 10 desires
585
+ - List the social media they use and channels they follow
586
+ - List top 10 lead magnets used for marketing purpose
587
+
588
+ And anything else can be used for sales and marketing purpose to target those specific avatars. Please make sure you provide long info for each of the point you mention in avatar""".format(pros_and_cons)
589
+
590
+ # Editable prompt
591
+ prompt = st.text_area("Edit the prompt:", value=prompt, height=200)
592
+
593
+ if st.button('Generate avatars'):
594
+ messages = [
595
+ {"role": "system", "content": "You are a helpful assistant who helps identify avatars from product reviews"},
596
+ {"role": "user", "content": prompt}
597
+ ]
598
+
599
+ # Get model response
600
+ model_response = get_chatgpt_response(messages, selected_model)
601
+
602
+ # Display the output
603
+ st.text('Here are the top 5 Avatars:')
604
+ st.write(model_response)
605
+
606
+ # Store the result in the session state
607
+ st.session_state.avatars = model_response
608
+
609
+ # if "generate_more" not in st.session_state:
610
+ # st.session_state.generate_more = False
611
+
612
+ # if st.button('Generate more avatars'):
613
+ # st.session_state.generate_more = True
614
+
615
+
616
+ # if st.session_state.generate_more:
617
+
618
+ # messages1 = [
619
+ # {"role": "system", "content": "You are a helpful assistant who helps identify avatars from product reviews"},
620
+ # {"role": "user", "content": """Please summarize these avatars.
621
+ # mention their income in summary. /n {}""".format(model_response)}
622
+ # ]
623
+
624
+ # model_response1 = get_chatgpt_response(messages1, selected_model)
625
+
626
+ # prompt2 = """Below are the pros and cons we have identified from customer reviews for our product:
627
+
628
+ # -----------------------------------------------
629
+ # {}
630
+
631
+ # -----------------------------------------------
632
+ # please exclude these avatar as we already have them. this is summary of earlier avatars
633
+
634
+ # {}
635
+ # -----------------------------------------------
636
+ # You need to use above information and Write a list of the top 5 Avatars (persona) that you identify from the product reviews of the listing.
637
+ # For each avatar pls specify the following:
638
+
639
+ # - Name
640
+ # - Gender
641
+ # - Age range
642
+ # - Income range
643
+ # - Demographics
644
+ # - General description of the avatar
645
+ # - Personality Traits
646
+ # - Interests and Hobbies
647
+ # - Pains (Psychological, Spiritual, Physical)
648
+ # - Top 10 insecurities
649
+ # - Top 10 books that they read with title and author
650
+ # - Top 5 movies
651
+ # - Top 5 TV shows
652
+ # - List their top 10 desires
653
+ # - List the social media they use and channels they follow
654
+ # - List top 10 lead magnets used for marketing purpose
655
+
656
+ # And anything else can be used for sales and marketing purpose to target those specific avatars. Please make sure you provide long info for each of the point you mention in avatar""".format(pros_and_cons, model_response1)
657
+
658
+ # # Editable prompt
659
+ # prompt2 = st.text_area("Edit the prompt:", value=prompt2, height=200)
660
+
661
+ # if st.button('Generate additional avatars'):
662
+ # st.session_state.more_avatars_generated = True
663
+
664
+ # messages2 = [
665
+ # {"role": "system", "content": "You are a helpful assistant who helps identify avatars from product reviews"},
666
+ # {"role": "user", "content": prompt2}
667
+ # ]
668
+
669
+ # # Get model response
670
+ # model_response2 = get_chatgpt_response(messages2, selected_model)
671
+
672
+ # final_avatars= model_response + '\n\n' + model_response2
673
+
674
+ # st.write(final_avatars)
675
+
676
+
677
+ # st.session_state.avatars = final_avatars
678
+
679
+ # else:
680
+ # st.write(st.session_state.avatars)
681
+
682
+
683
+
684
+ # Create a button that will trigger writing to Google Sheets
685
+ if st.button('Write avatars to sheet'):
686
+ sheet_id = sheet_url.split('/')[5] # Open the Google spreadsheet
687
+ sheet = gc.open_by_key(sheet_id)
688
+
689
+ # Retrieve the result from the session state
690
+ avatars = st.session_state.avatars
691
+
692
+ st.write("Button clicked, processing data...")
693
+
694
+ # Create a new worksheet and write the final output to it
695
+
696
+ try:
697
+ # Create a new worksheet named "avatars"
698
+ worksheet_output = sheet.add_worksheet(title="avatars", rows="100", cols="20")
699
+
700
+ # Prepare a list to store the avatar information
701
+ avatars_data = [avatars]
702
+
703
+ # Convert the list of avatar data into a DataFrame
704
+ df_avatars = pd.DataFrame(avatars_data, columns=['Avatar Info'])
705
+
706
+ # Write the DataFrame to the new "avatars" worksheet
707
+ set_with_dataframe(worksheet_output, df_avatars)
708
+ st.write("Data written successfully to Google Sheets.")
709
+ except Exception as e:
710
+ st.error(f"An error occurred while writing to Google Sheets: {e}")
711
+
712
+
713
+
714
+
715
+ if page == 'Tone of Voice Manual':
716
+ st.title('Tone of Voice Manual Page')
717
+
718
+ # Authenticate Google Sheets API
719
+ gc = gspread.service_account(filename='arctic-rite-381810-e8bee8664772.json')
720
+
721
+ # Ask user for Google Sheet URL
722
+ sheet_url = st.text_input('Enter the URL of your Google Sheet')
723
+
724
+ # Extract sheet ID from URL
725
+ sheet_id = sheet_url.split('/')[5]
726
+ # Open the Google spreadsheet
727
+ sheet = gc.open_by_key(sheet_id)
728
+
729
+ # Add this in the part of your Streamlit app where the user can select the model
730
+ models = ["gpt-3.5-turbo", "gpt-4"]
731
+ selected_model = st.selectbox("Choose a model:", models)
732
+
733
+ # Add the prompt
734
+ prompt = """------------------------------------------------------------------------------------------------------
735
+ using reviews pros and cons we identified below avatar and related info of avatar for our amazon listing
736
+
737
+ ------------------------------------------------------------------------------------------------------
738
+
739
+
740
+
741
+ ------------------------------------------------------------------------------------------------------
742
+ Please generate a tone of voice manual to address the above Avatar.
743
+
744
+ can you also address below in tone of voice manual.
745
+
746
+ 1. Make a list of all your communications channels
747
+ 2. Highlight the tonal values that apply to each channel
748
+ 3. Write best-practice tone of voice examples
749
+
750
+ Please make this manual very exhaustive and detailed. explain each of the point with long content
751
+
752
+ """
753
+
754
+ # Editable prompt
755
+ prompt = st.text_area("Edit the prompt:", value=prompt, height=200)
756
+
757
+ if st.button('Generate Tone of Voice Manual'):
758
+ messages = [
759
+ {"role": "system", "content": "You are a helpful assistant who helps create a tone of voice manual for avatars"},
760
+ {"role": "user", "content": prompt}
761
+ ]
762
+
763
+ # Get model response
764
+ model_response = get_chatgpt_response(messages, selected_model)
765
+
766
+ # Display the output
767
+ st.text('Generated Tone of Voice Manual:')
768
+ st.write(model_response)
769
+
770
+ # Store the result in the session state
771
+ st.session_state.tone_of_voice_manual = model_response
772
+
773
+ # Create a button that will trigger writing to Google Sheets
774
+ # if st.button('Write Tone of Voice Manual to sheet'):
775
+ # sheet_id = sheet_url.split('/')[5] # Open the Google spreadsheet
776
+ # sheet = gc.open_by_key(sheet_id)
777
+
778
+ # # Retrieve the result from the session state
779
+ # tone_of_voice_manual = st.session_state.tone_of_voice_manual
780
+
781
+ # st.write("Button clicked, processing data...")
782
+
783
+ # # Create a new worksheet and write the final output to it
784
+ # try:
785
+ # # Create a new worksheet named "tone_of_voice_manual"
786
+ # worksheet_output = sheet.add_worksheet(title="tone_of_voice_manual", rows="100", cols="20")
787
+
788
+ # # Prepare a list to store the tone of voice manual
789
+ # tone_of_voice_manual_data = [tone_of_voice_manual]
790
+
791
+ # # Convert the list of tone of voice manual data into a DataFrame
792
+ # df_tone_of_voice_manual = pd.DataFrame(tone_of_voice_manual_data, columns=['Tone of Voice Manual'])
793
+
794
+ # # Write the DataFrame to the new "tone_of_voice_manual" worksheet
795
+ # set_with_dataframe(worksheet_output, df_tone_of_voice_manual)
796
+ # st.write("Data written successfully to Google Sheets.")
797
+ # except Exception as e:
798
+ # st.error(f"An error occurred while writing to Google Sheets: {e}")
799
+
800
+
801
+ # Write Tone of Voice Manual to sheet
802
+ if st.button('Write Tone of Voice Manual to sheet'):
803
+ sheet_id = sheet_url.split('/')[5] # Open the Google spreadsheet
804
+ sheet = gc.open_by_key(sheet_id)
805
+
806
+ # Retrieve the result from the session state
807
+ tone_of_voice_manual = st.session_state.tone_of_voice_manual
808
+
809
+ st.write("Button clicked, processing data...")
810
+
811
+ try:
812
+ # Check if the "tone_of_voice_manual" worksheet already exists
813
+ worksheet_output = None
814
+ for wks in sheet.worksheets():
815
+ if wks.title == 'tone_of_voice_manual':
816
+ worksheet_output = wks
817
+ break
818
+
819
+ # If the worksheet does not exist, create a new one
820
+ if worksheet_output is None:
821
+ worksheet_output = sheet.add_worksheet(title="tone_of_voice_manual", rows="100", cols="20")
822
+
823
+ # Read the existing data from the worksheet
824
+ existing_data = get_as_dataframe(worksheet_output)
825
+
826
+ # Remove empty columns from the existing data
827
+ existing_data = existing_data.dropna(how='all', axis=1)
828
+
829
+ # Prepare a list to store the tone of voice manual data
830
+ tone_of_voice_manual_data = [tone_of_voice_manual]
831
+
832
+ # Convert the list of tone of voice manual data into a DataFrame
833
+ new_data = pd.DataFrame(tone_of_voice_manual_data, columns=['Tone of Voice Manual'])
834
+
835
+ # Append the new data to the existing data
836
+ updated_data = pd.concat([existing_data, new_data], axis=1)
837
+
838
+ # Clear the worksheet before writing the updated data
839
+ worksheet_output.clear()
840
+
841
+ # Write the updated data to the "tone_of_voice_manual" worksheet
842
+ set_with_dataframe(worksheet_output, updated_data)
843
+ st.write("Data written successfully to Google Sheets.")
844
+ except Exception as e:
845
+ st.error(f"An error occurred while writing to Google Sheets: {e}")
846
+