-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
42 lines (34 loc) · 1005 Bytes
/
main.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
####
# Ananonymizer API
###
from fastapi import FastAPI
from typing import Union
from pydantic import BaseModel
from presidio_analyzer import AnalyzerEngine
from presidio_anonymizer import AnonymizerEngine
app = FastAPI()
class Event(BaseModel):
text: Union[str, None] = None
anonymizer = AnonymizerEngine()
analyzer = AnalyzerEngine()
@app.get("/health")
async def health() -> dict:
"""
Health check endpoint.
"""
return {"status": "ok"}
@app.post("/anonymize")
async def anonymize_data(item: Event) -> dict:
"""
Anonymize the provided text data.
Args:
item (Event): The event containing the text to be anonymized.
Returns:
dict: A dictionary containing the anonymized text.
"""
text = item.text
if text is None:
return {"text": ""}
results = analyzer.analyze(text=text, language='en')
anonymized_text = anonymizer.anonymize(text=text, analyzer_results=results)
return {"text": anonymized_text.text}