File size: 3,553 Bytes
254ab0e
 
 
 
 
 
c8b694b
254ab0e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
from PIL import Image
import io

# Your n8n webhook URL
WEBHOOK_URL = "https://sanjay192005.app.n8n.cloud/webhook/a3acb431-0656-43cb-91f9-4d09827c4226"

def redact_pii(image):
    """
    Takes an uploaded image and sends it to the n8n webhook for PII redaction.
    Returns the redacted image.
    """
    if image is None:
        return None, "Please upload an image first."
    
    try:
        # Convert the image to bytes
        img = Image.fromarray(image)
        img_byte_arr = io.BytesIO()
        img.save(img_byte_arr, format='JPEG')
        img_byte_arr.seek(0)
        
        # Prepare the file for upload
        files = {
            'file': ('image.jpg', img_byte_arr, 'image/jpeg')
        }
        
        # Send POST request to n8n webhook
        response = requests.post(WEBHOOK_URL, files=files, timeout=120)
        
        # Check if request was successful
        if response.status_code == 200:
            # Convert response bytes to image
            redacted_image = Image.open(io.BytesIO(response.content))
            return redacted_image, "βœ… Redaction completed successfully!"
        else:
            return None, f"❌ Error: Server returned status code {response.status_code}"
            
    except requests.exceptions.Timeout:
        return None, "❌ Error: Request timed out. Please try again."
    except requests.exceptions.RequestException as e:
        return None, f"❌ Error: {str(e)}"
    except Exception as e:
        return None, f"❌ Error processing image: {str(e)}"

# Create the Gradio interface
with gr.Blocks(title="PII Redaction Tool", theme=gr.themes.Soft()) as demo:
    
    gr.Markdown(
        """
        # πŸ”’ PII Redaction Tool
        Upload a document image containing Personally Identifiable Information (PII). 
        The system will automatically detect and redact:
        - **Names**
        - **Dates of Birth**
        - **ID Numbers** (Aadhaar, etc.)
        - **Addresses**
        - **Faces**
        """
    )
    
    with gr.Row():
        with gr.Column():
            gr.Markdown("### πŸ“€ Upload Document")
            input_image = gr.Image(
                label="Upload Image",
                type="numpy",
                height=400
            )
            redact_btn = gr.Button("πŸ”’ Redact PII", variant="primary", size="lg")
            
        with gr.Column():
            gr.Markdown("### βœ… Redacted Document")
            output_image = gr.Image(
                label="Redacted Image",
                height=400
            )
            status_text = gr.Textbox(
                label="Status",
                interactive=False,
                lines=2
            )
    
    gr.Markdown(
        """
        ---
        ### ⚠️ Privacy Notice
        - Your documents are processed securely
        - No data is stored permanently
        - Images are transmitted over secure connections
        
        ### πŸ“ Supported Documents
        - Aadhaar Cards
        - Passports
        - Driver's Licenses
        - Any document with text and faces
        """
    )
    
    # Connect the button to the function
    redact_btn.click(
        fn=redact_pii,
        inputs=input_image,
        outputs=[output_image, status_text]
    )
    
    # Also allow pressing Enter to submit
    input_image.change(
        fn=lambda: "Image loaded. Click 'Redact PII' to process.",
        inputs=None,
        outputs=status_text
    )

# Launch the app
if __name__ == "__main__":
    demo.launch()