File size: 1,943 Bytes
9d3239d
 
 
 
631dead
9d3239d
 
 
 
54bc438
 
9d3239d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

# Load a lightweight model
generator = pipeline("text2text-generation", model="facebook/bart-large", tokenizer="facebook/bart-large")

# Function to construct the anonymization prompt
def construct_prompt(input_text):
    prompt = f"""
    Please rewrite the input text by writting the text in double.
    
    "{input_text}"
    """
    return prompt.strip()

# Function to process the input text and generate the anonymized output
def anonymize_text(input_text):
    # Construct the instruction prompt
    prompt = construct_prompt(input_text)
    
    # Generate the output using the model
    response = generator(prompt, max_length=512, num_return_sequences=1)
    generated_text = response[0]['generated_text']
    
    # Attempt to parse the JSON output
    try:
        result = eval(generated_text)  # Convert the output string to a Python dictionary
        anonymized_text = result.get("anonymized_text", input_text)
        name_mapping = result.get("name_mapping", {})
    except Exception as e:
        anonymized_text = input_text
        name_mapping = {}
    
    return anonymized_text, name_mapping

# Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("## Text Anonymizer")
    gr.Markdown("Enter text containing personal names, and the model will anonymize it by replacing the names with pseudonyms. The app will also return a dictionary linking original names to their replacements.")
    
    input_text = gr.Textbox(label="Input Text", placeholder="Enter text here...")
    anonymized_text = gr.Textbox(label="Anonymized Text", interactive=False)
    name_mapping = gr.JSON(label="Name Mapping")
    
    def process_text(input_text):
        return anonymize_text(input_text)
    
    submit_button = gr.Button("Anonymize")
    submit_button.click(process_text, inputs=[input_text], outputs=[anonymized_text, name_mapping])

# Launch the app
demo.launch()