File size: 16,391 Bytes
50a9c82
 
 
6af554a
 
 
 
 
 
50a9c82
 
 
 
6af554a
 
50a9c82
 
 
 
 
 
6af554a
50a9c82
 
 
 
 
 
 
 
 
 
655b450
b6bafd7
655b450
27eb4fe
 
6af554a
 
 
 
 
 
 
 
b6bafd7
 
50a9c82
 
 
27eb4fe
327a303
 
 
 
 
 
 
 
50a9c82
2aa7929
50a9c82
 
 
 
 
 
 
 
 
 
 
 
 
6af554a
27eb4fe
960bd93
 
 
6604630
50a9c82
6604630
50a9c82
 
 
 
 
 
 
 
 
6af554a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50a9c82
6af554a
 
 
 
b6bafd7
 
6af554a
 
b6bafd7
 
 
 
50a9c82
 
655b450
 
 
 
 
 
6af554a
 
 
 
 
 
 
 
 
 
b6bafd7
 
6af554a
 
 
960bd93
6af554a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6bafd7
 
6af554a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50a9c82
88af45a
50a9c82
 
 
 
 
 
 
 
0f9c891
 
 
 
 
 
 
 
 
655b450
 
 
 
 
 
 
 
 
 
b6bafd7
 
655b450
50a9c82
b6bafd7
6923dbf
88af45a
ce294e4
53a68f0
88af45a
53a68f0
b6bafd7
 
 
 
 
 
50a9c82
 
ce294e4
b6bafd7
 
 
 
 
 
 
 
 
 
 
 
 
 
50a9c82
 
b6bafd7
 
 
 
 
 
53a68f0
50a9c82
ce294e4
b6bafd7
 
 
 
 
 
 
 
50a9c82
 
 
655b450
 
 
 
50a9c82
 
 
 
 
 
 
2409abe
7cb54ea
 
2409abe
 
 
50a9c82
cc9a127
429a28c
50a9c82
 
b6bafd7
 
 
 
 
 
 
 
 
 
 
 
50a9c82
 
88af45a
50a9c82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88af45a
50a9c82
88af45a
50a9c82
 
 
88af45a
 
 
 
 
 
 
 
 
 
 
50a9c82
88af45a
50a9c82
 
 
 
88af45a
50a9c82
 
 
 
 
88af45a
 
50a9c82
 
 
88af45a
50a9c82
 
 
b851225
50a9c82
 
 
 
 
 
 
 
88af45a
50a9c82
 
 
88af45a
50a9c82
 
 
 
6af554a
 
 
50a9c82
 
 
 
 
 
 
 
 
88af45a
50a9c82
 
 
 
 
c8880ea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
import gradio as gr
import mysql.connector
import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import (
    ChatPromptTemplate,
    PromptTemplate,
    FewShotPromptTemplate,
)

# Use a pipeline as a high-level helper
from transformers import pipeline

from sentence_transformers import SentenceTransformer, util

# get db info from env vars
db_host = os.environ.get("DB_HOST")
db_user = os.environ.get("DB_USER")
db_pass = os.environ.get("DB_PASS")
db_name = os.environ.get("DB_NAME")

openai_api_key = os.environ.get("OPENAI_API_KEY")

db_connection = mysql.connector.connect(
    host=db_host,
    user=db_user,
    password=db_pass,
    database=db_name,
)

db_cursor = db_connection.cursor()

ORG_ID = 731
AI_PERSON_ID = 11056

potential_labels = []

llm = ChatOpenAI(openai_api_key=openai_api_key, model="gpt-4")

system_prompt = "You are a representative for a local government. A constituent has reached out to you with a question about a local policy. Base your response using the examples below. Be sure to address all points and concerns raised by the constituent. If you do not have enough information to be able to answer the question (you do not see an example that answers the question from the constituent), please make note and another representative will fill in the missing information.\n\n"

examples_prompt = PromptTemplate(
    input_variables=["example"], template="Example:\n\n {example}"
)

ai_response = ""


def get_potential_labels():
    # get potential labels from db
    global potential_labels
    db_connection = mysql.connector.connect(
        host=db_host,
        user=db_user,
        password=db_pass,
        database=db_name,
    )

    db_cursor = db_connection.cursor()
    potential_labels = db_cursor.execute(
        "SELECT message_category_name FROM radmap_frog12.message_categorys"
    )

    potential_labels = db_cursor.fetchall()

    potential_labels = [label[0] for label in potential_labels]

    return potential_labels


potential_labels = get_potential_labels()


# Function to handle the classification
def classify_email_and_generate_response(representative_email, constituent_email):
    potential_labels = get_potential_labels()
    classifier_model = pipeline(
        "zero-shot-classification", model="MoritzLaurer/deberta-v3-large-zeroshot-v1"
    )
    print("classifying email")
    model_out = classifier_model(constituent_email, potential_labels, multi_label=True)
    print("classification complete")
    top_labels = [
        label
        for label, score in zip(model_out["labels"], model_out["scores"])
        if score > 0.95
    ]
    if top_labels == []:
        # Find the index of the highest score
        max_score_index = model_out["scores"].index(max(model_out["scores"]))
        # Return the label with the highest score
        top_labels = [model_out["labels"][max_score_index]]

    labels_with_enough_examples = ["Enforcement", "Financial", "Rules"]
    # see if any of the labels are in labels_with_enough_examples, if so get the messages for that category, else return

    examples = get_similar_messages(constituent_email)

    if representative_email != "":
        current_thread = (
            "Representative message: \n\n"
            + representative_email
            + "\n\nConstituent message: \n\n"
            + constituent_email
        )
    else:
        current_thread = "Constituent message: \n\n" + constituent_email

    prompt = FewShotPromptTemplate(
        examples=examples,
        example_prompt=examples_prompt,
        prefix=system_prompt,
        suffix="Current thread:\n\n {current_thread}\n\nYour response:\n\n",
        input_variables=["current_thread"],
    )

    formatted_prompt = prompt.format(current_thread=current_thread)

    print(formatted_prompt)

    print("Generating GPT4 response")
    import time

    start = time.time()
    ai_out = llm.invoke(formatted_prompt).content

    print("GPT4 response generated in", time.time() - start, "seconds")

    global ai_response
    ai_response = ai_out

    return ", ".join(top_labels), ai_out


def remove_spaces_after_comma(s):
    parts = s.split(",")
    parts = [part.strip() for part in parts]
    return ",".join(parts)


def get_similar_messages(constituent_email):
    db_connection = mysql.connector.connect(
        host=db_host,
        user=db_user,
        password=db_pass,
        database=db_name,
    )
    db_cursor = db_connection.cursor()

    messages_for_category = db_cursor.execute(
        "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)",
        (AI_PERSON_ID,),
    )

    messages_for_category = db_cursor.fetchall()
    embedding_model = SentenceTransformer("all-MiniLM-L6-v2")

    all_message_chains = []

    for message in messages_for_category:
        # TODO: refactor for when integrated with RADMAP
        # if person_id is set
        if message[1] != 0:
            message_chain = "Representative message: \n\n" + message[2] + "\n\n"
            is_representative_turn = False
        else:
            message_chain = "Constituent message: \n\n" + message[2] + "\n\n"
            is_representative_turn = True
            embedding = embedding_model.encode([message[2]])[0]

        next_message_id = message[0]

        while next_message_id:
            next_message = db_cursor.execute(
                "SELECT id, body FROM radmap_frog12.messages WHERE previous_message_id = %s AND person_id <> %s",
                (next_message_id, AI_PERSON_ID),
            )
            next_message = db_cursor.fetchall()
            if not next_message:
                break
            if is_representative_turn:
                message_chain += (
                    "Representative message: \n\n" + next_message[0][1] + "\n\n"
                )
                is_representative_turn = False
            else:
                message_chain += (
                    "Constituent message: \n\n" + next_message[0][1] + "\n\n"
                )
                is_representative_turn = True

                embedding = embedding_model.encode([next_message[0][1]])[0]

            next_message_id = next_message[0][0]

        all_message_chains.append((message_chain, embedding))

        target_embedding = embedding_model.encode([constituent_email])[0]

        # Compute cosine-similarities and keep the top 3 most similar sentences

        top_messages = []

        for message, embedding in all_message_chains:
            cosine_score = util.pytorch_cos_sim(embedding, target_embedding)
            if cosine_score > 0.98:
                continue
            top_messages.append((message, cosine_score))

        top_messages = sorted(top_messages, key=lambda x: x[1], reverse=True)

    return [{"example": message} for message, score in top_messages[0:3]]


# Function to handle saving data
def save_data(orig_user_email, constituent_email, labels, user_response, current_user):
    # save the data to the database
    # orig_user_email should have volley 0
    # constituent_email should have volley 1
    # user_response should have volley 2
    # app_id, org_id, and person_id should be 0
    # subject should be "Email Classification and Response Tracking"
    # body should be the original email

    db_connection = mysql.connector.connect(
        host=db_host,
        user=db_user,
        password=db_pass,
        database=db_name,
    )

    db_cursor = db_connection.cursor()

    if current_user == "Sheryl Springer":
        person_id = 11021
    elif current_user == "Diane Taylor":
        person_id = 11023
    elif current_user == "Ann E. Belyea":
        person_id = 11025
    elif current_user == "Marcelo Mejia":
        person_id = 11027
    elif current_user == "Rishi Vasudeva":
        person_id = 11029
    else:
        return "You need to select a user to save data"

    try:
        first_message_id = 0
        if orig_user_email != "":
            db_cursor.execute(
                "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)",
                (ORG_ID, person_id, orig_user_email, message_id),
            )

            first_message_id = db_cursor.lastrowid

            db_cursor.execute(
                "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)",
                (first_message_id, ORG_ID, person_id, orig_user_email, 0),
            )

        db_cursor.execute(
            "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)",
            (ORG_ID, constituent_email, first_message_id),
        )

        second_message_id = db_cursor.lastrowid

        db_cursor.execute(
            "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)",
            (second_message_id, ORG_ID, constituent_email, first_message_id),
        )

        # add the ai response to the database
        db_cursor.execute(
            "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)",
            (ORG_ID, AI_PERSON_ID, ai_response, second_message_id),
        )

        ai_message_id = db_cursor.lastrowid

        db_cursor.execute(
            "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)",
            (ai_message_id, ORG_ID, AI_PERSON_ID, ai_response, second_message_id),
        )

        db_cursor.execute(
            "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)",
            (ORG_ID, person_id, user_response, second_message_id),
        )

        third_message_id = db_cursor.lastrowid

        db_cursor.execute(
            "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)",
            (third_message_id, ORG_ID, person_id, user_response, second_message_id),
        )

        # insert a row into the message_categorys_associations table for each valid label in labels with the message_id of the constituent_email
        # if there is a comma, remove all spaces after the comma

        labels = remove_spaces_after_comma(labels)
        labels = labels.split(",")
        for label in labels:
            label_exists = db_cursor.execute(
                "SELECT * FROM radmap_frog12.message_categorys WHERE message_category_name = %s",
                (label,),
            )
            label_exists = db_cursor.fetchall()
            if label_exists:
                message_id = db_cursor.execute(
                    "SELECT id FROM radmap_frog12.messages WHERE body = %s",
                    (constituent_email,),
                )
                message_id = db_cursor.fetchall()

                db_cursor.execute(
                    "INSERT INTO radmap_frog12.message_category_associations (message_id, message_category_id) VALUES (%s, %s)",
                    (message_id[0][0], label_exists[0][0]),
                )

                message_catergory_association_id = db_cursor.lastrowid

                # save to logs
                db_cursor.execute(
                    "INSERT INTO radmap_frog12.message_category_associations_log (datetime, message_category_associations_id, message_id, message_category_id) VALUES (NOW(), %s, %s, %s)",
                    (
                        message_catergory_association_id,
                        message_id[0][0],
                        label_exists[0][0],
                    ),
                )

        db_connection.commit()

        return "Response successfully saved to database"

    except Exception as e:
        print(e)
        db_connection.rollback()
        return "Error saving data to database"


# read auth from env vars
auth_username = os.environ.get("AUTH_USERNAME")
auth_password = os.environ.get("AUTH_PASSWORD")

# Define your username and password pairs
auth = [(auth_username, auth_password)]

# Start building the Gradio interface
# Start building the Gradio interface with two columns
with gr.Blocks(theme=gr.themes.Soft()) as app:
    with gr.Row():
        gr.Markdown("## Campaign Messaging Assistant")

    with gr.Row():
        with gr.Column():
            current_user = gr.Dropdown(
                label="Current User",
                choices=[
                    "Sheryl Springer",
                    "Ann E. Belyea",
                    "Marcelo Mejia",
                    "Rishi Vasudeva",
                    "Diane Taylor",
                ],
            )

            email_labels_input = gr.Markdown(
                "## Message Category Library\n ### " + ", ".join(potential_labels),
            )

            original_email_input = gr.TextArea(
                placeholder="Enter the original email sent by you",
                label="Your Original Email (if any)",
            )

            spacer1 = gr.Label(visible=False)

            constituent_response_input = gr.TextArea(
                placeholder="Enter the incoming message",
                label="Incoming Message (may be a response to original email)",
                lines=15,
            )

            classify_button = gr.Button("Process Message", variant="primary")

        with gr.Column():
            classification_output = gr.TextArea(
                label="Message Categories (modify as needed, but only use categories from Library on left). Separate categories with commas",
                lines=1,
                interactive=True,
            )

            spacer2 = gr.Label(visible=False)

            user_response_input = gr.TextArea(
                placeholder="Enter your response to the constituent",
                label="Suggested Response (modify as needed)",
                lines=25,
            )

            save_button = gr.Button("Save Response", variant="primary")
            save_output = gr.Label(label="Backend Response")

    # Define button actions
    classify_button.click(
        fn=classify_email_and_generate_response,
        inputs=[original_email_input, constituent_response_input],
        outputs=[classification_output, user_response_input],
    )

    save_button.click(
        fn=save_data,
        inputs=[
            original_email_input,
            constituent_response_input,
            classification_output,
            user_response_input,
            current_user,
        ],
        outputs=save_output,
    )

# Launch the app
app.launch(auth=auth, debug=True)