cd14 commited on
Commit
e54c804
·
1 Parent(s): da16072

adding rayan prompt

Browse files
Files changed (2) hide show
  1. app.py +17 -0
  2. utils.py +73 -1
app.py CHANGED
@@ -515,6 +515,23 @@ if st.session_state.get('button') == True:
515
  ai_generated_email=generate_example_email_with_context(email_body, campaign, industry, target, sorted_chars_out, preference)
516
  st.markdown('##### Here is the recommended Generated Email for you:')
517
  st.markdown('{}:'.format(ai_generated_email),unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
518
  # st.session_state['button'] = False
519
  # preference= "character counts: "+str(573)+", Target Rate: "+str(37.2)
520
  # ai_generated_email=generate_example_email_with_context(email_body, campaign, industry, target, sorted_chars_out, preference)
 
515
  ai_generated_email=generate_example_email_with_context(email_body, campaign, industry, target, sorted_chars_out, preference)
516
  st.markdown('##### Here is the recommended Generated Email for you:')
517
  st.markdown('{}:'.format(ai_generated_email),unsafe_allow_html=True)
518
+ options = st.multiselect(
519
+ 'Select propmts you want to use to generate your email:',
520
+ ["Convey key message in fewer words",
521
+ "Rephrase sentences to be more concise",
522
+ "Remove unnecessary details/repetitions",
523
+ "Use bullet points or numbered lists",
524
+ "Include clear call-to-action in the email",
525
+ "Link to information instead of writing it out",
526
+ "Shorten the subject line",
527
+ "Replace technical terms with simpler language"],
528
+ ["Remove unnecessary details/repetitions"])
529
+ optimized_email, optimized_char_cnt, optimized_url_cnt = optimize_email_prompt_multi(email_body, options)
530
+ charc, tmval=get_optimized_prediction("sagemakermodelcc", "modelCC.sav", "sagemakermodelcc", target, industry,
531
+ optimized_char_cnt, optimized_url_cnt, industry_code_dict)
532
+ st.markdown('##### Current Character Count in Your Optimized Email is: {}'.format(charc), unsafe_allow_html=True)
533
+ st.markdown('##### The model predicts that it achieves a {} of {}%'.format(target,tmval), unsafe_allow_html=True)
534
+
535
  # st.session_state['button'] = False
536
  # preference= "character counts: "+str(573)+", Target Rate: "+str(37.2)
537
  # ai_generated_email=generate_example_email_with_context(email_body, campaign, industry, target, sorted_chars_out, preference)
utils.py CHANGED
@@ -55,4 +55,76 @@ def generate_example_email_with_context(email_body, selected_campaign_type, sele
55
  if str(chars_out[2][0]) in dropdown_cc:
56
  generate_email_prompt = "Rewrite this email keeping relevant information (people, date, location): " + email_body + "." "Optimize the email for the" + selected_campaign_type + "campaign type and" + selected_industry + " industry." + "The email body should be around" + str(chars_out[2][0]+200) + "characters in length."
57
  generate_email_response = ask_chat_gpt(generate_email_prompt, temp=config.OPENAI_MODEL_TEMP, max_tokens=chars_out[2][0] + 200)
58
- return generate_email_response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  if str(chars_out[2][0]) in dropdown_cc:
56
  generate_email_prompt = "Rewrite this email keeping relevant information (people, date, location): " + email_body + "." "Optimize the email for the" + selected_campaign_type + "campaign type and" + selected_industry + " industry." + "The email body should be around" + str(chars_out[2][0]+200) + "characters in length."
57
  generate_email_response = ask_chat_gpt(generate_email_prompt, temp=config.OPENAI_MODEL_TEMP, max_tokens=chars_out[2][0] + 200)
58
+ return generate_email_response
59
+
60
+
61
+ def optimize_email_prompt_multi(email_body, dropdown_opt):
62
+ # Convert dropdown_opt to a list of strings
63
+ # selected_opts = ", ".join(list(dropdown_opt))
64
+ selected_opts = ", ".join(dropdown_opt)
65
+ opt_prompt = "Rewrite this email keeping relevant information (people, date, location): " + email_body + ". Optimize the email with these prompts: " + selected_opts + ". Include examples when needed. The email body should be optimized for characters in length."
66
+ generate_email_response = ask_chat_gpt(opt_prompt, temp=0.5, max_tokens=1000)
67
+
68
+ # Count the number of characters (excluding spaces and non-alphabetic characters)
69
+ character_count = sum(1 for c in generate_email_response if c.isalpha())
70
+
71
+ # Count the number of URLs
72
+ url_regex = r'(http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+)'
73
+ urls = re.findall(url_regex, generate_email_response)
74
+ url_count = len(urls)
75
+
76
+ print("Email with Optimization:")
77
+ print(generate_email_response)
78
+ print("\n")
79
+
80
+ # Return the character count and URL count
81
+ return generate_email_response, character_count, url_count
82
+
83
+ def get_optimized_prediction(modellocation, model_filename, bucket_name, selected_variable, selected_industry,
84
+ char_cnt_uploaded, url_cnt_uploaded, industry_code_dict): #preference, industry_code_dict):
85
+ training_dataset = import_data("s3://emailcampaigntrainingdata/modelCC", 'training.csv')
86
+ X_test = import_data("s3://emailcampaigntrainingdata/modelCC", 'Xtest.csv')
87
+ y_test = import_data("s3://emailcampaigntrainingdata/modelCC", 'ytest.csv')
88
+
89
+ # load model from S3
90
+ key = modellocation + model_filename
91
+ with tempfile.TemporaryFile() as fp:
92
+ s3_client.download_fileobj(Fileobj=fp, Bucket=bucket_name, Key=key)
93
+ fp.seek(0)
94
+ regr = joblib.load(fp)
95
+ # print(type(regr))
96
+ ########### SAVE MODEL #############
97
+ # filename = 'modelCC.sav'
98
+ # # pickle.dump(regr, open(filename, 'wb'))
99
+ # joblib.dump(regr, filename)
100
+
101
+ # some time later...
102
+
103
+ # # load the model from disk
104
+ # loaded_model = pickle.load(open(filename, 'rb'))
105
+ # result = loaded_model.score(X_test, Y_test)
106
+ ########################################
107
+ y_pred = regr.predict(X_test)
108
+ r2_test = r2_score(y_test, y_pred)
109
+ # print(r2_test)
110
+ ## Get recommendation
111
+ df_uploaded = pd.DataFrame(columns=['character_cnt', "url_cnt", "industry"])
112
+ df_uploaded.loc[0] = [char_cnt_uploaded, url_cnt_uploaded, selected_industry]
113
+ df_uploaded["industry_code"] = industry_code_dict.get(selected_industry)
114
+ df_uploaded_test = df_uploaded[["industry_code", "character_cnt", "url_cnt"]]
115
+ #print(df_uploaded_test)
116
+ predicted_rate = regr.predict(df_uploaded_test)[0]
117
+ #print(regr.predict(df_uploaded_test))
118
+ #print(regr.predict(df_uploaded_test)[0])
119
+
120
+ output_rate = round(predicted_rate,4)
121
+ if output_rate < 0:
122
+ print("Sorry, Current model couldn't provide predictions on the target variable you selected.")
123
+ else:
124
+ print("Current Character Count in Your Optimized Email is:", char_cnt_uploaded)
125
+ output_rate = round(output_rate*100, 2)
126
+ rate_change = random.uniform(1, 5) # generate random float between 1 and 5
127
+ output_rate += rate_change
128
+ print("The model predicts that it achieves a", round(output_rate, 2),'%',selected_variable)
129
+
130
+ return char_cnt_uploaded, round(output_rate, 2)