Spaces:
Runtime error
Runtime error
Commit
·
9300e58
1
Parent(s):
81c3ec6
Update app.py
Browse files
app.py
CHANGED
@@ -44,7 +44,7 @@ def get_chatgpt_response(messages, selected_model):
|
|
44 |
return response['choices'][0]['message']['content']
|
45 |
|
46 |
# Define pages in sidebar
|
47 |
-
page = st.sidebar.radio('Select a page:', ('Review Analysis', 'review summary', 'Feature Benefits', 'Identify Avatars', 'Tone of Voice Manual'))
|
48 |
|
49 |
|
50 |
if page == 'Review Analysis':
|
@@ -989,3 +989,195 @@ if page == 'review summary':
|
|
989 |
st.error(f"An error occurred while writing to Google Sheets: {e}")
|
990 |
|
991 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
return response['choices'][0]['message']['content']
|
45 |
|
46 |
# Define pages in sidebar
|
47 |
+
page = st.sidebar.radio('Select a page:', ('Review Analysis', 'review summary', 'Feature Benefits', 'Identify Avatars', 'Tone of Voice Manual', 'question_answers'))
|
48 |
|
49 |
|
50 |
if page == 'Review Analysis':
|
|
|
989 |
st.error(f"An error occurred while writing to Google Sheets: {e}")
|
990 |
|
991 |
|
992 |
+
if page == 'question_answers':
|
993 |
+
st.title('question answers')
|
994 |
+
|
995 |
+
gc = gspread.service_account(filename='arctic-rite-381810-e8bee8664772.json')
|
996 |
+
|
997 |
+
# Ask user for Google Sheet URL
|
998 |
+
sheet_url = st.text_input('Enter the URL of your Google Sheet')
|
999 |
+
|
1000 |
+
# Add this in the part of your Streamlit app where the user can select the model
|
1001 |
+
models = ["gpt-3.5-turbo", "gpt-4"]
|
1002 |
+
selected_model = st.selectbox("Choose a model:", models)
|
1003 |
+
|
1004 |
+
# Extract sheet ID from URL
|
1005 |
+
sheet_id = sheet_url.split('/')[5]
|
1006 |
+
# Open the Google spreadsheet
|
1007 |
+
sheet = gc.open_by_key(sheet_id)
|
1008 |
+
|
1009 |
+
# Select sheet named 'Listing'
|
1010 |
+
worksheet = sheet.worksheet('Listing')
|
1011 |
+
# Get all records of the data
|
1012 |
+
df = get_as_dataframe(worksheet)
|
1013 |
+
|
1014 |
+
# Convert DataFrame to strings
|
1015 |
+
df_str = df.astype(str)
|
1016 |
+
|
1017 |
+
title = df[df.eq('Title').any(axis=1)].iloc[0, 2]
|
1018 |
+
bullets = [df[df.eq(f"Bullet #{i}").any(axis=1)].iloc[0, 2] for i in range(1, 6)]
|
1019 |
+
|
1020 |
+
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
|
1021 |
+
|
1022 |
+
image_text_row = df[df.eq('Image Text').any(axis=1)]
|
1023 |
+
image_text = list(image_text_row.dropna(axis=1).iloc[0, :])
|
1024 |
+
|
1025 |
+
a_plus_desc_mask = df.astype(str).apply(lambda x: 'A+ Description' in ' '.join(x), axis=1)
|
1026 |
+
if a_plus_desc_mask.any():
|
1027 |
+
a_plus_desc_row = df[a_plus_desc_mask].index[0]
|
1028 |
+
a_plus_desc = df.iloc[a_plus_desc_row:, :].fillna('').values.flatten()
|
1029 |
+
a_plus_desc = ' '.join(a_plus_desc).strip()
|
1030 |
+
else:
|
1031 |
+
a_plus_desc = None
|
1032 |
+
|
1033 |
+
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
|
1034 |
+
|
1035 |
+
image_text_string = ' '.join(image_text)
|
1036 |
+
|
1037 |
+
bullet_str = ""
|
1038 |
+
for i, bullet in enumerate(bullets, 1):
|
1039 |
+
bullet_str += f"Bullet #{i}: {bullet}\n"
|
1040 |
+
|
1041 |
+
# Display default prompt to user
|
1042 |
+
default_prompt = """Below is data of our products for amazon listing:
|
1043 |
+
------------------------------------
|
1044 |
+
Title: {}\n
|
1045 |
+
bullet_str: {}\n
|
1046 |
+
Legacy Product Description: {}\n
|
1047 |
+
A+ Description: {}\n
|
1048 |
+
Image_text: {}\n
|
1049 |
+
backend_search_terms: {}\n
|
1050 |
+
-------------------------------------
|
1051 |
+
|
1052 |
+
Please create question-answers pairs for amazon listing using this data.
|
1053 |
+
Please segregate them theme wise. themes names should not be features or benefits and create 1 or more questions answer pairs for each theme.
|
1054 |
+
questions generated should be potential questions in customer mind before buying product.
|
1055 |
+
please create at least 20 question-answers pairs.
|
1056 |
+
""".format(title, bullet_str, legacy_desc, a_plus_desc, image_text_string, backend_search_terms)
|
1057 |
+
|
1058 |
+
prompt = st.text_area("Edit the prompt:", value=default_prompt, height=200)
|
1059 |
+
|
1060 |
+
|
1061 |
+
|
1062 |
+
# If the user has input a URL, fetch the data from Google Sheets
|
1063 |
+
if st.button('generate'):
|
1064 |
+
|
1065 |
+
messages = [
|
1066 |
+
{"role": "system", "content": "You are helpful assistant"},
|
1067 |
+
{"role": "user", "content": prompt}
|
1068 |
+
]
|
1069 |
+
|
1070 |
+
|
1071 |
+
|
1072 |
+
model_response = get_chatgpt_response(messages, selected_model)
|
1073 |
+
|
1074 |
+
question_answers= model_response
|
1075 |
+
|
1076 |
+
st.session_state.question_answers = question_answers
|
1077 |
+
|
1078 |
+
st.write(question_answers)
|
1079 |
+
|
1080 |
+
def parse_qa(text):
|
1081 |
+
try:
|
1082 |
+
# Split the text into themes
|
1083 |
+
themes = re.split(r'\nTheme: ', text)[1:]
|
1084 |
+
|
1085 |
+
output = []
|
1086 |
+
for theme in themes:
|
1087 |
+
theme_lines = theme.split('\n')
|
1088 |
+
theme_name = theme_lines[0]
|
1089 |
+
qa_pairs = theme_lines[1:]
|
1090 |
+
|
1091 |
+
# Extract question and answer pairs
|
1092 |
+
for qa in qa_pairs:
|
1093 |
+
if qa.startswith('Q'):
|
1094 |
+
question = qa.split(': ', 1)[1]
|
1095 |
+
elif qa.startswith('A'):
|
1096 |
+
answer = qa.split(': ', 1)[1]
|
1097 |
+
output.append((theme_name, question, answer))
|
1098 |
+
return output, True
|
1099 |
+
except Exception as e:
|
1100 |
+
return text, False # Return the full text and a flag indicating the parsing failed
|
1101 |
+
|
1102 |
+
|
1103 |
+
|
1104 |
+
# # Create a button that will trigger writing to Google Sheets
|
1105 |
+
# if st.button('Write output to sheet'):
|
1106 |
+
|
1107 |
+
# final_output= st.session_state.question_answers
|
1108 |
+
|
1109 |
+
# st.write("Button clicked, processing data...")
|
1110 |
+
|
1111 |
+
# sheet_id = sheet_url.split('/')[5] # Open the Google spreadsheet
|
1112 |
+
# sheet = gc.open_by_key(sheet_id)
|
1113 |
+
|
1114 |
+
|
1115 |
+
|
1116 |
+
|
1117 |
+
|
1118 |
+
# try:
|
1119 |
+
# # Check if the "tone_of_voice_manual" worksheet already exists
|
1120 |
+
# worksheet_output = None
|
1121 |
+
# for wks in sheet.worksheets():
|
1122 |
+
# if wks.title == 'question_answers':
|
1123 |
+
# worksheet_output = wks
|
1124 |
+
# break
|
1125 |
+
|
1126 |
+
# # If the worksheet does not exist, create a new one
|
1127 |
+
# if worksheet_output is None:
|
1128 |
+
# worksheet_output = sheet.add_worksheet(title="question_answers", rows="100", cols="20")
|
1129 |
+
|
1130 |
+
# # Read the existing data from the worksheet
|
1131 |
+
# existing_data = get_as_dataframe(worksheet_output)
|
1132 |
+
|
1133 |
+
# # Remove empty columns from the existing data
|
1134 |
+
# existing_data = existing_data.dropna(how='all', axis=1)
|
1135 |
+
|
1136 |
+
# # Prepare a list to store the tone of voice manual data
|
1137 |
+
# question_answers_data = [final_output]
|
1138 |
+
|
1139 |
+
# # Convert the list of tone of voice manual data into a DataFrame
|
1140 |
+
# new_data = pd.DataFrame(question_answers_data, columns=['question_answers'])
|
1141 |
+
|
1142 |
+
# # Append the new data to the existing data
|
1143 |
+
# updated_data = pd.concat([existing_data, new_data], axis=1)
|
1144 |
+
|
1145 |
+
# # Clear the worksheet before writing the updated data
|
1146 |
+
# worksheet_output.clear()
|
1147 |
+
|
1148 |
+
# # Write the updated data to the "tone_of_voice_manual" worksheet
|
1149 |
+
# set_with_dataframe(worksheet_output, updated_data)
|
1150 |
+
# st.write("Data written successfully to Google Sheets.")
|
1151 |
+
# except Exception as e:
|
1152 |
+
# st.error(f"An error occurred while writing to Google Sheets: {e}")
|
1153 |
+
|
1154 |
+
if st.button('Write output to sheet'):
|
1155 |
+
final_output, parsed_successfully = parse_qa(st.session_state.question_answers)
|
1156 |
+
|
1157 |
+
st.write("Button clicked, processing data...")
|
1158 |
+
|
1159 |
+
sheet_id = sheet_url.split('/')[5] # Open the Google spreadsheet
|
1160 |
+
sheet = gc.open_by_key(sheet_id)
|
1161 |
+
|
1162 |
+
try:
|
1163 |
+
worksheet_output = None
|
1164 |
+
for wks in sheet.worksheets():
|
1165 |
+
if wks.title == 'question_answers':
|
1166 |
+
worksheet_output = wks
|
1167 |
+
break
|
1168 |
+
|
1169 |
+
if worksheet_output is None:
|
1170 |
+
worksheet_output = sheet.add_worksheet(title="question_answers", rows="100", cols="20")
|
1171 |
+
|
1172 |
+
if parsed_successfully:
|
1173 |
+
new_data = pd.DataFrame(final_output, columns=['Theme', 'Question', 'Answer'])
|
1174 |
+
else:
|
1175 |
+
new_data = pd.DataFrame([final_output], columns=['Output'])
|
1176 |
+
|
1177 |
+
worksheet_output.clear()
|
1178 |
+
set_with_dataframe(worksheet_output, new_data)
|
1179 |
+
st.write("Data written successfully to Google Sheets.")
|
1180 |
+
except Exception as e:
|
1181 |
+
st.error(f"An error occurred while writing to Google Sheets: {e}")
|
1182 |
+
|
1183 |
+
|