File size: 2,099 Bytes
ff20e6e
 
379b083
 
 
 
 
 
 
 
 
 
ff20e6e
379b083
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff20e6e
379b083
 
 
 
 
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
from typing import Tuple, List

import gradio as gr

from presidio_analyzer import AnalyzerEngine
from presidio_analyzer.nlp_engine import NlpEngineProvider
from presidio_anonymizer import AnonymizerEngine

from faker import Faker
from presidio_anonymizer.entities.engine import OperatorConfig


def process(text: str, fake_data: bool) -> Tuple[str, List]:
    configuration = {
        "nlp_engine_name": "spacy",
        "models": [{"lang_code": "hu", "model_name": "hu_core_news_lg", }],
    }

    provider = NlpEngineProvider(nlp_configuration=configuration)
    nlp_engine = provider.create_engine()

    analyzer = AnalyzerEngine(nlp_engine=nlp_engine,
                              supported_languages=["hu"])

    results = analyzer.analyze(
        text=text, entities=["PERSON", "LOCATION"], language="hu")

    fake = Faker(locale=["hu_HU"])

    fake_operators = {
        "PERSON": OperatorConfig("custom", {"lambda": lambda x: fake.name()}),
        "LOCATION": OperatorConfig("custom", {"lambda": lambda x: fake.address()}),
    }

    anonymizer = AnonymizerEngine()
    anonymized_text = anonymizer.anonymize(
        text=text, analyzer_results=results, operators=fake_operators) if fake_data else anonymizer.anonymize(text=text, analyzer_results=results)
    return anonymized_text.text, anonymized_text.items


EXAMPLES = [
    ["Vespucci 1450-es években született Firenzében, és 1497 és 1504 között legalább két felfedező úton vett részt – az egyiket spanyol, a másikat portugál támogatással.", False],
    ["Elon Musk 1971-ben született a Dél-afrikai Köztársaságban, anyja Maye Musk (született: Haldeman) modell, apja Errol Musk mérnök, pilóta.", True]
]

demo = gr.Interface(
    fn=process,
    inputs=[gr.Textbox(value=EXAMPLES[0][0], lines=10, label="Input text", show_label=True),
            gr.Checkbox(value=EXAMPLES[0][1], label="Use fake data", show_label=True),],
    outputs=[gr.Textbox(label="Anonymized text", show_label=True),
             gr.Textbox(label="Tags", show_label=True)],
    examples=EXAMPLES,
    cache_examples=True,
)