Spaces:
Sleeping
Sleeping
init
Browse files- app.py +188 -0
- requirements.txt +1 -0
app.py
ADDED
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import mysql.connector
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Use a pipeline as a high-level helper
|
6 |
+
from transformers import pipeline
|
7 |
+
|
8 |
+
classifier_model = pipeline(
|
9 |
+
"zero-shot-classification", model="MoritzLaurer/deberta-v3-large-zeroshot-v1"
|
10 |
+
)
|
11 |
+
|
12 |
+
# get db info from env vars
|
13 |
+
db_host = os.environ.get("DB_HOST")
|
14 |
+
db_user = os.environ.get("DB_USER")
|
15 |
+
db_pass = os.environ.get("DB_PASS")
|
16 |
+
db_name = os.environ.get("DB_NAME")
|
17 |
+
|
18 |
+
|
19 |
+
db_connection = mysql.connector.connect(
|
20 |
+
host=db_host,
|
21 |
+
user=db_user,
|
22 |
+
password=db_pass,
|
23 |
+
database=db_name,
|
24 |
+
)
|
25 |
+
|
26 |
+
db_cursor = db_connection.cursor()
|
27 |
+
|
28 |
+
|
29 |
+
def get_potential_labels():
|
30 |
+
# get potential labels from db
|
31 |
+
potential_labels = db_cursor.execute(
|
32 |
+
"SELECT message_category_name FROM message_categorys"
|
33 |
+
)
|
34 |
+
|
35 |
+
potential_labels = db_cursor.fetchall()
|
36 |
+
|
37 |
+
potential_labels = [label[0] for label in potential_labels]
|
38 |
+
|
39 |
+
return potential_labels
|
40 |
+
|
41 |
+
|
42 |
+
# make gloabl variable for potential labels
|
43 |
+
global potential_labels
|
44 |
+
|
45 |
+
potential_labels = get_potential_labels()
|
46 |
+
|
47 |
+
|
48 |
+
# Function to handle the classification
|
49 |
+
def classify_email(constituent_email):
|
50 |
+
potential_labels = get_potential_labels()
|
51 |
+
|
52 |
+
model_out = classifier_model(constituent_email, potential_labels, multi_label=True)
|
53 |
+
top_labels = [
|
54 |
+
label
|
55 |
+
for label, score in zip(model_out["labels"], model_out["scores"])
|
56 |
+
if score > 0.95
|
57 |
+
]
|
58 |
+
if top_labels == []:
|
59 |
+
# Find the index of the highest score
|
60 |
+
max_score_index = model_out["scores"].index(max(model_out["scores"]))
|
61 |
+
# Return the label with the highest score
|
62 |
+
return model_out["labels"][max_score_index]
|
63 |
+
|
64 |
+
return ", ".join(top_labels)
|
65 |
+
|
66 |
+
|
67 |
+
# Function to handle saving data
|
68 |
+
def save_data(orig_user_email, constituent_email, labels, user_response):
|
69 |
+
# save the data to the database
|
70 |
+
# orig_user_email should have volley 0
|
71 |
+
# constituent_email should have volley 1
|
72 |
+
# user_response should have volley 2
|
73 |
+
# app_id, org_id, and person_id should be 0
|
74 |
+
# subject should be "Email Classification and Response Tracking"
|
75 |
+
# body should be the original email
|
76 |
+
|
77 |
+
try:
|
78 |
+
db_cursor.execute(
|
79 |
+
"INSERT INTO messages (app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, volley) VALUES (0, 0, 0, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', 0)",
|
80 |
+
(orig_user_email,),
|
81 |
+
)
|
82 |
+
|
83 |
+
db_cursor.execute(
|
84 |
+
"INSERT INTO messages (app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, volley) VALUES (0, 0, 0, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', 1)",
|
85 |
+
(constituent_email,),
|
86 |
+
)
|
87 |
+
|
88 |
+
db_cursor.execute(
|
89 |
+
"INSERT INTO messages (app_id, org_id, person_id, communication_method_id, status_id, subject, body, send_date, message_type, volley) VALUES (0, 0, 0, 1, 1, 'Email Classification and Response Tracking', %s, NOW(), 'Email Classification and Response Tracking', 2)",
|
90 |
+
(user_response,),
|
91 |
+
)
|
92 |
+
|
93 |
+
# insert a row into the message_categorys_associations table for each valid label in labels with the message_id of the constituent_email
|
94 |
+
labels = labels.split(", ")
|
95 |
+
for label in labels:
|
96 |
+
label_exists = db_cursor.execute(
|
97 |
+
"SELECT * FROM radmap_frog12.message_categorys WHERE message_category_name = %s",
|
98 |
+
(label,),
|
99 |
+
)
|
100 |
+
label_exists = db_cursor.fetchall()
|
101 |
+
if label_exists:
|
102 |
+
db_cursor.execute(
|
103 |
+
"INSERT INTO message_category_associations (message_id, message_category_id) VALUES ((SELECT id FROM messages WHERE body = %s), (SELECT id FROM message_categorys WHERE message_category_name = %s))",
|
104 |
+
(constituent_email, label),
|
105 |
+
)
|
106 |
+
|
107 |
+
db_connection.commit()
|
108 |
+
|
109 |
+
return "Data successfully saved to database"
|
110 |
+
|
111 |
+
except Exception as e:
|
112 |
+
print(e)
|
113 |
+
db_connection.rollback()
|
114 |
+
return "Error saving data to database"
|
115 |
+
|
116 |
+
|
117 |
+
# read auth from env vars
|
118 |
+
auth_username = os.environ.get("AUTH_USERNAME")
|
119 |
+
auth_password = os.environ.get("AUTH_PASSWORD")
|
120 |
+
|
121 |
+
# Define your username and password pairs
|
122 |
+
auth = [(auth_username, auth_password)]
|
123 |
+
|
124 |
+
# Start building the Gradio interface
|
125 |
+
# Start building the Gradio interface with two columns
|
126 |
+
with gr.Blocks() as app:
|
127 |
+
with gr.Row():
|
128 |
+
gr.Markdown("## Email Classification and Response Tracking")
|
129 |
+
|
130 |
+
with gr.Row():
|
131 |
+
with gr.Column():
|
132 |
+
email_labels_input = gr.Markdown(
|
133 |
+
"## Valid Email Labels\n ### " + ", ".join(potential_labels),
|
134 |
+
)
|
135 |
+
|
136 |
+
original_email_input = gr.TextArea(
|
137 |
+
placeholder="Enter the original email sent by you",
|
138 |
+
label="Your Original Email",
|
139 |
+
)
|
140 |
+
|
141 |
+
spacer1 = gr.Label(visible=False)
|
142 |
+
|
143 |
+
constituent_response_input = gr.TextArea(
|
144 |
+
placeholder="Enter the constituent's response",
|
145 |
+
label="Constituent's Response",
|
146 |
+
lines=15,
|
147 |
+
)
|
148 |
+
|
149 |
+
classify_button = gr.Button("Classify Email")
|
150 |
+
|
151 |
+
with gr.Column():
|
152 |
+
classification_output = gr.TextArea(
|
153 |
+
label="Current Email Labels",
|
154 |
+
lines=1,
|
155 |
+
interactive=True,
|
156 |
+
)
|
157 |
+
|
158 |
+
spacer2 = gr.Label(visible=False)
|
159 |
+
|
160 |
+
user_response_input = gr.TextArea(
|
161 |
+
placeholder="Enter your response to the constituent",
|
162 |
+
label="Your Response",
|
163 |
+
lines=25,
|
164 |
+
)
|
165 |
+
|
166 |
+
save_button = gr.Button("Save Data")
|
167 |
+
save_output = gr.Label(label="Backend Response")
|
168 |
+
|
169 |
+
# Define button actions
|
170 |
+
classify_button.click(
|
171 |
+
fn=classify_email,
|
172 |
+
inputs=constituent_response_input,
|
173 |
+
outputs=classification_output,
|
174 |
+
)
|
175 |
+
|
176 |
+
save_button.click(
|
177 |
+
fn=save_data,
|
178 |
+
inputs=[
|
179 |
+
original_email_input,
|
180 |
+
constituent_response_input,
|
181 |
+
classification_output,
|
182 |
+
user_response_input,
|
183 |
+
],
|
184 |
+
outputs=save_output,
|
185 |
+
)
|
186 |
+
|
187 |
+
# Launch the app
|
188 |
+
app.launch(auth=auth, debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
mysql-connector-python
|