AMead10 commited on
Commit
b6bafd7
·
1 Parent(s): 960bd93

finish phase 2

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +62 -11
.gitignore CHANGED
@@ -1,2 +1,3 @@
1
  __pycache__
2
  examples
 
 
1
  __pycache__
2
  examples
3
+ .env
app.py CHANGED
@@ -31,6 +31,7 @@ db_connection = mysql.connector.connect(
31
  db_cursor = db_connection.cursor()
32
 
33
  ORG_ID = 731
 
34
 
35
  potential_labels = []
36
 
@@ -42,6 +43,8 @@ examples_prompt = PromptTemplate(
42
  input_variables=["example"], template="Example:\n\n {example}"
43
  )
44
 
 
 
45
 
46
  def get_potential_labels():
47
  # get potential labels from db
@@ -119,10 +122,14 @@ def classify_email_and_generate_response(representative_email, constituent_email
119
  import time
120
 
121
  start = time.time()
122
- out = "GPT4:\n\n" + llm.invoke(formatted_prompt).content
 
123
  print("GPT4 response generated in", time.time() - start, "seconds")
124
 
125
- return ", ".join(top_labels), out
 
 
 
126
 
127
 
128
  def remove_spaces_after_comma(s):
@@ -141,7 +148,8 @@ def get_similar_messages(constituent_email):
141
  db_cursor = db_connection.cursor()
142
 
143
  messages_for_category = db_cursor.execute(
144
- "SELECT id, person_id, body FROM radmap_frog12.messages WHERE id IN (SELECT message_id FROM radmap_frog12.message_category_associations)"
 
145
  )
146
 
147
  messages_for_category = db_cursor.fetchall()
@@ -164,8 +172,8 @@ def get_similar_messages(constituent_email):
164
 
165
  while next_message_id:
166
  next_message = db_cursor.execute(
167
- "SELECT id, body FROM radmap_frog12.messages WHERE previous_message_id = %s",
168
- (next_message_id,),
169
  )
170
  next_message = db_cursor.fetchall()
171
  if not next_message:
@@ -233,28 +241,59 @@ def save_data(orig_user_email, constituent_email, labels, user_response, current
233
  person_id = 11027
234
  elif current_user == "Rishi Vasudeva":
235
  person_id = 11029
 
 
236
 
237
  try:
238
- message_id = 0
239
  if orig_user_email != "":
240
  db_cursor.execute(
241
  "INSERT INTO radmap_frog12.messages (app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, previous_message_id) VALUES (345678, %s, %s, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', %s)",
242
  (ORG_ID, person_id, orig_user_email, message_id),
243
  )
244
 
245
- #
246
- message_id = db_cursor.lastrowid
 
 
 
 
247
 
248
  db_cursor.execute(
249
  "INSERT INTO radmap_frog12.messages (app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, previous_message_id) VALUES (345678, %s, 0, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', %s)",
250
- (ORG_ID, constituent_email, message_id),
 
 
 
 
 
 
 
 
 
 
 
 
 
251
  )
252
 
253
- message_id = db_cursor.lastrowid
 
 
 
 
 
254
 
255
  db_cursor.execute(
256
  "INSERT INTO radmap_frog12.messages (app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, previous_message_id) VALUES (345678, %s, %s, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', %s)",
257
- (ORG_ID, person_id, user_response, message_id),
 
 
 
 
 
 
 
258
  )
259
 
260
  # insert a row into the message_categorys_associations table for each valid label in labels with the message_id of the constituent_email
@@ -280,6 +319,18 @@ def save_data(orig_user_email, constituent_email, labels, user_response, current
280
  (message_id[0][0], label_exists[0][0]),
281
  )
282
 
 
 
 
 
 
 
 
 
 
 
 
 
283
  db_connection.commit()
284
 
285
  return "Response successfully saved to database"
 
31
  db_cursor = db_connection.cursor()
32
 
33
  ORG_ID = 731
34
+ AI_PERSON_ID = 11056
35
 
36
  potential_labels = []
37
 
 
43
  input_variables=["example"], template="Example:\n\n {example}"
44
  )
45
 
46
+ ai_response = ""
47
+
48
 
49
  def get_potential_labels():
50
  # get potential labels from db
 
122
  import time
123
 
124
  start = time.time()
125
+ ai_out = llm.invoke(formatted_prompt).content
126
+
127
  print("GPT4 response generated in", time.time() - start, "seconds")
128
 
129
+ global ai_response
130
+ ai_response = ai_out
131
+
132
+ return ", ".join(top_labels), ai_out
133
 
134
 
135
  def remove_spaces_after_comma(s):
 
148
  db_cursor = db_connection.cursor()
149
 
150
  messages_for_category = db_cursor.execute(
151
+ "SELECT id, person_id, body FROM radmap_frog12.messages WHERE person_id <> %s AND id IN (SELECT message_id FROM radmap_frog12.message_category_associations)",
152
+ (AI_PERSON_ID,),
153
  )
154
 
155
  messages_for_category = db_cursor.fetchall()
 
172
 
173
  while next_message_id:
174
  next_message = db_cursor.execute(
175
+ "SELECT id, body FROM radmap_frog12.messages WHERE previous_message_id = %s AND person_id <> %s",
176
+ (next_message_id, AI_PERSON_ID),
177
  )
178
  next_message = db_cursor.fetchall()
179
  if not next_message:
 
241
  person_id = 11027
242
  elif current_user == "Rishi Vasudeva":
243
  person_id = 11029
244
+ else:
245
+ return "You need to select a user to save data"
246
 
247
  try:
248
+ first_message_id = 0
249
  if orig_user_email != "":
250
  db_cursor.execute(
251
  "INSERT INTO radmap_frog12.messages (app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, previous_message_id) VALUES (345678, %s, %s, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', %s)",
252
  (ORG_ID, person_id, orig_user_email, message_id),
253
  )
254
 
255
+ first_message_id = db_cursor.lastrowid
256
+
257
+ db_cursor.execute(
258
+ "INSERT INTO radmap_frog12.messages_log (datetime, message_id, app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, previous_message_id) VALUES (NOW(), %s, 345678, %s, %s, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', %s)",
259
+ (first_message_id, ORG_ID, person_id, orig_user_email, 0),
260
+ )
261
 
262
  db_cursor.execute(
263
  "INSERT INTO radmap_frog12.messages (app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, previous_message_id) VALUES (345678, %s, 0, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', %s)",
264
+ (ORG_ID, constituent_email, first_message_id),
265
+ )
266
+
267
+ second_message_id = db_cursor.lastrowid
268
+
269
+ db_cursor.execute(
270
+ "INSERT INTO radmap_frog12.messages_log (datetime, message_id, app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, previous_message_id) VALUES (NOW(), %s, 345678, %s, 0, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', %s)",
271
+ (second_message_id, ORG_ID, constituent_email, first_message_id),
272
+ )
273
+
274
+ # add the ai response to the database
275
+ db_cursor.execute(
276
+ "INSERT INTO radmap_frog12.messages (app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, previous_message_id) VALUES (345678, %s, %s, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', %s)",
277
+ (ORG_ID, AI_PERSON_ID, ai_response, second_message_id),
278
  )
279
 
280
+ ai_message_id = db_cursor.lastrowid
281
+
282
+ db_cursor.execute(
283
+ "INSERT INTO radmap_frog12.messages_log (datetime, message_id, app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, previous_message_id) VALUES (NOW(), %s, 345678, %s, %s, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', %s)",
284
+ (ai_message_id, ORG_ID, AI_PERSON_ID, ai_response, second_message_id),
285
+ )
286
 
287
  db_cursor.execute(
288
  "INSERT INTO radmap_frog12.messages (app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, previous_message_id) VALUES (345678, %s, %s, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', %s)",
289
+ (ORG_ID, person_id, user_response, second_message_id),
290
+ )
291
+
292
+ third_message_id = db_cursor.lastrowid
293
+
294
+ db_cursor.execute(
295
+ "INSERT INTO radmap_frog12.messages_log (datetime, message_id, app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, previous_message_id) VALUES (NOW(), %s, 345678, %s, %s, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', %s)",
296
+ (third_message_id, ORG_ID, person_id, user_response, second_message_id),
297
  )
298
 
299
  # insert a row into the message_categorys_associations table for each valid label in labels with the message_id of the constituent_email
 
319
  (message_id[0][0], label_exists[0][0]),
320
  )
321
 
322
+ message_catergory_association_id = db_cursor.lastrowid
323
+
324
+ # save to logs
325
+ db_cursor.execute(
326
+ "INSERT INTO radmap_frog12.message_category_associations_log (datetime, message_category_associations_id, message_id, message_category_id) VALUES (NOW(), %s, %s, %s)",
327
+ (
328
+ message_catergory_association_id,
329
+ message_id[0][0],
330
+ label_exists[0][0],
331
+ ),
332
+ )
333
+
334
  db_connection.commit()
335
 
336
  return "Response successfully saved to database"