-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
279 lines (207 loc) · 7.61 KB
/
app.py
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
from flask import Flask, request, jsonify
import re
from openai import OpenAI
import json
client = OpenAI(
api_key="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",
)
app = Flask(__name__)
@app.route('/inspect')
def home():
content = request.json
content_list = content["contents"]
if len(content_list) == 0:
return content_list, 200
if not content["policy"]:
return content_list, 200
warnings = []
email_policy = content["policy"]["emailRule"]
if email_policy and email_policy != "allow":
email_list = contains_emails(content_list)
if email_policy == "block":
block = should_block(email_list)
if block:
data = {
"action": "block"
}
return jsonify(data)
elif email_policy == "allow_but_redact":
redacted = redact(email_list, content_list)
content_list = redacted
elif email_policy == "allow_but_warn":
warnings.append("email is found")
ssn_policy = content["policy"]["ssnRule"]
if ssn_policy and ssn_policy != "allow":
ssn_list = contains_ssn(content_list)
if ssn_policy == "block":
block = should_block(ssn_list)
if block:
data = {
"action": "block"
}
return jsonify(data)
elif ssn_policy == "allow_but_redact":
redacted = redact(ssn_list, content_list)
content_list = redacted
elif ssn_policy == "allow_but_warn":
warnings.append("ssn is found")
regex_policy = content["policy"]["regularExpressionRules"]
if regex_policy and len(regex_policy) != 0:
for regex_config in regex_policy:
regex = regex_config["definition"]
action = regex_config["action"]
regex_match_list = contains_regex_matches(regex, content_list)
if action == "block":
block = should_block(regex_match_list)
if block:
data = {
"action": "block"
}
return jsonify(data)
elif action == "allow_but_redact":
redacted = redact(ssn_list, content_list)
elif action == "allow_but_warn":
warnings.append("regular rule {} matching found".format(regex))
namePolicy = content["policy"]["nameRule"]
if namePolicy and namePolicy != "allow":
names = use_openai_extract_names(content_list)
if namePolicy == "block":
block = should_block(ssn_list)
if block:
data = {
"action": "block"
}
return jsonify(data)
elif ssn_policy == "allow_but_redact":
redacted = redact(ssn_list, content_list)
content_list = redacted
elif ssn_policy == "allow_but_warn":
warnings.append("ssn is found")
addressRule = content["policy"]["addressRule"]
if addressRule:
if addressRule != "allow":
return content_list, 200
return content_list, 200
if __name__ == '__main__':
app.run(debug=True)
def contains_ssn(string_list):
ssn_pattern = r'\b\d{3}-\d{2}-\d{4}\b'
result = []
for text in string_list:
ssns = re.findall(ssn_pattern, text)
result.append(ssns)
return result
def contains_emails(string_list):
email_pattern = re.compile(
r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b')
result = []
for text in string_list:
emails = re.findall(email_pattern, text)
result.append(emails)
return result
def redact(target_string_list, string_list):
result = []
updated = ""
for idx, text in string_list:
updated = text
for target in target_string_list[idx]:
updated = updated.replace(target, "***")
result.append(updated)
return result
def redact_names(name_list, string_list):
result = []
updated = ""
for text in string_list:
updated = text
for target in name_list:
updated = updated.replace(target, "***")
result.append(updated)
return result
def redact_addresses(address_list, string_list):
result = []
updated = ""
for text in string_list:
updated = text
for target in address_list:
updated = updated.replace(target, "***")
result.append(updated)
return result
def contains_regex_matches(regex, string_list):
result = []
for text in string_list:
matched = re.findall(regex, text)
result.append(matched)
return result
def should_block(target_string_list):
for text_list in target_string_list:
if len(text_list) != 0:
return False
return True
def use_openai_extract_names(texts):
names = []
try:
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant. You take in an array of strings and ouput JSON with one field called names. names field is an array of strings consisted of extracted people names from the given text.",
},
{
"role": "user",
"content": "[{}]".format(', '.join(texts)),
}
],
response_format={"type": "json_object"},
model="gpt-4-1106-preview",
)
content = response.choices[0].message.content
obj = json.loads(content)
return obj["names"]
except Exception as e:
print(f"An error occurred: {e}")
return names
def use_openai_extract_addresses(texts):
addresses = []
try:
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant. You take in an array of strings and ouput JSON with one field called addresses. addresses field is an array of strings consisted of extracted physical addresses from the given text.",
},
{
"role": "user",
"content": "[{}]".format(', '.join(texts)),
}
],
response_format={"type": "json_object"},
model="gpt-4-1106-preview",
)
content = response.choices[0].message.content
obj = json.loads(content)
return obj["addresses"]
except Exception as e:
print(f"An error occurred: {e}")
return addresses
def use_openai_find_entities_using_custom_policies(texts, requirement):
try:
response = client.chat.completions.create(
messages=[
{
"role": "system",
"content": "You are a helpful assistant. You take in an array of strings and ouput JSON with one field called relevant_texts_found. relevant_texts_found is a boolean field that indicates whether or not given texts contain subtexts that fullfill the following requirements: {}".format(requirement),
},
{
"role": "user",
"content": "[{}]".format(', '.join(texts)),
}
],
response_format={"type": "json_object"},
model="gpt-4-1106-preview",
)
content = response.choices[0].message.content
obj = json.loads(content)
return obj["relevant_texts_found"]
except Exception as e:
print(f"An error occurred: {e}")
return False