-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
350 lines (293 loc) · 13.6 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
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
import os
import uvicorn
import subprocess
import argparse
from utils.couch_db_handler import CouchDBHandler
from utils.text_editor import Editor
from pydantic import BaseModel
from fastapi import FastAPI, HTTPException, Body
from typing import List, Annotated
DESCRIPTION = """
AI-NER let's you anonymize your text documents by using a model of your choice from huggingface 🤗.
## Configuration of AI-NER
Before you can anonymize your text documents, you have to add (or update) a configuration for a model and tasks.
* `/insert_model`: Adds or updates a configuration for the models. It automatically downloads new models from huggingface.
* `/insert_tasks`: Adds or updates a configuration for a tasks. You have to choose here which model you want to use for each task.
## Usage of AI-NER
After you have configured models and tasks, you can anonymize your text documents in two steps:
1. Run the `/set_tasks` route to let the text editor know which configs you want to choose.
2. Send your text document via `/anonymize` to the text editor.
"""
class Config(BaseModel):
config_name: str
config_dict: dict
class Text(BaseModel):
input_text: str
class Texts(BaseModel):
input_text: List[str]
class App:
def __init__(self, ip: str = "127.0.0.1", port: int = 8000, debug: bool = False) -> None:
"""
Builds the App Object for the Server Backend
:param ip: ip to serve
:param port: port to serve
"""
self._ip = ip
self._port = port
self._debug = debug
self._app = FastAPI(
title="AI-NER: Text editing with Language Models from Huggingface 🤗",
description=DESCRIPTION
)
self._task_db = CouchDBHandler("config_tasks")
self._model_db = CouchDBHandler("config_models")
self._text_editor = None #Editor("config_task/default_task.yaml", self._model_db)
self._configure_routes()
@staticmethod
def modify_config(configs: List[Config], model_db: CouchDBHandler):
"""
Helper function to modify configs in the couchDB for either the tasks or the models.
configs are either updated or added (if they dont exist yet).
:param configs: List of configurations
:param model_db: the handler for the DB
:return: None
"""
config_dict = {config.config_name: config.config_dict for config in configs}
all_config_names = model_db.get_all_config_names()
for key, value in config_dict.items():
if "link" in value and not os.path.exists(value["model"]):
model_dir = "/".join(value["model"].split("/")[:-1])
subprocess.call(f"mkdir -p {model_dir}", shell=True)
subprocess.call(f"wget {value['link']} -P {model_dir}", shell=True)
value.pop("link")
elif "link" in value:
value.pop("link")
method = "add_config" if key not in all_config_names else "update_config"
getattr(model_db, method)(value, key)
def set_tasks(self, configuration: List[str]) -> bool:
"""
Updates the text editor with configured tasks (and models) from the couchdb.
:param configuration: List of configured tasks to be run by the editor \n
:return: True if successfully set all tasks
"""
config_dict = dict()
for config in configuration:
config_dict[config] = self._task_db.get_config(config)
self._text_editor = Editor(config_dict, self._model_db)
return True
def _configure_routes(self) -> None:
"""
Creates the route(s)
:return: None
"""
@self._app.post("/insert_models")
async def insert_models(configs: Annotated[List[Config], Body(
examples=[[
{
"config_name": "Sauerkraut",
"config_dict": {
"link": "https://huggingface.co/TheBloke/SauerkrautLM-7B-v1-mistral-GGUF/resolve/main/sauerkrautlm-7b-v1-mistral.Q4_0.gguf",
"model": "models/sauerkrautlm-7b-v1-mistral.Q4_0.gguf",
"max_tokens": 2000,
"temperature": 0,
"top_p": 0
}
},
{
"config_name": "flair-german",
"config_dict": {
"link": "https://huggingface.co/flair/ner-german-large/resolve/main/pytorch_model.bin",
"model": "models/flair/ner-german-large/pytorch_model.bin"
}
},
]]
)]
) -> bool:
"""
Inserts model configs into the couchdb.
If a configuration under the name already exists, the configuration will be overwritten.
:param configs: Object consisting of the config name and config details \n
:return: True if everything went successful
"""
self.modify_config(configs, self._model_db)
return True
@self._app.post("/delete_models")
async def delete_models(config_names: List[str]) -> bool:
"""
Deletes a configuration of a model from the couchdb.
If the config doesnt exist, an error will be raised.
:param config_names: List of names of model configs that will be deleted \n
:return: True if successfully deleted
"""
for config_name in config_names:
config = self._model_db.get_config(config_name)
subprocess.call(f"rm {config['model']}", shell=True)
self._model_db.delete_config(config_name)
return True
@self._app.get("/get_all_models")
async def get_all_models() -> dict:
"""
Returns all configured models that are currently stored in the couchdb.
:return: Dictionary of all model configs
"""
config = {}
all_models = self._model_db.get_all_config_names()
for model_name in all_models:
config[model_name] = self._model_db.get_config(model_name)
return config
@self._app.post("/insert_tasks")
async def insert_tasks(configs: Annotated[List[Config], Body(
examples=[[
{
"config_name": "email-address",
"config_dict": {
"model": {
"model_wrapper": "regex_model/Regex"
},
"pattern": r"\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b",
"replace_token": "[email protected]"
}
},
{
"config_name": "datum",
"config_dict": {
"model": {
"model_wrapper": "regex_model/Regex"
},
"pattern": r"(?:[0-2][0-9]|[1-9]|30|31)[.\\/,\\s](?:0?[1-9]|10|11|12)(?:(?:[.\\/,\\s](?:[1-2][0-9])?(?:[0-9]{2}))|[.\\/,]|\\b)",
"replace_token": ">DATUM<",
}
},
{
"config_name": "persons",
"config_dict": {
"model": {
"model_wrapper": "ner_model/FlairModel",
"model_config": "flair-german"
},
"replace_token": ">NAME<",
"entity_type": "PER"
}
}
]])]
) -> bool:
"""
Inserts and appends a configuration of a task to the couchdb.
A task defines what to anonymize (e.g. persons, customer IDs, ...).
:param configs: Configuration for an anonymization task \n
:return: True if successfully inserted
"""
self.modify_config(configs, self._task_db)
return True
@self._app.post("/delete_tasks")
async def delete_tasks(config_names: List[str]) -> bool:
"""
Deletes a configured task inside the couchDB.
:param config_names: Name of the config task to be deleted \n
:return: True if successfully deleted
"""
for config_name in config_names:
self._task_db.delete_config(config_name)
return True
@self._app.get("/get_all_tasks")
async def get_all_tasks() -> dict:
"""
Returns all configured tasks that are currently stored in the couchdb.
:return: Dictionary of all config tasks
"""
config = {}
all_models = self._task_db.get_all_config_names()
for model_name in all_models:
config[model_name] = self._task_db.get_config(model_name)
return config
@self._app.post("/anonymize_string")
async def anonymize_string(text: Annotated[Text, Body(
examples=[{
"input_text": """
Sehr geehrte Damen und Herren,
ich würde gerne Ihr neuestes Produkt für 9.99€ kaufen.
Wie bereits mit Herrn Peterson am Telefon am 10.11.2023 ausgemacht,
überschicke ich Ihnen hiermit meine gesamten Daten:
Name: Christian Mayer
Adresse: Guststraße 3, 12345 Frankfurt am Main
IBAN: DE70500105171174417511
Kunden-Nr.: 118255779
Sie können mich weiterhin unter meiner Handy-Nr 0172 229 0 229 erreichen.
Mit freundlichen Grüße,
Christian Mayer
"""
}]
)],
configuration: Annotated[List[str], Body(
examples=[[
"email-address",
"datum",
"persons"
]]
)]
) -> str:
"""
Anonymizes the input text by running the text editor over the configured and set tasks and models.
:param configuration:
:param text: Input text to be anonymized \n
:return: Anonymized text
"""
input_text = text.input_text
tasks_set = self.set_tasks(configuration)
if input_text is None or len(input_text) == 0:
raise HTTPException(status_code=400, detail="No value provided")
if not tasks_set:
raise HTTPException(status_code=400, detail="Tasks configurations were not set correctly")
output_text = self._text_editor.edit_text(input_text)
if self._debug:
self._text_editor.save_history("data/history/last_anonymize_str.json")
return output_text
@self._app.post("/anonymize_batch")
async def anonymize_batch(texts: Annotated[Texts, Body(
examples=[{
"input_text": ["""Paul Dirac/Sykes Herne/Team Blue % Kontakt: Kundin % Kundin meldet dass der Techniker informiert ist""",
"""-Pierre Alphonso Laurent/CNX Bochum/Sales ID: 11211 Kontakt: Kundin % Anliegen: Kundin ist umgezogen""",
"""Arwah Abadhi/Sykes Bochum/Team Lila % Kontakt: Kundin % Kundin meldet dass der Techniker informiert ist"""]
}]
)],
configuration: Annotated[List[str], Body(
examples=[[
"email-address",
"datum",
"persons",
"locations",
"organisations"
]]
)]
) -> List[str]:
input_texts = texts.input_text
tasks_set = self.set_tasks(configuration)
if input_texts is None or len(input_texts) == 0:
raise HTTPException(status_code=400, detail="no value provided")
if not tasks_set:
raise HTTPException(status_code=400, detail="Tasks configurations were not set correctly")
output_texts = []
for i, input_text in enumerate(input_texts):
output_texts.append(self._text_editor.edit_text(input_text))
if self._debug:
self._text_editor.save_history(f"data/history/anonymize_bulk_{i}.json")
return output_texts
def run(self) -> None:
"""
Run the api
:return: None
"""
uvicorn.run(self._app, host=self._ip, port=self._port)
if __name__ == '__main__':
if not os.path.exists("data/history"):
os.mkdir("data/history")
parser = argparse.ArgumentParser(description='Host AI-NER.')
parser.add_argument('-p', '--port', type=int, default=5000, help='the TCP/Port value')
parser.add_argument('--debug', action='store_true')
parser.add_argument('localaddress', nargs='*', help='the local Address where the server will listen')
os.environ["COUCHDB_USER"] = "admin"
os.environ["COUCHDB_PASSWORD"] = "JensIsCool"
os.environ["COUCHDB_IP"] = "127.0.0.1:5984"
api = App(ip=args.localaddress, port=args.port, debug=args.debug)
api.run()